use of java.nio.file.SecureDirectoryStream in project jimfs by google.
the class JimfsUnixLikeFileSystemTest method testClosedSecureDirectoryStreamAttributeViewAndIterator.
@Test
public void testClosedSecureDirectoryStreamAttributeViewAndIterator() throws IOException {
Files.createDirectory(path("/foo"));
Files.createDirectory(path("/foo/bar"));
SecureDirectoryStream<Path> stream = (SecureDirectoryStream<Path>) Files.newDirectoryStream(path("/foo"));
Iterator<Path> iter = stream.iterator();
BasicFileAttributeView view1 = stream.getFileAttributeView(BasicFileAttributeView.class);
BasicFileAttributeView view2 = stream.getFileAttributeView(path("bar"), BasicFileAttributeView.class);
try {
stream.iterator();
fail("expected IllegalStateException");
} catch (IllegalStateException expected) {
}
stream.close();
try {
iter.next();
fail("expected ClosedDirectoryStreamException");
} catch (ClosedDirectoryStreamException expected) {
}
try {
view1.readAttributes();
fail("expected ClosedDirectoryStreamException");
} catch (ClosedDirectoryStreamException expected) {
}
try {
view2.readAttributes();
fail("expected ClosedDirectoryStreamException");
} catch (ClosedDirectoryStreamException expected) {
}
try {
view1.setTimes(null, null, null);
fail("expected ClosedDirectoryStreamException");
} catch (ClosedDirectoryStreamException expected) {
}
try {
view2.setTimes(null, null, null);
fail("expected ClosedDirectoryStreamException");
} catch (ClosedDirectoryStreamException expected) {
}
}
use of java.nio.file.SecureDirectoryStream in project guava by google.
the class MoreFiles method deleteRecursively.
/**
* Deletes the file or directory at the given {@code path} recursively. Deletes symbolic links,
* not their targets (subject to the caveat below).
*
* <p>If an I/O exception occurs attempting to read, open or delete any file under the given
* directory, this method skips that file and continues. All such exceptions are collected and,
* after attempting to delete all files, an {@code IOException} is thrown containing those
* exceptions as {@linkplain Throwable#getSuppressed() suppressed exceptions}.
*
* <h2>Warning: Security of recursive deletes</h2>
*
* <p>On a file system that supports symbolic links and does <i>not</i> support
* {@link SecureDirectoryStream}, it is possible for a recursive delete to delete files and
* directories that are <i>outside</i> the directory being deleted. This can happen if, after
* checking that a file is a directory (and not a symbolic link), that directory is replaced by a
* symbolic link to an outside directory before the call that opens the directory to read its
* entries.
*
* <p>By default, this method throws {@link InsecureRecursiveDeleteException} if it can't
* guarantee the security of recursive deletes. If you wish to allow the recursive deletes
* anyway, pass {@link RecursiveDeleteOption#ALLOW_INSECURE} to this method to override that
* behavior.
*
* @throws NoSuchFileException if {@code path} does not exist <i>(optional specific
* exception)</i>
* @throws InsecureRecursiveDeleteException if the security of recursive deletes can't be
* guaranteed for the file system and {@link RecursiveDeleteOption#ALLOW_INSECURE} was not
* specified
* @throws IOException if {@code path} or any file in the subtree rooted at it can't be deleted
* for any reason
*/
public static void deleteRecursively(Path path, RecursiveDeleteOption... options) throws IOException {
Path parentPath = getParentPath(path);
if (parentPath == null) {
throw new FileSystemException(path.toString(), null, "can't delete recursively");
}
// created lazily if needed
Collection<IOException> exceptions = null;
try {
boolean sdsSupported = false;
try (DirectoryStream<Path> parent = Files.newDirectoryStream(parentPath)) {
if (parent instanceof SecureDirectoryStream) {
sdsSupported = true;
exceptions = deleteRecursivelySecure((SecureDirectoryStream<Path>) parent, path.getFileName());
}
}
if (!sdsSupported) {
checkAllowsInsecure(path, options);
exceptions = deleteRecursivelyInsecure(path);
}
} catch (IOException e) {
if (exceptions == null) {
throw e;
} else {
exceptions.add(e);
}
}
if (exceptions != null) {
throwDeleteFailed(path, exceptions);
}
}
Aggregations