Skip to content

Commit

Permalink
fixes #173
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Jun 6, 2023
1 parent faa2ac3 commit 46df247
Show file tree
Hide file tree
Showing 2 changed files with 58 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,61 @@ public void afterEach() throws IOException {
Files.deleteIfExists(file);
}

//https://github.com/cryptomator/cryptofs/issues/173
@Test
public void testPages() throws IOException {
FileChannel writer = null;
try {
writer = FileChannel.open(file, CREATE, WRITE);
writeAndAssert(writer, 81920, 3719);
try (var reader = FileChannel.open(file, CREATE, READ)) {
writeAndAssert(writer, 81920, 17345);
readAndAssert(reader, 98304, 1036, 961);
writeAndAssert(writer, 98304, 65536);
writer.close();
}

try (var reader2 = FileChannel.open(file, CREATE, READ)) {
readAndAssert(reader2, 98304, 16384, 16384);
}
} finally {
if (writer != null) {
writer.close();
}
}

}

private void readAndAssert(FileChannel source, int position, int numBytes, int toRead) throws IOException {
var buf = ByteBuffer.allocate(numBytes);
int read = source.read(buf, position);
Assertions.assertEquals(toRead, read);
}

private void writeAndAssert(FileChannel destination, int position, int numOfBytes) throws IOException {
var buf = createOfSize(numOfBytes);
int written = destination.write(buf, position);
Assertions.assertEquals(numOfBytes, written);
}

private ByteBuffer createOfSize(int size) {
if (size < 16) {
throw new IllegalArgumentException("too small!");
}
byte[] content = "16bytesLongtext.".getBytes(StandardCharsets.UTF_8);
int reps = size / 16;
int rest = size % 16;
ByteBuffer buf = ByteBuffer.allocate(size);
for (int i = 0; i < reps; i++) {
buf.put(content);
}
if (rest > 0) {
buf.put(".".repeat(rest).getBytes(StandardCharsets.UTF_8));
}
buf.flip();
return buf;
}

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

0 comments on commit 46df247

Please sign in to comment.