use of java.nio.file.DirectoryNotEmptyException in project cryptofs by cryptomator.
the class CryptoFileSystemImpl method move.
void move(CryptoPath cleartextSource, CryptoPath cleartextTarget, CopyOption... options) throws IOException {
if (cleartextSource.equals(cleartextTarget)) {
return;
}
Path ciphertextSourceFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource, CiphertextFileType.FILE);
Path ciphertextSourceDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource, CiphertextFileType.DIRECTORY);
if (Files.exists(ciphertextSourceFile)) {
// FILE:
Path ciphertextTargetFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget, CiphertextFileType.FILE);
Files.move(ciphertextSourceFile, ciphertextTargetFile, options);
} else if (Files.exists(ciphertextSourceDirFile)) {
// DIRECTORY:
Path ciphertextTargetDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget, CiphertextFileType.DIRECTORY);
if (!ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING)) {
// try to move, don't replace:
Files.move(ciphertextSourceDirFile, ciphertextTargetDirFile, options);
} else if (ArrayUtils.contains(options, StandardCopyOption.ATOMIC_MOVE)) {
// replace atomically (impossible):
assert ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING);
throw new AtomicMoveNotSupportedException(cleartextSource.toString(), cleartextTarget.toString(), "Replacing directories during move requires non-atomic status checks.");
} else {
// move and replace (if dir is empty):
assert ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING);
assert !ArrayUtils.contains(options, StandardCopyOption.ATOMIC_MOVE);
if (Files.exists(ciphertextTargetDirFile)) {
Path ciphertextTargetDir = cryptoPathMapper.getCiphertextDirPath(cleartextTarget);
try (DirectoryStream<Path> ds = Files.newDirectoryStream(ciphertextTargetDir)) {
if (ds.iterator().hasNext()) {
throw new DirectoryNotEmptyException(cleartextTarget.toString());
}
}
Files.delete(ciphertextTargetDir);
}
Files.move(ciphertextSourceDirFile, ciphertextTargetDirFile, options);
}
dirIdProvider.move(ciphertextSourceDirFile, ciphertextTargetDirFile);
} else {
throw new NoSuchFileException(cleartextSource.toString());
}
}
use of java.nio.file.DirectoryNotEmptyException in project ballerina by ballerina-lang.
the class UserRepositoryUtils method uninstallSourcePackage.
public static void uninstallSourcePackage(String packageStr) {
Path packagePath = Paths.get(packageStr);
Path userRepoSrcPath = initializeUserRepository().resolve(USER_REPO_ARTIFACTS_DIRNAME).resolve(USER_REPO_SRC_DIRNAME);
Path dirPathToDelete = userRepoSrcPath.resolve(packagePath);
if (Files.exists(dirPathToDelete, LinkOption.NOFOLLOW_LINKS) && !Files.isDirectory(dirPathToDelete, LinkOption.NOFOLLOW_LINKS)) {
throw new RuntimeException("a file exists with the same name as the package name: " + dirPathToDelete.toString());
} else if (!Files.exists(dirPathToDelete, LinkOption.NOFOLLOW_LINKS)) {
// File doesn't exists
throw new RuntimeException("package does not exist: " + dirPathToDelete.toString());
}
try {
Files.list(dirPathToDelete).filter(filePath -> !Files.isDirectory(filePath, LinkOption.NOFOLLOW_LINKS)).filter(filePath -> filePath.toString().endsWith(BLANG_SRC_FILE_SUFFIX)).forEach(filePath -> {
try {
Files.delete(filePath);
} catch (IOException e) {
throw new RuntimeException("error uninstalling package: " + packageStr + ": " + e.getMessage(), e);
}
});
deleteEmptyDirsUpTo(dirPathToDelete, userRepoSrcPath);
} catch (DirectoryNotEmptyException e) {
throw new RuntimeException("error uninstalling package: " + packageStr + ": directory not empty: " + dirPathToDelete.toString(), e);
} catch (IOException e) {
throw new RuntimeException("error uninstalling package: " + packageStr + ": " + e.getMessage(), e);
}
}
use of java.nio.file.DirectoryNotEmptyException in project ballerina by ballerina-lang.
the class FileSystemService method createErrorResponse.
/**
* Creates an error response for the given IO Exception.
*
* @param ex Thrown Exception
* @return Error Message
*/
private Response createErrorResponse(Throwable ex) {
JsonObject entity = new JsonObject();
String errMsg = ex.getMessage();
if (ex instanceof AccessDeniedException) {
errMsg = "Access Denied to " + ex.getMessage();
} else if (ex instanceof NoSuchFileException) {
errMsg = "No such file: " + ex.getMessage();
} else if (ex instanceof FileAlreadyExistsException) {
errMsg = "File already exists: " + ex.getMessage();
} else if (ex instanceof NotDirectoryException) {
errMsg = "Not a directory: " + ex.getMessage();
} else if (ex instanceof ReadOnlyFileSystemException) {
errMsg = "Read only: " + ex.getMessage();
} else if (ex instanceof DirectoryNotEmptyException) {
errMsg = "Directory not empty: " + ex.getMessage();
} else if (ex instanceof FileNotFoundException) {
errMsg = "File not found: " + ex.getMessage();
}
entity.addProperty("Error", errMsg);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(entity).header(ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, '*').type(MediaType.APPLICATION_JSON).build();
}
use of java.nio.file.DirectoryNotEmptyException in project structr by structr.
the class StructrFilePath method delete.
@Override
public void delete() throws IOException {
final App app = StructrApp.getInstance(fs.getSecurityContext());
final AbstractFile actualFile = getActualFile();
try (final Tx tx = app.tx()) {
// if a folder is to be deleted, check contents
if (actualFile instanceof Folder && ((Folder) actualFile).getChildren().iterator().hasNext()) {
throw new DirectoryNotEmptyException(getActualFile().getPath());
} else {
app.delete(actualFile);
// remove cached version
this.cachedActualFile = null;
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("Unable to delete file {}: {}", new Object[] { getActualFile().getPath(), fex.getMessage() });
}
}
use of java.nio.file.DirectoryNotEmptyException in project flink by apache.
the class LocalFileSystem method rename.
@Override
public boolean rename(final Path src, final Path dst) throws IOException {
final File srcFile = pathToFile(src);
final File dstFile = pathToFile(dst);
final File dstParent = dstFile.getParentFile();
// Files.move fails if the destination directory doesn't exist
// noinspection ResultOfMethodCallIgnored -- we don't care if the directory existed or was
// created
dstParent.mkdirs();
try {
Files.move(srcFile.toPath(), dstFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (NoSuchFileException | AccessDeniedException | DirectoryNotEmptyException | SecurityException ex) {
// catch the errors that are regular "move failed" exceptions and return false
return false;
}
}
Aggregations