Search in sources :

Example 11 with AtomicMoveNotSupportedException

use of java.nio.file.AtomicMoveNotSupportedException in project buck by facebook.

the class Main method moveToTrash.

private static void moveToTrash(ProjectFilesystem filesystem, Console console, BuildId buildId, Path... pathsToMove) throws IOException {
    Path trashPath = filesystem.getBuckPaths().getTrashDir().resolve(buildId.toString());
    filesystem.mkdirs(trashPath);
    for (Path pathToMove : pathsToMove) {
        try {
            // Technically this might throw AtomicMoveNotSupportedException,
            // but we're moving a path within buck-out, so we don't expect this
            // to throw.
            //
            // If it does throw, we'll complain loudly and synchronously delete
            // the file instead.
            filesystem.move(pathToMove, trashPath.resolve(pathToMove.getFileName()), StandardCopyOption.ATOMIC_MOVE);
        } catch (NoSuchFileException e) {
            LOG.verbose(e, "Ignoring missing path %s", pathToMove);
        } catch (AtomicMoveNotSupportedException e) {
            console.getStdErr().format("Atomic moves not supported, falling back to synchronous delete: %s", e);
            MoreFiles.deleteRecursivelyIfExists(pathToMove);
        }
    }
}
Also used : ClassPath(com.google.common.reflect.ClassPath) Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException)

Example 12 with AtomicMoveNotSupportedException

use of java.nio.file.AtomicMoveNotSupportedException in project elasticsearch by elastic.

the class ExceptionSerializationTests method testFileSystemExceptions.

public void testFileSystemExceptions() throws IOException {
    for (FileSystemException ex : Arrays.asList(new FileSystemException("a", "b", "c"), new NoSuchFileException("a", "b", "c"), new NotDirectoryException("a"), new DirectoryNotEmptyException("a"), new AtomicMoveNotSupportedException("a", "b", "c"), new FileAlreadyExistsException("a", "b", "c"), new AccessDeniedException("a", "b", "c"), new FileSystemLoopException("a"))) {
        FileSystemException serialize = serialize(ex);
        assertEquals(serialize.getClass(), ex.getClass());
        assertEquals("a", serialize.getFile());
        if (serialize.getClass() == NotDirectoryException.class || serialize.getClass() == FileSystemLoopException.class || serialize.getClass() == DirectoryNotEmptyException.class) {
            assertNull(serialize.getOtherFile());
            assertNull(serialize.getReason());
        } else {
            assertEquals(serialize.getClass().toString(), "b", serialize.getOtherFile());
            assertEquals(serialize.getClass().toString(), "c", serialize.getReason());
        }
    }
}
Also used : FileSystemException(java.nio.file.FileSystemException) NotDirectoryException(java.nio.file.NotDirectoryException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) FileSystemLoopException(java.nio.file.FileSystemLoopException)

Example 13 with AtomicMoveNotSupportedException

use of java.nio.file.AtomicMoveNotSupportedException in project google-cloud-java by GoogleCloudPlatform.

the class CloudStorageFileSystemProvider method move.

@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
    initStorage();
    for (CopyOption option : options) {
        if (option == StandardCopyOption.ATOMIC_MOVE) {
            throw new AtomicMoveNotSupportedException(source.toString(), target.toString(), "Google Cloud Storage does not support atomic move operations.");
        }
    }
    copy(source, target, options);
    delete(source);
}
Also used : CopyOption(java.nio.file.CopyOption) StandardCopyOption(java.nio.file.StandardCopyOption) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException)

Example 14 with AtomicMoveNotSupportedException

use of java.nio.file.AtomicMoveNotSupportedException in project Terasology by MovingBlocks.

the class SaveTransaction method renameMergeFolder.

private void renameMergeFolder() throws IOException {
    Path directoryForUnfinishedFiles = storagePathProvider.getUnfinishedSaveTransactionPath();
    Path directoryForFinishedFiles = storagePathProvider.getUnmergedChangesPath();
    try {
        Files.move(directoryForUnfinishedFiles, directoryForFinishedFiles, StandardCopyOption.ATOMIC_MOVE);
    } catch (AtomicMoveNotSupportedException e) {
        logger.warn("Atomic rename of merge folder was not possible, doing it non atomically...");
        Files.move(directoryForUnfinishedFiles, directoryForFinishedFiles);
    }
}
Also used : Path(java.nio.file.Path) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException)

Example 15 with AtomicMoveNotSupportedException

use of java.nio.file.AtomicMoveNotSupportedException in project Terasology by MovingBlocks.

the class SaveTransactionHelper method mergeChanges.

/**
 * Merges all outstanding changes into the save game. If this operation gets interrupted it can be started again
 * without any file corruption when the file system supports atomic moves.
 * <br><br>
 * The write lock for the save directory should be acquired before this method gets called.
 */
public void mergeChanges() throws IOException {
    final Path sourceDirectory = storagePathProvider.getUnmergedChangesPath();
    final Path targetDirectory = storagePathProvider.getStoragePathDirectory();
    Files.walkFileTree(sourceDirectory, new SimpleFileVisitor<Path>() {

        boolean atomicNotPossibleLogged;

        @Override
        public FileVisitResult preVisitDirectory(Path sourceSubDir, BasicFileAttributes attrs) throws IOException {
            Path targetSubDir = targetDirectory.resolve(sourceDirectory.relativize(sourceSubDir));
            if (!Files.isDirectory(targetSubDir)) {
                Files.createDirectory(targetSubDir);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path sourcePath, BasicFileAttributes attrs) throws IOException {
            Path targetPath = targetDirectory.resolve(sourceDirectory.relativize(sourcePath));
            try {
                // Delete file, as behavior of atomic move is undefined if target file exists:
                Files.deleteIfExists(targetPath);
                Files.move(sourcePath, targetPath, StandardCopyOption.ATOMIC_MOVE);
            } catch (AtomicMoveNotSupportedException e) {
                if (!atomicNotPossibleLogged) {
                    logger.warn("Atomic move was not possible, doing it non atomically...");
                    atomicNotPossibleLogged = true;
                }
                Files.move(sourcePath, targetPath);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            try {
                Files.delete(dir);
            } catch (DirectoryNotEmptyException e) {
                /**
                 * Happens rarely for some players on windows (See issue #2160). Exact reason for this behavior is
                 * unknown. Maybe they have some kind of background task that processes that creates a temporary
                 * files in new directories to store some intermediate scan result.
                 */
                logger.warn("The save job could not cleanup a temporarly created directory, it will retry once in one second");
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e1) {
                    // Reset flag and ignore it
                    Thread.currentThread().interrupt();
                }
                Files.delete(dir);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)22 Path (java.nio.file.Path)12 IOException (java.io.IOException)9 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)7 NoSuchFileException (java.nio.file.NoSuchFileException)7 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)6 FileSystemException (java.nio.file.FileSystemException)5 AccessDeniedException (java.nio.file.AccessDeniedException)4 CopyOption (java.nio.file.CopyOption)4 FileSystemLoopException (java.nio.file.FileSystemLoopException)4 NotDirectoryException (java.nio.file.NotDirectoryException)4 StandardCopyOption (java.nio.file.StandardCopyOption)4 EOFException (java.io.EOFException)3 FileNotFoundException (java.io.FileNotFoundException)3 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)3 IndexFormatTooNewException (org.apache.lucene.index.IndexFormatTooNewException)3 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)3 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)3 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)3 ElasticsearchException (org.elasticsearch.ElasticsearchException)3