1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

File: Avoid leaving behind file system changes when moveFileTo() fails

This commit is contained in:
attila 2024-01-09 11:14:30 +01:00
parent b8f3030e0b
commit 3f52b29432
3 changed files with 16 additions and 0 deletions

View file

@ -360,6 +360,14 @@ File File::getParentDirectory() const
return createFileWithoutCheckingPath (getPathUpToLastSlash());
}
bool File::isNonEmptyDirectory() const
{
if (! isDirectory())
return false;
return RangedDirectoryIterator (*this, false, "*", findFilesAndDirectories) != RangedDirectoryIterator();
}
//==============================================================================
String File::getFileName() const
{

View file

@ -510,6 +510,10 @@ public:
Also note that on some OSes (e.g. Windows), moving files between different
volumes may not be possible.
This function will often fail to move directories because of the ambiguities
about merging existing directories. Use copyDirectoryTo() and deleteRecursively()
in these situations.
@returns true if the operation succeeds
*/
bool moveFileTo (const File& targetLocation) const;
@ -1150,6 +1154,7 @@ private:
static String parseAbsolutePath (const String&);
String getPathUpToLastSlash() const;
bool isNonEmptyDirectory() const;
Result createDirectoryInternal (const String&) const;
bool copyInternal (const File&) const;

View file

@ -405,6 +405,9 @@ bool File::moveInternal (const File& dest) const
if (rename (fullPath.toUTF8(), dest.getFullPathName().toUTF8()) == 0)
return true;
if (isNonEmptyDirectory())
return false;
if (hasWriteAccess() && copyInternal (dest))
{
if (deleteFile())