From 6d55fe78fe181ff6bec7fc235fa10ca3cf0c10c0 Mon Sep 17 00:00:00 2001 From: hogliux Date: Wed, 13 Jun 2018 12:07:25 +0100 Subject: [PATCH] Added a flag to specify if File::deleteRecursively should follow symlinks or not --- modules/juce_core/files/juce_File.cpp | 6 +++--- modules/juce_core/files/juce_File.h | 12 +++++++++--- modules/juce_core/native/juce_posix_SharedCode.h | 11 +++++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/modules/juce_core/files/juce_File.cpp b/modules/juce_core/files/juce_File.cpp index 6d0b44c92b..26171e7a15 100644 --- a/modules/juce_core/files/juce_File.cpp +++ b/modules/juce_core/files/juce_File.cpp @@ -256,13 +256,13 @@ bool File::setExecutePermission (bool shouldBeExecutable) const return setFileExecutableInternal (shouldBeExecutable); } -bool File::deleteRecursively() const +bool File::deleteRecursively (bool followSymlinks) const { bool worked = true; - if (isDirectory()) + if (isDirectory() && (followSymlinks || ! isSymbolicLink())) for (auto& f : findChildFiles (File::findFilesAndDirectories, false)) - worked = f.deleteRecursively() && worked; + worked = f.deleteRecursively (followSymlinks) && worked; return deleteFile() && worked; } diff --git a/modules/juce_core/files/juce_File.h b/modules/juce_core/files/juce_File.h index 12a6ad799f..253cf01c75 100644 --- a/modules/juce_core/files/juce_File.h +++ b/modules/juce_core/files/juce_File.h @@ -457,6 +457,9 @@ public: If this file is actually a directory, it may not be deleted correctly if it contains files. See deleteRecursively() as a better way of deleting directories. + If this file is a symlink, then the symlink will be deleted and not the target + of the symlink. + @returns true if the file has been successfully deleted (or if it didn't exist to begin with). @see deleteRecursively @@ -468,11 +471,14 @@ public: If this file is a directory, this will try to delete it and all its subfolders. If it's just a file, it will just try to delete the file. - @returns true if the file and all its subfolders have been successfully deleted - (or if it didn't exist to begin with). + + @param followSymlinks If true, then any symlink pointing to a directory will also + recursively delete the contents of that directory + @returns true if the file and all its subfolders have been successfully + deleted (or if it didn't exist to begin with). @see deleteFile */ - bool deleteRecursively() const; + bool deleteRecursively (bool followSymlinks = false) const; /** Moves this file or folder to the trash. diff --git a/modules/juce_core/native/juce_posix_SharedCode.h b/modules/juce_core/native/juce_posix_SharedCode.h index ce89f5ffe8..b05f1de9d5 100644 --- a/modules/juce_core/native/juce_posix_SharedCode.h +++ b/modules/juce_core/native/juce_posix_SharedCode.h @@ -428,11 +428,14 @@ bool File::setFileTimesInternal (int64 modificationTime, int64 accessTime, int64 bool File::deleteFile() const { - if (! exists() && ! isSymbolicLink()) - return true; + if (! isSymbolicLink()) + { + if (! exists()) + return true; - if (isDirectory()) - return rmdir (fullPath.toUTF8()) == 0; + if (isDirectory()) + return rmdir (fullPath.toUTF8()) == 0; + } return remove (fullPath.toUTF8()) == 0; }