Compare commits

...

1 Commits

Author SHA1 Message Date
Boliang Zhang (from Dev Box)
3e6fe88be6 Clean up old update executables reliably
- Make startup cleanup unconditional (remove upToDate state gate)
- Add cleanup after successful install in InstallNewVersionStage2
- Add exception safety inside cleanup_updates() to protect all callers

Fixes #46816
2026-04-18 00:11:42 +08:00
3 changed files with 45 additions and 32 deletions

View File

@@ -181,6 +181,8 @@ bool InstallNewVersionStage2(std::wstring installer_path)
state.state = UpdateState::upToDate;
});
updating::cleanup_updates();
return true;
}

View File

@@ -196,46 +196,57 @@ namespace updating
void cleanup_updates()
{
auto update_dir = updating::get_pending_updates_path();
if (std::filesystem::exists(update_dir))
try
{
// Msi and exe files
for (const auto& entry : std::filesystem::directory_iterator(update_dir))
auto update_dir = updating::get_pending_updates_path();
if (std::filesystem::exists(update_dir))
{
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"))
// Msi and exe files
for (const auto& entry : std::filesystem::directory_iterator(update_dir))
{
std::error_code err;
std::filesystem::remove(entry, err);
if (err.value())
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"))
{
Logger::warn("Failed to delete installer file {}. {}", entry.path().string(), err.message());
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");
if (std::filesystem::exists(rootPath))
{
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::wstring::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());
}
}
}
}
}
// Log files
auto rootPath{ PTSettingsHelper::get_root_save_folder_location() };
auto currentVersion = left_trim<wchar_t>(get_product_version(), L"v");
if (std::filesystem::exists(rootPath))
catch (const std::exception& e)
{
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());
}
}
}
Logger::error("Failed to clean up old update files: {}", e.what());
}
catch (...)
{
Logger::error("Failed to clean up old update files: unknown exception");
}
}
}

View File

@@ -552,7 +552,7 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR l
std::thread{ [] {
auto state = UpdateState::read();
if (state.state == UpdateState::upToDate)
if (state.state != UpdateState::readyToInstall || state.downloadedInstallerFilename.empty())
{
updating::cleanup_updates();
}