Search in sources :

Example 61 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project cryptofs by cryptomator.

the class CryptoFileSystemImpl method copy.

void copy(CryptoPath cleartextSource, CryptoPath cleartextTarget, CopyOption... options) throws IOException {
    if (cleartextSource.equals(cleartextTarget)) {
        return;
    }
    Path ciphertextSourceFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource, CiphertextFileType.FILE);
    Path ciphertextSourceDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource, CiphertextFileType.DIRECTORY);
    if (Files.exists(ciphertextSourceFile)) {
        // FILE:
        Path ciphertextTargetFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget, CiphertextFileType.FILE);
        Files.copy(ciphertextSourceFile, ciphertextTargetFile, options);
    } else if (Files.exists(ciphertextSourceDirFile)) {
        // DIRECTORY (non-recursive as per contract):
        Path ciphertextTargetDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget, CiphertextFileType.DIRECTORY);
        if (!Files.exists(ciphertextTargetDirFile)) {
            // create new:
            createDirectory(cleartextTarget);
        } else if (ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING)) {
            // keep existing (if empty):
            Path ciphertextTargetDir = cryptoPathMapper.getCiphertextDirPath(cleartextTarget);
            try (DirectoryStream<Path> ds = Files.newDirectoryStream(ciphertextTargetDir)) {
                if (ds.iterator().hasNext()) {
                    throw new DirectoryNotEmptyException(cleartextTarget.toString());
                }
            }
        } else {
            throw new FileAlreadyExistsException(cleartextTarget.toString());
        }
        if (ArrayUtils.contains(options, StandardCopyOption.COPY_ATTRIBUTES)) {
            Path ciphertextSourceDir = cryptoPathMapper.getCiphertextDirPath(cleartextSource);
            Path ciphertextTargetDir = cryptoPathMapper.getCiphertextDirPath(cleartextTarget);
            copyAttributes(ciphertextSourceDir, ciphertextTargetDir);
        }
    } else {
        throw new NoSuchFileException(cleartextSource.toString());
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException)

Example 62 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project cryptofs by cryptomator.

the class CopyOperation method copy.

public void copy(CryptoPath source, CryptoPath target, CopyOption... options) throws IOException {
    if (source.equals(target)) {
        return;
    }
    if (pathsBelongToSameFileSystem(source, target)) {
        source.getFileSystem().copy(source, target, options);
    } else {
        Optional<BasicFileAttributes> sourceAttrs = attributes(source);
        Optional<BasicFileAttributes> targetAttrs = attributes(target);
        if (!sourceAttrs.isPresent()) {
            throw new NoSuchFileException(source.toUri().toString());
        }
        if (targetAttrs.isPresent()) {
            if (ArrayUtils.contains(options, REPLACE_EXISTING)) {
                provider(target).delete(target);
            } else {
                throw new FileAlreadyExistsException(target.toUri().toString());
            }
        }
        if (sourceAttrs.get().isDirectory()) {
            provider(target).createDirectory(target);
        } else {
            transferFullFile(source, target);
        }
        if (ArrayUtils.contains(options, COPY_ATTRIBUTES)) {
            BasicFileAttributeView targetAttrView = provider(target).getFileAttributeView(target, BasicFileAttributeView.class);
            if (targetAttrView != null) {
                targetAttrView.setTimes(sourceAttrs.get().lastModifiedTime(), sourceAttrs.get().lastAccessTime(), sourceAttrs.get().creationTime());
            }
        }
    }
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) NoSuchFileException(java.nio.file.NoSuchFileException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 63 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project cryptofs by cryptomator.

the class CopyOperationTest method testCopyExistingFileToNonExistingFileOnDifferentFileSystem.

@Test
public void testCopyExistingFileToNonExistingFileOnDifferentFileSystem() throws IOException {
    FileChannelMock targetFile = new FileChannelMock(100);
    BasicFileAttributes aPathFromFsAAttributes = mock(BasicFileAttributes.class);
    when(provider.readAttributes(aPathFromFsA, BasicFileAttributes.class)).thenReturn(aPathFromFsAAttributes);
    when(provider.readAttributes(aPathFromFsB, BasicFileAttributes.class)).thenThrow(new NoSuchFileException("aPathFromFsB"));
    when(provider.newFileChannel(aPathFromFsA, EnumSet.of(READ))).thenReturn(new FileChannelMock(repeat(42).times(20).asByteBuffer()));
    when(provider.newFileChannel(aPathFromFsB, EnumSet.of(CREATE_NEW, WRITE))).thenReturn(targetFile);
    inTest.copy(aPathFromFsA, aPathFromFsB);
    assertThat(targetFile.data(), contains(repeat(42).times(20).asByteBuffer()));
}
Also used : FileChannelMock(org.cryptomator.cryptofs.mocks.FileChannelMock) NoSuchFileException(java.nio.file.NoSuchFileException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test)

Example 64 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project cryptofs by cryptomator.

the class CopyOperationTest method testCopyExistingFileToNonExistingFileOnDifferentFileSystemWithCopyAttributesFlagSetsFileTimes.

@Test
public void testCopyExistingFileToNonExistingFileOnDifferentFileSystemWithCopyAttributesFlagSetsFileTimes() throws IOException {
    FileTime creationTime = FileTime.fromMillis(3883483);
    FileTime lastModifiedTime = FileTime.fromMillis(3883484);
    FileTime lastAccessTime = FileTime.fromMillis(3883485);
    FileChannelMock targetFile = new FileChannelMock(100);
    BasicFileAttributes aPathFromFsAAttributes = mock(BasicFileAttributes.class);
    BasicFileAttributeView aPathFromFsBAttributeView = mock(BasicFileAttributeView.class);
    when(provider.readAttributes(aPathFromFsA, BasicFileAttributes.class)).thenReturn(aPathFromFsAAttributes);
    when(provider.getFileAttributeView(aPathFromFsB, BasicFileAttributeView.class)).thenReturn(aPathFromFsBAttributeView);
    when(provider.readAttributes(aPathFromFsB, BasicFileAttributes.class)).thenThrow(new NoSuchFileException("aPathFromFsB"));
    when(provider.newFileChannel(aPathFromFsA, EnumSet.of(READ))).thenReturn(new FileChannelMock(repeat(42).times(20).asByteBuffer()));
    when(provider.newFileChannel(aPathFromFsB, EnumSet.of(CREATE_NEW, WRITE))).thenReturn(targetFile);
    when(aPathFromFsAAttributes.creationTime()).thenReturn(creationTime);
    when(aPathFromFsAAttributes.lastModifiedTime()).thenReturn(lastModifiedTime);
    when(aPathFromFsAAttributes.lastAccessTime()).thenReturn(lastAccessTime);
    inTest.copy(aPathFromFsA, aPathFromFsB, COPY_ATTRIBUTES);
    assertThat(targetFile.data(), contains(repeat(42).times(20).asByteBuffer()));
    verify(aPathFromFsBAttributeView).setTimes(lastModifiedTime, lastAccessTime, creationTime);
}
Also used : FileChannelMock(org.cryptomator.cryptofs.mocks.FileChannelMock) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) NoSuchFileException(java.nio.file.NoSuchFileException) FileTime(java.nio.file.attribute.FileTime) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test)

Example 65 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project cryptofs by cryptomator.

the class ConflictResolverTest method testRenameLongFile.

@Test
public void testRenameLongFile() throws IOException {
    String longCiphertextName = "ABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGH2345====";
    assert longCiphertextName.length() > Constants.SHORT_NAMES_MAX_LENGTH;
    Mockito.when(testFileName.toString()).thenReturn("ABCDEF== (1).lng");
    Mockito.when(longFileNameProvider.inflate("ABCDEF==.lng")).thenReturn("FEDCBA==");
    Mockito.when(longFileNameProvider.deflate(longCiphertextName)).thenReturn("FEDCBA==.lng");
    Mockito.when(longFileNameProvider.isDeflated("ABCDEF== (1).lng")).thenReturn(true);
    Mockito.when(filenameCryptor.decryptFilename(Mockito.eq("FEDCBA=="), Mockito.any())).thenReturn("fedcba");
    Mockito.when(filenameCryptor.encryptFilename(Mockito.startsWith("fedcba ("), Mockito.any())).thenReturn(longCiphertextName);
    Mockito.doThrow(new NoSuchFileException("FEDCBA==.lng")).when(testFileSystemProvider).checkAccess(Mockito.argThat(hasFileName("FEDCBA==.lng")));
    Path resolved = conflictResolver.resolveConflictsIfNecessary(testFile, dirId);
    Mockito.verify(longFileNameProvider).deflate(longCiphertextName);
    Mockito.verify(testFileSystemProvider).move(Mockito.argThat(hasFileName("ABCDEF== (1).lng")), Mockito.argThat(hasFileName("FEDCBA==.lng")), Mockito.any());
    Assert.assertEquals("FEDCBA==.lng", resolved.getFileName().toString());
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) Test(org.junit.Test)

Aggregations

NoSuchFileException (java.nio.file.NoSuchFileException)262 IOException (java.io.IOException)107 Path (java.nio.file.Path)104 FileNotFoundException (java.io.FileNotFoundException)41 Test (org.junit.Test)35 InputStream (java.io.InputStream)31 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)25 File (java.io.File)22 NotDirectoryException (java.nio.file.NotDirectoryException)19 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)18 ArrayList (java.util.ArrayList)16 HashSet (java.util.HashSet)16 OutputStream (java.io.OutputStream)15 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)15 FileChannel (java.nio.channels.FileChannel)14 AccessDeniedException (java.nio.file.AccessDeniedException)14 ByteBuffer (java.nio.ByteBuffer)13 HashMap (java.util.HashMap)13 Map (java.util.Map)12 SeekableByteChannel (java.nio.channels.SeekableByteChannel)11