diff --git a/.gitignore b/.gitignore index a91e51c2..fb65cb07 100644 --- a/.gitignore +++ b/.gitignore @@ -177,3 +177,7 @@ nunit-Squirrel.Tests.xml ## Pester Test Output tests/Test.xml + +## CPP db crap +*.db +*.opendb diff --git a/src/Setup/UpdateRunner.cpp b/src/Setup/UpdateRunner.cpp index 54e24dc5..03aa0d09 100644 --- a/src/Setup/UpdateRunner.cpp +++ b/src/Setup/UpdateRunner.cpp @@ -128,6 +128,30 @@ HRESULT CUpdateRunner::ShellExecuteFromExplorer(LPWSTR pszFile, LPWSTR pszParame CComVariant(SW_SHOWDEFAULT)); } +bool CUpdateRunner::DirectoryExistsW(wchar_t* szPath) +{ + DWORD dwAttrib = GetFileAttributes(szPath); + + return (dwAttrib != INVALID_FILE_ATTRIBUTES && + (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); +} + +bool CUpdateRunner::DirectoryIsWritableW(wchar_t * szPath) +{ + try { + wchar_t szTempFileName[MAX_PATH]; + UINT uRetVal = GetTempFileNameW(szPath, L"Squirrel", 0, szTempFileName); + if (uRetVal == 0) { + return false; + } + DeleteFile(szTempFileName); + return true; + } + catch (...) { + return false; + } +} + int CUpdateRunner::ExtractUpdaterAndRun(wchar_t* lpCommandLine, bool useFallbackDir) { PROCESS_INFORMATION pi = { 0 }; @@ -137,11 +161,20 @@ int CUpdateRunner::ExtractUpdaterAndRun(wchar_t* lpCommandLine, bool useFallback wchar_t logFile[MAX_PATH]; std::vector to_delete; + bool envSquirrelTempIsOk = false; wchar_t *envSquirrelTemp = _wgetenv(L"SQUIRREL_TEMP"); if (envSquirrelTemp) { - _swprintf_c(targetDir, _countof(targetDir), L"%s", envSquirrelTemp); + if (DirectoryExistsW(envSquirrelTemp)) { + if (DirectoryIsWritableW(envSquirrelTemp)) { + if (!PathIsUNCW(envSquirrelTemp)) { + _swprintf_c(targetDir, _countof(targetDir), L"%s", envSquirrelTemp); + bool envSquirrelTempIsOk = false; + } + } + } } - else { + + if(!envSquirrelTempIsOk) { if (!useFallbackDir) { SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, targetDir); } else { diff --git a/src/Setup/UpdateRunner.h b/src/Setup/UpdateRunner.h index 4973c76b..35da0d93 100644 --- a/src/Setup/UpdateRunner.h +++ b/src/Setup/UpdateRunner.h @@ -6,5 +6,7 @@ public: static void DisplayErrorMessage(CString& errorMessage, wchar_t* logFile); static HRESULT AreWeUACElevated(); static HRESULT ShellExecuteFromExplorer(LPWSTR pszFile, LPWSTR pszParameters); + static bool DirectoryExistsW(wchar_t* szPath); + static bool DirectoryIsWritableW(wchar_t* szPath); static int ExtractUpdaterAndRun(wchar_t* lpCommandLine, bool useFallbackDir); };