use of java.nio.file.FileSystemException in project elasticsearch by elastic.
the class ExceptionSerializationTests method testFileSystemExceptions.
public void testFileSystemExceptions() throws IOException {
for (FileSystemException ex : Arrays.asList(new FileSystemException("a", "b", "c"), new NoSuchFileException("a", "b", "c"), new NotDirectoryException("a"), new DirectoryNotEmptyException("a"), new AtomicMoveNotSupportedException("a", "b", "c"), new FileAlreadyExistsException("a", "b", "c"), new AccessDeniedException("a", "b", "c"), new FileSystemLoopException("a"))) {
FileSystemException serialize = serialize(ex);
assertEquals(serialize.getClass(), ex.getClass());
assertEquals("a", serialize.getFile());
if (serialize.getClass() == NotDirectoryException.class || serialize.getClass() == FileSystemLoopException.class || serialize.getClass() == DirectoryNotEmptyException.class) {
assertNull(serialize.getOtherFile());
assertNull(serialize.getReason());
} else {
assertEquals(serialize.getClass().toString(), "b", serialize.getOtherFile());
assertEquals(serialize.getClass().toString(), "c", serialize.getReason());
}
}
}
use of java.nio.file.FileSystemException 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);
}
}
use of java.nio.file.FileSystemException in project gradle by gradle.
the class WatchServiceRegistrar method watchDir.
protected void watchDir(Path dir) throws IOException {
LOG.debug("Registering watch for {}", dir);
if (Thread.currentThread().isInterrupted()) {
LOG.debug("Skipping adding watch since current thread is interrupted.");
}
// on Windows, check if any parent is already watched
for (Path path = dir; path != null; path = FILE_TREE_WATCHING_SUPPORTED ? path.getParent() : null) {
WatchKey previousWatchKey = watchKeys.get(path);
if (previousWatchKey != null && previousWatchKey.isValid()) {
LOG.debug("Directory {} is already watched and the watch is valid, not adding another one.", path);
return;
}
}
int retryCount = 0;
IOException lastException = null;
while (retryCount++ < 2) {
try {
WatchKey watchKey = dir.register(watchService, WATCH_KINDS, WATCH_MODIFIERS);
watchKeys.put(dir, watchKey);
return;
} catch (IOException e) {
LOG.debug("Exception in registering for watching of " + dir, e);
lastException = e;
if (e instanceof NoSuchFileException) {
LOG.debug("Return silently since directory doesn't exist.");
return;
}
if (e instanceof FileSystemException && e.getMessage() != null && e.getMessage().contains("Bad file descriptor")) {
// retry after getting "Bad file descriptor" exception
LOG.debug("Retrying after 'Bad file descriptor'");
continue;
}
// So, we just ignore the exception if the dir doesn't exist anymore
if (!Files.exists(dir)) {
// return silently when directory doesn't exist
LOG.debug("Return silently since directory doesn't exist.");
return;
} else {
// no retry
throw e;
}
}
}
LOG.debug("Retry count exceeded, throwing last exception");
throw lastException;
}
use of java.nio.file.FileSystemException in project neo4j by neo4j.
the class DumperTest method shouldGiveAClearErrorMessageIfTheArchivesParentDirectoryIsAFile.
@Test
public void shouldGiveAClearErrorMessageIfTheArchivesParentDirectoryIsAFile() throws IOException {
Path directory = testDirectory.directory("a-directory").toPath();
Path archive = testDirectory.file("subdir/the-archive.dump").toPath();
Files.write(archive.getParent(), new byte[0]);
try {
new Dumper().dump(directory, archive, Predicates.alwaysFalse());
fail("Expected an exception");
} catch (FileSystemException e) {
assertEquals(archive.getParent().toString() + ": Not a directory", e.getMessage());
}
}
Aggregations