use of org.cryptomator.cryptofs.CryptoPathMapper.Directory in project cryptofs by cryptomator.
the class DirectoryStreamFactoryTest method testCloseClosesAllNonClosedDirectoryStreams.
@SuppressWarnings("unchecked")
@Test
public void testCloseClosesAllNonClosedDirectoryStreams() throws IOException {
Filter<? super Path> filter = mock(Filter.class);
CryptoPath pathA = mock(CryptoPath.class);
CryptoPath pathB = mock(CryptoPath.class);
Path dirPathA = mock(Path.class);
when(dirPathA.getFileSystem()).thenReturn(fileSystem);
Path dirPathB = mock(Path.class);
when(dirPathB.getFileSystem()).thenReturn(fileSystem);
when(cryptoPathMapper.getCiphertextDir(pathA)).thenReturn(new Directory("dirIdA", dirPathA));
when(cryptoPathMapper.getCiphertextDir(pathB)).thenReturn(new Directory("dirIdB", dirPathB));
DirectoryStream<Path> streamA = mock(DirectoryStream.class);
DirectoryStream<Path> streamB = mock(DirectoryStream.class);
when(provider.newDirectoryStream(same(dirPathA), any())).thenReturn(streamA);
when(provider.newDirectoryStream(same(dirPathB), any())).thenReturn(streamB);
inTest.newDirectoryStream(pathA, filter);
inTest.newDirectoryStream(pathB, filter);
inTest.close();
verify(streamA).close();
verify(streamB).close();
}
use of org.cryptomator.cryptofs.CryptoPathMapper.Directory in project cryptofs by cryptomator.
the class DirectoryStreamFactoryTest method testNewDirectoryStreamAfterClosedThrowsClosedFileSystemException.
@SuppressWarnings("unchecked")
@Test
public void testNewDirectoryStreamAfterClosedThrowsClosedFileSystemException() throws IOException {
CryptoPath path = mock(CryptoPath.class);
Filter<? super Path> filter = mock(Filter.class);
String dirId = "dirIdAbc";
Path dirPath = mock(Path.class);
when(dirPath.getFileSystem()).thenReturn(fileSystem);
when(cryptoPathMapper.getCiphertextDir(path)).thenReturn(new Directory(dirId, dirPath));
when(provider.newDirectoryStream(same(dirPath), any())).thenReturn(mock(DirectoryStream.class));
thrown.expect(ClosedFileSystemException.class);
inTest.close();
inTest.newDirectoryStream(path, filter);
}
use of org.cryptomator.cryptofs.CryptoPathMapper.Directory in project cryptofs by cryptomator.
the class DirectoryStreamFactory method newDirectoryStream.
public CryptoDirectoryStream newDirectoryStream(CryptoPath cleartextDir, Filter<? super Path> filter) throws IOException {
Directory ciphertextDir = cryptoPathMapper.getCiphertextDir(cleartextDir);
CryptoDirectoryStream stream = new //
CryptoDirectoryStream(//
ciphertextDir, //
cleartextDir, //
cryptor.fileNameCryptor(), //
cryptoPathMapper, //
longFileNameProvider, //
conflictResolver, //
filter, //
closed -> streams.remove(closed), //
finallyUtil, encryptedNamePattern);
streams.put(stream, stream);
if (closed) {
stream.close();
throw new ClosedFileSystemException();
}
return stream;
}
use of org.cryptomator.cryptofs.CryptoPathMapper.Directory in project cryptofs by cryptomator.
the class DirectoryStreamFactoryTest method testNewDirectoryStreamCreatesDirectoryStream.
@SuppressWarnings("unchecked")
@Test
public void testNewDirectoryStreamCreatesDirectoryStream() throws IOException {
CryptoPath path = mock(CryptoPath.class);
Filter<? super Path> filter = mock(Filter.class);
String dirId = "dirIdAbc";
Path dirPath = mock(Path.class);
when(dirPath.getFileSystem()).thenReturn(fileSystem);
when(cryptoPathMapper.getCiphertextDir(path)).thenReturn(new Directory(dirId, dirPath));
DirectoryStream<Path> directoryStream = inTest.newDirectoryStream(path, filter);
assertNotNull(directoryStream);
}
use of org.cryptomator.cryptofs.CryptoPathMapper.Directory in project cryptofs by cryptomator.
the class CryptoFileSystemImpl method createDirectory.
void createDirectory(CryptoPath cleartextDir, FileAttribute<?>... attrs) throws IOException {
CryptoPath cleartextParentDir = cleartextDir.getParent();
if (cleartextParentDir == null) {
return;
}
Path ciphertextParentDir = cryptoPathMapper.getCiphertextDirPath(cleartextParentDir);
if (!Files.exists(ciphertextParentDir)) {
throw new NoSuchFileException(cleartextParentDir.toString());
}
Path ciphertextFile = cryptoPathMapper.getCiphertextFilePath(cleartextDir, CiphertextFileType.FILE);
if (Files.exists(ciphertextFile)) {
throw new FileAlreadyExistsException(cleartextDir.toString());
}
Path ciphertextDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextDir, CiphertextFileType.DIRECTORY);
Directory ciphertextDir = cryptoPathMapper.getCiphertextDir(cleartextDir);
// atomically check for FileAlreadyExists and create otherwise:
try (FileChannel channel = FileChannel.open(ciphertextDirFile, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), attrs)) {
channel.write(ByteBuffer.wrap(ciphertextDir.dirId.getBytes(UTF_8)));
}
// create dir if and only if the dirFile has been created right now (not if it has been created before):
try {
Files.createDirectories(ciphertextDir.path);
} catch (IOException e) {
// make sure there is no orphan dir file:
Files.delete(ciphertextDirFile);
dirIdProvider.delete(ciphertextDirFile);
throw e;
}
}
Aggregations