diff --git a/main/filesystem-crypto/src/main/java/org/cryptomator/filesystem/crypto/CryptoFileSystem.java b/main/filesystem-crypto/src/main/java/org/cryptomator/filesystem/crypto/CryptoFileSystem.java index d5617c982..242f07071 100644 --- a/main/filesystem-crypto/src/main/java/org/cryptomator/filesystem/crypto/CryptoFileSystem.java +++ b/main/filesystem-crypto/src/main/java/org/cryptomator/filesystem/crypto/CryptoFileSystem.java @@ -8,7 +8,8 @@ *******************************************************************************/ package org.cryptomator.filesystem.crypto; -import java.nio.ByteBuffer; +import java.io.UncheckedIOException; +import java.time.Instant; import java.util.Optional; import org.cryptomator.crypto.engine.Cryptor; @@ -16,12 +17,11 @@ import org.cryptomator.crypto.engine.InvalidPassphraseException; import org.cryptomator.filesystem.File; import org.cryptomator.filesystem.FileSystem; import org.cryptomator.filesystem.Folder; -import org.cryptomator.filesystem.WritableFile; public class CryptoFileSystem extends CryptoFolder implements FileSystem { private static final String DATA_ROOT_DIR = "d"; - private static final String ROOT_DIR_FILE = "root"; + private static final String ROOT_DIRECOTRY_ID = ""; private final Folder physicalRoot; private final CryptoFileSystemDelegate delegate; @@ -40,9 +40,14 @@ public class CryptoFileSystem extends CryptoFolder implements FileSystem { return delegate; } + @Override + protected String getDirectoryId() { + return ROOT_DIRECOTRY_ID; + } + @Override protected File physicalFile() { - return physicalDataRoot().file(ROOT_DIR_FILE); + throw new UnsupportedOperationException("Crypto filesystem root doesn't provide a directory file, as the directory ID is fixed."); } @Override @@ -57,23 +62,31 @@ public class CryptoFileSystem extends CryptoFolder implements FileSystem { @Override public boolean exists() { - return physicalFile().exists() && physicalFolder().exists(); + return physicalFolder().exists(); + } + + @Override + public Optional creationTime() throws UncheckedIOException { + return physicalFolder().creationTime(); + } + + @Override + public Instant lastModified() { + return physicalFolder().lastModified(); } @Override public void delete() { - // no-op. + throw new UnsupportedOperationException("Can not delete CryptoFileSytem root."); + } + + @Override + public void moveTo(Folder target) { + throw new UnsupportedOperationException("Can not move CryptoFileSytem root."); } @Override public void create() { - physicalDataRoot().create(); - final File dirFile = physicalFile(); - final String directoryId = getDirectoryId(); - try (WritableFile writable = dirFile.openWritable()) { - final ByteBuffer buf = ByteBuffer.wrap(directoryId.getBytes()); - writable.write(buf); - } physicalFolder().create(); } diff --git a/main/filesystem-crypto/src/test/java/org/cryptomator/filesystem/crypto/CryptoFileSystemComponentIntegrationTest.java b/main/filesystem-crypto/src/test/java/org/cryptomator/filesystem/crypto/CryptoFileSystemComponentIntegrationTest.java index 94acb537f..826187e31 100644 --- a/main/filesystem-crypto/src/test/java/org/cryptomator/filesystem/crypto/CryptoFileSystemComponentIntegrationTest.java +++ b/main/filesystem-crypto/src/test/java/org/cryptomator/filesystem/crypto/CryptoFileSystemComponentIntegrationTest.java @@ -67,7 +67,6 @@ public class CryptoFileSystemComponentIntegrationTest { Assert.assertTrue(masterkeyBkupFile.exists()); Assert.assertTrue(physicalDataRoot.exists()); Assert.assertEquals(3, physicalFs.children().count()); // d + masterkey.cryptomator + masterkey.cryptomator.bkup - Assert.assertEquals(1, physicalDataRoot.files().count()); // ROOT file Assert.assertEquals(1, physicalDataRoot.folders().count()); // ROOT directory } diff --git a/main/filesystem-crypto/src/test/java/org/cryptomator/filesystem/crypto/CryptoFileSystemTest.java b/main/filesystem-crypto/src/test/java/org/cryptomator/filesystem/crypto/CryptoFileSystemTest.java index cf88835c4..26acd2400 100644 --- a/main/filesystem-crypto/src/test/java/org/cryptomator/filesystem/crypto/CryptoFileSystemTest.java +++ b/main/filesystem-crypto/src/test/java/org/cryptomator/filesystem/crypto/CryptoFileSystemTest.java @@ -13,6 +13,7 @@ import static org.cryptomator.filesystem.FileSystemVisitor.fileSystemVisitor; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.ByteBuffer; +import java.time.Instant; import java.util.Arrays; import java.util.concurrent.atomic.AtomicInteger; @@ -73,6 +74,45 @@ public class CryptoFileSystemTest { Assert.assertTrue(bazBarFolder.exists()); } + @Test(timeout = 1000, expected = UnsupportedOperationException.class) + public void testMovingOfRootDir() throws UncheckedIOException, IOException { + // mock stuff and prepare crypto FS: + final Cryptor cryptor = new NoCryptor(); + final FileSystem physicalFs = new InMemoryFileSystem(); + final FileSystem fs = new CryptoFileSystem(physicalFs, cryptor, Mockito.mock(CryptoFileSystemDelegate.class), "foo"); + fs.create(); + fs.moveTo(fs.folder("subFolder")); + } + + @Test(timeout = 1000, expected = UnsupportedOperationException.class) + public void testDeletingOfRootDir() throws UncheckedIOException, IOException { + // mock stuff and prepare crypto FS: + final Cryptor cryptor = new NoCryptor(); + final FileSystem physicalFs = new InMemoryFileSystem(); + final FileSystem fs = new CryptoFileSystem(physicalFs, cryptor, Mockito.mock(CryptoFileSystemDelegate.class), "foo"); + fs.create(); + fs.delete(); + } + + @Test(timeout = 100000) + public void testCreationAndLastModifiedDateOfRootDir() throws UncheckedIOException, IOException, InterruptedException { + // mock stuff and prepare crypto FS: + final Cryptor cryptor = new NoCryptor(); + final FileSystem physicalFs = new InMemoryFileSystem(); + + final Instant minDate = Instant.now(); + Thread.sleep(10); + final FileSystem fs = new CryptoFileSystem(physicalFs, cryptor, Mockito.mock(CryptoFileSystemDelegate.class), "foo"); + Thread.sleep(10); + final Instant maxDate = Instant.now(); + + Assert.assertTrue(fs.creationTime().isPresent()); + Assert.assertTrue(fs.creationTime().get().isAfter(minDate)); + Assert.assertTrue(fs.creationTime().get().isBefore(maxDate)); + Assert.assertTrue(fs.lastModified().isAfter(minDate)); + Assert.assertTrue(fs.lastModified().isBefore(maxDate)); + } + @Test(timeout = 1000, expected = IllegalArgumentException.class) public void testDirectoryMovingWithinBloodline() throws UncheckedIOException, IOException { // mock stuff and prepare crypto FS: