public void testPredicates() throws IOException { Path file = createTempFile(); Path dir = tempDir.resolve("dir"); Files.createDirectory(dir); assertTrue(MoreFiles.isDirectory().apply(dir)); assertFalse(MoreFiles.isRegularFile().apply(dir)); assertFalse(MoreFiles.isDirectory().apply(file)); assertTrue(MoreFiles.isRegularFile().apply(file)); Path symlinkToDir = tempDir.resolve("symlinkToDir"); Path symlinkToFile = tempDir.resolve("symlinkToFile"); Files.createSymbolicLink(symlinkToDir, dir); Files.createSymbolicLink(symlinkToFile, file); assertTrue(MoreFiles.isDirectory().apply(symlinkToDir)); assertFalse(MoreFiles.isRegularFile().apply(symlinkToDir)); assertFalse(MoreFiles.isDirectory().apply(symlinkToFile)); assertTrue(MoreFiles.isRegularFile().apply(symlinkToFile)); assertFalse(MoreFiles.isDirectory(NOFOLLOW_LINKS).apply(symlinkToDir)); assertFalse(MoreFiles.isRegularFile(NOFOLLOW_LINKS).apply(symlinkToFile)); }
/** * Secure recursive delete using {@code SecureDirectoryStream}. Returns a collection of exceptions * that occurred or null if no exceptions were thrown. */ private static @Nullable Collection<IOException> deleteRecursivelySecure( SecureDirectoryStream<Path> dir, Path path) { Collection<IOException> exceptions = null; try { if (isDirectory(dir, path, NOFOLLOW_LINKS)) { try (SecureDirectoryStream<Path> childDir = dir.newDirectoryStream(path, NOFOLLOW_LINKS)) { exceptions = deleteDirectoryContentsSecure(childDir); } // If exceptions is not null, something went wrong trying to delete the contents of the // directory, so we shouldn't try to delete the directory as it will probably fail. if (exceptions == null) { dir.deleteDirectory(path); } } else { dir.deleteFile(path); } return exceptions; } catch (IOException e) { return addException(exceptions, e); } }
/** * Secure recursive delete using {@code SecureDirectoryStream}. Returns a collection of exceptions * that occurred or null if no exceptions were thrown. */ private static @Nullable Collection<IOException> deleteRecursivelySecure( SecureDirectoryStream<Path> dir, Path path) { Collection<IOException> exceptions = null; try { if (isDirectory(dir, path, NOFOLLOW_LINKS)) { try (SecureDirectoryStream<Path> childDir = dir.newDirectoryStream(path, NOFOLLOW_LINKS)) { exceptions = deleteDirectoryContentsSecure(childDir); } // If exceptions is not null, something went wrong trying to delete the contents of the // directory, so we shouldn't try to delete the directory as it will probably fail. if (exceptions == null) { dir.deleteDirectory(path); } } else { dir.deleteFile(path); } return exceptions; } catch (IOException e) { return addException(exceptions, e); } }
/** * Secure recursive delete using {@code SecureDirectoryStream}. Returns a collection of exceptions * that occurred or null if no exceptions were thrown. */ @NullableDecl private static Collection<IOException> deleteRecursivelySecure( SecureDirectoryStream<Path> dir, Path path) { Collection<IOException> exceptions = null; try { if (isDirectory(dir, path, NOFOLLOW_LINKS)) { try (SecureDirectoryStream<Path> childDir = dir.newDirectoryStream(path, NOFOLLOW_LINKS)) { exceptions = deleteDirectoryContentsSecure(childDir); } // If exceptions is not null, something went wrong trying to delete the contents of the // directory, so we shouldn't try to delete the directory as it will probably fail. if (exceptions == null) { dir.deleteDirectory(path); } } else { dir.deleteFile(path); } return exceptions; } catch (IOException e) { return addException(exceptions, e); } }
public void testPredicates() throws IOException { Path file = createTempFile(); Path dir = tempDir.resolve("dir"); Files.createDirectory(dir); assertTrue(MoreFiles.isDirectory().apply(dir)); assertFalse(MoreFiles.isRegularFile().apply(dir)); assertFalse(MoreFiles.isDirectory().apply(file)); assertTrue(MoreFiles.isRegularFile().apply(file)); Path symlinkToDir = tempDir.resolve("symlinkToDir"); Path symlinkToFile = tempDir.resolve("symlinkToFile"); Files.createSymbolicLink(symlinkToDir, dir); Files.createSymbolicLink(symlinkToFile, file); assertTrue(MoreFiles.isDirectory().apply(symlinkToDir)); assertFalse(MoreFiles.isRegularFile().apply(symlinkToDir)); assertFalse(MoreFiles.isDirectory().apply(symlinkToFile)); assertTrue(MoreFiles.isRegularFile().apply(symlinkToFile)); assertFalse(MoreFiles.isDirectory(NOFOLLOW_LINKS).apply(symlinkToDir)); assertFalse(MoreFiles.isRegularFile(NOFOLLOW_LINKS).apply(symlinkToFile)); }
/** * Secure recursive delete using {@code SecureDirectoryStream}. Returns a collection of exceptions * that occurred or null if no exceptions were thrown. */ private static @Nullable Collection<IOException> deleteRecursivelySecure( SecureDirectoryStream<Path> dir, Path path) { Collection<IOException> exceptions = null; try { if (isDirectory(dir, path, NOFOLLOW_LINKS)) { try (SecureDirectoryStream<Path> childDir = dir.newDirectoryStream(path, NOFOLLOW_LINKS)) { exceptions = deleteDirectoryContentsSecure(childDir); } // If exceptions is not null, something went wrong trying to delete the contents of the // directory, so we shouldn't try to delete the directory as it will probably fail. if (exceptions == null) { dir.deleteDirectory(path); } } else { dir.deleteFile(path); } return exceptions; } catch (IOException e) { return addException(exceptions, e); } }
/** * Secure recursive delete using {@code SecureDirectoryStream}. Returns a collection of exceptions * that occurred or null if no exceptions were thrown. */ @NullableDecl private static Collection<IOException> deleteRecursivelySecure( SecureDirectoryStream<Path> dir, Path path) { Collection<IOException> exceptions = null; try { if (isDirectory(dir, path, NOFOLLOW_LINKS)) { try (SecureDirectoryStream<Path> childDir = dir.newDirectoryStream(path, NOFOLLOW_LINKS)) { exceptions = deleteDirectoryContentsSecure(childDir); } // If exceptions is not null, something went wrong trying to delete the contents of the // directory, so we shouldn't try to delete the directory as it will probably fail. if (exceptions == null) { dir.deleteDirectory(path); } } else { dir.deleteFile(path); } return exceptions; } catch (IOException e) { return addException(exceptions, e); } }