Changed behavior of lamexp_temp_folder() function: Try to create the temporary folder in %TMP% first and fall back to "%LOCALAPPDATA%\Temp" only if %TMP% doesn't exist or isn't writable.

This commit is contained in:
LoRd_MuldeR 2011-03-08 01:12:40 +01:00
parent 9f8db9c010
commit f2ab4c0467
3 changed files with 109 additions and 18 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 1 #define VER_LAMEXP_MINOR_LO 1
#define VER_LAMEXP_BUILD 351 #define VER_LAMEXP_BUILD 356
#define VER_LAMEXP_SUFFIX Beta-6 #define VER_LAMEXP_SUFFIX Beta-6
/* /*

View File

@ -517,7 +517,7 @@ static const HANDLE g_debug_thread = lamexp_debug_thread_init();
/* /*
* Check for compatibility mode * Check for compatibility mode
*/ */
static bool lamexp_check_compatibility_mode(const char *exportName) static bool lamexp_check_compatibility_mode(const char *exportName, const char *executableName)
{ {
QLibrary kernel32("kernel32.dll"); QLibrary kernel32("kernel32.dll");
@ -525,7 +525,8 @@ static bool lamexp_check_compatibility_mode(const char *exportName)
{ {
if(kernel32.resolve(exportName) != NULL) if(kernel32.resolve(exportName) != NULL)
{ {
qFatal("Windows compatibility mode detected. Program will exit!"); qWarning("Function '%s' exported from 'kernel32.dll' -> Windows compatibility mode!", exportName);
qFatal("%s", QApplication::tr("Executable '%1' doesn't support Windows compatibility mode.").arg(QString::fromLatin1(executableName)).toLatin1().constData());
return false; return false;
} }
} }
@ -590,35 +591,46 @@ bool lamexp_init_qt(int argc, char* argv[])
return true; return true;
} }
//Extract executable name from argv[] array
char *executableName = argv[0];
while(char *temp = strpbrk(executableName, "\\/:?"))
{
executableName = temp + 1;
}
//Check Qt version //Check Qt version
qDebug("Using Qt Framework v%s, compiled with Qt v%s", qVersion(), QT_VERSION_STR); qDebug("Using Qt Framework v%s, compiled with Qt v%s [%s]", qVersion(), QT_VERSION_STR, QT_PACKAGEDATE_STR);
QT_REQUIRE_VERSION(argc, argv, QT_VERSION_STR); if(_stricmp(qVersion(), QT_VERSION_STR))
{
qFatal("%s", QApplication::tr("Executable '%1' requires Qt v%2, but found Qt v%3.").arg(QString::fromLatin1(executableName), QString::fromLatin1(QT_VERSION_STR), QString::fromLatin1(qVersion())).toLatin1().constData());
return false;
}
//Check the Windows version //Check the Windows version
switch(QSysInfo::windowsVersion() & QSysInfo::WV_NT_based) switch(QSysInfo::windowsVersion() & QSysInfo::WV_NT_based)
{ {
case QSysInfo::WV_2000: case QSysInfo::WV_2000:
qDebug("Running on Windows 2000 (not offically supported!).\n"); qDebug("Running on Windows 2000 (not offically supported!).\n");
lamexp_check_compatibility_mode("GetNativeSystemInfo"); lamexp_check_compatibility_mode("GetNativeSystemInfo", executableName);
break; break;
case QSysInfo::WV_XP: case QSysInfo::WV_XP:
qDebug("Running on Windows XP.\n"); qDebug("Running on Windows XP.\n");
lamexp_check_compatibility_mode("GetLargePageMinimum"); lamexp_check_compatibility_mode("GetLargePageMinimum", executableName);
break; break;
case QSysInfo::WV_2003: case QSysInfo::WV_2003:
qDebug("Running on Windows Server 2003 or Windows XP x64-Edition.\n"); qDebug("Running on Windows Server 2003 or Windows XP x64-Edition.\n");
lamexp_check_compatibility_mode("GetLocaleInfoEx"); lamexp_check_compatibility_mode("GetLocaleInfoEx", executableName);
break; break;
case QSysInfo::WV_VISTA: case QSysInfo::WV_VISTA:
qDebug("Running on Windows Vista or Windows Server 2008.\n"); qDebug("Running on Windows Vista or Windows Server 2008.\n");
lamexp_check_compatibility_mode("CreateRemoteThreadEx"); lamexp_check_compatibility_mode("CreateRemoteThreadEx", executableName);
break; break;
case QSysInfo::WV_WINDOWS7: case QSysInfo::WV_WINDOWS7:
qDebug("Running on Windows 7 or Windows Server 2008 R2.\n"); qDebug("Running on Windows 7 or Windows Server 2008 R2.\n");
lamexp_check_compatibility_mode(NULL); lamexp_check_compatibility_mode(NULL, executableName);
break; break;
default: default:
qFatal("Unsupported OS, only Windows 2000 or later is supported!"); qFatal("%s", QApplication::tr("Executable '%1' requires Windows 2000 or later.").arg(QString::fromLatin1(executableName)).toLatin1().constData());
break; break;
} }
@ -644,7 +656,7 @@ bool lamexp_init_qt(int argc, char* argv[])
{ {
if(!supportedFormats.contains(g_lamexp_imageformats[i])) if(!supportedFormats.contains(g_lamexp_imageformats[i]))
{ {
qFatal("Qt initialization error: At least one image format plugin is missing! (%s)", g_lamexp_imageformats[i]); qFatal("Qt initialization error: QImageIOHandler for '%s' missing!", g_lamexp_imageformats[i]);
return false; return false;
} }
} }
@ -653,9 +665,6 @@ bool lamexp_init_qt(int argc, char* argv[])
g_lamexp_translation.files.insert(LAMEXP_DEFAULT_LANGID, ""); g_lamexp_translation.files.insert(LAMEXP_DEFAULT_LANGID, "");
g_lamexp_translation.names.insert(LAMEXP_DEFAULT_LANGID, "English"); g_lamexp_translation.names.insert(LAMEXP_DEFAULT_LANGID, "English");
//Init language files
//lamexp_init_translations();
//Check for process elevation //Check for process elevation
if(!lamexp_check_elevation()) if(!lamexp_check_elevation())
{ {
@ -825,7 +834,88 @@ QString lamexp_rand_str(void)
const QString &lamexp_temp_folder2(void) const QString &lamexp_temp_folder2(void)
{ {
static const char *TEMP_STR = "Temp"; static const char *TEMP_STR = "Temp";
const QString WRITE_TEST_DATA = lamexp_rand_str();
const QString SUB_FOLDER = lamexp_rand_str();
//Already initialized?
if(!g_lamexp_temp_folder.isEmpty())
{
if(QDir(g_lamexp_temp_folder).exists())
{
return g_lamexp_temp_folder;
}
else
{
g_lamexp_temp_folder.clear();
}
}
//Try the %TMP% or %TEMP% directory first
QDir temp = QDir::temp();
if(temp.exists())
{
temp.mkdir(SUB_FOLDER);
if(temp.cd(SUB_FOLDER) && temp.exists())
{
QFile testFile(QString("%1/~%2.tmp").arg(temp.canonicalPath(), lamexp_rand_str()));
if(testFile.open(QIODevice::ReadWrite))
{
if(testFile.write(WRITE_TEST_DATA.toLatin1().constData()) >= strlen(WRITE_TEST_DATA.toLatin1().constData()))
{
g_lamexp_temp_folder = temp.canonicalPath();
}
testFile.remove();
}
}
if(!g_lamexp_temp_folder.isEmpty())
{
return g_lamexp_temp_folder;
}
}
//Create TEMP folder in %LOCALAPPDATA%
QDir localAppData = QDir(lamexp_known_folder(lamexp_folder_localappdata));
if(!localAppData.path().isEmpty())
{
if(!localAppData.exists())
{
localAppData.mkpath(".");
}
if(localAppData.exists())
{
if(!localAppData.entryList(QDir::AllDirs).contains(TEMP_STR, Qt::CaseInsensitive))
{
localAppData.mkdir(TEMP_STR);
}
if(localAppData.cd(TEMP_STR) && localAppData.exists())
{
localAppData.mkdir(SUB_FOLDER);
if(localAppData.cd(SUB_FOLDER) && localAppData.exists())
{
QFile testFile(QString("%1/~%2.tmp").arg(localAppData.canonicalPath(), lamexp_rand_str()));
if(testFile.open(QIODevice::ReadWrite))
{
if(testFile.write(WRITE_TEST_DATA.toLatin1().constData()) >= strlen(WRITE_TEST_DATA.toLatin1().constData()))
{
g_lamexp_temp_folder = localAppData.canonicalPath();
}
testFile.remove();
}
}
}
}
if(!g_lamexp_temp_folder.isEmpty())
{
return g_lamexp_temp_folder;
}
}
//Failed to create TEMP folder!
qFatal("Temporary directory could not be initialized!\n\nFirst attempt:%s\n\nSecond attempt:\n%s", temp.canonicalPath().toUtf8().constData(), localAppData.canonicalPath().toUtf8().constData());
return g_lamexp_temp_folder;
}
/*
if(g_lamexp_temp_folder.isEmpty()) if(g_lamexp_temp_folder.isEmpty())
{ {
QDir temp = QDir::temp(); QDir temp = QDir::temp();
@ -880,6 +970,7 @@ const QString &lamexp_temp_folder2(void)
return g_lamexp_temp_folder; return g_lamexp_temp_folder;
} }
*/
/* /*
* Clean folder * Clean folder

View File

@ -168,7 +168,7 @@ int lamexp_main(int argc, char* argv[])
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
#ifdef _DEBUG #ifdef _DEBUG
int iResult; int iResult = -1;
qInstallMsgHandler(lamexp_message_handler); qInstallMsgHandler(lamexp_message_handler);
LAMEXP_MEMORY_CHECK(iResult = lamexp_main(argc, argv)); LAMEXP_MEMORY_CHECK(iResult = lamexp_main(argc, argv));
lamexp_finalization(); lamexp_finalization();
@ -176,7 +176,7 @@ int main(int argc, char* argv[])
#else #else
try try
{ {
int iResult; int iResult = -1;
qInstallMsgHandler(lamexp_message_handler); qInstallMsgHandler(lamexp_message_handler);
iResult = lamexp_main(argc, argv); iResult = lamexp_main(argc, argv);
lamexp_finalization(); lamexp_finalization();