Search in sources :

Example 1 with Directory

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();
}
Also used : Path(java.nio.file.Path) Directory(org.cryptomator.cryptofs.CryptoPathMapper.Directory) Test(org.junit.Test)

Example 2 with Directory

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);
}
Also used : Path(java.nio.file.Path) DirectoryStream(java.nio.file.DirectoryStream) Directory(org.cryptomator.cryptofs.CryptoPathMapper.Directory) Test(org.junit.Test)

Example 3 with Directory

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;
}
Also used : ClosedFileSystemException(java.nio.file.ClosedFileSystemException) Directory(org.cryptomator.cryptofs.CryptoPathMapper.Directory)

Example 4 with Directory

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);
}
Also used : Path(java.nio.file.Path) Directory(org.cryptomator.cryptofs.CryptoPathMapper.Directory) Test(org.junit.Test)

Example 5 with Directory

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;
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileChannel(java.nio.channels.FileChannel) NoSuchFileException(java.nio.file.NoSuchFileException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Directory(org.cryptomator.cryptofs.CryptoPathMapper.Directory)

Aggregations

Directory (org.cryptomator.cryptofs.CryptoPathMapper.Directory)9 Path (java.nio.file.Path)8 Test (org.junit.Test)5 IOException (java.io.IOException)2 DirectoryStream (java.nio.file.DirectoryStream)2 Before (org.junit.Before)2 UncheckedIOException (java.io.UncheckedIOException)1 FileChannel (java.nio.channels.FileChannel)1 ClosedFileSystemException (java.nio.file.ClosedFileSystemException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 FileSystem (java.nio.file.FileSystem)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 FileSystemProvider (java.nio.file.spi.FileSystemProvider)1 ArrayList (java.util.ArrayList)1