From: LoRd_MuldeR Date: Mon, 5 Jan 2015 20:42:20 +0000 (+0100) Subject: Updated the remove_directory() function to clear the directory attributes before... X-Git-Tag: v1.02~17 X-Git-Url: http://git.osdn.net/view?p=mutilities%2FMUtilities.git;a=commitdiff_plain;h=2f66f6b662840e9004651b0b4eb1d7dd7e7ea809 Updated the remove_directory() function to clear the directory attributes before trying to delete the directory. It turns out that, on the Windows platform, directories *can* be read-only - despite the fact that the Windows Explorer can NOT set (or clear) the "read-only" flag for directories and despite the fact that Windows Explorer seems to totally ignore the "read-only" fag when it has been set somehow. --- diff --git a/src/Global.cpp b/src/Global.cpp index fb5295a..ca1864b 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -240,6 +240,8 @@ const QString &MUtils::temp_folder(void) // REMOVE DIRECTORY / FILE /////////////////////////////////////////////////////////////////////////////// +static const QFile::Permissions FILE_PERMISSIONS_NONE = QFile::ReadOther | QFile::WriteOther; + bool MUtils::remove_file(const QString &fileName) { QFileInfo fileInfo(fileName); @@ -251,7 +253,7 @@ bool MUtils::remove_file(const QString &fileName) for(int i = 0; i < 32; i++) { QFile file(fileName); - file.setPermissions(QFile::ReadOther | QFile::WriteOther); + file.setPermissions(FILE_PERMISSIONS_NONE); if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove()) { return true; @@ -263,20 +265,26 @@ bool MUtils::remove_file(const QString &fileName) return false; } -static bool remove_directory_helper(QDir folder) +static bool remove_directory_helper(const QDir &folder) { if(!folder.exists()) { return true; } - const QString dirName = folder.dirName(); - if(dirName.isEmpty() || (!folder.cdUp())) + if(!dirName.isEmpty()) { - return false; + QDir parent(folder); + if(parent.cdUp()) + { + QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE); + if(parent.rmdir(dirName)) + { + return true; + } + } } - - return folder.rmdir(dirName); + return false; } bool MUtils::remove_directory(const QString &folderPath, const bool &recursive) @@ -305,22 +313,10 @@ bool MUtils::remove_directory(const QString &folderPath, const bool &recursive) for(int i = 0; i < 32; i++) { - if(!folder.exists()) + if(remove_directory_helper(folder)) { return true; } - const QString dirName = folder.dirName(); - if(!dirName.isEmpty()) - { - QDir parent(folder); - if(parent.cdUp()) - { - if(parent.rmdir(dirName)) - { - return true; - } - } - } folder.refresh(); }