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