Skip to content

Commit f84d710

Browse files
allow readonly file channels even when exceeding maxCleartextNameLength
1 parent aa51d21 commit f84d710

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,9 @@ private FileChannel newFileChannelFromSymlink(CryptoPath cleartextPath, Effectiv
348348
}
349349

350350
private FileChannel newFileChannelFromFile(CryptoPath cleartextFilePath, EffectiveOpenOptions options, FileAttribute<?>... attrs) throws IOException {
351-
assertCleartextNameLengthAllowed(cleartextFilePath);
351+
if (options.create() || options.createNew()) {
352+
assertCleartextNameLengthAllowed(cleartextFilePath);
353+
}
352354
CiphertextFilePath ciphertextPath = cryptoPathMapper.getCiphertextFilePath(cleartextFilePath);
353355
Path ciphertextFilePath = ciphertextPath.getFilePath();
354356
if (options.createNew() && openCryptoFiles.get(ciphertextFilePath).isPresent()) {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ String masterkeyFilename() {
152152
return (String) get(PROPERTY_MASTERKEY_FILENAME);
153153
}
154154

155-
public int maxCleartextNameLength() {
155+
int maxCleartextNameLength() {
156156
return (int) get(PROPERTY_MAX_CLEARTEXT_NAME_LENGTH);
157157
}
158158

@@ -235,9 +235,11 @@ private <T> void checkedSet(Class<T> type, String key, Map<String, ?> properties
235235
}
236236

237237
/**
238-
* Sets the maximum ciphertext filename length for a CryptoFileSystem.
238+
* Sets the maximum cleartext filename length for a CryptoFileSystem. This value is checked during write
239+
* operations. Read access to nodes with longer names should be unaffected. Setting this value to {@code 0} or
240+
* a negative value effectively disables write access.
239241
*
240-
* @param maxCleartextNameLength The maximum ciphertext filename length allowed
242+
* @param maxCleartextNameLength The maximum cleartext filename length allowed
241243
* @return this
242244
* @since 2.0.0
243245
*/

src/test/java/org/cryptomator/cryptofs/CryptoFileSystemImplTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.hamcrest.CoreMatchers;
2020
import org.hamcrest.MatcherAssert;
2121
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.api.Assumptions;
2223
import org.junit.jupiter.api.BeforeEach;
2324
import org.junit.jupiter.api.DisplayName;
2425
import org.junit.jupiter.api.Nested;
@@ -367,6 +368,50 @@ public void setup() throws IOException {
367368
when(openCryptoFile.newFileChannel(any())).thenReturn(fileChannel);
368369
}
369370

371+
@Nested
372+
public class LimitedCleartextNameLength {
373+
374+
@BeforeEach
375+
public void setup() throws IOException {
376+
Assumptions.assumeTrue(cleartextPath.getFileName().toString().length() == 9);
377+
}
378+
379+
@Test
380+
@DisplayName("read-only always works")
381+
public void testNewFileChannelReadOnlyDespiteMaxName() throws IOException {
382+
Mockito.doReturn(0).when(fileSystemProperties).maxCleartextNameLength();
383+
384+
FileChannel ch = inTest.newFileChannel(cleartextPath, EnumSet.of(StandardOpenOption.READ));
385+
386+
Assertions.assertSame(fileChannel, ch);
387+
verify(readonlyFlag, Mockito.never()).assertWritable();
388+
}
389+
390+
@Test
391+
@DisplayName("create new fails when exceeding limit")
392+
public void testNewFileChannelCreate1() {
393+
Mockito.doReturn(0).when(fileSystemProperties).maxCleartextNameLength();
394+
395+
Assertions.assertThrows(FileNameTooLongException.class, () -> {
396+
inTest.newFileChannel(cleartextPath, EnumSet.of(StandardOpenOption.WRITE, StandardOpenOption.CREATE));
397+
});
398+
399+
verifyNoInteractions(openCryptoFiles);
400+
}
401+
402+
@Test
403+
@DisplayName("create new succeeds when within limit")
404+
public void testNewFileChannelCreate2() throws IOException {
405+
Mockito.doReturn(10).when(fileSystemProperties).maxCleartextNameLength();
406+
407+
FileChannel ch = inTest.newFileChannel(cleartextPath, EnumSet.of(StandardOpenOption.READ));
408+
409+
Assertions.assertSame(fileChannel, ch);
410+
verify(readonlyFlag, Mockito.never()).assertWritable();
411+
}
412+
413+
}
414+
370415
@Test
371416
@DisplayName("newFileChannel read-only")
372417
public void testNewFileChannelReadOnly() throws IOException {

0 commit comments

Comments
 (0)