From f0fb8c22b0383d65570cbb13a270cf33137dc530 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Wed, 16 Nov 2022 02:41:25 +0100 Subject: [PATCH 1/2] Updater: Check for write permissions in directory of Updater.exe. --- Source/Core/WinUpdater/Main.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Source/Core/WinUpdater/Main.cpp b/Source/Core/WinUpdater/Main.cpp index caa9f40958..85b29e8226 100644 --- a/Source/Core/WinUpdater/Main.cpp +++ b/Source/Core/WinUpdater/Main.cpp @@ -31,7 +31,15 @@ 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; + } + + FILE* test_fh = + _wfopen((std::filesystem::path(*path).parent_path() / "Updater.log").c_str(), L"w"); if (test_fh == nullptr) need_admin = true; @@ -47,13 +55,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; From 5ebb894685a97570c3d12eb00956c32d56557629 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Tue, 22 Nov 2022 01:36:04 +0100 Subject: [PATCH 2/2] Updater: Delete the file we check for write rights with on Windows. --- Source/Core/WinUpdater/Main.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Source/Core/WinUpdater/Main.cpp b/Source/Core/WinUpdater/Main.cpp index 85b29e8226..6b7fd801ef 100644 --- a/Source/Core/WinUpdater/Main.cpp +++ b/Source/Core/WinUpdater/Main.cpp @@ -38,13 +38,15 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine return 1; } - FILE* test_fh = - _wfopen((std::filesystem::path(*path).parent_path() / "Updater.log").c_str(), L"w"); + 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) {