cryptomator / cryptofs

Java Filesystem Provider with integrated encryption
GNU Affero General Public License v3.0
94 stars 35 forks source link

Files.size(..) reports wrong size, while CryptoFileChannel contains unflushed bytes #26

Closed overheadhunter closed 6 years ago

overheadhunter commented 6 years ago

Similar to issue #22:

When opening a file channel, cryptofs keeps some bytes cached. This causes problems when getting the file size from CryptoBasicFileAttributes#size().

Path p = cryptoFileSystem.getPath("/demo.txt");
try (FileChannel channel = FileChannel.open(p, CREATE, WRITE)) {
    channel.write(ByteBuffer.wrap(new byte[10]));
    assertEquals(10, channel.size()); // ok
    assertEquals(10, Files.size(p)); // error
}

If the content gets flushed, everything works fine:

Path p = cryptoFileSystem.getPath("/demo.txt");
try (FileChannel channel = FileChannel.open(p, CREATE, WRITE)) {
    channel.write(ByteBuffer.wrap(new byte[10]));
    channel.force(false);
    assertEquals(10, channel.size()); // ok
    assertEquals(10, Files.size(p)); // ok
}

We need to make sure, that BasicFileAttributes get the size from OpenCryptoFile#size(), if the file is currently opened.