Merge pull request #11281 from AdmiralCurtiss/updater-admin-check

Updater: Check for write permissions in directory of Updater.exe.
This commit is contained in:
Admiral H. Curtiss 2022-11-22 23:50:13 +01:00 committed by GitHub
commit 5e442f6ffa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 10 deletions

View File

@ -31,12 +31,22 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
// Test for write permissions
bool need_admin = false;
FILE* test_fh = fopen("Updater.log", "w");
auto path = GetModuleName(hInstance);
if (!path)
{
UI::Error("Failed to get updater filename.");
return 1;
}
if (test_fh == nullptr)
const auto test_fh = ::CreateFileW(
(std::filesystem::path(*path).parent_path() / "directory_writable_check.tmp").c_str(),
GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS,
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, nullptr);
if (test_fh == INVALID_HANDLE_VALUE)
need_admin = true;
else
fclose(test_fh);
CloseHandle(test_fh);
if (need_admin)
{
@ -47,13 +57,6 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
return 1;
}
auto path = GetModuleName(hInstance);
if (!path)
{
MessageBox(nullptr, L"Failed to get updater filename.", L"Error", MB_ICONERROR);
return 1;
}
// Relaunch the updater as administrator
ShellExecuteW(nullptr, L"runas", path->c_str(), pCmdLine, NULL, SW_SHOW);
return 0;