From e29772c3d1cda713c9d688ef0146133eace43c19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Kunz?= Date: Tue, 22 Oct 2024 10:48:12 +0200 Subject: [PATCH] KDESKTOP-1130 - Run installer using WinApi function --- src/libcommonserver/utility/utility.cpp | 31 +++++++++++++++++++++++++ src/libcommonserver/utility/utility.h | 1 + src/server/updater/windowsupdater.cpp | 10 +++----- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/libcommonserver/utility/utility.cpp b/src/libcommonserver/utility/utility.cpp index d7ef61fb9..0e01c5fd3 100644 --- a/src/libcommonserver/utility/utility.cpp +++ b/src/libcommonserver/utility/utility.cpp @@ -772,6 +772,37 @@ bool Utility::longPath(const SyncPath &shortPathIn, SyncPath &longPathOut, bool return true; } + +bool Utility::runDetachedProcess(std::wstring cmd) { + PROCESS_INFORMATION pinfo; + STARTUPINFOW startupInfo = {sizeof(STARTUPINFO), + 0, + 0, + 0, + (ulong) CW_USEDEFAULT, + (ulong) CW_USEDEFAULT, + (ulong) CW_USEDEFAULT, + (ulong) CW_USEDEFAULT, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0}; + bool success = success = CreateProcess(0, cmd.data(), 0, 0, FALSE, CREATE_UNICODE_ENVIRONMENT | CREATE_NEW_CONSOLE, 0, 0, + &startupInfo, &pinfo); + + if (success) { + CloseHandle(pinfo.hThread); + CloseHandle(pinfo.hProcess); + } + return success; +} + #endif diff --git a/src/libcommonserver/utility/utility.h b/src/libcommonserver/utility/utility.h index 13921c6b8..d0332616c 100644 --- a/src/libcommonserver/utility/utility.h +++ b/src/libcommonserver/utility/utility.h @@ -161,6 +161,7 @@ struct COMMONSERVER_EXPORT Utility { #ifdef _WIN32 static bool fileExists(DWORD dwordError) noexcept; static bool longPath(const SyncPath &shortPathIn, SyncPath &longPathOut, bool ¬Found); + static bool runDetachedProcess(std::wstring cmd); #endif static bool checkIfDirEntryIsManaged(std::filesystem::recursive_directory_iterator &dirIt, bool &isManaged, bool &isLink, IoError &ioError); diff --git a/src/server/updater/windowsupdater.cpp b/src/server/updater/windowsupdater.cpp index a9a07ef53..911a6ef62 100644 --- a/src/server/updater/windowsupdater.cpp +++ b/src/server/updater/windowsupdater.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include #include "windowsupdater.h" #include "log/log.h" #include "jobs/network/directdownloadjob.h" @@ -48,14 +49,9 @@ void WindowsUpdater::startInstaller() { setState(UpdateState::DownloadError); return; } - LOGW_INFO(Log::instance()->getLogger(), L"Starting updater " << Utility::formatSyncPath(filepath)); - - auto *updaterThread = new std::jthread([filepath] { - const auto cmd = filepath.string() + " /S /launch"; - std::system(cmd.c_str()); - }); - updaterThread->detach(); + auto cmd = filepath.wstring() + L" /S /launch"; + Utility::runDetachedProcess(cmd); } void WindowsUpdater::downloadUpdate() noexcept {