Search in sources :

Example 31 with FileSystem

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

the class MoreFilesTest method testDeleteDirectoryContents_symlinkToDir_sdsNotSupported_allowInsecure.

public void testDeleteDirectoryContents_symlinkToDir_sdsNotSupported_allowInsecure() throws IOException {
    try (FileSystem fs = newTestFileSystem()) {
        Path symlink = fs.getPath("/symlinktodir");
        Path dir = fs.getPath("dir");
        assertEquals(6, MoreFiles.listFiles(dir).size());
        MoreFiles.deleteDirectoryContents(symlink, ALLOW_INSECURE);
        assertEquals(0, MoreFiles.listFiles(dir).size());
    }
}
Also used : Path(java.nio.file.Path) FileSystem(java.nio.file.FileSystem)

Example 32 with FileSystem

use of java.nio.file.FileSystem 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 33 with FileSystem

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

the class MoreFilesTest method testDeleteRecursively_symlinkToDir_sdsNotSupported_allowInsecure.

public void testDeleteRecursively_symlinkToDir_sdsNotSupported_allowInsecure() throws IOException {
    try (FileSystem fs = newTestFileSystem()) {
        Path symlink = fs.getPath("/symlinktodir");
        Path dir = fs.getPath("dir");
        assertEquals(6, MoreFiles.listFiles(dir).size());
        MoreFiles.deleteRecursively(symlink, ALLOW_INSECURE);
        assertFalse(Files.exists(symlink));
        assertTrue(Files.exists(dir));
        assertEquals(6, MoreFiles.listFiles(dir).size());
    }
}
Also used : Path(java.nio.file.Path) FileSystem(java.nio.file.FileSystem)

Example 34 with FileSystem

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

the class MoreFilesTest method testDirectoryDeletion_emptyDir.

public void testDirectoryDeletion_emptyDir() throws IOException {
    for (DirectoryDeleteMethod method : EnumSet.allOf(DirectoryDeleteMethod.class)) {
        try (FileSystem fs = newTestFileSystem(SECURE_DIRECTORY_STREAM)) {
            Path emptyDir = fs.getPath("dir/e");
            assertEquals(0, MoreFiles.listFiles(emptyDir).size());
            method.delete(emptyDir);
            method.assertDeleteSucceeded(emptyDir);
        }
    }
}
Also used : Path(java.nio.file.Path) FileSystem(java.nio.file.FileSystem)

Example 35 with FileSystem

use of java.nio.file.FileSystem in project wire by square.

the class SchemaLoader method load.

public Schema load() throws IOException {
    if (sources.isEmpty()) {
        throw new IllegalStateException("No sources added.");
    }
    try (Closer closer = Closer.create()) {
        // Map the physical path to the file system root. For regular directories the key and the
        // value are equal. For ZIP files the key is the path to the .zip, and the value is the root
        // of the file system within it.
        Map<Path, Path> directories = new LinkedHashMap<>();
        for (Path source : sources) {
            if (Files.isRegularFile(source)) {
                FileSystem sourceFs = FileSystems.newFileSystem(source, getClass().getClassLoader());
                closer.register(sourceFs);
                directories.put(source, getOnlyElement(sourceFs.getRootDirectories()));
            } else {
                directories.put(source, source);
            }
        }
        return loadFromDirectories(directories);
    }
}
Also used : Closer(com.google.common.io.Closer) Path(java.nio.file.Path) FileSystem(java.nio.file.FileSystem) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

FileSystem (java.nio.file.FileSystem)159 Path (java.nio.file.Path)112 Test (org.junit.Test)66 IOException (java.io.IOException)25 File (java.io.File)17 ArrayList (java.util.ArrayList)14 FilterFileSystem (org.apache.lucene.mockfile.FilterFileSystem)13 URI (java.net.URI)11 FilterPath (org.apache.lucene.mockfile.FilterPath)11 InputStream (java.io.InputStream)10 OutputStream (java.io.OutputStream)10 FileStore (java.nio.file.FileStore)8 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)6 HashMap (java.util.HashMap)6 DefaultProjectFilesystemDelegate (com.facebook.buck.io.DefaultProjectFilesystemDelegate)5 ProjectFilesystemDelegate (com.facebook.buck.io.ProjectFilesystemDelegate)5 WindowsFS (org.apache.lucene.mockfile.WindowsFS)5 FSDirectory (org.apache.lucene.store.FSDirectory)5 BuckConfig (com.facebook.buck.cli.BuckConfig)4 FakeBuckConfig (com.facebook.buck.cli.FakeBuckConfig)4