cryptomator / cryptofs

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

Moving dirs/symlinks lead to error when Source and Target have shortened names #111

Closed infeo closed 3 years ago

infeo commented 3 years ago

Moving a directory (or symlink) with a shortened name to a target, which name also needs to be shortend, makes the content of the dir inaccessible.

It is caused by the combination of the implenations of the moveDirectory(...) (or moveSymlink(...)) method and of the persistence method for the long name:

overheadhunter commented 3 years ago

I'd rather do this as a 2.0.x hotfix and maybe even backport it to 1.x

infeo commented 3 years ago

From my point of view there are two solutions:

  1. Introduce a force flag to the "persist" methods
  2. Change persistInternal method to always open the file channel

Since we do nothing in the FileAlreadyExistsException, I prefer solution 2 for its absolutly minimal changeset. The only worry is performance, but looking at the usages of persistsLongName(), nearly always name.c9r file needs to be created. The only exception are already existing files with shortened names. Everytime a filechannel is opened to such a file, persistLongName() is also called (but nothing a simple if cannot solve):

    private FileChannel newFileChannelFromFile(CryptoPath cleartextFilePath, EffectiveOpenOptions options, FileAttribute<?>... attrs) throws IOException {
        /*
         * Some Code
         */
        if (options.createNew() && openCryptoFiles.get(ciphertextFilePath).isPresent()) {
            throw new FileAlreadyExistsException(cleartextFilePath.toString());
        } else {
            /* More Code */ 

            FileChannel ch = openCryptoFiles.getOrCreate(ciphertextFilePath).newFileChannel(options);
            if (options.writable()) {
                ciphertextPath.persistLongFileName(); //always call the function
                stats.incrementAccessesWritten();
            }
            /* final statements */
            return ch;
        }
    }
overheadhunter commented 3 years ago

Agreed! Performance impact should be negligible and shortening occurs rarely.