Example usage for java.nio.file DirectoryNotEmptyException DirectoryNotEmptyException

List of usage examples for java.nio.file DirectoryNotEmptyException DirectoryNotEmptyException

Introduction

In this page you can find the example usage for java.nio.file DirectoryNotEmptyException DirectoryNotEmptyException.

Prototype

public DirectoryNotEmptyException(String dir) 

Source Link

Document

Constructs an instance of this class.

Usage

From source file:de.tiqsolutions.hdfs.HadoopFileSystemProvider.java

static void rethrowRemoteException(RemoteException e, Path p1, Path p2) throws IOException {
    switch (e.getClassName()) {
    case "org.apache.hadoop.fs.PathIsNotEmptyDirectoryException":
        throw new DirectoryNotEmptyException(p1.toString());

    case "org.apache.hadoop.fs.PathExistsException":
    case "org.apache.hadoop.fs.FileAlreadyExistsException":
        throw new FileAlreadyExistsException(Objects.toString(p1), Objects.toString(p2),
                e.getLocalizedMessage());

    case "org.apache.hadoop.fs.PathPermissionException":
    case "org.apache.hadoop.fs.PathAccessDeniedException":
        throw new AccessDeniedException(Objects.toString(p1), Objects.toString(p2), e.getLocalizedMessage());

    case "org.apache.hadoop.fs.ParentNotDirectoryException":
    case "org.apache.hadoop.fs.DirectoryListingStartAfterNotFoundException":
    case "org.apache.hadoop.fs.PathIsNotDirectoryException":
        throw new NotDirectoryException(Objects.toString(p1));

    case "org.apache.hadoop.fs.PathIsDirectoryException":
    case "org.apache.hadoop.fs.InvalidPathException":
    case "org.apache.hadoop.fs.PathNotFoundException":
        throw new NoSuchFileException(Objects.toString(p1), Objects.toString(p2), e.getLocalizedMessage());

    case "org.apache.hadoop.fs.UnresolvedLinkException":
        throw new NotLinkException(Objects.toString(p1), Objects.toString(p2), e.getLocalizedMessage());

    case "org.apache.hadoop.fs.PathIOException":
    case "org.apache.hadoop.fs.ChecksumException":
    case "org.apache.hadoop.fs.InvalidRequestException":
    case "org.apache.hadoop.fs.UnsupportedFileSystemException":
    case "org.apache.hadoop.fs.ZeroCopyUnavailableException":

    }//from w  w  w . j  av  a 2 s. c  o  m

    throw new IOException(e.getLocalizedMessage(), e);
}

From source file:org.apache.taverna.robundle.Bundles.java

protected static void safeMoveOrCopy(Path source, Path destination, boolean move) throws IOException {
    // First just try to do an atomic move with overwrite
    try {/*from www.j  a v a 2 s  .c  om*/
        if (move && source.getFileSystem().provider().equals(destination.getFileSystem().provider())) {
            move(source, destination, ATOMIC_MOVE, REPLACE_EXISTING);
            return;
        }
    } catch (AtomicMoveNotSupportedException ex) {
        // Do the fallback by temporary files below
    }

    destination = destination.toAbsolutePath();

    String tmpName = destination.getFileName().toString();
    Path tmpDestination = createTempFile(destination.getParent(), tmpName, ".tmp");
    Path backup = null;
    try {
        if (move) {
            /*
             * This might do a copy if filestores differ .. hence to avoid
             * an incomplete (and partially overwritten) destination, we do
             * it first to a temporary file
             */
            move(source, tmpDestination, REPLACE_EXISTING);
        } else {
            copy(source, tmpDestination, REPLACE_EXISTING);
        }

        if (exists(destination)) {
            if (isDirectory(destination))
                // ensure it is empty
                try (DirectoryStream<Path> ds = newDirectoryStream(destination)) {
                    if (ds.iterator().hasNext())
                        throw new DirectoryNotEmptyException(destination.toString());
                }
            // Keep the files for roll-back in case it goes bad
            backup = createTempFile(destination.getParent(), tmpName, ".orig");
            move(destination, backup, REPLACE_EXISTING);
        }
        // OK ; let's swap over
        try {
            // prefer ATOMIC_MOVE
            move(tmpDestination, destination, REPLACE_EXISTING, ATOMIC_MOVE);
        } catch (AtomicMoveNotSupportedException ex) {
            /*
             * possibly a network file system as src/dest should be in same
             * folder
             */
            move(tmpDestination, destination, REPLACE_EXISTING);
        } finally {
            if (!exists(destination) && backup != null)
                // Restore the backup
                move(backup, destination);
        }
        // It went well, tidy up
        if (backup != null)
            deleteIfExists(backup);
    } finally {
        deleteIfExists(tmpDestination);
    }
}

From source file:org.cryptomator.cryptofs.CryptoFileSystemImpl.java

void delete(CryptoPath cleartextPath) throws IOException {
    Path ciphertextFile = cryptoPathMapper.getCiphertextFilePath(cleartextPath, CiphertextFileType.FILE);
    // try to delete ciphertext file:
    if (!Files.deleteIfExists(ciphertextFile)) {
        // filePath doesn't exist, maybe it's an directory:
        Path ciphertextDir = cryptoPathMapper.getCiphertextDirPath(cleartextPath);
        Path ciphertextDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextPath,
                CiphertextFileType.DIRECTORY);
        try {//w  w w. ja v a2  s  . co m
            Files.delete(ciphertextDir);
            if (!Files.deleteIfExists(ciphertextDirFile)) {
                // should not happen. Nevertheless this is a valid state, so who no big deal...
                LOG.warn("Successfully deleted dir {}, but didn't find corresponding dir file {}",
                        ciphertextDir, ciphertextDirFile);
            }
            dirIdProvider.delete(ciphertextDirFile);
        } catch (NoSuchFileException e) {
            // translate ciphertext path to cleartext path
            throw new NoSuchFileException(cleartextPath.toString());
        } catch (DirectoryNotEmptyException e) {
            // translate ciphertext path to cleartext path
            throw new DirectoryNotEmptyException(cleartextPath.toString());
        }
    }
}

From source file:org.cryptomator.cryptofs.CryptoFileSystemImpl.java

void copy(CryptoPath cleartextSource, CryptoPath cleartextTarget, CopyOption... options) throws IOException {
    if (cleartextSource.equals(cleartextTarget)) {
        return;/*from   w  w w.j  a v  a 2s  . c o m*/
    }
    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.copy(ciphertextSourceFile, ciphertextTargetFile, options);
    } else if (Files.exists(ciphertextSourceDirFile)) {
        // DIRECTORY (non-recursive as per contract):
        Path ciphertextTargetDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget,
                CiphertextFileType.DIRECTORY);
        if (!Files.exists(ciphertextTargetDirFile)) {
            // create new:
            createDirectory(cleartextTarget);
        } else if (ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING)) {
            // keep existing (if empty):
            Path ciphertextTargetDir = cryptoPathMapper.getCiphertextDirPath(cleartextTarget);
            try (DirectoryStream<Path> ds = Files.newDirectoryStream(ciphertextTargetDir)) {
                if (ds.iterator().hasNext()) {
                    throw new DirectoryNotEmptyException(cleartextTarget.toString());
                }
            }
        } else {
            throw new FileAlreadyExistsException(cleartextTarget.toString());
        }
        if (ArrayUtils.contains(options, StandardCopyOption.COPY_ATTRIBUTES)) {
            Path ciphertextSourceDir = cryptoPathMapper.getCiphertextDirPath(cleartextSource);
            Path ciphertextTargetDir = cryptoPathMapper.getCiphertextDirPath(cleartextTarget);
            copyAttributes(ciphertextSourceDir, ciphertextTargetDir);
        }
    } else {
        throw new NoSuchFileException(cleartextSource.toString());
    }
}

From source file:org.cryptomator.cryptofs.CryptoFileSystemImpl.java

void move(CryptoPath cleartextSource, CryptoPath cleartextTarget, CopyOption... options) throws IOException {
    if (cleartextSource.equals(cleartextTarget)) {
        return;/*from  w w  w. j  a va  2s.c om*/
    }
    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());
    }
}

From source file:org.cryptomator.ui.model.Vault.java

public void create(CharSequence passphrase) throws IOException {
    try {/*from w  w  w  .  ja  v a  2s  .c  o m*/
        FileSystem fs = getNioFileSystem();
        if (fs.files().map(File::name).filter(s -> s.endsWith(VAULT_FILE_EXTENSION)).count() > 0) {
            throw new FileAlreadyExistsException("masterkey.cryptomator", null, "Vault location not empty.");
        } else if (fs.folders().count() > 0) {
            throw new DirectoryNotEmptyException(fs.toString());
        }
        cryptoFileSystemFactory.initializeNew(fs, passphrase);
    } catch (UncheckedIOException e) {
        throw new IOException(e);
    }
}