Skip to content

Commit 49f5af2

Browse files
authored
Merge pull request #286 from cryptomator/feature/invalid-file-node-exception
Fix: Custom filesystem exception for invalid ciphertext nodes
2 parents 7f800a9 + 4dd38ab commit 49f5af2

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/main/java/org/cryptomator/cryptofs/CryptoPathMapper.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public void assertNonExisting(CryptoPath cleartextPath) throws FileAlreadyExists
8585
/**
8686
* @param cleartextPath A path
8787
* @return The file type for the given path (if it exists)
88-
* @throws NoSuchFileException If no node exists at the given path for any known type
88+
* @throws NoSuchFileException If the ciphertext path does not exist
89+
* @throws InvalidFileNodeException If the node points to a ciphertext directory, which does not contain an identification file
8990
* @throws IOException
9091
*/
9192
public CiphertextFileType getCiphertextFileType(CryptoPath cleartextPath) throws NoSuchFileException, IOException {
@@ -105,7 +106,7 @@ public CiphertextFileType getCiphertextFileType(CryptoPath cleartextPath) throws
105106
} else {
106107
eventConsumer.accept(new BrokenFileNodeEvent(cleartextPath, ciphertextPath.getRawPath()));
107108
LOG.warn("Did not find valid content inside of {}", ciphertextPath.getRawPath());
108-
throw new NoSuchFileException(cleartextPath.toString(), null, "Could not determine type of file " + ciphertextPath.getRawPath());
109+
throw new InvalidFileNodeException(cleartextPath.toString(), ciphertextPath.getRawPath().toString());
109110
}
110111
} else {
111112
// assume "file" if not a directory (even if it isn't a "regular" file, see issue #81):
@@ -117,7 +118,7 @@ public CiphertextFileType getCiphertextFileType(CryptoPath cleartextPath) throws
117118
public CiphertextFilePath getCiphertextFilePath(CryptoPath cleartextPath) throws IOException {
118119
CryptoPath parentPath = cleartextPath.getParent();
119120
if (parentPath == null) {
120-
throw new IllegalArgumentException("Invalid file path (must have a parent): " + cleartextPath); //TODO: cleartext path in Logs!
121+
throw new IllegalArgumentException("Invalid file path (must have a parent): " + cleartextPath);
121122
}
122123
CiphertextDirectory parent = getCiphertextDir(parentPath);
123124
String cleartextName = cleartextPath.getFileName().toString();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.cryptomator.cryptofs;
2+
3+
import java.nio.file.FileSystemException;
4+
5+
/**
6+
* Exception thrown if a c9s or c9r directory does not contain any identification files
7+
*/
8+
public class InvalidFileNodeException extends FileSystemException {
9+
10+
public InvalidFileNodeException(String cleartext, String ciphertext) {
11+
super(cleartext, null, "Unknown type of node %s: Missing identification file".formatted(ciphertext));
12+
}
13+
}

src/test/java/org/cryptomator/cryptofs/CryptoPathMapperTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ public void testDetectionPriority(boolean dirFileExists, boolean symlinkFileExis
390390
}
391391

392392
@Test
393-
@DisplayName("Throw NoSuchFileException if no known file exists")
393+
@DisplayName("Throw a FileSystemException if no id file exists")
394394
public void testNoKnownFileExists() throws IOException {
395395
Mockito.when(underlyingFileSystemProvider.readAttributes(c9rPath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenReturn(c9rAttrs);
396396
Mockito.when(c9rAttrs.isDirectory()).thenReturn(true);
@@ -404,7 +404,7 @@ public void testNoKnownFileExists() throws IOException {
404404
CryptoPathMapper mapper = new CryptoPathMapper(pathToVault, cryptor, dirIdProvider, longFileNameProvider, vaultConfig, eventConsumer);
405405

406406
CryptoPath path = fileSystem.getPath("/CLEAR");
407-
Assertions.assertThrows(NoSuchFileException.class, () -> mapper.getCiphertextFileType(path));
407+
Assertions.assertThrows(InvalidFileNodeException.class, () -> mapper.getCiphertextFileType(path));
408408
var isBrokenFileNodeEvent = (ArgumentMatcher<FilesystemEvent>) ev -> ev instanceof BrokenFileNodeEvent;
409409
verify(eventConsumer).accept(ArgumentMatchers.argThat(isBrokenFileNodeEvent));
410410
}

0 commit comments

Comments
 (0)