Skip to content

Commit 6677067

Browse files
pass through attributes during fsProvider.newFileChannel(path, openOptions, attrs)
1 parent 2b02de3 commit 6677067

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ private FileChannel newFileChannelFromFile(CryptoPath cleartextFilePath, Effecti
378378
Files.createDirectories(ciphertextPath.getRawPath()); // suppresses FileAlreadyExists
379379
}
380380

381-
FileChannel ch = openCryptoFiles.getOrCreate(ciphertextFilePath).newFileChannel(options); // might throw FileAlreadyExists
381+
FileChannel ch = openCryptoFiles.getOrCreate(ciphertextFilePath).newFileChannel(options, attrs); // might throw FileAlreadyExists
382382
try {
383383
if (options.writable()) {
384384
ciphertextPath.persistLongFileName();

src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.nio.channels.FileChannel;
2323
import java.nio.file.Path;
24+
import java.nio.file.attribute.FileAttribute;
2425
import java.nio.file.attribute.FileTime;
2526
import java.time.Instant;
2627
import java.util.Optional;
@@ -65,7 +66,7 @@ public OpenCryptoFile(FileCloseListener listener, ChunkCache chunkCache, Cryptor
6566
* @return A new file channel. Ideally used in a try-with-resource statement. If the channel is not properly closed, this OpenCryptoFile will stay open indefinite.
6667
* @throws IOException
6768
*/
68-
public synchronized FileChannel newFileChannel(EffectiveOpenOptions options) throws IOException {
69+
public synchronized FileChannel newFileChannel(EffectiveOpenOptions options, FileAttribute<?>... attrs) throws IOException {
6970
Path path = currentFilePath.get();
7071

7172
if (options.truncateExisting()) {
@@ -75,7 +76,7 @@ public synchronized FileChannel newFileChannel(EffectiveOpenOptions options) thr
7576
FileChannel ciphertextFileChannel = null;
7677
CleartextFileChannel cleartextFileChannel = null;
7778
try {
78-
ciphertextFileChannel = path.getFileSystem().provider().newFileChannel(path, options.createOpenOptionsForEncryptedFile());
79+
ciphertextFileChannel = path.getFileSystem().provider().newFileChannel(path, options.createOpenOptionsForEncryptedFile(), attrs);
7980
final FileHeader header;
8081
final boolean isNewHeader;
8182
if (ciphertextFileChannel.size() == 0l) {

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.nio.file.attribute.PosixFileAttributeView;
5555
import java.nio.file.attribute.PosixFileAttributes;
5656
import java.nio.file.attribute.PosixFilePermission;
57+
import java.nio.file.attribute.PosixFilePermissions;
5758
import java.nio.file.attribute.UserPrincipal;
5859
import java.nio.file.spi.FileSystemProvider;
5960
import java.util.Arrays;
@@ -366,7 +367,7 @@ public void setup() throws IOException {
366367
when(ciphertextPath.getFilePath()).thenReturn(ciphertextFilePath);
367368
when(openCryptoFiles.getOrCreate(ciphertextFilePath)).thenReturn(openCryptoFile);
368369
when(ciphertextFilePath.getName(3)).thenReturn(mock(CryptoPath.class, "path.c9r"));
369-
when(openCryptoFile.newFileChannel(any())).thenReturn(fileChannel);
370+
when(openCryptoFile.newFileChannel(any(), any())).thenReturn(fileChannel);
370371
}
371372

372373
@Nested
@@ -411,6 +412,18 @@ public void testNewFileChannelCreate2() throws IOException {
411412
verify(readonlyFlag, Mockito.never()).assertWritable();
412413
}
413414

415+
@Test
416+
@DisplayName("create new succeeds when within limit")
417+
public void testNewFileChannelCreate3() throws IOException {
418+
Mockito.doReturn(10).when(fileSystemProperties).maxCleartextNameLength();
419+
var attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxr-x---"));
420+
421+
FileChannel ch = inTest.newFileChannel(cleartextPath, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), attrs);
422+
423+
Assertions.assertSame(fileChannel, ch);
424+
verify(openCryptoFile).newFileChannel(Mockito.any(), Mockito.eq(attrs));
425+
}
426+
414427
}
415428

416429
@Test

src/test/java/org/cryptomator/cryptofs/fh/OpenCryptoFileTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.cryptomator.cryptofs.fh;
22

3+
import com.google.common.jimfs.Configuration;
4+
import com.google.common.jimfs.Feature;
35
import com.google.common.jimfs.Jimfs;
46
import org.cryptomator.cryptofs.EffectiveOpenOptions;
57
import org.cryptomator.cryptofs.ReadonlyFlag;
@@ -28,6 +30,7 @@
2830
import java.nio.file.Files;
2931
import java.nio.file.Path;
3032
import java.nio.file.StandardOpenOption;
33+
import java.nio.file.attribute.PosixFilePermissions;
3134
import java.time.Instant;
3235
import java.util.EnumSet;
3336
import java.util.concurrent.atomic.AtomicLong;
@@ -55,7 +58,7 @@ public class OpenCryptoFileTest {
5558

5659
@BeforeAll
5760
public static void setup() {
58-
FS = Jimfs.newFileSystem("OpenCryptoFileTest");
61+
FS = Jimfs.newFileSystem("OpenCryptoFileTest", Configuration.unix().toBuilder().setAttributeViews("basic", "posix").build());
5962
CURRENT_FILE_PATH = new AtomicReference<>(FS.getPath("currentFile"));
6063
}
6164

@@ -131,8 +134,9 @@ public void testGetSizeBeforeCreatingFileChannel() {
131134
@Order(10)
132135
@DisplayName("create first FileChannel")
133136
public void createFileChannel() throws IOException {
137+
var attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxr-x---"));
134138
EffectiveOpenOptions options = EffectiveOpenOptions.from(EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), readonlyFlag);
135-
FileChannel ch = openCryptoFile.newFileChannel(options);
139+
FileChannel ch = openCryptoFile.newFileChannel(options, attrs);
136140
Assertions.assertSame(cleartextFileChannel, ch);
137141
verify(chunkIO).registerChannel(ciphertextChannel.get(), true);
138142
}

0 commit comments

Comments
 (0)