Search in sources :

Example 6 with FileSystemException

use of java.nio.file.FileSystemException in project neo4j by neo4j.

the class LoaderTest method shouldGiveAClearErrorMessageIfTheDestinationsParentDirectoryIsAFile.

@Test
public void shouldGiveAClearErrorMessageIfTheDestinationsParentDirectoryIsAFile() throws IOException, IncorrectFormat {
    Path archive = testDirectory.file("the-archive.dump").toPath();
    Path destination = testDirectory.directory("subdir/the-destination").toPath();
    Files.write(destination.getParent(), new byte[0]);
    try {
        new Loader().load(archive, destination);
        fail("Expected an exception");
    } catch (FileSystemException e) {
        assertEquals(destination.getParent().toString() + ": Not a directory", e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) FileSystemException(java.nio.file.FileSystemException) Test(org.junit.Test)

Example 7 with FileSystemException

use of java.nio.file.FileSystemException in project guava by google.

the class MoreFilesTest method testDirectoryDeletion_directorySymlinkRace.

/**
   * This test attempts to create a situation in which one thread is constantly changing a file
   * from being a real directory to being a symlink to another directory. It then calls
   * deleteDirectoryContents thousands of times on a directory whose subtree contains the file
   * that's switching between directory and symlink to try to ensure that under no circumstance
   * does deleteDirectoryContents follow the symlink to the other directory and delete that
   * directory's contents.
   *
   * <p>We can only test this with a file system that supports SecureDirectoryStream, because it's
   * not possible to protect against this if the file system doesn't.
   */
public void testDirectoryDeletion_directorySymlinkRace() throws IOException {
    for (DirectoryDeleteMethod method : EnumSet.allOf(DirectoryDeleteMethod.class)) {
        try (FileSystem fs = newTestFileSystem(SECURE_DIRECTORY_STREAM)) {
            Path dirToDelete = fs.getPath("dir/b/i");
            Path changingFile = dirToDelete.resolve("j/l");
            Path symlinkTarget = fs.getPath("/dontdelete");
            ExecutorService executor = Executors.newSingleThreadExecutor();
            startDirectorySymlinkSwitching(changingFile, symlinkTarget, executor);
            try {
                for (int i = 0; i < 5000; i++) {
                    try {
                        Files.createDirectories(changingFile);
                        Files.createFile(dirToDelete.resolve("j/k"));
                    } catch (FileAlreadyExistsException expected) {
                    // if a file already exists, that's fine... just continue
                    }
                    try {
                        method.delete(dirToDelete);
                    } catch (FileSystemException expected) {
                    // the delete method may or may not throw an exception, but if it does that's fine
                    // and expected
                    }
                    // this test is mainly checking that the contents of /dontdelete aren't deleted under
                    // any circumstances
                    assertEquals(3, MoreFiles.listFiles(symlinkTarget).size());
                    Thread.yield();
                }
            } finally {
                executor.shutdownNow();
            }
        }
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileSystemException(java.nio.file.FileSystemException) FileSystem(java.nio.file.FileSystem) ExecutorService(java.util.concurrent.ExecutorService)

Example 8 with FileSystemException

use of java.nio.file.FileSystemException in project jetty.project by eclipse.

the class AllowSymLinkAliasCheckerTest method setupRoot.

private void setupRoot() throws IOException {
    rootPath = MavenTestingUtils.getTargetTestingPath(AllowSymLinkAliasCheckerTest.class.getSimpleName());
    FS.ensureEmpty(rootPath);
    Path testdir = rootPath.resolve("testdir");
    FS.ensureDirExists(testdir);
    try {
        // If we used testdir (Path) from above, these symlinks
        // would point to an absolute path.
        // Create a relative symlink testdirlnk -> testdir
        Files.createSymbolicLink(rootPath.resolve("testdirlnk"), new File("testdir").toPath());
        // Create a relative symlink testdirprefixlnk -> ./testdir
        Files.createSymbolicLink(rootPath.resolve("testdirprefixlnk"), new File("./testdir").toPath());
        // Create a relative symlink testdirsuffixlnk -> testdir/
        Files.createSymbolicLink(rootPath.resolve("testdirsuffixlnk"), new File("testdir/").toPath());
        // Create a relative symlink testdirwraplnk -> ./testdir/
        Files.createSymbolicLink(rootPath.resolve("testdirwraplnk"), new File("./testdir/").toPath());
    } catch (UnsupportedOperationException | FileSystemException e) {
        // If unable to create symlink, no point testing the rest.
        // This is the path that Microsoft Windows takes.
        assumeNoException(e);
    }
    Path testfileTxt = testdir.resolve("testfile.txt");
    Files.createFile(testfileTxt);
    try (OutputStream out = Files.newOutputStream(testfileTxt)) {
        out.write("Hello TestFile".getBytes(StandardCharsets.UTF_8));
    }
    try {
        Path testfileTxtLnk = testdir.resolve("testfilelnk.txt");
        // Create a relative symlink testfilelnk.txt -> testfile.txt
        // If we used testfileTxt (Path) from above, this symlink
        // would point to an absolute path.
        Files.createSymbolicLink(testfileTxtLnk, new File("testfile.txt").toPath());
    } catch (UnsupportedOperationException | FileSystemException e) {
        // If unable to create symlink, no point testing the rest.
        // This is the path that Microsoft Windows takes.
        assumeNoException(e);
    }
    try {
        Path testfilePrefixTxtLnk = testdir.resolve("testfileprefixlnk.txt");
        // Create a relative symlink testfileprefixlnk.txt -> ./testfile.txt
        // If we used testfileTxt (Path) from above, this symlink
        // would point to an absolute path.
        Files.createSymbolicLink(testfilePrefixTxtLnk, new File("./testfile.txt").toPath());
    } catch (UnsupportedOperationException | FileSystemException e) {
        // If unable to create symlink, no point testing the rest.
        // This is the path that Microsoft Windows takes.
        assumeNoException(e);
    }
}
Also used : Path(java.nio.file.Path) FileSystemException(java.nio.file.FileSystemException) OutputStream(java.io.OutputStream) File(java.io.File)

Example 9 with FileSystemException

use of java.nio.file.FileSystemException in project elasticsearch by elastic.

the class StreamOutput method writeException.

public void writeException(Throwable throwable) throws IOException {
    if (throwable == null) {
        writeBoolean(false);
    } else {
        writeBoolean(true);
        boolean writeCause = true;
        boolean writeMessage = true;
        if (throwable instanceof CorruptIndexException) {
            writeVInt(1);
            writeOptionalString(((CorruptIndexException) throwable).getOriginalMessage());
            writeOptionalString(((CorruptIndexException) throwable).getResourceDescription());
            writeMessage = false;
        } else if (throwable instanceof IndexFormatTooNewException) {
            writeVInt(2);
            writeOptionalString(((IndexFormatTooNewException) throwable).getResourceDescription());
            writeInt(((IndexFormatTooNewException) throwable).getVersion());
            writeInt(((IndexFormatTooNewException) throwable).getMinVersion());
            writeInt(((IndexFormatTooNewException) throwable).getMaxVersion());
            writeMessage = false;
            writeCause = false;
        } else if (throwable instanceof IndexFormatTooOldException) {
            writeVInt(3);
            IndexFormatTooOldException t = (IndexFormatTooOldException) throwable;
            writeOptionalString(t.getResourceDescription());
            if (t.getVersion() == null) {
                writeBoolean(false);
                writeOptionalString(t.getReason());
            } else {
                writeBoolean(true);
                writeInt(t.getVersion());
                writeInt(t.getMinVersion());
                writeInt(t.getMaxVersion());
            }
            writeMessage = false;
            writeCause = false;
        } else if (throwable instanceof NullPointerException) {
            writeVInt(4);
            writeCause = false;
        } else if (throwable instanceof NumberFormatException) {
            writeVInt(5);
            writeCause = false;
        } else if (throwable instanceof IllegalArgumentException) {
            writeVInt(6);
        } else if (throwable instanceof AlreadyClosedException) {
            writeVInt(7);
        } else if (throwable instanceof EOFException) {
            writeVInt(8);
            writeCause = false;
        } else if (throwable instanceof SecurityException) {
            writeVInt(9);
        } else if (throwable instanceof StringIndexOutOfBoundsException) {
            writeVInt(10);
            writeCause = false;
        } else if (throwable instanceof ArrayIndexOutOfBoundsException) {
            writeVInt(11);
            writeCause = false;
        } else if (throwable instanceof FileNotFoundException) {
            writeVInt(12);
            writeCause = false;
        } else if (throwable instanceof FileSystemException) {
            writeVInt(13);
            if (throwable instanceof NoSuchFileException) {
                writeVInt(0);
            } else if (throwable instanceof NotDirectoryException) {
                writeVInt(1);
            } else if (throwable instanceof DirectoryNotEmptyException) {
                writeVInt(2);
            } else if (throwable instanceof AtomicMoveNotSupportedException) {
                writeVInt(3);
            } else if (throwable instanceof FileAlreadyExistsException) {
                writeVInt(4);
            } else if (throwable instanceof AccessDeniedException) {
                writeVInt(5);
            } else if (throwable instanceof FileSystemLoopException) {
                writeVInt(6);
            } else {
                writeVInt(7);
            }
            writeOptionalString(((FileSystemException) throwable).getFile());
            writeOptionalString(((FileSystemException) throwable).getOtherFile());
            writeOptionalString(((FileSystemException) throwable).getReason());
            writeCause = false;
        } else if (throwable instanceof IllegalStateException) {
            writeVInt(14);
        } else if (throwable instanceof LockObtainFailedException) {
            writeVInt(15);
        } else if (throwable instanceof InterruptedException) {
            writeVInt(16);
            writeCause = false;
        } else if (throwable instanceof IOException) {
            writeVInt(17);
        } else {
            ElasticsearchException ex;
            if (throwable instanceof ElasticsearchException && ElasticsearchException.isRegistered(throwable.getClass(), version)) {
                ex = (ElasticsearchException) throwable;
            } else {
                ex = new NotSerializableExceptionWrapper(throwable);
            }
            writeVInt(0);
            writeVInt(ElasticsearchException.getId(ex.getClass()));
            ex.writeTo(this);
            return;
        }
        if (writeMessage) {
            writeOptionalString(throwable.getMessage());
        }
        if (writeCause) {
            writeException(throwable.getCause());
        }
        ElasticsearchException.writeStackTraces(throwable, this);
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ElasticsearchException(org.elasticsearch.ElasticsearchException) FileSystemException(java.nio.file.FileSystemException) NotDirectoryException(java.nio.file.NotDirectoryException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) EOFException(java.io.EOFException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IOException(java.io.IOException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) FileSystemLoopException(java.nio.file.FileSystemLoopException)

Example 10 with FileSystemException

use of java.nio.file.FileSystemException in project elasticsearch by elastic.

the class ESFileStore method getFileStoreWindows.

/** 
     * remove this code and just use getFileStore for windows on java 9
     * works around https://bugs.openjdk.java.net/browse/JDK-8034057
     */
@SuppressForbidden(reason = "works around https://bugs.openjdk.java.net/browse/JDK-8034057")
static FileStore getFileStoreWindows(Path path, FileStore[] fileStores) throws IOException {
    assert Constants.WINDOWS;
    try {
        return Files.getFileStore(path);
    } catch (FileSystemException possibleBug) {
        final char driveLetter;
        // if something goes wrong, we just deliver the original exception
        try {
            String root = path.toRealPath().getRoot().toString();
            if (root.length() < 2) {
                throw new RuntimeException("root isn't a drive letter: " + root);
            }
            driveLetter = Character.toLowerCase(root.charAt(0));
            if (Character.isAlphabetic(driveLetter) == false || root.charAt(1) != ':') {
                throw new RuntimeException("root isn't a drive letter: " + root);
            }
        } catch (Exception checkFailed) {
            // something went wrong, 
            possibleBug.addSuppressed(checkFailed);
            throw possibleBug;
        }
        // we have a drive letter: the hack begins!!!!!!!!
        try {
            // we have no choice but to parse toString of all stores and find the matching drive letter
            for (FileStore store : fileStores) {
                String toString = store.toString();
                int length = toString.length();
                if (length > 3 && toString.endsWith(":)") && toString.charAt(length - 4) == '(') {
                    if (Character.toLowerCase(toString.charAt(length - 3)) == driveLetter) {
                        return store;
                    }
                }
            }
            throw new RuntimeException("no filestores matched");
        } catch (Exception weTried) {
            IOException newException = new IOException("Unable to retrieve filestore for '" + path + "', tried matching against " + Arrays.toString(fileStores), weTried);
            newException.addSuppressed(possibleBug);
            throw newException;
        }
    }
}
Also used : FileStore(java.nio.file.FileStore) FileSystemException(java.nio.file.FileSystemException) IOException(java.io.IOException) FileSystemException(java.nio.file.FileSystemException) IOException(java.io.IOException) SuppressForbidden(org.elasticsearch.common.SuppressForbidden)

Aggregations

FileSystemException (java.nio.file.FileSystemException)14 Path (java.nio.file.Path)9 IOException (java.io.IOException)5 Test (org.junit.Test)5 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)4 NoSuchFileException (java.nio.file.NoSuchFileException)4 AccessDeniedException (java.nio.file.AccessDeniedException)3 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)3 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)3 FileSystemLoopException (java.nio.file.FileSystemLoopException)3 NotDirectoryException (java.nio.file.NotDirectoryException)3 EOFException (java.io.EOFException)2 FileNotFoundException (java.io.FileNotFoundException)2 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)2 IndexFormatTooNewException (org.apache.lucene.index.IndexFormatTooNewException)2 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)2 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)2 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 File (java.io.File)1