Skip to content

Commit

Permalink
Fix GUP crashes when %TEMP% is not set
Browse files Browse the repository at this point in the history
Use a fallback TMP, TEMP or use the other directory.

Fix #16, close #18
  • Loading branch information
mere-human authored and donho committed Sep 23, 2021
1 parent fe6e7fe commit 3d5cb3f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
37 changes: 35 additions & 2 deletions src/winmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const wchar_t MSGID_DOWNLOADSTOPPED[] = L"Download is stopped by user. Update is
const wchar_t MSGID_CLOSEAPP[] = L" is opened.\rUpdater will close it in order to process the installation.\rContinue?";
const wchar_t MSGID_ABORTORNOT[] = L"Do you want to abort update download?";
const wchar_t MSGID_UNZIPFAILED[] = L"Can't unzip:\nOperation not permitted or decompression failed";
const wchar_t MSGID_NODOWNLOADFOLDER[] = L"Can't find a download folder";
const wchar_t MSGID_HELP[] = L"Usage :\r\
\r\
gup --help\r\
Expand Down Expand Up @@ -917,6 +918,34 @@ bool runInstaller(const wstring& app2runPath, const wstring& binWindowsClassName
return true;
}

// Returns a folder suitable for exe installer download destination.
// In case of error, shows a message box and returns an empty string.
std::wstring getDestDir(const GupNativeLang& nativeLang, const GupParameters& gupParams)
{
// Note: Other fallback directories may be Downloads, %UserProfile%, etc.
const wchar_t* envVar = _wgetenv(L"TEMP");
if (envVar)
return envVar;
envVar = _wgetenv(L"TMP");
if (envVar)
return envVar;

envVar = _wgetenv(L"AppData");
if (envVar)
{
std::wstring result = envVar;
result += L"\\Notepad++";
if (::PathFileExists(result.c_str()))
return result;
}

std::wstring message = nativeLang.getMessageString("MSGID_NODOWNLOADFOLDER");
if (message.empty())
message = MSGID_NODOWNLOADFOLDER;
::MessageBox(NULL, message.c_str(), gupParams.getMessageBoxTitle().c_str(), MB_ICONERROR | MB_OK);
return {};
}


#ifdef _DEBUG
#define WRITE_LOG(fn, suffix, log) writeLog(fn, suffix, log);
Expand Down Expand Up @@ -1071,7 +1100,9 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR lpszCmdLine, int)
}

// install
std::wstring dlDest = _wgetenv(L"TEMP");
std::wstring dlDest = getDestDir(nativeLang, gupParams);
if (dlDest.empty())
return -1;
dlDest += L"\\";
dlDest += ::PathFindFileName(dlUrl.c_str());

Expand Down Expand Up @@ -1235,7 +1266,9 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR lpszCmdLine, int)
// Download executable bin
//

std::wstring dlDest = _wgetenv(L"TEMP");
std::wstring dlDest = getDestDir(nativeLang, gupParams);
if (dlDest.empty())
return -1;
dlDest += L"\\";
dlDest += ::PathFindFileName(gupDlInfo.getDownloadLocation().c_str());

Expand Down
2 changes: 1 addition & 1 deletion src/xmlTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void GupExtraOptions::writeProxyInfo(const wchar_t* fn, const wchar_t* proxySrv,
newProxySettings.SaveFile();
}

std::wstring GupNativeLang::getMessageString(std::string msgID)
std::wstring GupNativeLang::getMessageString(const std::string& msgID) const
{
if (!_nativeLangRoot)
return L"";
Expand Down
2 changes: 1 addition & 1 deletion src/xmlTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class GupNativeLang : public XMLTool {
_xmlDoc.LoadFile(xmlFileName);
_nativeLangRoot = _xmlDoc.FirstChild("GUP_NativeLangue");
};
std::wstring getMessageString(std::string msgID);
std::wstring getMessageString(const std::string& msgID) const;

protected:
TiXmlNode *_nativeLangRoot;
Expand Down

0 comments on commit 3d5cb3f

Please sign in to comment.