mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-01-18 22:43:31 +08:00
[Runner] Cleanup updates directory at startup (#19875)
* cleanup updates and logs at startup * perform cleanup in separate thread
This commit is contained in:
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)
|
||||
{
|
||||
return false;
|
||||
|
@ -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<std::time_t> githubUpdateLastCheckedDate;
|
||||
|
@ -45,6 +45,7 @@
|
||||
#include <common/utils/winapi_error.h>
|
||||
#include <common/utils/window.h>
|
||||
#include <common/version/version.h>
|
||||
#include <common/utils/string_utils.h>
|
||||
#include <gdiplus.h>
|
||||
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user