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);
}
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));
}
}
}
}
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() };
}
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;
}
};
}
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) {
}
}
Aggregations