瀏覽代碼

Fixed CryptoFileSystem

* avoid creation of a file and folder with equal names
Markus Kreusch 9 年之前
父節點
當前提交
8cdb6d0eab

+ 6 - 0
main/filesystem-crypto/src/main/java/org/cryptomator/filesystem/crypto/CryptoFile.java

@@ -11,6 +11,9 @@ package org.cryptomator.filesystem.crypto;
 import static java.nio.charset.StandardCharsets.UTF_8;
 
 import java.util.Optional;
+import java.io.UncheckedIOException;
+import java.nio.file.FileAlreadyExistsException;
+import java.time.Instant;
 
 import org.cryptomator.crypto.engine.Cryptor;
 import org.cryptomator.filesystem.File;
@@ -38,6 +41,9 @@ public class CryptoFile extends CryptoNode implements File {
 
 	@Override
 	public WritableFile openWritable() {
+		if (parent.folder(name).exists()) {
+			throw new UncheckedIOException(new FileAlreadyExistsException(toString()));
+		}
 		return new CryptoWritableFile(cryptor.getFileContentCryptor(), forceGetPhysicalFile().openWritable());
 	}
 

+ 4 - 0
main/filesystem-crypto/src/main/java/org/cryptomator/filesystem/crypto/CryptoFolder.java

@@ -16,6 +16,7 @@ import java.io.Reader;
 import java.io.UncheckedIOException;
 import java.io.Writer;
 import java.nio.channels.Channels;
+import java.nio.file.FileAlreadyExistsException;
 import java.util.Optional;
 import java.util.UUID;
 import java.util.concurrent.atomic.AtomicReference;
@@ -152,6 +153,9 @@ class CryptoFolder extends CryptoNode implements Folder {
 		} else if (!newDirIdGiven) {
 			throw new IllegalStateException("Newly created folder, that didn't exist before, already had an directoryId.");
 		}
+		if (parent.file(name).exists()) {
+			throw new UncheckedIOException(new FileAlreadyExistsException(toString()));
+		}
 		try (Writer writer = Channels.newWriter(dirFile.openWritable(), UTF_8.newEncoder(), -1)) {
 			writer.write(directoryId.get());
 		} catch (IOException e) {

+ 0 - 5
main/filesystem-invariants-tests/src/test/java/org/cryptomator/filesystem/invariants/FileTests.java

@@ -2,9 +2,7 @@ package org.cryptomator.filesystem.invariants;
 
 import static org.cryptomator.common.test.matcher.OptionalMatcher.presentOptionalWithValueThat;
 import static org.cryptomator.filesystem.invariants.matchers.InstantMatcher.inRangeInclusiveWithTolerance;
-import static org.hamcrest.CoreMatchers.containsString;
 import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.not;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assume.assumeThat;
 
@@ -180,9 +178,6 @@ public class FileTests {
 		assumeThat(wayToObtainANonExistingFile.returnedFilesExist(), is(false));
 		assumeThat(wayToObtainAnExistingFolder.returnedFoldersExist(), is(true));
 
-		// TODO implement checks in CryptoFileSystem to avoid creation of a file and folder with equal names
-		assumeThat(fileSystemFactory.toString(), not(containsString("Crypto")));
-
 		FileSystem fileSystem = fileSystemFactory.create();
 		File file = wayToObtainANonExistingFile.fileWithName(fileSystem, FILE_NAME);
 		wayToObtainAnExistingFolder.folderWithName(fileSystem, FILE_NAME);

+ 21 - 0
main/filesystem-invariants-tests/src/test/java/org/cryptomator/filesystem/invariants/FolderTests.java

@@ -8,10 +8,13 @@ import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assume.assumeThat;
 
+import java.io.UncheckedIOException;
+
 import org.cryptomator.filesystem.File;
 import org.cryptomator.filesystem.FileSystem;
 import org.cryptomator.filesystem.Folder;
 import org.cryptomator.filesystem.invariants.FileSystemFactories.FileSystemFactory;
+import org.cryptomator.filesystem.invariants.WaysToObtainAFile.WayToObtainAFile;
 import org.cryptomator.filesystem.invariants.WaysToObtainAFolder.WayToObtainAFolder;
 import org.junit.Rule;
 import org.junit.experimental.theories.DataPoints;
@@ -38,6 +41,9 @@ public class FolderTests {
 	@DataPoints
 	public static final Iterable<WayToObtainAFolder> WAYS_TO_OBTAIN_A_FOLDER = new WaysToObtainAFolder();
 
+	@DataPoints
+	public static final Iterable<WayToObtainAFile> WAYS_TO_OBTAIN_A_FILE = new WaysToObtainAFile();
+
 	@Rule
 	public final ExpectedException thrown = ExpectedException.none();
 
@@ -200,4 +206,19 @@ public class FolderTests {
 		assertThat(folder.isAncestorOf(childsChild), is(true));
 	}
 
+	@Theory
+	public void testFolderWhichExistsAsFileCanNotBeCreated(FileSystemFactory fileSystemFactory, WayToObtainAFolder wayToObtainANonExistingFolder, WayToObtainAFile wayToObtainAnExistingFile) {
+		assumeThat(wayToObtainAnExistingFile.returnedFilesExist(), is(true));
+		assumeThat(wayToObtainANonExistingFolder.returnedFoldersExist(), is(false));
+
+		FileSystem fileSystem = fileSystemFactory.create();
+
+		Folder folder = wayToObtainANonExistingFolder.folderWithName(fileSystem, FOLDER_NAME);
+		wayToObtainAnExistingFile.fileWithName(fileSystem, FOLDER_NAME);
+
+		thrown.expect(UncheckedIOException.class);
+
+		folder.create();
+	}
+
 }