mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 12:18:50 +02:00
[Runner] Cleanup updates directory at startup (#19875)
* cleanup updates and logs at startup * perform cleanup in separate thread
This commit is contained in:
committed by
GitHub
parent
c85305695e
commit
5c431b5ac5
@@ -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)
|
if (!success)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ struct UpdateState
|
|||||||
{
|
{
|
||||||
enum State
|
enum State
|
||||||
{
|
{
|
||||||
upToDate = 0,
|
upToDate = 0,
|
||||||
errorDownloading = 1,
|
errorDownloading = 1,
|
||||||
readyToDownload = 2,
|
readyToDownload = 2,
|
||||||
readyToInstall = 3
|
readyToInstall = 3
|
||||||
} state = upToDate;
|
} state = upToDate;
|
||||||
std::wstring releasePageUrl;
|
std::wstring releasePageUrl;
|
||||||
std::optional<std::time_t> githubUpdateLastCheckedDate;
|
std::optional<std::time_t> githubUpdateLastCheckedDate;
|
||||||
|
|||||||
@@ -45,6 +45,7 @@
|
|||||||
#include <common/utils/winapi_error.h>
|
#include <common/utils/winapi_error.h>
|
||||||
#include <common/utils/window.h>
|
#include <common/utils/window.h>
|
||||||
#include <common/version/version.h>
|
#include <common/version/version.h>
|
||||||
|
#include <common/utils/string_utils.h>
|
||||||
#include <gdiplus.h>
|
#include <gdiplus.h>
|
||||||
|
|
||||||
namespace
|
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<wchar_t>(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)
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
||||||
{
|
{
|
||||||
Gdiplus::GdiplusStartupInput gpStartupInput;
|
Gdiplus::GdiplusStartupInput gpStartupInput;
|
||||||
@@ -395,6 +440,10 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
|
|||||||
// then modules to guarantee the reverse destruction order.
|
// then modules to guarantee the reverse destruction order.
|
||||||
modules();
|
modules();
|
||||||
|
|
||||||
|
std::thread{ [] {
|
||||||
|
cleanup_updates();
|
||||||
|
} }.detach();
|
||||||
|
|
||||||
auto general_settings = load_general_settings();
|
auto general_settings = load_general_settings();
|
||||||
|
|
||||||
// Apply the general settings but don't save it as the modules() variable has not been loaded yet
|
// 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);
|
const bool run_elevated_setting = general_settings.GetNamedBoolean(L"run_elevated", false);
|
||||||
|
|
||||||
if (elevated && with_dont_elevate_arg && !run_elevated_setting)
|
if (elevated && with_dont_elevate_arg && !run_elevated_setting)
|
||||||
|
|
||||||
{
|
{
|
||||||
Logger::info("Scheduling restart as non elevated");
|
Logger::info("Scheduling restart as non elevated");
|
||||||
schedule_restart_as_non_elevated();
|
schedule_restart_as_non_elevated();
|
||||||
result = 0;
|
result = 0;
|
||||||
}
|
}
|
||||||
else if (elevated || !run_elevated_setting || with_dont_elevate_arg)
|
else if (elevated || !run_elevated_setting || with_dont_elevate_arg)
|
||||||
|
|
||||||
{
|
{
|
||||||
result = runner(elevated, open_settings, settings_window, openOobe, openScoobe);
|
result = runner(elevated, open_settings, settings_window, openOobe, openScoobe);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user