Skip to content

Commit

Permalink
use helper function to execute all close actions
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Jul 17, 2024
1 parent e247a2e commit 6757801
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/main/java/org/cryptomator/cryptofs/ch/CleartextFileChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReadWriteLock;
Expand Down Expand Up @@ -323,33 +325,38 @@ long beginOfChunk(long cleartextPos) {

@Override
protected void implCloseChannel() throws IOException {
try {
flush();
} finally {
implCloseChannelFinally1();
}
var closeActions = List.<CloseAction>of(this::flush, //
super::implCloseChannel, //
() -> closeListener.closed(this), //
ciphertextFileChannel::close, //
this::tryPersistLastModified);
tryAll(closeActions.iterator());
}

private void implCloseChannelFinally1() throws IOException {
private void tryPersistLastModified() {
try {
super.implCloseChannel();
} finally {
closeListener.closed(this);
implCloseChannelFinally2();
persistLastModified();
} catch (NoSuchFileException nsfe) {
//no-op, see https://github.com/cryptomator/cryptofs/issues/169
} catch (IOException e) {
LOG.warn("Failed to persist last modified timestamp for encrypted file: {}", e.getMessage());
}
}

private void implCloseChannelFinally2() throws IOException {
try {
ciphertextFileChannel.close();
} finally {
private void tryAll(Iterator<CloseAction> actions) throws IOException {
if (actions.hasNext()) {
try {
persistLastModified();
} catch (NoSuchFileException nsfe) {
//no-op, see https://github.com/cryptomator/cryptofs/issues/169
} catch (IOException e) {
LOG.warn("Failed to persist last modified timestamp for encrypted file: {}", e.getMessage());
actions.next().run();
} finally {
tryAll(actions);
}
}
}

@FunctionalInterface
private interface CloseAction {

void run() throws IOException;
}

}

0 comments on commit 6757801

Please sign in to comment.