Skip to content

Commit bd9032f

Browse files
committed
Force writings to inUseFile
1 parent 7d3998a commit bd9032f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/main/java/org/cryptomator/cryptofs/inuse/RealUseToken.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.io.UncheckedIOException;
1212
import java.nio.ByteBuffer;
1313
import java.nio.channels.ByteChannel;
14-
import java.nio.channels.SeekableByteChannel;
14+
import java.nio.channels.FileChannel;
1515
import java.nio.channels.WritableByteChannel;
1616
import java.nio.file.Files;
1717
import java.nio.file.OpenOption;
@@ -53,7 +53,7 @@ public static RealUseToken createWithExistingFile(Path p, String owner, Cryptor
5353
private final ReentrantReadWriteLock.WriteLock fileCreationSync = new ReentrantReadWriteLock().writeLock();
5454

5555
private volatile Path filePath;
56-
private volatile SeekableByteChannel channel;
56+
private volatile FileChannel channel;
5757
private volatile boolean closed;
5858

5959
RealUseToken(Path filePath, String owner, Cryptor cryptor, ConcurrentMap<Path, RealUseToken> useTokens, Executor tokenPersistor, OpenOption openMode) {
@@ -79,7 +79,7 @@ private void createInUseFile(Set<OpenOption> openOptions) {
7979
if (closed) {
8080
return;
8181
}
82-
this.channel = Files.newByteChannel(filePath, openOptions);
82+
this.channel = FileChannel.open(filePath, openOptions);
8383
writeInUseFile();
8484
} catch (IOException e) {
8585
LOG.debug("Failed to write in-use file {} with open options {}.", filePath, openOptions, e);
@@ -97,7 +97,6 @@ void refresh() {
9797
return;
9898
}
9999
writeInUseFile();
100-
channel.position(0);
101100
} catch (IOException e) {
102101
LOG.debug("Failed to refresh in-use file {}.", filePath, e);
103102
} finally {
@@ -106,15 +105,19 @@ void refresh() {
106105
}
107106

108107
int writeInUseFile() throws IOException {
108+
channel.position(0);
109+
final int bytesWritten;
109110
try (var nonClosingWrapper = new NonClosingByteChannel(channel); //
110111
var encChannel = encWrapper.wrapWithEncryption(nonClosingWrapper, cryptor)) {
111112
var rawInfo = new ByteArrayOutputStream(Constants.INUSE_CLEARTEXT_SIZE);
112113
var prop = new Properties();
113114
prop.put(UseToken.OWNER_KEY, owner);
114115
prop.put(UseToken.LASTUPDATED_KEY, Instant.now().toString());
115116
prop.store(rawInfo, null);
116-
return encChannel.write(ByteBuffer.wrap(rawInfo.toByteArray()));
117+
bytesWritten = encChannel.write(ByteBuffer.wrap(rawInfo.toByteArray()));
117118
}
119+
channel.force(false);
120+
return bytesWritten;
118121
}
119122

120123
@Override

0 commit comments

Comments
 (0)