Implemented a method to "detect" the user's TEMP folder that doesn't depend on the %TMP% environment variable. QDir::temp() internally uses GetTempPath(), which *does* depend on %TMP%.

This commit is contained in:
LoRd_MuldeR 2010-12-07 22:58:28 +01:00
parent 801500ce94
commit 40b0300284
6 changed files with 68 additions and 25 deletions

View File

@ -25,7 +25,7 @@
#define VER_LAMEXP_MAJOR 4 #define VER_LAMEXP_MAJOR 4
#define VER_LAMEXP_MINOR_HI 0 #define VER_LAMEXP_MINOR_HI 0
#define VER_LAMEXP_MINOR_LO 0 #define VER_LAMEXP_MINOR_LO 0
#define VER_LAMEXP_BUILD 128 #define VER_LAMEXP_BUILD 130
#define VER_LAMEXP_SUFFIX TechPreview #define VER_LAMEXP_SUFFIX TechPreview
/* /*

View File

@ -30,7 +30,6 @@
#include <QFileDialog> #include <QFileDialog>
#include <QTimer> #include <QTimer>
#include <QProcess> #include <QProcess>
#include <QUuid>
#include <QDate> #include <QDate>
#include <QRegExp> #include <QRegExp>
#include <QDesktopServices> #include <QDesktopServices>
@ -197,7 +196,7 @@ void UpdateDialog::checkForUpdates(void)
if(connectionScore < MIN_CONNSCORE) if(connectionScore < MIN_CONNSCORE)
{ {
m_logFile->append(QStringList() << "" << "Testing host:" << known_hosts[i] << ""); m_logFile->append(QStringList() << "" << "Testing host:" << known_hosts[i] << "");
QString outFile = QString("%1/%2.htm").arg(QDir::tempPath(), QUuid::createUuid().toString()); QString outFile = QString("%1/%2.htm").arg(lamexp_temp_folder(), lamexp_rand_str());
if(getFile(known_hosts[i], outFile)) if(getFile(known_hosts[i], outFile))
{ {
connectionScore++; connectionScore++;
@ -288,9 +287,9 @@ bool UpdateDialog::tryUpdateMirror(UpdateInfo *updateInfo, const QString &url)
bool success = false; bool success = false;
m_logFile->append(QStringList() << "" << "Trying mirror:" << url); m_logFile->append(QStringList() << "" << "Trying mirror:" << url);
QUuid uuid = QUuid::createUuid(); QString randPart = lamexp_rand_str();
QString outFileVersionInfo = QString("%1/%2.ver").arg(QDir::tempPath(), uuid.toString()); QString outFileVersionInfo = QString("%1/%2.ver").arg(lamexp_temp_folder(), randPart);
QString outFileSignature = QString("%1/%2.sig").arg(QDir::tempPath(), uuid.toString()); QString outFileSignature = QString("%1/%2.sig").arg(lamexp_temp_folder(), randPart);
m_logFile->append(QStringList() << "" << "Downloading update info:"); m_logFile->append(QStringList() << "" << "Downloading update info:");
bool ok1 = getFile(QString("%1%2").arg(url,mirror_url_postfix), outFileVersionInfo); bool ok1 = getFile(QString("%1%2").arg(url,mirror_url_postfix), outFileVersionInfo);
@ -373,12 +372,9 @@ bool UpdateDialog::checkSignature(const QString &file, const QString &signature)
qWarning("CheckSignature: File and signature should be in same folder!"); qWarning("CheckSignature: File and signature should be in same folder!");
return false; return false;
} }
if(QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0)
QString keyring = QString("%1/%2.gpg").arg(QFileInfo(file).absolutePath(), QUuid::createUuid().toString());
if(!QFile::copy(m_binaryKeys, keyring))
{ {
qWarning("CheckSignature: Failed to copy keyring file to destination folder!"); qWarning("CheckSignature: File and keyring should be in same folder!");
return false; return false;
} }
@ -392,11 +388,10 @@ bool UpdateDialog::checkSignature(const QString &file, const QString &signature)
connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit())); connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit()));
connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit())); connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit()));
process.start(m_binaryGnuPG, QStringList() << "--homedir" << "." << "--keyring" << QFileInfo(keyring).fileName() << QFileInfo(signature).fileName() << QFileInfo(file).fileName()); process.start(m_binaryGnuPG, QStringList() << "--homedir" << "." << "--keyring" << QFileInfo(m_binaryKeys).fileName() << QFileInfo(signature).fileName() << QFileInfo(file).fileName());
if(!process.waitForStarted()) if(!process.waitForStarted())
{ {
QFile::remove(keyring);
return false; return false;
} }
@ -409,8 +404,6 @@ bool UpdateDialog::checkSignature(const QString &file, const QString &signature)
} }
} }
QFile::remove(keyring);
m_logFile->append(QString().sprintf("Exited with code %d", process.exitCode())); m_logFile->append(QString().sprintf("Exited with code %d", process.exitCode()));
return (process.exitCode() == 0); return (process.exitCode() == 0);
} }

View File

@ -38,11 +38,15 @@
#include <QMutex> #include <QMutex>
#include <QTextCodec> #include <QTextCodec>
#include <QLibrary> #include <QLibrary>
#include <QRegExp>
//LameXP includes //LameXP includes
#include "Resource.h" #include "Resource.h"
#include "LockedFile.h" #include "LockedFile.h"
//Windows includes
#include <Windows.h>
//CRT includes //CRT includes
#include <stdio.h> #include <stdio.h>
#include <io.h> #include <io.h>
@ -583,15 +587,58 @@ void lamexp_ipc_read(unsigned int *command, char* message, size_t buffSize)
LAMEXP_DELETE(lamexp_ipc); LAMEXP_DELETE(lamexp_ipc);
} }
/*
* Get a random string
*/
QString lamexp_rand_str(void)
{
QRegExp regExp("\\{(\\w+)-(\\w+)-(\\w+)-(\\w+)-(\\w+)\\}");
QString uuid = QUuid::createUuid().toString();
if(regExp.indexIn(uuid) >= 0)
{
return QString().append(regExp.cap(1)).append(regExp.cap(2)).append(regExp.cap(3)).append(regExp.cap(4)).append(regExp.cap(5));
}
throw "The RegExp didn't match on the UUID string. This shouldn't happen ;-)";
}
/* /*
* Get LameXP temp folder * Get LameXP temp folder
*/ */
const QString &lamexp_temp_folder(void) const QString &lamexp_temp_folder(void)
{ {
const GUID LocalAppDataLowID={0xA520A1A4,0x1780,0x4FF6,{0xBD,0x18,0x16,0x73,0x43,0xC5,0xAF,0x16}};
typedef HANDLE (WINAPI *SHGetKnownFolderPathFun)(__in const GUID &rfid, __in DWORD dwFlags, __in HANDLE hToken, __out PWSTR *ppszPath);
if(g_lamexp_temp_folder.isEmpty()) if(g_lamexp_temp_folder.isEmpty())
{ {
QDir temp = QDir::temp(); QDir temp = QDir::temp();
QLibrary Kernel32Lib("shell32.dll");
SHGetKnownFolderPathFun SHGetKnownFolderPathPtr = (SHGetKnownFolderPathFun) Kernel32Lib.resolve("SHGetKnownFolderPath");
if(SHGetKnownFolderPathPtr)
{
WCHAR *localAppDataLowPath = NULL;
if(SHGetKnownFolderPathPtr(LocalAppDataLowID, 0x00008000, NULL, &localAppDataLowPath) == S_OK)
{
QDir localAppDataLow = QDir(QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast<const unsigned short*>(localAppDataLowPath))));
if(localAppDataLow.exists())
{
if(!localAppDataLow.entryList(QDir::AllDirs).contains("Temp"))
{
localAppDataLow.mkdir("Temp");
}
if(localAppDataLow.cd("Temp"))
{
temp.setPath(localAppDataLow.canonicalPath());
}
}
CoTaskMemFree(localAppDataLowPath);
}
}
if(!temp.exists()) if(!temp.exists())
{ {
temp.mkpath("."); temp.mkpath(".");
@ -602,19 +649,19 @@ const QString &lamexp_temp_folder(void)
} }
} }
QString uuid = QUuid::createUuid().toString(); QString subDir = QString("%1.tmp").arg(lamexp_rand_str());
if(!temp.mkdir(uuid)) if(!temp.mkdir(subDir))
{ {
qFatal("Temporary directory could not be created:\n%s", QString("%1/%2").arg(temp.canonicalPath(), uuid).toUtf8().constData()); qFatal("Temporary directory could not be created:\n%s", QString("%1/%2").arg(temp.canonicalPath(), subDir).toUtf8().constData());
return g_lamexp_temp_folder; return g_lamexp_temp_folder;
} }
if(!temp.cd(uuid)) if(!temp.cd(subDir))
{ {
qFatal("Temporary directory could not be entered:\n%s", QString("%1/%2").arg(temp.canonicalPath(), uuid).toUtf8().constData()); qFatal("Temporary directory could not be entered:\n%s", QString("%1/%2").arg(temp.canonicalPath(), subDir).toUtf8().constData());
return g_lamexp_temp_folder; return g_lamexp_temp_folder;
} }
QFile testFile(QString("%1/~test.txt").arg(temp.canonicalPath())); QFile testFile(QString("%1/.%2").arg(temp.canonicalPath(), lamexp_rand_str()));
if(!testFile.open(QIODevice::ReadWrite) || testFile.write("LAMEXP_TEST\n") < 12) if(!testFile.open(QIODevice::ReadWrite) || testFile.write("LAMEXP_TEST\n") < 12)
{ {
qFatal("Write access to temporary directory has been denied:\n%s", temp.canonicalPath().toUtf8().constData()); qFatal("Write access to temporary directory has been denied:\n%s", temp.canonicalPath().toUtf8().constData());

View File

@ -76,6 +76,7 @@ bool lamexp_check_tool(const QString &toolName);
const QString lamexp_lookup_tool(const QString &toolName); const QString lamexp_lookup_tool(const QString &toolName);
unsigned int lamexp_tool_version(const QString &toolName); unsigned int lamexp_tool_version(const QString &toolName);
void lamexp_finalization(void); void lamexp_finalization(void);
QString lamexp_rand_str(void);
const QString &lamexp_temp_folder(void); const QString &lamexp_temp_folder(void);
void lamexp_ipc_read(unsigned int *command, char* message, size_t buffSize); void lamexp_ipc_read(unsigned int *command, char* message, size_t buffSize);
void lamexp_ipc_send(unsigned int command, const char* message); void lamexp_ipc_send(unsigned int command, const char* message);

View File

@ -51,6 +51,8 @@ int lamexp_main(int argc, char* argv[])
//Init console //Init console
lamexp_init_console(argc, argv); lamexp_init_console(argc, argv);
lamexp_rand_str();
//Print version info //Print version info
qDebug("LameXP - Audio Encoder Front-End"); qDebug("LameXP - Audio Encoder Front-End");
qDebug("Version %d.%02d %s, Build %d [%s], compiled with %s", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build(), lamexp_version_date().toString(Qt::ISODate).toLatin1().constData(), lamexp_version_compiler()); qDebug("Version %d.%02d %s, Build %d [%s], compiled with %s", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build(), lamexp_version_date().toString(Qt::ISODate).toLatin1().constData(), lamexp_version_compiler());

View File

@ -222,7 +222,7 @@ QString ProcessThread::generateOutFileName(void)
} }
} }
QFile writeTest(QString("%1/%2").arg(targetDir.canonicalPath(), QUuid::createUuid().toString())); QFile writeTest(QString("%1/.%2").arg(targetDir.canonicalPath(), lamexp_rand_str()));
if(!writeTest.open(QIODevice::ReadWrite)) if(!writeTest.open(QIODevice::ReadWrite))
{ {
handleMessage(QString("The target output directory is NOT writable:\n%1").arg(targetDir.absolutePath())); handleMessage(QString("The target output directory is NOT writable:\n%1").arg(targetDir.absolutePath()));
@ -252,11 +252,11 @@ QString ProcessThread::generateOutFileName(void)
QString ProcessThread::generateTempFileName(void) QString ProcessThread::generateTempFileName(void)
{ {
QMutexLocker lock(m_mutex_genFileName); QMutexLocker lock(m_mutex_genFileName);
QString tempFileName = QString("%1/%2.wav").arg(QDir::tempPath(), QUuid::createUuid().toString()); QString tempFileName = QString("%1/%2.wav").arg(lamexp_temp_folder(), lamexp_rand_str());
while(QFileInfo(tempFileName).exists()) while(QFileInfo(tempFileName).exists())
{ {
tempFileName = QString("%1/%2.wav").arg(QDir::tempPath(), QUuid::createUuid().toString()); tempFileName = QString("%1/%2.wav").arg(lamexp_temp_folder(), lamexp_rand_str());
} }
QFile file(tempFileName); QFile file(tempFileName);