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());
}
}
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();
}
}
}
}
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());
}
}
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);
}
}
}
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);
}
}
Aggregations