Skip to content

Commit 0b0c9f8

Browse files
Merge branch 'release/1.5.2'
2 parents 82d8a4c + 1847024 commit 0b0c9f8

12 files changed

+77
-30
lines changed

pom.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>org.cryptomator</groupId>
44
<artifactId>cryptofs</artifactId>
5-
<version>1.5.1</version>
5+
<version>1.5.2</version>
66
<name>Cryptomator Crypto Filesystem</name>
77
<description>This library provides the Java filesystem provider used by Cryptomator.</description>
88
<url>https://github.com/cryptomator/cryptofs</url>
@@ -16,7 +16,7 @@
1616
<properties>
1717
<cryptolib.version>1.2.0</cryptolib.version>
1818
<dagger.version>2.15</dagger.version>
19-
<guava.version>23.6-jre</guava.version>
19+
<guava.version>24.1-jre</guava.version>
2020
<slf4j.version>1.7.25</slf4j.version>
2121
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2222
</properties>
@@ -137,6 +137,8 @@
137137
<artifactId>maven-compiler-plugin</artifactId>
138138
<version>3.7.0</version>
139139
<configuration>
140+
<source>1.8</source>
141+
<target>1.8</target>
140142
<release>8</release>
141143
<showWarnings>true</showWarnings>
142144
</configuration>
@@ -152,7 +154,7 @@
152154
<plugin>
153155
<groupId>org.owasp</groupId>
154156
<artifactId>dependency-check-maven</artifactId>
155-
<version>3.0.2</version>
157+
<version>3.1.2</version>
156158
<configuration>
157159
<cveValidForHours>24</cveValidForHours>
158160
<failBuildOnCVSS>0</failBuildOnCVSS>
@@ -176,7 +178,7 @@
176178
<plugin>
177179
<groupId>org.jacoco</groupId>
178180
<artifactId>jacoco-maven-plugin</artifactId>
179-
<version>0.7.9</version>
181+
<version>0.8.1</version>
180182
<executions>
181183
<execution>
182184
<id>prepare-agent</id>

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.nio.file.Path;
1212
import java.nio.file.attribute.BasicFileAttributes;
13+
import java.util.Optional;
1314

1415
import org.cryptomator.cryptolib.Cryptors;
1516
import org.cryptomator.cryptolib.api.Cryptor;
@@ -19,11 +20,13 @@ class CryptoBasicFileAttributes implements DelegatingBasicFileAttributes {
1920
private final BasicFileAttributes delegate;
2021
protected final Path ciphertextPath;
2122
private final Cryptor cryptor;
23+
private final Optional<Long> sizeAccordingToOpenChannel;
2224

23-
public CryptoBasicFileAttributes(BasicFileAttributes delegate, Path ciphertextPath, Cryptor cryptor) {
25+
public CryptoBasicFileAttributes(BasicFileAttributes delegate, Path ciphertextPath, Cryptor cryptor, Optional<Long> sizeAccordingToOpenChannel) {
2426
this.delegate = delegate;
2527
this.ciphertextPath = ciphertextPath;
2628
this.cryptor = cryptor;
29+
this.sizeAccordingToOpenChannel = sizeAccordingToOpenChannel;
2730
}
2831

2932
@Override
@@ -49,6 +52,8 @@ public long size() {
4952
return -1l;
5053
} else if (isSymbolicLink()) {
5154
return -1l;
55+
} else if (sizeAccordingToOpenChannel.isPresent()) {
56+
return sizeAccordingToOpenChannel.get();
5257
} else {
5358
return Cryptors.cleartextSize(getDelegate().size() - cryptor.fileHeaderCryptor().headerSize(), cryptor);
5459
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010

1111
import java.nio.file.Path;
1212
import java.nio.file.attribute.DosFileAttributes;
13+
import java.util.Optional;
1314

1415
import org.cryptomator.cryptolib.api.Cryptor;
1516

1617
class CryptoDosFileAttributes extends CryptoBasicFileAttributes implements DelegatingDosFileAttributes {
1718

1819
private final DosFileAttributes delegate;
1920

20-
public CryptoDosFileAttributes(DosFileAttributes delegate, Path ciphertextPath, Cryptor cryptor) {
21-
super(delegate, ciphertextPath, cryptor);
21+
public CryptoDosFileAttributes(DosFileAttributes delegate, Path ciphertextPath, Cryptor cryptor, Optional<Long> sizeAccordingToOpenChannel) {
22+
super(delegate, ciphertextPath, cryptor, sizeAccordingToOpenChannel);
2223
this.delegate = delegate;
2324
}
2425

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.file.attribute.PosixFileAttributes;
1717
import java.util.HashMap;
1818
import java.util.Map;
19+
import java.util.Optional;
1920

2021
import javax.inject.Inject;
2122

@@ -26,29 +27,31 @@ class CryptoFileAttributeProvider {
2627

2728
private final Map<Class<? extends BasicFileAttributes>, AttributeProvider<? extends BasicFileAttributes>> attributeProviders = new HashMap<>();
2829
private final Cryptor cryptor;
30+
private final OpenCryptoFiles openCryptoFiles;
2931

3032
@Inject
31-
public CryptoFileAttributeProvider(Cryptor cryptor) {
33+
public CryptoFileAttributeProvider(Cryptor cryptor, OpenCryptoFiles openCryptoFiles) {
34+
this.cryptor = cryptor;
35+
this.openCryptoFiles = openCryptoFiles;
3236
attributeProviders.put(BasicFileAttributes.class, (AttributeProvider<BasicFileAttributes>) CryptoBasicFileAttributes::new);
3337
attributeProviders.put(PosixFileAttributes.class, (AttributeProvider<PosixFileAttributes>) CryptoPosixFileAttributes::new);
3438
attributeProviders.put(DosFileAttributes.class, (AttributeProvider<DosFileAttributes>) CryptoDosFileAttributes::new);
35-
this.cryptor = cryptor;
3639
}
3740

3841
@SuppressWarnings("unchecked")
3942
public <A extends BasicFileAttributes> A readAttributes(Path ciphertextPath, Class<A> type) throws IOException {
4043
if (attributeProviders.containsKey(type)) {
4144
A ciphertextAttrs = Files.readAttributes(ciphertextPath, type);
4245
AttributeProvider<A> provider = (AttributeProvider<A>) attributeProviders.get(type);
43-
return provider.provide(ciphertextAttrs, ciphertextPath, cryptor);
46+
return provider.provide(ciphertextAttrs, ciphertextPath, cryptor, openCryptoFiles.get(ciphertextPath).map(OpenCryptoFile::size));
4447
} else {
4548
throw new UnsupportedOperationException("Unsupported file attribute type: " + type);
4649
}
4750
}
4851

4952
@FunctionalInterface
50-
private static interface AttributeProvider<A extends BasicFileAttributes> {
51-
A provide(A delegate, Path ciphertextPath, Cryptor cryptor);
53+
private interface AttributeProvider<A extends BasicFileAttributes> {
54+
A provide(A delegate, Path ciphertextPath, Cryptor cryptor, Optional<Long> sizeAccordingToOpenChannel);
5255
}
5356

5457
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ DirectoryStream<Path> newDirectoryStream(CryptoPath cleartextDir, Filter<? super
340340
FileChannel newFileChannel(CryptoPath cleartextPath, Set<? extends OpenOption> optionsSet, FileAttribute<?>... attrs) throws IOException {
341341
EffectiveOpenOptions options = EffectiveOpenOptions.from(optionsSet);
342342
Path ciphertextPath = cryptoPathMapper.getCiphertextFilePath(cleartextPath, CiphertextFileType.FILE);
343-
return openCryptoFiles.get(ciphertextPath, options).newFileChannel(options);
343+
return openCryptoFiles.getOrCreate(ciphertextPath, options).newFileChannel(options);
344344
}
345345

346346
void delete(CryptoPath cleartextPath) throws IOException {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010

1111
import java.nio.file.Path;
1212
import java.nio.file.attribute.PosixFileAttributes;
13+
import java.util.Optional;
1314

1415
import org.cryptomator.cryptolib.api.Cryptor;
1516

1617
class CryptoPosixFileAttributes extends CryptoBasicFileAttributes implements DelegatingPosixFileAttributes {
1718

1819
private final PosixFileAttributes delegate;
1920

20-
public CryptoPosixFileAttributes(PosixFileAttributes delegate, Path ciphertextPath, Cryptor cryptor) {
21-
super(delegate, ciphertextPath, cryptor);
21+
public CryptoPosixFileAttributes(PosixFileAttributes delegate, Path ciphertextPath, Cryptor cryptor, Optional<Long> sizeAccordingToOpenChannel) {
22+
super(delegate, ciphertextPath, cryptor, sizeAccordingToOpenChannel);
2223
this.delegate = delegate;
2324
}
2425

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.io.IOException;
1515
import java.nio.file.Path;
16+
import java.util.Optional;
1617
import java.util.concurrent.ConcurrentHashMap;
1718
import java.util.concurrent.ConcurrentMap;
1819

@@ -31,8 +32,12 @@ public OpenCryptoFiles(CryptoFileSystemComponent component, FinallyUtil finallyU
3132
this.finallyUtil = finallyUtil;
3233
}
3334

34-
public OpenCryptoFile get(Path path, EffectiveOpenOptions options) throws IOException {
35-
Path normalizedPath = path.toAbsolutePath().normalize();
35+
public Optional<OpenCryptoFile> get(Path ciphertextPath) {
36+
return Optional.ofNullable(openCryptoFiles.get(ciphertextPath));
37+
}
38+
39+
public OpenCryptoFile getOrCreate(Path ciphertextPath, EffectiveOpenOptions options) throws IOException {
40+
Path normalizedPath = ciphertextPath.toAbsolutePath().normalize();
3641

3742
OpenCryptoFile result = allowUncheckedThrowsOf(IOException.class).from(() -> {
3843
return openCryptoFiles.computeIfAbsent(normalizedPath, ignored -> create(normalizedPath, options));

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.nio.file.Paths;
1515
import java.nio.file.attribute.BasicFileAttributes;
1616
import java.nio.file.spi.FileSystemProvider;
17+
import java.util.Optional;
1718

1819
import org.cryptomator.cryptolib.api.Cryptor;
1920
import org.cryptomator.cryptolib.api.FileContentCryptor;
@@ -49,7 +50,7 @@ public void setup() throws IOException {
4950

5051
@Test
5152
public void testIsDirectory() {
52-
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor);
53+
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor, Optional.empty());
5354

5455
Mockito.when(delegateAttr.isDirectory()).thenReturn(false);
5556
Assert.assertFalse(attr.isDirectory());
@@ -60,7 +61,7 @@ public void testIsDirectory() {
6061

6162
@Test
6263
public void testIsRegularFile() {
63-
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor);
64+
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor, Optional.empty());
6465

6566
Mockito.when(delegateAttr.isRegularFile()).thenReturn(true);
6667
Assert.assertTrue(attr.isRegularFile());
@@ -71,13 +72,13 @@ public void testIsRegularFile() {
7172

7273
@Test
7374
public void testIsOther() {
74-
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor);
75+
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor, Optional.empty());
7576
Assert.assertFalse(attr.isOther());
7677
}
7778

7879
@Test
7980
public void testIsSymbolicLink() {
80-
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor);
81+
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor, Optional.empty());
8182
Assert.assertFalse(attr.isSymbolicLink());
8283
}
8384

@@ -89,17 +90,30 @@ public void testSizeOfFile() throws IOException {
8990
Mockito.when(delegateAttr.isOther()).thenReturn(false);
9091
Mockito.when(ciphertextFilePath.getFileName()).thenReturn(Paths.get("foo"));
9192

92-
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor);
93+
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor, Optional.empty());
9394

9495
Assert.assertEquals(1337l, attr.size());
9596
}
9697

98+
@Test
99+
public void testSizeOfOpenFile() throws IOException {
100+
Mockito.when(delegateAttr.isDirectory()).thenReturn(false);
101+
Mockito.when(delegateAttr.isSymbolicLink()).thenReturn(false);
102+
Mockito.when(delegateAttr.isOther()).thenReturn(false);
103+
Mockito.when(ciphertextFilePath.getFileName()).thenReturn(Paths.get("foo"));
104+
105+
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor, Optional.of(1338l));
106+
107+
Assert.assertEquals(1338l, attr.size());
108+
Mockito.verify(delegateAttr, Mockito.never()).size();
109+
}
110+
97111
@Test
98112
public void testSizeOfDirectory() throws IOException {
99113
Mockito.when(delegateAttr.size()).thenReturn(4096l);
100114
Mockito.when(delegateAttr.isDirectory()).thenReturn(true);
101115

102-
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor);
116+
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor, Optional.empty());
103117

104118
Assert.assertEquals(4096l, attr.size());
105119
}
@@ -110,7 +124,7 @@ public void testSizeWithException() throws IOException {
110124
Mockito.when(delegateAttr.isRegularFile()).thenReturn(true);
111125
Mockito.when(ciphertextFilePath.getFileName()).thenReturn(Paths.get("foo"));
112126

113-
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor);
127+
BasicFileAttributes attr = new CryptoBasicFileAttributes(delegateAttr, ciphertextFilePath, cryptor, Optional.empty());
114128
attr.size();
115129
}
116130

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.mockito.Mockito.when;
77

88
import java.nio.file.attribute.DosFileAttributes;
9+
import java.util.Optional;
910

1011
import org.cryptomator.cryptolib.api.Cryptor;
1112
import org.junit.Rule;
@@ -30,7 +31,7 @@ public class CryptoDosFileAttributesTest {
3031
private CryptoPath path = mock(CryptoPath.class);
3132
private Cryptor cryptor = mock(Cryptor.class);
3233

33-
private CryptoDosFileAttributes inTest = new CryptoDosFileAttributes(delegate, path, cryptor);
34+
private CryptoDosFileAttributes inTest = new CryptoDosFileAttributes(delegate, path, cryptor, null);
3435

3536
@Test
3637
public void testGetDelegateReturnsDelegate() {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
public class CryptoFileAttributeProviderTest {
2626

2727
private Cryptor cryptor;
28+
private OpenCryptoFiles openCryptoFiles;
2829
private Path ciphertextFilePath;
2930

3031
@Before
3132
public void setup() throws IOException {
3233
cryptor = Mockito.mock(Cryptor.class);
34+
openCryptoFiles = Mockito.mock(OpenCryptoFiles.class);
3335
ciphertextFilePath = Mockito.mock(Path.class);
3436
FileSystem fs = Mockito.mock(FileSystem.class);
3537
Mockito.when(ciphertextFilePath.getFileSystem()).thenReturn(fs);
@@ -45,21 +47,21 @@ public void setup() throws IOException {
4547

4648
@Test
4749
public void testReadBasicAttributes() throws IOException {
48-
CryptoFileAttributeProvider prov = new CryptoFileAttributeProvider(cryptor);
50+
CryptoFileAttributeProvider prov = new CryptoFileAttributeProvider(cryptor, openCryptoFiles);
4951
BasicFileAttributes attr = prov.readAttributes(ciphertextFilePath, BasicFileAttributes.class);
5052
Assert.assertTrue(attr instanceof BasicFileAttributes);
5153
}
5254

5355
@Test
5456
public void testReadPosixAttributes() throws IOException {
55-
CryptoFileAttributeProvider prov = new CryptoFileAttributeProvider(cryptor);
57+
CryptoFileAttributeProvider prov = new CryptoFileAttributeProvider(cryptor, openCryptoFiles);
5658
PosixFileAttributes attr = prov.readAttributes(ciphertextFilePath, PosixFileAttributes.class);
5759
Assert.assertTrue(attr instanceof PosixFileAttributes);
5860
}
5961

6062
@Test
6163
public void testReadDosAttributes() throws IOException {
62-
CryptoFileAttributeProvider prov = new CryptoFileAttributeProvider(cryptor);
64+
CryptoFileAttributeProvider prov = new CryptoFileAttributeProvider(cryptor, openCryptoFiles);
6365
DosFileAttributes attr = prov.readAttributes(ciphertextFilePath, DosFileAttributes.class);
6466
Assert.assertTrue(attr instanceof DosFileAttributes);
6567
}
@@ -70,7 +72,7 @@ private interface UnsupportedAttributes extends BasicFileAttributes {
7072

7173
@Test(expected = UnsupportedOperationException.class)
7274
public void testReadUnsupportedAttributes() throws IOException {
73-
CryptoFileAttributeProvider prov = new CryptoFileAttributeProvider(cryptor);
75+
CryptoFileAttributeProvider prov = new CryptoFileAttributeProvider(cryptor, openCryptoFiles);
7476
prov.readAttributes(ciphertextFilePath, UnsupportedAttributes.class);
7577
}
7678

0 commit comments

Comments
 (0)