OSDN Git Service

Some improvements to LockedFile class.
authorLoRd_MuldeR <mulder2@gmx.de>
Sun, 22 Nov 2015 16:36:54 +0000 (17:36 +0100)
committerLoRd_MuldeR <mulder2@gmx.de>
Sun, 22 Nov 2015 16:36:54 +0000 (17:36 +0100)
src/Config.h
src/FileHash.cpp
src/Global.h
src/Global_Tools.cpp
src/LockedFile.cpp
src/LockedFile.h

index 6ec5cb5..867a105 100644 (file)
@@ -34,8 +34,8 @@
 #define VER_LAMEXP_MINOR_HI                                    1
 #define VER_LAMEXP_MINOR_LO                                    3
 #define VER_LAMEXP_TYPE                                                RC
-#define VER_LAMEXP_PATCH                                       3
-#define VER_LAMEXP_BUILD                                       1834
+#define VER_LAMEXP_PATCH                                       4
+#define VER_LAMEXP_BUILD                                       1840
 #define VER_LAMEXP_CONFG                                       1818
 
 ///////////////////////////////////////////////////////////////////////////////
index 405d3eb..fbebb49 100644 (file)
@@ -32,26 +32,27 @@ static const char *g_salt = "ee9f7bdabc170763d2200a7e3030045aafe380011aefc1730e5
 
 QByteArray FileHash::computeHash(QFile &file)
 {
-       QByteArray hash = QByteArray::fromHex(g_blnk);
+       QByteArray hash = QByteArray::fromHex(g_blnk).toHex();
 
        if(file.isOpen() && file.reset())
        {
                MUtils::Hash::Keccak keccak;
-
                const QByteArray data = file.readAll();
-               const QByteArray seed = QByteArray::fromHex(g_seed);
-               const QByteArray salt = QByteArray::fromHex(g_salt);
-       
-               if(keccak.init(MUtils::Hash::Keccak::hb384))
+               if (data.size() >= 16)
                {
-                       bool ok = true;
-                       ok = ok && keccak.addData(seed);
-                       ok = ok && keccak.addData(data);
-                       ok = ok && keccak.addData(salt);
-                       if(ok)
+                       const QByteArray seed = QByteArray::fromHex(g_seed);
+                       const QByteArray salt = QByteArray::fromHex(g_salt);
+                       if (keccak.init(MUtils::Hash::Keccak::hb384))
                        {
-                               const QByteArray digest = keccak.finalize();
-                               if(!digest.isEmpty()) hash = digest.toHex();
+                               bool ok = true;
+                               ok = ok && keccak.addData(seed);
+                               ok = ok && keccak.addData(data);
+                               ok = ok && keccak.addData(salt);
+                               if (ok)
+                               {
+                                       const QByteArray digest = keccak.finalize();
+                                       if (!digest.isEmpty()) hash = digest.toHex();
+                               }
                        }
                }
        }
index bbdca1c..8341504 100644 (file)
@@ -45,7 +45,7 @@ class LockedFile;
  */
 void           lamexp_tools_register(const QString &toolName, LockedFile *const file, const quint32 &version, const QString &tag = QString());
 bool           lamexp_tools_check   (const QString &toolName);
-const QString& lamexp_tools_lookup  (const QString &toolName);
+const QString  lamexp_tools_lookup  (const QString &toolName);
 const quint32& lamexp_tools_version (const QString &toolName, QString *const tagOut = NULL);
 
 /*
index 7a74dd3..59f3a49 100644 (file)
@@ -133,7 +133,7 @@ bool lamexp_tools_check(const QString &toolName)
 /*
  * Lookup tool path
  */
-const QString &lamexp_tools_lookup(const QString &toolName)
+const QString lamexp_tools_lookup(const QString &toolName)
 {
        QReadLocker readLock(&g_lamexp_tools_lock);
 
index a164bce..b963cd9 100644 (file)
@@ -117,23 +117,52 @@ static __forceinline void doValidateFileExists(const QString &filePath)
 
 static __forceinline void doLockFile(HANDLE &fileHandle, const QString &filePath, QFile *const outFile)
 {
+       bool success = false;
+       fileHandle = INVALID_HANDLE_VALUE;
+
+       //Try to open the file!
        for(int i = 0; i < 64; i++)
        {
-               fileHandle = CreateFileW(MUTILS_WCHR(QDir::toNativeSeparators(filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
-               if(VALID_HANDLE(fileHandle))
+               const HANDLE hTemp = CreateFileW(MUTILS_WCHR(QDir::toNativeSeparators(filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
+               if(VALID_HANDLE(hTemp))
                {
-                       break;
+                       fileHandle = hTemp;
+                       break; /*file opened successfully*/
                }
                if(i == 0)
                {
-                       qWarning("Failed to lock file on first attemp, retrying...");
+                       qWarning("Failed to open file on first attemp, retrying...");
                }
-               Sleep(25);
+               Sleep(1);
        }
        
+       //Now try to actually lock the file!
+       if (VALID_HANDLE(fileHandle))
+       {
+               for (int i = 0; i < 64; i++)
+               {
+                       LARGE_INTEGER fileSize;
+                       if (GetFileSizeEx(fileHandle, &fileSize))
+                       {
+                               OVERLAPPED overlapped = { 0U, 0U, 0U, 0U, 0U };
+                               if (LockFileEx(fileHandle, LOCKFILE_FAIL_IMMEDIATELY, 0, fileSize.LowPart, fileSize.HighPart, &overlapped))
+                               {
+                                       success = true;
+                                       break; /*file locked successfully*/
+                               }
+                               Sleep(1);
+                       }
+                       if (i == 0)
+                       {
+                               qWarning("Failed to lock file on first attemp, retrying...");
+                       }
+               }
+       }
+
        //Locked successfully?
-       if(!VALID_HANDLE(fileHandle))
+       if(!success)
        {
+               CLOSE_HANDLE(fileHandle);
                if(outFile)
                {
                        QFile::remove(QFileInfo(*outFile).canonicalFilePath());
@@ -172,7 +201,7 @@ static __forceinline void doValidateHash(HANDLE &fileHandle, const int &fileDesc
        }
 
        //Opened successfully
-       if(!checkFile.isOpen())
+       if((!checkFile.isOpen()) || checkFile.peek(1).isEmpty())
        {
                QFile::remove(filePath);
                MUTILS_THROW_FMT("File '%s' could not be read!", MUTILS_UTF8(QFileInfo(filePath).fileName()));
@@ -287,7 +316,16 @@ LockedFile::~LockedFile(void)
        }
 }
 
-const QString &LockedFile::filePath()
+const QString LockedFile::filePath(void)
 {
+       if (m_fileDescriptor >= 0)
+       {
+               const QString path = MUtils::OS::get_file_path(m_fileDescriptor);
+               if (!path.isEmpty())
+               {
+                       return path;
+               }
+               MUTILS_THROW_FMT("Failed to determine file path!");
+       }
        return m_filePath;
 }
index e5eb42e..fe0995c 100644 (file)
@@ -35,7 +35,7 @@ public:
        LockedFile(const QString &filePath, const bool bOwnsFile = false);
        ~LockedFile(void);
 
-       const QString &filePath();
+       const QString filePath(void);
 
 private:
        const bool m_bOwnsFile;