From 5c431b5ac55eab2f972bc4e701c4902d7ae31fa8 Mon Sep 17 00:00:00 2001 From: Davide Giacometti Date: Tue, 23 Aug 2022 22:32:45 +0200 Subject: [PATCH] [Runner] Cleanup updates directory at startup (#19875) * cleanup updates and logs at startup * perform cleanup in separate thread --- src/Update/PowerToys.Update.cpp | 23 -------------- src/common/updating/updateState.h | 8 ++--- src/runner/main.cpp | 51 +++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 29 deletions(-) diff --git a/src/Update/PowerToys.Update.cpp b/src/Update/PowerToys.Update.cpp index 641e8a9929..31e4eb8143 100644 --- a/src/Update/PowerToys.Update.cpp +++ b/src/Update/PowerToys.Update.cpp @@ -170,29 +170,6 @@ bool InstallNewVersionStage2(std::wstring installer_path, std::wstring_view inst } } - for (const auto& entry : fs::directory_iterator(updating::get_pending_updates_path())) - { - auto entryPath = entry.path().wstring(); - std::transform(entryPath.begin(), entryPath.end(), entryPath.begin(), ::towlower); - - // Delete only .msi and .exe - if (entryPath.ends_with(L".msi") || entryPath.ends_with(L".exe")) - { - // Skipping current installer in case of failed update - if (installer_path.find(entryPath) != std::string::npos && !success) - { - continue; - } - - std::error_code err; - fs::remove(entry, err); - if (err.value()) - { - Logger::warn("Failed to delete file {}. {}", entry.path().string(), err.message()); - } - } - } - if (!success) { return false; diff --git a/src/common/updating/updateState.h b/src/common/updating/updateState.h index 09cb0c2f41..35ca699a47 100644 --- a/src/common/updating/updateState.h +++ b/src/common/updating/updateState.h @@ -9,10 +9,10 @@ struct UpdateState { enum State { - upToDate = 0, - errorDownloading = 1, - readyToDownload = 2, - readyToInstall = 3 + upToDate = 0, + errorDownloading = 1, + readyToDownload = 2, + readyToInstall = 3 } state = upToDate; std::wstring releasePageUrl; std::optional githubUpdateLastCheckedDate; diff --git a/src/runner/main.cpp b/src/runner/main.cpp index 9ddf2269aa..ae72211669 100644 --- a/src/runner/main.cpp +++ b/src/runner/main.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include namespace @@ -291,6 +292,50 @@ toast_notification_handler_result toast_notification_handler(const std::wstring_ } } +void cleanup_updates() +{ + auto state = UpdateState::read(); + if (state.state != UpdateState::upToDate) + { + return; + } + + // Msi and exe files + for (const auto& entry : std::filesystem::directory_iterator(updating::get_pending_updates_path())) + { + auto entryPath = entry.path().wstring(); + std::transform(entryPath.begin(), entryPath.end(), entryPath.begin(), ::towlower); + + if (entryPath.ends_with(L".msi") || entryPath.ends_with(L".exe")) + { + std::error_code err; + std::filesystem::remove(entry, err); + if (err.value()) + { + Logger::warn("Failed to delete installer file {}. {}", entry.path().string(), err.message()); + } + } + } + + // Log files + auto rootPath{ PTSettingsHelper::get_root_save_folder_location() }; + auto currentVersion = left_trim(get_product_version(), L"v"); + for (const auto& entry : std::filesystem::directory_iterator(rootPath)) + { + auto entryPath = entry.path().wstring(); + std::transform(entryPath.begin(), entryPath.end(), entryPath.begin(), ::towlower); + if (entry.is_regular_file() && entryPath.ends_with(L".log") && entryPath.find(currentVersion) == std::string::npos) + { + std::error_code err; + std::filesystem::remove(entry, err); + if (err.value()) + { + Logger::warn("Failed to delete log file {}. {}", entry.path().string(), err.message()); + } + } + } +} + int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Gdiplus::GdiplusStartupInput gpStartupInput; @@ -395,6 +440,10 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine // then modules to guarantee the reverse destruction order. modules(); + std::thread{ [] { + cleanup_updates(); + } }.detach(); + auto general_settings = load_general_settings(); // Apply the general settings but don't save it as the modules() variable has not been loaded yet @@ -405,14 +454,12 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine const bool run_elevated_setting = general_settings.GetNamedBoolean(L"run_elevated", false); if (elevated && with_dont_elevate_arg && !run_elevated_setting) - { Logger::info("Scheduling restart as non elevated"); schedule_restart_as_non_elevated(); result = 0; } else if (elevated || !run_elevated_setting || with_dont_elevate_arg) - { result = runner(elevated, open_settings, settings_window, openOobe, openScoobe);