Skip to content

Commit

Permalink
Merge pull request #174 from cryptomator/feature/173-use-data-directly
Browse files Browse the repository at this point in the history
Use Chunk.data() directly when putting chunk
  • Loading branch information
infeo authored Jun 7, 2023
2 parents 798becd + 3e6fe77 commit 34056de
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/cryptomator/cryptofs/fh/ChunkCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ public Chunk putChunk(long chunkIndex, ByteBuffer chunkData) throws IllegalArgum
if (chunk == null) {
chunk = new Chunk(chunkData, true, () -> releaseChunk(chunkIndex));
} else {
var dst = chunk.data().duplicate().clear();
var dst = chunk.data().clear();
Preconditions.checkArgument(chunkData.remaining() == dst.remaining());
dst.put(chunkData);
dst.put(chunkData) //
.flip();
chunk.dirty().set(true);
}
chunk.currentAccesses().incrementAndGet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
import java.util.stream.Stream;

Expand Down Expand Up @@ -175,6 +174,25 @@ public void afterEach() throws IOException {
Files.deleteIfExists(file);
}

//https://github.com/cryptomator/cryptofs/issues/173
@Test
@DisplayName("First incomplete, then completely filled chunks are stored completely")
public void testFullChunksAreSavedCompletely() throws IOException {
int halfAChunk = 16_384; //half of cleartext chunk size
try (var writer = FileChannel.open(file, CREATE, WRITE)) {
writer.write(ByteBuffer.allocate(3 * halfAChunk), 0); //fill chunk 0, half fill chunk 1
writer.write(ByteBuffer.allocate(5 * halfAChunk), 0); //fill chunks 0 and 1, half fill chunk 2
}

try (var reader = FileChannel.open(file, CREATE, READ)) {
Assertions.assertAll(() -> reader.read(ByteBuffer.allocate(2 * halfAChunk), 0), //read chunk 0
() -> reader.read(ByteBuffer.allocate(2 * halfAChunk), 2 * halfAChunk), //read chunk 1
() -> reader.read(ByteBuffer.allocate(halfAChunk), 4 * halfAChunk) //read chunk 2
);
}

}

@Test
public void testLockEmptyChannel() throws IOException {
try (FileChannel ch = FileChannel.open(file, CREATE, WRITE)) {
Expand Down

0 comments on commit 34056de

Please sign in to comment.