Search in sources :

Example 16 with DirectoryNotEmptyException

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());
    }
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException)

Example 17 with DirectoryNotEmptyException

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);
    }
}
Also used : Path(java.nio.file.Path) Files(java.nio.file.Files) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) COMPILER_PHASE(org.ballerinalang.compiler.CompilerOptionName.COMPILER_PHASE) IOException(java.io.IOException) BLangPrograms.createDirectory(org.ballerinalang.util.program.BLangPrograms.createDirectory) Compiler(org.wso2.ballerinalang.compiler.Compiler) BLANG_SRC_FILE_SUFFIX(org.ballerinalang.util.BLangConstants.BLANG_SRC_FILE_SUFFIX) StandardCopyOption(java.nio.file.StandardCopyOption) PROJECT_DIR(org.ballerinalang.compiler.CompilerOptionName.PROJECT_DIR) LinkOption(java.nio.file.LinkOption) USER_REPO_DEFAULT_DIRNAME(org.ballerinalang.util.BLangConstants.USER_REPO_DEFAULT_DIRNAME) USER_REPO_ARTIFACTS_DIRNAME(org.ballerinalang.util.BLangConstants.USER_REPO_ARTIFACTS_DIRNAME) Paths(java.nio.file.Paths) USER_REPO_SRC_DIRNAME(org.ballerinalang.util.BLangConstants.USER_REPO_SRC_DIRNAME) BLangPrograms(org.ballerinalang.util.program.BLangPrograms) USER_REPO_ENV_KEY(org.ballerinalang.util.BLangConstants.USER_REPO_ENV_KEY) PRESERVE_WHITESPACE(org.ballerinalang.compiler.CompilerOptionName.PRESERVE_WHITESPACE) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) Path(java.nio.file.Path) USER_HOME(org.ballerinalang.util.BLangConstants.USER_HOME) USER_REPO_OBJ_DIRNAME(org.ballerinalang.util.BLangConstants.USER_REPO_OBJ_DIRNAME) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CopyOption(java.nio.file.CopyOption) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException)

Example 18 with DirectoryNotEmptyException

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();
}
Also used : AccessDeniedException(java.nio.file.AccessDeniedException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) NotDirectoryException(java.nio.file.NotDirectoryException) NoSuchFileException(java.nio.file.NoSuchFileException) FileNotFoundException(java.io.FileNotFoundException) JsonObject(com.google.gson.JsonObject) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) ReadOnlyFileSystemException(java.nio.file.ReadOnlyFileSystemException)

Example 19 with DirectoryNotEmptyException

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() });
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) AbstractFile(org.structr.web.entity.AbstractFile) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) Folder(org.structr.web.entity.Folder)

Example 20 with DirectoryNotEmptyException

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;
    }
}
Also used : AccessDeniedException(java.nio.file.AccessDeniedException) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) File(java.io.File)

Aggregations

DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)21 NoSuchFileException (java.nio.file.NoSuchFileException)12 IOException (java.io.IOException)10 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)8 Path (java.nio.file.Path)8 AccessDeniedException (java.nio.file.AccessDeniedException)6 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)6 NotDirectoryException (java.nio.file.NotDirectoryException)6 FileSystemException (java.nio.file.FileSystemException)5 FileNotFoundException (java.io.FileNotFoundException)4 FileSystemLoopException (java.nio.file.FileSystemLoopException)4 EOFException (java.io.EOFException)3 File (java.io.File)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 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)3 MCRPath (org.mycore.datamodel.niofs.MCRPath)3