Skip to content

Commit 7f800a9

Browse files
authored
Merge pull request #284 from cryptomator/feature/more-events
Feature: More events and event refinement
2 parents 8507188 + d5e238c commit 7f800a9

File tree

10 files changed

+157
-44
lines changed

10 files changed

+157
-44
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.google.common.io.BaseEncoding;
1414
import org.cryptomator.cryptofs.common.CiphertextFileType;
1515
import org.cryptomator.cryptofs.common.Constants;
16+
import org.cryptomator.cryptofs.event.BrokenFileNodeEvent;
17+
import org.cryptomator.cryptofs.event.FilesystemEvent;
1618
import org.cryptomator.cryptolib.api.Cryptor;
1719
import org.slf4j.Logger;
1820
import org.slf4j.LoggerFactory;
@@ -27,6 +29,7 @@
2729
import java.nio.file.Path;
2830
import java.nio.file.attribute.BasicFileAttributes;
2931
import java.util.Optional;
32+
import java.util.function.Consumer;
3033

3134
import static org.cryptomator.cryptofs.common.Constants.DATA_DIR_NAME;
3235

@@ -41,18 +44,20 @@ public class CryptoPathMapper {
4144
private final DirectoryIdProvider dirIdProvider;
4245
private final LongFileNameProvider longFileNameProvider;
4346
private final VaultConfig vaultConfig;
47+
private final Consumer<FilesystemEvent> eventConsumer;
4448
private final LoadingCache<DirIdAndName, String> ciphertextNames;
4549
private final CiphertextDirCache ciphertextDirCache;
4650

4751
private final CiphertextDirectory rootDirectory;
4852

4953
@Inject
50-
CryptoPathMapper(@PathToVault Path pathToVault, Cryptor cryptor, DirectoryIdProvider dirIdProvider, LongFileNameProvider longFileNameProvider, VaultConfig vaultConfig) {
54+
CryptoPathMapper(@PathToVault Path pathToVault, Cryptor cryptor, DirectoryIdProvider dirIdProvider, LongFileNameProvider longFileNameProvider, VaultConfig vaultConfig, Consumer<FilesystemEvent> eventConsumer) {
5155
this.dataRoot = pathToVault.resolve(DATA_DIR_NAME);
5256
this.cryptor = cryptor;
5357
this.dirIdProvider = dirIdProvider;
5458
this.longFileNameProvider = longFileNameProvider;
5559
this.vaultConfig = vaultConfig;
60+
this.eventConsumer = eventConsumer;
5661
this.ciphertextNames = Caffeine.newBuilder().maximumSize(MAX_CACHED_CIPHERTEXT_NAMES).build(this::getCiphertextFileName);
5762
this.ciphertextDirCache = new CiphertextDirCache();
5863
this.rootDirectory = resolveDirectory(Constants.ROOT_DIR_ID);
@@ -98,6 +103,7 @@ public CiphertextFileType getCiphertextFileType(CryptoPath cleartextPath) throws
98103
} else if (ciphertextPath.isShortened() && Files.exists(ciphertextPath.getFilePath(), LinkOption.NOFOLLOW_LINKS)) {
99104
return CiphertextFileType.FILE;
100105
} else {
106+
eventConsumer.accept(new BrokenFileNodeEvent(cleartextPath, ciphertextPath.getRawPath()));
101107
LOG.warn("Did not find valid content inside of {}", ciphertextPath.getRawPath());
102108
throw new NoSuchFileException(cleartextPath.toString(), null, "Could not determine type of file " + ciphertextPath.getRawPath());
103109
}
@@ -111,7 +117,7 @@ public CiphertextFileType getCiphertextFileType(CryptoPath cleartextPath) throws
111117
public CiphertextFilePath getCiphertextFilePath(CryptoPath cleartextPath) throws IOException {
112118
CryptoPath parentPath = cleartextPath.getParent();
113119
if (parentPath == null) {
114-
throw new IllegalArgumentException("Invalid file path (must have a parent): " + cleartextPath);
120+
throw new IllegalArgumentException("Invalid file path (must have a parent): " + cleartextPath); //TODO: cleartext path in Logs!
115121
}
116122
CiphertextDirectory parent = getCiphertextDir(parentPath);
117123
String cleartextName = cleartextPath.getFileName().toString();

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
package org.cryptomator.cryptofs;
22

33
import com.github.benmanes.caffeine.cache.CacheLoader;
4+
import org.cryptomator.cryptofs.event.BrokenDirFileEvent;
5+
import org.cryptomator.cryptofs.event.FilesystemEvent;
46

57
import javax.inject.Inject;
68
import java.io.IOException;
79
import java.io.InputStream;
8-
import java.io.UncheckedIOException;
910
import java.nio.channels.Channels;
1011
import java.nio.channels.FileChannel;
1112
import java.nio.charset.StandardCharsets;
1213
import java.nio.file.NoSuchFileException;
1314
import java.nio.file.Path;
1415
import java.nio.file.StandardOpenOption;
1516
import java.util.UUID;
17+
import java.util.function.Consumer;
1618

1719
@CryptoFileSystemScoped
1820
class DirectoryIdLoader implements CacheLoader<Path, String> {
1921

2022
private static final int MAX_DIR_ID_LENGTH = 1000;
2123

24+
private final Consumer<FilesystemEvent> eventConsumer;
25+
2226
@Inject
23-
public DirectoryIdLoader() {
27+
public DirectoryIdLoader(Consumer<FilesystemEvent> eventConsumer) {
28+
this.eventConsumer = eventConsumer;
2429
}
2530

2631
@Override
@@ -29,8 +34,10 @@ public String load(Path dirFilePath) throws IOException {
2934
InputStream in = Channels.newInputStream(ch)) {
3035
long size = ch.size();
3136
if (size == 0) {
37+
eventConsumer.accept(new BrokenDirFileEvent(dirFilePath));
3238
throw new IOException("Invalid, empty directory file: " + dirFilePath);
3339
} else if (size > MAX_DIR_ID_LENGTH) {
40+
eventConsumer.accept(new BrokenDirFileEvent(dirFilePath));
3441
throw new IOException("Unexpectedly large directory file: " + dirFilePath);
3542
} else {
3643
assert size <= MAX_DIR_ID_LENGTH; // thus int
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.cryptomator.cryptofs.event;
2+
3+
import java.nio.file.Path;
4+
5+
/**
6+
* Emitted, if a dir.c9r file is empty or exceeds 1000 Bytes.
7+
*
8+
* @param ciphertextPath path to the broken dir.c9r file
9+
*/
10+
public record BrokenDirFileEvent(Path ciphertextPath) implements FilesystemEvent {
11+
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.cryptomator.cryptofs.event;
2+
3+
import java.nio.file.Path;
4+
5+
/**
6+
* Emitted, if a path within the cryptographic filesystem is accessed, but the directory representing it is missing identification files.
7+
*
8+
* @param cleartextPath path within the cryptographic filesystem
9+
* @param ciphertextPath path of the incomplete, encrypted directory
10+
*
11+
* @see org.cryptomator.cryptofs.health.type.UnknownType
12+
*/
13+
public record BrokenFileNodeEvent(Path cleartextPath, Path ciphertextPath) implements FilesystemEvent {
14+
15+
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.cryptomator.cryptofs.event;
22

3-
import org.cryptomator.cryptolib.api.AuthenticationFailedException;
4-
53
import java.nio.file.Path;
64

75
/**
@@ -10,6 +8,5 @@
108
* @param ciphertextPath path to the encrypted resource
119
* @param e thrown exception
1210
*/
13-
public record DecryptionFailedEvent(Path ciphertextPath, AuthenticationFailedException e) implements FilesystemEvent {
14-
11+
public record DecryptionFailedEvent(Path ciphertextPath, Exception e) implements FilesystemEvent {
1512
}

src/main/java/org/cryptomator/cryptofs/event/FilesystemEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
*
2323
* @apiNote Events might have occured a long time ago in a galaxy far, far away... therefore, any feedback method is non-blocking and might fail due to changes in the filesystem.
2424
*/
25-
public sealed interface FilesystemEvent permits ConflictResolutionFailedEvent, ConflictResolvedEvent, DecryptionFailedEvent {
25+
public sealed interface FilesystemEvent permits BrokenDirFileEvent, BrokenFileNodeEvent, ConflictResolutionFailedEvent, ConflictResolvedEvent, DecryptionFailedEvent {
2626

2727
}

src/main/java/org/cryptomator/cryptofs/fh/FileHeaderHolder.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.cryptomator.cryptofs.event.DecryptionFailedEvent;
44
import org.cryptomator.cryptofs.event.FilesystemEvent;
5-
import org.cryptomator.cryptolib.api.AuthenticationFailedException;
65
import org.cryptomator.cryptolib.api.CryptoException;
76
import org.cryptomator.cryptolib.api.Cryptor;
87
import org.cryptomator.cryptolib.api.FileHeader;
@@ -81,9 +80,7 @@ FileHeader loadExisting(FileChannel ch) throws IOException {
8180
isPersisted.set(true);
8281
return existingHeader;
8382
} catch (IllegalArgumentException | CryptoException e) {
84-
if (e instanceof AuthenticationFailedException afe) {
85-
eventConsumer.accept(new DecryptionFailedEvent(path.get(), afe));
86-
}
83+
eventConsumer.accept(new DecryptionFailedEvent(path.get(), e));
8784
throw new IOException("Unable to decrypt header of file " + path.get(), e);
8885
}
8986
}

0 commit comments

Comments
 (0)