OSDN Git Service

Added the copy_file() function + some improvements to directory clean-up code.
authorLoRd_MuldeR <mulder2@gmx.de>
Fri, 2 Jan 2015 22:50:14 +0000 (23:50 +0100)
committerLoRd_MuldeR <mulder2@gmx.de>
Fri, 2 Jan 2015 22:50:14 +0000 (23:50 +0100)
include/MUtils/Global.h
include/MUtils/OSSupport.h
src/DirLocker.h
src/Global.cpp
src/OSSupport_Win32.cpp

index 0d6822d..8498495 100644 (file)
@@ -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);
index 9dbb359..d54e6bc 100644 (file)
@@ -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);
index ceb94f5..c45f79b 100644 (file)
@@ -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)
                                {
index 3741d87..17db27c 100644 (file)
@@ -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));
index d67ba16..0e9062c 100644 (file)
@@ -114,6 +114,23 @@ const QStringList &MUtils::OS::arguments(void)
 }
 
 ///////////////////////////////////////////////////////////////////////////////
+// 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
 ///////////////////////////////////////////////////////////////////////////////