File::copyFileTo deletes wrong file in some cases

A user of one of our products hit an edge case in copyFileTo where *this == newFile does return false for the same physical file because it compares the path strings.
This leads to the unlucky case that the function deletes the file and after that copyInternal fails because the file isn’t there anymore.

Here the JUCE code:

bool File::copyFileTo (const File& newFile) const
{
    return (*this == newFile)
            || (exists() && newFile.deleteFile() && copyInternal (newFile));
}

Here is an example of such a case where both paths point to the same directory or file in different ways:
/Volumes/Macintosh HD/Library/Audio/Plug-Ins
is the same as:
/Library/Audio/Plug-Ins

We should make sure that this can’t happen. Is there a way to resolve this problem without handling every special case?

I don’t see any solution for this at the moment. Any help is welcome.

3 Likes

using getLinkedTarget ?

Check if realpath returns the same string for both paths?

This did not make any difference in my tests. I do not get the same path string this way.

This method name sounds promising, but I couldn’t find that one!?

Maybe we need to get a unique identifier for the file that helps to verify if the file is the same or a way to get a cleaned-up path from the operating system.

realpath is only available on linux. It is called GetFullPathName on Windows. Better to call absolute_path in juce to keep your code cross platform capable.