Some improvements to LockedFile class.

This commit is contained in:
LoRd_MuldeR 2015-11-22 17:36:54 +01:00
parent 4b5158c4ae
commit 593ef23a22
6 changed files with 66 additions and 27 deletions

View 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
///////////////////////////////////////////////////////////////////////////////

View 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();
}
}
}
}

View 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);
/*

View 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);

View 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);
}
//Locked successfully?
if(!VALID_HANDLE(fileHandle))
//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(!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;
}

View 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;