Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11281 from AdmiralCurtiss/updater-admin-check
Updater: Check for write permissions in directory of Updater.exe.
  • Loading branch information
AdmiralCurtiss committed Nov 22, 2022
2 parents 19b6251 + 5ebb894 commit 5e442f6
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions Source/Core/WinUpdater/Main.cpp
Expand Up @@ -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;
}

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 == nullptr)
if (test_fh == INVALID_HANDLE_VALUE)
need_admin = true;
else
fclose(test_fh);
CloseHandle(test_fh);

if (need_admin)
{
Expand All @@ -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;
Expand Down

0 comments on commit 5e442f6

Please sign in to comment.