From 593ef23a228240d5c08897cb3ffdb853e3c0fdec Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sun, 22 Nov 2015 17:36:54 +0100 Subject: [PATCH] Some improvements to LockedFile class. --- src/Config.h | 4 ++-- src/FileHash.cpp | 27 +++++++++++++------------- src/Global.h | 2 +- src/Global_Tools.cpp | 2 +- src/LockedFile.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++-------- src/LockedFile.h | 2 +- 6 files changed, 65 insertions(+), 26 deletions(-) diff --git a/src/Config.h b/src/Config.h index 6ec5cb5c..867a105f 100644 --- a/src/Config.h +++ b/src/Config.h @@ -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 /////////////////////////////////////////////////////////////////////////////// diff --git a/src/FileHash.cpp b/src/FileHash.cpp index 405d3eb9..fbebb492 100644 --- a/src/FileHash.cpp +++ b/src/FileHash.cpp @@ -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(); + } } } } diff --git a/src/Global.h b/src/Global.h index bbdca1cc..83415040 100644 --- a/src/Global.h +++ b/src/Global.h @@ -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); /* diff --git a/src/Global_Tools.cpp b/src/Global_Tools.cpp index 7a74dd39..59f3a49c 100644 --- a/src/Global_Tools.cpp +++ b/src/Global_Tools.cpp @@ -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); diff --git a/src/LockedFile.cpp b/src/LockedFile.cpp index a164bce4..b963cd9f 100644 --- a/src/LockedFile.cpp +++ b/src/LockedFile.cpp @@ -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; } diff --git a/src/LockedFile.h b/src/LockedFile.h index e5eb42e9..fe0995ca 100644 --- a/src/LockedFile.h +++ b/src/LockedFile.h @@ -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; -- 2.11.0