diff --git a/include/MUtils/Global.h b/include/MUtils/Global.h index 0d6822d..8498495 100644 --- a/include/MUtils/Global.h +++ b/include/MUtils/Global.h @@ -86,7 +86,7 @@ namespace MUtils //Remove File/Dir MUTILS_API bool remove_file(const QString &fileName); - MUTILS_API bool remove_directory(const QString &folderPath); + MUTILS_API bool remove_directory(const QString &folderPath, const bool &recursive); //String sorting MUTILS_API void natural_string_sort(QStringList &list, const bool bIgnoreCase); diff --git a/include/MUtils/OSSupport.h b/include/MUtils/OSSupport.h index 9dbb359..d54e6bc 100644 --- a/include/MUtils/OSSupport.h +++ b/include/MUtils/OSSupport.h @@ -102,6 +102,9 @@ namespace MUtils //CLI Arguments MUTILS_API const QStringList &arguments(void); + //Copy file + MUTILS_API bool copy_file(const QString &sourcePath, const QString &outputPath, const bool &overwrite = true); + //Get the OS version MUTILS_API const Version::os_version_t &os_version(void); MUTILS_API const char *os_friendly_name(const MUtils::OS::Version::os_version_t &os_version); diff --git a/src/DirLocker.h b/src/DirLocker.h index ceb94f5..c45f79b 100644 --- a/src/DirLocker.h +++ b/src/DirLocker.h @@ -86,23 +86,24 @@ namespace MUtils bool okay = false; if(!m_lockFile.isNull()) { - for(int i = 0; i < 8; i++) + for(int i = 0; i < 16; i++) { if(m_lockFile->remove()) { break; } - OS::sleep_ms(1); + OS::sleep_ms(125); } + m_lockFile.reset(NULL); } - for(int i = 0; i < 8; i++) + for(int i = 0; i < 16; i++) { - if(MUtils::remove_directory(m_dirPath)) + if(MUtils::remove_directory(m_dirPath, true)) { okay = true; break; } - OS::sleep_ms(1); + OS::sleep_ms(125); } if(!okay) { diff --git a/src/Global.cpp b/src/Global.cpp index 3741d87..17db27c 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -231,17 +231,34 @@ bool MUtils::remove_file(const QString &fileName) { QFile file(fileName); file.setPermissions(QFile::ReadOther | QFile::WriteOther); - if(file.remove()) + if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove()) { return true; } + fileInfo.refresh(); } qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName)); return false; } -bool MUtils::remove_directory(const QString &folderPath) +static bool remove_directory_helper(QDir folder) +{ + if(!folder.exists()) + { + return true; + } + + const QString dirName = folder.dirName(); + if(dirName.isEmpty() || (!folder.cdUp())) + { + return false; + } + + return folder.rmdir(dirName); +} + +bool MUtils::remove_directory(const QString &folderPath, const bool &recursive) { QDir folder(folderPath); if(!folder.exists()) @@ -249,25 +266,41 @@ bool MUtils::remove_directory(const QString &folderPath) return true; } - const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden); - for(int i = 0; i < entryList.count(); i++) + if(recursive) { - if(entryList.at(i).isDir()) + const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden); + for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++) { - remove_directory(entryList.at(i).canonicalFilePath()); - } - else - { - remove_file(entryList.at(i).canonicalFilePath()); + if(iter->isDir()) + { + remove_directory(iter->canonicalFilePath(), true); + } + else if(iter->isFile()) + { + remove_file(iter->canonicalFilePath()); + } } } for(int i = 0; i < 32; i++) { - if(folder.rmdir(".")) + if(!folder.exists()) { return true; } + const QString dirName = folder.dirName(); + if(!dirName.isEmpty()) + { + QDir parent(folder); + if(parent.cdUp()) + { + if(parent.rmdir(dirName)) + { + return true; + } + } + } + folder.refresh(); } qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath)); diff --git a/src/OSSupport_Win32.cpp b/src/OSSupport_Win32.cpp index d67ba16..0e9062c 100644 --- a/src/OSSupport_Win32.cpp +++ b/src/OSSupport_Win32.cpp @@ -113,6 +113,23 @@ const QStringList &MUtils::OS::arguments(void) return (*(g_arguments_list.data())); } +/////////////////////////////////////////////////////////////////////////////// +// COPY FILE +/////////////////////////////////////////////////////////////////////////////// + +MUTILS_API bool MUtils::OS::copy_file(const QString &sourcePath, const QString &outputPath, const bool &overwrite) +{ + const BOOL result = CopyFileW(MUTILS_WCHR(QDir::toNativeSeparators(sourcePath)), MUTILS_WCHR(QDir::toNativeSeparators(outputPath)), overwrite ? FALSE : TRUE); + + if(result == FALSE) + { + const DWORD errorCode = GetLastError(); + qWarning("CopyFile() failed with error code 0x%08X!", errorCode); + } + + return (result != FALSE); +} + /////////////////////////////////////////////////////////////////////////////// // OS VERSION DETECTION ///////////////////////////////////////////////////////////////////////////////