cryptomator / cryptofs

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

Invalid file size immediately after opening file channel #22

Closed overheadhunter closed 6 years ago

overheadhunter commented 6 years ago

When opening a file channel, cryptofs doesn't immediately write the file header. This causes problems when determining the file size as currently implemented in CryptoBasicFileAttributes, as the file header is subtracted from an empty file.

Path p = cryptoFileSystem.getPath("/demo.txt");
try (FileChannel channel = FileChannel.open(p, CREATE, WRITE)) {
    assertEquals(0, channel.size()); // ok
    assertEquals(0, Files.size(p)); // error
}

If the header gets written (by flushing the channel first), everything works fine:

Path p = cryptoFileSystem.getPath("/demo.txt");
try (FileChannel channel = FileChannel.open(p, CREATE, WRITE)) {
    channel.force(false);
    assertEquals(0, channel.size()); // ok
    assertEquals(0, Files.size(p)); // ok
}