Skip to content

Commit e5fb49e

Browse files
committed
Remove unused code and simplify code
1 parent 943503d commit e5fb49e

File tree

2 files changed

+16
-50
lines changed

2 files changed

+16
-50
lines changed

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

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,11 @@
3535
public final class RealUseToken implements UseToken {
3636

3737
public static RealUseToken createWithNewFile(Path p, String owner, Cryptor cryptor, ConcurrentMap<Path, RealUseToken> useTokens) {
38-
return new RealUseToken(p, owner, cryptor, useTokens, ActivationType.CREATE);
38+
return new RealUseToken(p, owner, cryptor, useTokens, StandardOpenOption.CREATE_NEW);
3939
}
4040

4141
public static RealUseToken createWithExistingFile(Path p, String owner, Cryptor cryptor, ConcurrentMap<Path, RealUseToken> useTokens) {
42-
return new RealUseToken(p, owner, cryptor, useTokens, ActivationType.STEAL);
43-
}
44-
45-
public static RealUseToken createInvalid(Path p, ConcurrentMap<Path, RealUseToken> useTokens) {
46-
return new RealUseToken(p, "unused", null, useTokens, ActivationType.NONE);
42+
return new RealUseToken(p, owner, cryptor, useTokens, StandardOpenOption.TRUNCATE_EXISTING);
4743
}
4844

4945
private static final Logger LOG = LoggerFactory.getLogger(RealUseToken.class);
@@ -59,29 +55,19 @@ public static RealUseToken createInvalid(Path p, ConcurrentMap<Path, RealUseToke
5955
private volatile WritableByteChannel channel;
6056
private volatile boolean closed;
6157

62-
RealUseToken(Path filePath, String owner, Cryptor cryptor, ConcurrentMap<Path, RealUseToken> useTokens, ActivationType m) {
63-
this(filePath, owner, cryptor, useTokens, m, EncryptedChannels::wrapEncryptionAround);
58+
RealUseToken(Path filePath, String owner, Cryptor cryptor, ConcurrentMap<Path, RealUseToken> useTokens, OpenOption openMode) {
59+
this(filePath, owner, cryptor, useTokens, openMode, EncryptedChannels::wrapEncryptionAround);
6460
}
6561

66-
RealUseToken(Path filePath, String owner, Cryptor cryptor, ConcurrentMap<Path, RealUseToken> useTokens, ActivationType m, EncryptionDecorator encWrapper) {
62+
RealUseToken(Path filePath, String owner, Cryptor cryptor, ConcurrentMap<Path, RealUseToken> useTokens, OpenOption openMode, EncryptionDecorator encWrapper) {
6763
this.owner = owner;
6864
this.filePath = filePath;
6965
this.cryptor = cryptor;
7066
this.useTokens = useTokens;
7167
this.encWrapper = encWrapper;
72-
Set<OpenOption> openOptions = switch (m) {
73-
case STEAL -> Set.of(StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
74-
case CREATE -> Set.of(StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
75-
case NONE -> Set.of();
76-
};
77-
78-
if (m == ActivationType.NONE) {
79-
this.closed = true;
80-
this.creationTask = CompletableFuture.completedFuture(null);
81-
} else {
82-
this.closed = false;
83-
this.creationTask = CompletableFuture.runAsync(() -> createInUseFile(openOptions), CompletableFuture.delayedExecutor(Constants.INUSE_DELAY_MILLIS, TimeUnit.MILLISECONDS, Executors.newVirtualThreadPerTaskExecutor()));
84-
}
68+
this.closed = false;
69+
var openOptions = Set.of(StandardOpenOption.WRITE, openMode);
70+
this.creationTask = CompletableFuture.runAsync(() -> createInUseFile(openOptions), CompletableFuture.delayedExecutor(Constants.INUSE_DELAY_MILLIS, TimeUnit.MILLISECONDS, Executors.newVirtualThreadPerTaskExecutor()));
8571

8672
}
8773

@@ -191,12 +177,6 @@ public void close() {
191177
}
192178
}
193179

194-
enum ActivationType {
195-
CREATE,
196-
STEAL,
197-
NONE;
198-
}
199-
200180
interface EncryptionDecorator {
201181

202182
WritableByteChannel wrapWithEncryption(ByteChannel ch, Cryptor c);

src/test/java/org/cryptomator/cryptofs/inuse/RealUseTokenTest.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.io.IOException;
1616
import java.nio.file.Files;
1717
import java.nio.file.Path;
18+
import java.nio.file.StandardOpenOption;
1819
import java.nio.file.StandardWatchEventKinds;
1920
import java.nio.file.WatchService;
2021
import java.time.Duration;
@@ -62,7 +63,7 @@ public void afterEach() {
6263
@DisplayName("After 5 seconds of token creation, a new file is created")
6364
public void testFileCreation() {
6465
var filePath = tmpDir.resolve("inUse.file");
65-
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, RealUseToken.ActivationType.CREATE, encWrapper)) {
66+
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, StandardOpenOption.CREATE_NEW, encWrapper)) {
6667
Awaitility.await().atLeast(FILE_OPERATION_DELAY).atMost(FILE_OPERATION_MAX).until(() -> Files.exists(filePath));
6768
Assertions.assertTrue(Files.exists(filePath));
6869
}
@@ -77,7 +78,7 @@ public void testFileSteal() throws IOException {
7778
var watchKey = tmpDir.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
7879
var fileTime = Files.getLastModifiedTime(filePath);
7980

80-
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, RealUseToken.ActivationType.STEAL, encWrapper)) {
81+
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, StandardOpenOption.TRUNCATE_EXISTING, encWrapper)) {
8182
Awaitility.await().atLeast(FILE_OPERATION_DELAY).atMost(FILE_OPERATION_MAX).until(() -> fileTime.compareTo(Files.getLastModifiedTime(filePath)) < 0);
8283
var events = watchKey.pollEvents();
8384
var createEvent = events.stream().filter(e -> e.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY)).findAny();
@@ -90,28 +91,13 @@ public void testFileSteal() throws IOException {
9091
Assertions.assertNull(useTokens.get(filePath));
9192
}
9293

93-
@Test
94-
@DisplayName("Invalid token creation does nothing")
95-
public void testInvalid() throws IOException {
96-
var filePath = tmpDir.resolve("inUse.file");
97-
var watchKey = tmpDir.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
98-
99-
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, RealUseToken.ActivationType.NONE, encWrapper)) {
100-
Awaitility.await().pollDelay(FILE_OPERATION_MAX).timeout(FILE_OPERATION_MAX.multipliedBy(2)).until(() -> true);
101-
Assertions.assertTrue(Files.notExists(filePath));
102-
Assertions.assertTrue(token.isClosed());
103-
Assertions.assertNull(useTokens.get(filePath));
104-
MatcherAssert.assertThat(watchKey.pollEvents(), Matchers.empty());
105-
}
106-
}
107-
10894
@Test
10995
@DisplayName("After 5 seconds of token creation, failed steal closes the token ")
11096
public void testFileStealFails() throws IOException {
11197
var filePath = tmpDir.resolve("inUse.file"); //file does not exist
11298
var watchKey = tmpDir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
11399

114-
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, RealUseToken.ActivationType.STEAL, encWrapper)) {
100+
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, StandardOpenOption.TRUNCATE_EXISTING, encWrapper)) {
115101
Awaitility.await().atLeast(FILE_OPERATION_DELAY).atMost(FILE_OPERATION_MAX).until(token::isClosed);
116102
Assertions.assertTrue(Files.notExists(filePath));
117103
Assertions.assertTrue(token.isClosed());
@@ -126,7 +112,7 @@ public void testTokenCloseBeforeFileOperation() throws IOException {
126112
var filePath = tmpDir.resolve("inUse.file");
127113
var watchKey = tmpDir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
128114

129-
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, RealUseToken.ActivationType.CREATE, encWrapper)) {
115+
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, StandardOpenOption.CREATE_NEW, encWrapper)) {
130116
Assertions.assertTrue(Files.notExists(filePath));
131117
}
132118
Awaitility.await().pollDelay(FILE_OPERATION_MAX).timeout(FILE_OPERATION_MAX.multipliedBy(2)).until(() -> true);
@@ -142,7 +128,7 @@ public void testMoveToBefore() throws IOException {
142128
var targetPath = tmpDir.resolve("inUse2.file");
143129
var watchKey = tmpDir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
144130

145-
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, RealUseToken.ActivationType.CREATE, encWrapper)) {
131+
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, StandardOpenOption.CREATE_NEW, encWrapper)) {
146132
token.moveToInternal(targetPath);
147133

148134
//no file operation after move
@@ -172,7 +158,7 @@ public void testMoveToAfter() {
172158
var filePath = tmpDir.resolve("inUse.file");
173159
var targetPath = tmpDir.resolve("inUse2.file");
174160

175-
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, RealUseToken.ActivationType.CREATE, encWrapper)) {
161+
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, StandardOpenOption.CREATE_NEW, encWrapper)) {
176162
Awaitility.await().atLeast(FILE_OPERATION_DELAY).atMost(FILE_OPERATION_MAX).until(() -> Files.exists(filePath));
177163

178164
token.moveToInternal(targetPath);
@@ -195,7 +181,7 @@ public void testMoveToClosed() throws IOException {
195181
var targetPath = tmpDir.resolve("inUse2.file");
196182
var watchKey = tmpDir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
197183

198-
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, RealUseToken.ActivationType.CREATE, encWrapper)) {
184+
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, StandardOpenOption.CREATE_NEW, encWrapper)) {
199185
token.close();
200186
Awaitility.await().pollDelay(FILE_OPERATION_MAX).timeout(FILE_OPERATION_MAX.multipliedBy(2)).until(() -> true);
201187

0 commit comments

Comments
 (0)