Improved initialization of the TEMP directory.

This commit is contained in:
LoRd_MuldeR 2013-11-29 01:21:52 +01:00
parent ed34cf1fc8
commit 1d8709689b
4 changed files with 55 additions and 22 deletions

View File

@ -35,7 +35,7 @@
#define VER_LAMEXP_MINOR_LO 9 #define VER_LAMEXP_MINOR_LO 9
#define VER_LAMEXP_TYPE Alpha #define VER_LAMEXP_TYPE Alpha
#define VER_LAMEXP_PATCH 8 #define VER_LAMEXP_PATCH 8
#define VER_LAMEXP_BUILD 1481 #define VER_LAMEXP_BUILD 1489
#define VER_LAMEXP_CONFG 1348 #define VER_LAMEXP_CONFG 1348
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -69,7 +69,8 @@ typedef enum
{ {
lamexp_folder_localappdata = 0, lamexp_folder_localappdata = 0,
lamexp_folder_programfiles = 2, lamexp_folder_programfiles = 2,
lamexp_folder_systemfolder = 3 lamexp_folder_systemfolder = 3,
lamexp_folder_systroot_dir = 4
} }
lamexp_known_folder_t; lamexp_known_folder_t;

View File

@ -85,30 +85,37 @@ QString lamexp_rand_str(const bool bLong)
*/ */
static QString lamexp_try_init_folder(const QString &folderPath) static QString lamexp_try_init_folder(const QString &folderPath)
{ {
static const char *DATA = "Lorem ipsum dolor sit amet, consectetur, adipisci velit!";
bool success = false; bool success = false;
const QFileInfo folderInfo(folderPath); const QFileInfo folderInfo(folderPath);
const QDir folderDir(folderInfo.absoluteFilePath()); const QDir folderDir(folderInfo.absoluteFilePath());
//Create folder, if it does *not* exist yet //Create folder, if it does *not* exist yet
if(!folderDir.exists()) for(int i = 0; i < 16; i++)
{ {
if(folderDir.exists()) break;
folderDir.mkpath("."); folderDir.mkpath(".");
} }
//Make sure folder exists now *and* is writable //Make sure folder exists now *and* is writable
if(folderDir.exists()) if(folderDir.exists())
{ {
QFile testFile(folderDir.absoluteFilePath(QString("~%1.tmp").arg(lamexp_rand_str()))); const QByteArray testData = QByteArray(DATA);
if(testFile.open(QIODevice::ReadWrite)) for(int i = 0; i < 32; i++)
{ {
const QByteArray testData = QByteArray("Lorem ipsum dolor sit amet, consectetur, adipisci velit!"); QFile testFile(folderDir.absoluteFilePath(QString("~%1.tmp").arg(lamexp_rand_str())));
if(testFile.write(testData) >= strlen(testData)) if(testFile.open(QIODevice::ReadWrite | QIODevice::Truncate))
{ {
success = true; if(testFile.write(testData) >= testData.size())
{
success = true;
}
testFile.remove(); testFile.remove();
testFile.close();
} }
testFile.close(); if(success) break;
} }
} }
@ -118,9 +125,17 @@ static QString lamexp_try_init_folder(const QString &folderPath)
/* /*
* Initialize LameXP temp folder * Initialize LameXP temp folder
*/ */
#define INIT_TEMP_FOLDER(OUT,TMP) do \ #define INIT_TEMP_FOLDER_RAND(OUT_PTR, BASE_DIR) do \
{ \ { \
(OUT) = lamexp_try_init_folder(QString("%1/%2").arg((TMP), lamexp_rand_str())); \ for(int _i = 0; _i < 128; _i++) \
{ \
const QString _randDir = QString("%1/%2").arg((BASE_DIR), lamexp_rand_str()); \
if(!QDir(_randDir).exists()) \
{ \
*(OUT_PTR) = lamexp_try_init_folder(_randDir); \
if(!(OUT_PTR)->isEmpty()) break; \
} \
} \
} \ } \
while(0) while(0)
@ -165,16 +180,25 @@ const QString &lamexp_temp_folder2(void)
QString tempPath = lamexp_try_init_folder(QDir::temp().absolutePath()); QString tempPath = lamexp_try_init_folder(QDir::temp().absolutePath());
if(!tempPath.isEmpty()) if(!tempPath.isEmpty())
{ {
INIT_TEMP_FOLDER(*g_lamexp_temp_folder.path, tempPath); INIT_TEMP_FOLDER_RAND(g_lamexp_temp_folder.path, tempPath);
} }
//Otherwise create TEMP folder in %LOCALAPPDATA% //Otherwise create TEMP folder in %LOCALAPPDATA% or %SYSTEMROOT%
if(g_lamexp_temp_folder.path->isEmpty()) if(g_lamexp_temp_folder.path->isEmpty())
{ {
tempPath = lamexp_try_init_folder(QString("%1/Temp").arg(lamexp_known_folder(lamexp_folder_localappdata))); qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
if(!tempPath.isEmpty()) static const lamexp_known_folder_t folderId[2] = { lamexp_folder_localappdata, lamexp_folder_systroot_dir };
for(size_t id = 0; (g_lamexp_temp_folder.path->isEmpty() && (id < 2)); id++)
{ {
INIT_TEMP_FOLDER(*g_lamexp_temp_folder.path, tempPath); const QString &knownFolder = lamexp_known_folder(folderId[id]);
if(!knownFolder.isEmpty())
{
tempPath = lamexp_try_init_folder(QString("%1/Temp").arg(knownFolder));
if(!tempPath.isEmpty())
{
INIT_TEMP_FOLDER_RAND(g_lamexp_temp_folder.path, tempPath);
}
}
} }
} }

View File

@ -1155,13 +1155,16 @@ const QString &lamexp_known_folder(lamexp_known_folder_t folder_id)
typedef HRESULT (WINAPI *SHGetKnownFolderPathFun)(__in const GUID &rfid, __in DWORD dwFlags, __in HANDLE hToken, __out PWSTR *ppszPath); typedef HRESULT (WINAPI *SHGetKnownFolderPathFun)(__in const GUID &rfid, __in DWORD dwFlags, __in HANDLE hToken, __out PWSTR *ppszPath);
typedef HRESULT (WINAPI *SHGetFolderPathFun)(__in HWND hwndOwner, __in int nFolder, __in HANDLE hToken, __in DWORD dwFlags, __out LPWSTR pszPath); typedef HRESULT (WINAPI *SHGetFolderPathFun)(__in HWND hwndOwner, __in int nFolder, __in HANDLE hToken, __in DWORD dwFlags, __out LPWSTR pszPath);
static const int CSIDL_LOCAL_APPDATA = 0x001c; static const int CSIDL_LOCAL_APPDATA = 0x001c;
static const int CSIDL_PROGRAM_FILES = 0x0026; static const int CSIDL_PROGRAM_FILES = 0x0026;
static const int CSIDL_SYSTEM_FOLDER = 0x0025; static const int CSIDL_WINDOWS_FOLDER = 0x0024;
static const GUID GUID_LOCAL_APPDATA = {0xF1B32785,0x6FBA,0x4FCF,{0x9D,0x55,0x7B,0x8E,0x7F,0x15,0x70,0x91}}; static const int CSIDL_SYSTEM_FOLDER = 0x0025;
static const GUID GUID_LOCAL_APPDATA = {0xF1B32785,0x6FBA,0x4FCF,{0x9D,0x55,0x7B,0x8E,0x7F,0x15,0x70,0x91}};
static const GUID GUID_LOCAL_APPDATA_LOW = {0xA520A1A4,0x1780,0x4FF6,{0xBD,0x18,0x16,0x73,0x43,0xC5,0xAF,0x16}}; static const GUID GUID_LOCAL_APPDATA_LOW = {0xA520A1A4,0x1780,0x4FF6,{0xBD,0x18,0x16,0x73,0x43,0xC5,0xAF,0x16}};
static const GUID GUID_PROGRAM_FILES = {0x905e63b6,0xc1bf,0x494e,{0xb2,0x9c,0x65,0xb7,0x32,0xd3,0xd2,0x1a}}; static const GUID GUID_PROGRAM_FILES = {0x905e63b6,0xc1bf,0x494e,{0xb2,0x9c,0x65,0xb7,0x32,0xd3,0xd2,0x1a}};
static const GUID GUID_SYSTEM_FOLDER = {0x1AC14E77,0x02E7,0x4E5D,{0xB7,0x44,0x2E,0xB1,0xAE,0x51,0x98,0xB7}}; static const GUID GUID_WINDOWS_FOLDER = {0xF38BF404,0x1D43,0x42F2,{0x93,0x05,0x67,0xDE,0x0B,0x28,0xFC,0x23}};
static const GUID GUID_SYSTEM_FOLDER = {0x1AC14E77,0x02E7,0x4E5D,{0xB7,0x44,0x2E,0xB1,0xAE,0x51,0x98,0xB7}};
QReadLocker readLock(&g_lamexp_known_folder.lock); QReadLocker readLock(&g_lamexp_known_folder.lock);
@ -1186,6 +1189,11 @@ const QString &lamexp_known_folder(lamexp_known_folder_t folder_id)
folderCSIDL = CSIDL_SYSTEM_FOLDER; folderCSIDL = CSIDL_SYSTEM_FOLDER;
folderGUID = GUID_SYSTEM_FOLDER; folderGUID = GUID_SYSTEM_FOLDER;
break; break;
case lamexp_folder_systroot_dir:
folderCacheId = 3;
folderCSIDL = CSIDL_WINDOWS_FOLDER;
folderGUID = GUID_WINDOWS_FOLDER;
break;
default: default:
qWarning("Invalid 'known' folder was requested!"); qWarning("Invalid 'known' folder was requested!");
return *reinterpret_cast<QString*>(NULL); return *reinterpret_cast<QString*>(NULL);