use of java.nio.file.FileAlreadyExistsException 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.FileAlreadyExistsException in project jimfs by google.
the class JimfsUnixLikeFileSystemTest method testWriteLines_withStandardOptions.
@Test
public void testWriteLines_withStandardOptions() throws IOException {
Path test = path("/test.txt");
ImmutableList<String> lines = ImmutableList.of("hello", "world");
try {
// CREATE and CREATE_NEW not specified
Files.write(test, lines, UTF_8, WRITE);
fail();
} catch (NoSuchFileException expected) {
assertEquals(test.toString(), expected.getMessage());
}
// succeeds, file does not exist
Files.write(test, lines, UTF_8, CREATE_NEW);
assertThatPath(test).containsLines(lines);
try {
// CREATE_NEW requires file not exist
Files.write(test, lines, UTF_8, CREATE_NEW);
fail();
} catch (FileAlreadyExistsException expected) {
}
// succeeds, ok for file to already exist
Files.write(test, ImmutableList.of("foo"), UTF_8, CREATE);
// did not truncate or append, so overwrote
if (System.getProperty("line.separator").length() == 2) {
// on Windows, an extra character is overwritten by the \r\n line separator
assertThatPath(test).containsLines("foo", "", "world");
} else {
assertThatPath(test).containsLines("foo", "o", "world");
}
// default options
Files.write(test, lines, UTF_8, WRITE, CREATE, TRUNCATE_EXISTING);
assertThatPath(test).containsLines(lines);
Files.write(test, lines, UTF_8, WRITE, APPEND);
assertThatPath(test).containsLines("hello", "world", "hello", "world");
Files.write(test, lines, UTF_8, WRITE, CREATE, TRUNCATE_EXISTING, APPEND, SPARSE, DSYNC, SYNC);
assertThatPath(test).containsLines(lines);
try {
// READ not allowed
Files.write(test, lines, UTF_8, READ, WRITE);
fail();
} catch (UnsupportedOperationException expected) {
}
}
use of java.nio.file.FileAlreadyExistsException in project jimfs by google.
the class JimfsUnixLikeFileSystemTest method testWriteFile_withStandardOptions.
@Test
public void testWriteFile_withStandardOptions() throws IOException {
Path test = path("/test");
byte[] bytes = { 0, 1, 2, 3 };
try {
// CREATE and CREATE_NEW not specified
Files.write(test, bytes, WRITE);
fail();
} catch (NoSuchFileException expected) {
assertEquals(test.toString(), expected.getMessage());
}
// succeeds, file does not exist
Files.write(test, bytes, CREATE_NEW);
assertThatPath("/test").containsBytes(bytes);
try {
// CREATE_NEW requires file not exist
Files.write(test, bytes, CREATE_NEW);
fail();
} catch (FileAlreadyExistsException expected) {
assertEquals(test.toString(), expected.getMessage());
}
// succeeds, ok for file to already exist
Files.write(test, new byte[] { 4, 5 }, CREATE);
// did not truncate or append, so overwrote
assertThatPath("/test").containsBytes(4, 5, 2, 3);
// default options
Files.write(test, bytes, WRITE, CREATE, TRUNCATE_EXISTING);
assertThatPath("/test").containsBytes(bytes);
Files.write(test, bytes, WRITE, APPEND);
assertThatPath("/test").containsBytes(0, 1, 2, 3, 0, 1, 2, 3);
Files.write(test, bytes, WRITE, CREATE, TRUNCATE_EXISTING, APPEND, SPARSE, DSYNC, SYNC);
assertThatPath("/test").containsBytes(bytes);
try {
// READ not allowed
Files.write(test, bytes, READ, WRITE);
fail();
} catch (UnsupportedOperationException expected) {
}
}
use of java.nio.file.FileAlreadyExistsException in project jimfs by google.
the class ConfigurationTest method testFileSystemForCustomConfiguration.
@Test
public void testFileSystemForCustomConfiguration() throws IOException {
Configuration config = Configuration.builder(PathType.unix()).setRoots("/").setWorkingDirectory("/hello/world").setNameCanonicalNormalization(NFD, CASE_FOLD_UNICODE).setNameDisplayNormalization(NFC).setPathEqualityUsesCanonicalForm(true).setBlockSize(10).setMaxSize(100).setMaxCacheSize(50).setAttributeViews("unix").setDefaultAttributeValue("posix:permissions", PosixFilePermissions.fromString("---------")).build();
FileSystem fs = Jimfs.newFileSystem(config);
assertThat(fs.getRootDirectories()).containsExactlyElementsIn(ImmutableList.of(fs.getPath("/"))).inOrder();
assertThatPath(fs.getPath("").toRealPath()).isEqualTo(fs.getPath("/hello/world"));
assertThat(Iterables.getOnlyElement(fs.getFileStores()).getTotalSpace()).isEqualTo(100);
assertThat(fs.supportedFileAttributeViews()).containsExactly("basic", "owner", "posix", "unix");
Files.createFile(fs.getPath("/foo"));
assertThat(Files.getAttribute(fs.getPath("/foo"), "posix:permissions")).isEqualTo(PosixFilePermissions.fromString("---------"));
try {
Files.createFile(fs.getPath("/FOO"));
fail();
} catch (FileAlreadyExistsException expected) {
}
}
use of java.nio.file.FileAlreadyExistsException in project jimfs by google.
the class ConfigurationTest method testFileSystemForDefaultWindowsConfiguration.
@Test
public void testFileSystemForDefaultWindowsConfiguration() throws IOException {
FileSystem fs = Jimfs.newFileSystem(Configuration.windows());
assertThat(fs.getRootDirectories()).containsExactlyElementsIn(ImmutableList.of(fs.getPath("C:\\"))).inOrder();
assertThatPath(fs.getPath("").toRealPath()).isEqualTo(fs.getPath("C:\\work"));
assertThat(Iterables.getOnlyElement(fs.getFileStores()).getTotalSpace()).isEqualTo(4L * 1024 * 1024 * 1024);
assertThat(fs.supportedFileAttributeViews()).containsExactly("basic");
Files.createFile(fs.getPath("C:\\foo"));
try {
Files.createFile(fs.getPath("C:\\FOO"));
fail();
} catch (FileAlreadyExistsException expected) {
}
}
Aggregations