Skip to content

Commit

Permalink
use records
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Jan 28, 2025
1 parent e44c2e4 commit fecb0a2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
import org.cryptomator.cryptolib.api.AuthenticationFailedException;

import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicReference;

public class DecryptionFailedEvent extends FilesystemEvent {
/**
* Created, if decryption fails.
* @param ciphertextPath
* @param cleartextPath might be null
* @param e
*/
public record DecryptionFailedEvent(Path ciphertextPath, Path cleartextPath, AuthenticationFailedException e) implements FilesystemEvent {

private final Path resource;
private final AuthenticationFailedException e;

public DecryptionFailedEvent(Path resource, AuthenticationFailedException e) {
super(Type.DECRYPTION_FAILED);
this.resource = resource;
this.e = e;
}

public Path getResource() {
return resource;
@Override
public Type getType() {
return Type.DECRYPTION_FAILED;
}

}
31 changes: 12 additions & 19 deletions src/main/java/org/cryptomator/cryptofs/event/FilesystemEvent.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
package org.cryptomator.cryptofs.event;

public abstract class FilesystemEvent {
public interface FilesystemEvent {

private final Type type;

protected FilesystemEvent(Type type) {
this.type = type;
static <T extends FilesystemEvent> LockedEvent toLockedEvent(T fse) {
return toEvent(fse, LockedEvent.class);
}

LockedEvent toLockedEvent() {
return toEvent(LockedEvent.class);
static <T extends FilesystemEvent> DecryptionFailedEvent toDecryptionFailedEvent(T fse) throws ClassCastException {
return toEvent(fse, DecryptionFailedEvent.class);
}

DecryptionFailedEvent toDecryptionFailedEvent() {
return toEvent(DecryptionFailedEvent.class);
static <T extends FilesystemEvent> ConflictResolvedEvent toConflictResolvedEvent(T fse) throws ClassCastException {
return toEvent(fse, ConflictResolvedEvent.class);
}

<T extends FilesystemEvent> T toEvent(Class<T> clazz) {
try {
return clazz.cast(this);
} catch (ClassCastException e) {
throw new IllegalCallerException();
}
static <T extends FilesystemEvent, U extends FilesystemEvent> T toEvent(U o, Class<T> clazz) throws ClassCastException {
return clazz.cast(o);
}

public Type getType() {
return type;
}
Type getType();

public enum Type {
enum Type {
DECRYPTION_FAILED,
CONFLICT_RESOLVED,
LOCKED;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/cryptomator/cryptofs/event/LockedEvent.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.cryptomator.cryptofs.event;

public class LockedEvent extends FilesystemEvent {
public record LockedEvent() implements FilesystemEvent {

public LockedEvent() {
super(Type.LOCKED);
@Override
public Type getType() {
return Type.LOCKED;
}

}
2 changes: 1 addition & 1 deletion src/main/java/org/cryptomator/cryptofs/fh/ChunkLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ByteBuffer load(Long chunkIndex) throws IOException, AuthenticationFailed
}
return cleartextBuf;
} catch (AuthenticationFailedException e) {
eventConsumer.accept(new DecryptionFailedEvent(path.get(), e));
eventConsumer.accept(new DecryptionFailedEvent(path.get(), null, e));
throw e;
} finally {
bufferPool.recycle(ciphertextBuf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ FileHeader loadExisting(FileChannel ch) throws IOException {
return existingHeader;
} catch (IllegalArgumentException | CryptoException e) {
if (e instanceof AuthenticationFailedException afe) {
eventConsumer.accept(new DecryptionFailedEvent(path.get(), afe));
eventConsumer.accept(new DecryptionFailedEvent(path.get(), null, afe));
}
throw new IOException("Unable to decrypt header of file " + path.get(), e);
}
Expand Down

0 comments on commit fecb0a2

Please sign in to comment.