Search in sources :

Example 41 with FileAlreadyExistsException

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) {
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileSystem(java.nio.file.FileSystem) Test(org.junit.Test)

Example 42 with FileAlreadyExistsException

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) {
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileSystem(java.nio.file.FileSystem) Test(org.junit.Test)

Example 43 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project jimfs by google.

the class FileSystemView method copy.

/**
 * Copies or moves the file at the given source path to the given dest path.
 */
public void copy(JimfsPath source, FileSystemView destView, JimfsPath dest, Set<CopyOption> options, boolean move) throws IOException {
    checkNotNull(source);
    checkNotNull(destView);
    checkNotNull(dest);
    checkNotNull(options);
    boolean sameFileSystem = isSameFileSystem(destView);
    File sourceFile;
    // non-null after block completes iff source file was copied
    File copyFile = null;
    lockBoth(store.writeLock(), destView.store.writeLock());
    try {
        DirectoryEntry sourceEntry = lookUp(source, options).requireExists(source);
        DirectoryEntry destEntry = destView.lookUp(dest, Options.NOFOLLOW_LINKS);
        Directory sourceParent = sourceEntry.directory();
        sourceFile = sourceEntry.file();
        Directory destParent = destEntry.directory();
        if (move && sourceFile.isDirectory()) {
            if (sameFileSystem) {
                checkMovable(sourceFile, source);
                checkNotAncestor(sourceFile, destParent, destView);
            } else {
                // move to another file system is accomplished by copy-then-delete, so the source file
                // must be deletable to be moved
                checkDeletable(sourceFile, DeleteMode.ANY, source);
            }
        }
        if (destEntry.exists()) {
            if (destEntry.file().equals(sourceFile)) {
                return;
            } else if (options.contains(REPLACE_EXISTING)) {
                destView.delete(destEntry, DeleteMode.ANY, dest);
            } else {
                throw new FileAlreadyExistsException(dest.toString());
            }
        }
        if (move && sameFileSystem) {
            // Real move on the same file system.
            sourceParent.unlink(source.name());
            sourceParent.updateModifiedTime();
            destParent.link(dest.name(), sourceFile);
            destParent.updateModifiedTime();
        } else {
            // Doing a copy OR a move to a different file system, which must be implemented by copy and
            // delete.
            // By default, don't copy attributes.
            AttributeCopyOption attributeCopyOption = AttributeCopyOption.NONE;
            if (move) {
                // Copy only the basic attributes of the file to the other file system, as it may not
                // support all the attribute views that this file system does. This also matches the
                // behavior of moving a file to a foreign file system with a different
                // FileSystemProvider.
                attributeCopyOption = AttributeCopyOption.BASIC;
            } else if (options.contains(COPY_ATTRIBUTES)) {
                // As with move, if we're copying the file to a different file system, only copy its
                // basic attributes.
                attributeCopyOption = sameFileSystem ? AttributeCopyOption.ALL : AttributeCopyOption.BASIC;
            }
            // Copy the file, but don't copy its content while we're holding the file store locks.
            copyFile = destView.store.copyWithoutContent(sourceFile, attributeCopyOption);
            destParent.link(dest.name(), copyFile);
            destParent.updateModifiedTime();
            // In order for the copy to be atomic (not strictly necessary, but seems preferable since
            // we can) lock both source and copy files before leaving the file store locks. This
            // ensures that users cannot observe the copy's content until the content has been copied.
            // This also marks the source file as opened, preventing its content from being deleted
            // until after it's copied if the source file itself is deleted in the next step.
            lockSourceAndCopy(sourceFile, copyFile);
            if (move) {
                // It should not be possible for delete to throw an exception here, because we already
                // checked that the file was deletable above.
                delete(sourceEntry, DeleteMode.ANY, source);
            }
        }
    } finally {
        destView.store.writeLock().unlock();
        store.writeLock().unlock();
    }
    if (copyFile != null) {
        // different threads.
        try {
            sourceFile.copyContentTo(copyFile);
        } finally {
            // Unlock the files, allowing the content of the copy to be observed by the user. This also
            // closes the source file, allowing its content to be deleted if it was deleted.
            unlockSourceAndCopy(sourceFile, copyFile);
        }
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException)

Example 44 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project j2objc by google.

the class FilesTest method test_createFile.

/* J2ObjC removed: mockito unsupported
    @Mock
    private Path mockPath;
    @Mock
    private Path mockPath2;
    @Mock
    private FileSystem mockFileSystem;
    @Mock
    private FileSystemProvider mockFileSystemProvider;
     */
/* J2ObjC removed: mockito unsupported
    @Before
    public void setUp() throws Exception {
        when(mockPath.getFileSystem()).thenReturn(mockFileSystem);
        when(mockPath2.getFileSystem()).thenReturn(mockFileSystem);
        when(mockFileSystem.provider()).thenReturn(mockFileSystemProvider);
    }
     */
/* J2ObjC removed: mockito unsupported
    @Test
    public void test_newInputStream() throws IOException {
        try (InputStream is = new ByteArrayInputStream(new byte[0])) {

            when(mockFileSystemProvider.newInputStream(mockPath, READ)).thenReturn(is);

            assertSame(is, Files.newInputStream(mockPath, READ));

            verify(mockFileSystemProvider).newInputStream(mockPath, READ);
        }
    }
     */
/* J2ObjC removed: mockito unsupported
    @Test
    public void test_newOutputStream() throws IOException {
        try (OutputStream os = new ByteArrayOutputStream()) {

            when(mockFileSystemProvider.newOutputStream(mockPath, APPEND)).thenReturn(os);

            assertSame(os, Files.newOutputStream(mockPath, APPEND));

            verify(mockFileSystemProvider).newOutputStream(mockPath, APPEND);
        }
    }
     */
/* J2ObjC removed: mockito unsupported
    @Test
    public void test_newByteChannel() throws IOException {
        try (FileChannel sfc = FileChannel.open(filesSetup.getDataFilePath())) {
            HashSet<OpenOption> openOptions = new HashSet<>();
            openOptions.add(READ);

            when(mockFileSystemProvider.newByteChannel(mockPath, openOptions)).thenReturn(sfc);

            assertSame(sfc, Files.newByteChannel(mockPath, READ));

            verify(mockFileSystemProvider).newByteChannel(mockPath, openOptions);
        }
    }
     */
@Test
public void test_createFile() throws IOException {
    assertFalse(Files.exists(filesSetup.getTestPath()));
    Files.createFile(filesSetup.getTestPath());
    assertTrue(Files.exists(filesSetup.getTestPath()));
    // File with unicode name.
    Path unicodeFilePath = filesSetup.getPathInTestDir("परीक्षण फ़ाइल");
    Files.createFile(unicodeFilePath);
    Files.exists(unicodeFilePath);
    // When file exists.
    try {
        Files.createFile(filesSetup.getDataFilePath());
        fail();
    } catch (FileAlreadyExistsException expected) {
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) Test(org.junit.Test)

Example 45 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project j2objc by google.

the class FileAlreadyExistsExceptionTest method test_constructor$String$String$String.

public void test_constructor$String$String$String() {
    FileAlreadyExistsException exception = new FileAlreadyExistsException("file", "otherFile", "reason");
    assertEquals("file", exception.getFile());
    assertEquals("otherFile", exception.getOtherFile());
    assertEquals("reason", exception.getReason());
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException)

Aggregations

FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)120 Path (java.nio.file.Path)59 IOException (java.io.IOException)51 File (java.io.File)27 NoSuchFileException (java.nio.file.NoSuchFileException)23 Test (org.junit.Test)16 FileNotFoundException (java.io.FileNotFoundException)10 AccessDeniedException (java.nio.file.AccessDeniedException)9 FileSystemException (java.nio.file.FileSystemException)9 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)8 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)8 CopyOption (java.nio.file.CopyOption)7 NotDirectoryException (java.nio.file.NotDirectoryException)7 OutputStream (java.io.OutputStream)6 FileSystem (java.nio.file.FileSystem)6 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)5 FileVisitResult (java.nio.file.FileVisitResult)5 StandardCopyOption (java.nio.file.StandardCopyOption)5 MCRPath (org.mycore.datamodel.niofs.MCRPath)5 FileOutputStream (java.io.FileOutputStream)4