Search in sources :

Example 96 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project crate by crate.

the class FsBlobContainer method moveBlobAtomic.

public void moveBlobAtomic(final String sourceBlobName, final String targetBlobName, final boolean failIfAlreadyExists) throws IOException {
    final Path sourceBlobPath = path.resolve(sourceBlobName);
    final Path targetBlobPath = path.resolve(targetBlobName);
    // the existing file might be replaced or this method fails by throwing an IOException.
    if (Files.exists(targetBlobPath)) {
        if (failIfAlreadyExists) {
            throw new FileAlreadyExistsException("blob [" + targetBlobPath + "] already exists, cannot overwrite");
        } else {
            deleteBlobsIgnoringIfNotExists(Collections.singletonList(targetBlobName));
        }
    }
    Files.move(sourceBlobPath, targetBlobPath, StandardCopyOption.ATOMIC_MOVE);
}
Also used : BlobPath(org.elasticsearch.common.blobstore.BlobPath) Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException)

Example 97 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project beam by apache.

the class HadoopFileSystem method rename.

/**
 * Renames a {@link List} of file-like resources from one location to another.
 *
 * <p>The number of source resources must equal the number of destination resources. Destination
 * resources will be created recursively.
 *
 * @param srcResourceIds the references of the source resources
 * @param destResourceIds the references of the destination resources
 * @throws FileNotFoundException if the source resources are missing. When rename throws, the
 *     state of the resources is unknown but safe: for every (source, destination) pair of
 *     resources, the following are possible: a) source exists, b) destination exists, c) source
 *     and destination both exist. Thus no data is lost, however, duplicated resource are
 *     possible. In such scenarios, callers can use {@code match()} to determine the state of the
 *     resource.
 * @throws FileAlreadyExistsException if a target resource already exists and couldn't be
 *     overwritten.
 * @throws IOException if the underlying filesystem indicates the rename was not performed but no
 *     other errors were thrown.
 */
@Override
protected void rename(List<HadoopResourceId> srcResourceIds, List<HadoopResourceId> destResourceIds, MoveOptions... moveOptions) throws IOException {
    if (moveOptions.length > 0) {
        throw new UnsupportedOperationException("Support for move options is not yet implemented.");
    }
    for (int i = 0; i < srcResourceIds.size(); ++i) {
        final Path srcPath = srcResourceIds.get(i).toPath();
        final Path destPath = destResourceIds.get(i).toPath();
        // this enforces src and dest file systems to match
        final org.apache.hadoop.fs.FileSystem fs = srcPath.getFileSystem(configuration);
        // rename in HDFS requires the target directory to exist or silently fails (BEAM-4861)
        mkdirs(destPath);
        boolean success = fs.rename(srcPath, destPath);
        // testing first
        if (!success && fs.exists(srcPath) && fs.exists(destPath)) {
            LOG.debug(LOG_DELETING_EXISTING_FILE, Path.getPathWithoutSchemeAndAuthority(destPath));
            // not recursive
            fs.delete(destPath, false);
            success = fs.rename(srcPath, destPath);
        }
        if (!success) {
            if (!fs.exists(srcPath)) {
                throw new FileNotFoundException(String.format("Unable to rename resource %s to %s as source not found.", srcPath, destPath));
            } else if (fs.exists(destPath)) {
                throw new FileAlreadyExistsException(String.format("Unable to rename resource %s to %s as destination already exists and couldn't be deleted.", srcPath, destPath));
            } else {
                throw new IOException(String.format("Unable to rename resource %s to %s. No further information provided by underlying filesystem.", srcPath, destPath));
            }
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 98 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project robolectric by robolectric.

the class ShadowEnvironment method buildExternalStorageAppCacheDirs.

@Implementation(minSdk = KITKAT)
protected static File[] buildExternalStorageAppCacheDirs(String packageName) {
    Path externalStorageDirectoryPath = getExternalStorageDirectory().toPath();
    // Add cache directory in path.
    String cacheDirectory = packageName + "-cache";
    Path path = externalStorageDirectoryPath.resolve(cacheDirectory);
    try {
        Files.createDirectory(path);
    } catch (FileAlreadyExistsException e) {
        // That's ok
        return new File[] { path.toFile() };
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return new File[] { path.toFile() };
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) IOException(java.io.IOException) File(java.io.File) Implementation(org.robolectric.annotation.Implementation)

Example 99 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project vert.x by eclipse.

the class FileSystemImpl method copyRecursiveInternal.

private BlockingAction<Void> copyRecursiveInternal(String from, String to, boolean recursive) {
    Objects.requireNonNull(from);
    Objects.requireNonNull(to);
    return new BlockingAction<Void>() {

        public Void perform() {
            try {
                Path source = vertx.resolveFile(from).toPath();
                Path target = vertx.resolveFile(to).toPath();
                if (recursive) {
                    Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {

                        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                            Path targetDir = target.resolve(source.relativize(dir));
                            try {
                                Files.copy(dir, targetDir);
                            } catch (FileAlreadyExistsException e) {
                                if (!Files.isDirectory(targetDir)) {
                                    throw e;
                                }
                            }
                            return FileVisitResult.CONTINUE;
                        }

                        @Override
                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                            Files.copy(file, target.resolve(source.relativize(file)));
                            return FileVisitResult.CONTINUE;
                        }
                    });
                } else {
                    Files.copy(source, target);
                }
            } catch (IOException e) {
                throw new FileSystemException(getFileCopyErrorMessage(from, to), e);
            }
            return null;
        }
    };
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileSystemException(io.vertx.core.file.FileSystemException) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 100 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project j2objc by google.

the class FilesTest method test_createFile$String$Attr.

@Test
public void test_createFile$String$Attr() throws IOException {
    Set<PosixFilePermission> perm = PosixFilePermissions.fromString("rwx------");
    FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perm);
    Files.createFile(filesSetup.getTestPath(), attr);
    assertEquals(attr.value(), Files.getAttribute(filesSetup.getTestPath(), attr.name()));
    // Creating a new file and passing multiple attribute of the same name.
    perm = PosixFilePermissions.fromString("rw-------");
    FileAttribute<Set<PosixFilePermission>> attr1 = PosixFilePermissions.asFileAttribute(perm);
    Path filePath2 = filesSetup.getPathInTestDir("new_file");
    Files.createFile(filePath2, attr, attr1);
    // Value should be equal to the last attribute passed.
    assertEquals(attr1.value(), Files.getAttribute(filePath2, attr.name()));
    // When file exists.
    try {
        Files.createFile(filesSetup.getDataFilePath(), attr);
        fail();
    } catch (FileAlreadyExistsException expected) {
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) HashSet(java.util.HashSet) Set(java.util.Set) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Test(org.junit.Test)

Aggregations

FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)104 Path (java.nio.file.Path)49 IOException (java.io.IOException)44 File (java.io.File)24 NoSuchFileException (java.nio.file.NoSuchFileException)22 Test (org.junit.Test)15 FileNotFoundException (java.io.FileNotFoundException)10 FileSystemException (java.nio.file.FileSystemException)9 AccessDeniedException (java.nio.file.AccessDeniedException)8 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)8 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)8 NotDirectoryException (java.nio.file.NotDirectoryException)7 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)5 CopyOption (java.nio.file.CopyOption)5 FileSystem (java.nio.file.FileSystem)5 FileVisitResult (java.nio.file.FileVisitResult)5 MCRPath (org.mycore.datamodel.niofs.MCRPath)5 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 FileSystemLoopException (java.nio.file.FileSystemLoopException)4