This commit is contained in:
Davide Giacometti 2023-08-14 10:55:12 +02:00 коммит произвёл GitHub
Родитель 00dc4981d1
Коммит bcb0942b73
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 58 добавлений и 52 удалений

Просмотреть файл

@ -73,6 +73,9 @@ std::optional<fs::path> ObtainInstaller(bool& isUpToDate)
return std::nullopt;
}
// Cleanup old updates before downloading the latest
updating::cleanup_updates();
auto downloaded_installer = download_new_version(std::get<new_version_download_info>(*new_version_info)).get();
if (!downloaded_installer)
{

Просмотреть файл

@ -1,6 +1,7 @@
#include "pch.h"
#include <common/utils/HttpClient.h>
#include <common/utils/string_utils.h>
#include <common/version/version.h>
#include <common/version/helper.h>
@ -185,4 +186,48 @@ namespace updating
co_return download_success ? installer_download_path : std::nullopt;
}
void cleanup_updates()
{
auto update_dir = updating::get_pending_updates_path();
if (std::filesystem::exists(update_dir))
{
// Msi and exe files
for (const auto& entry : std::filesystem::directory_iterator(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"))
{
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::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());
}
}
}
}
}
}

Просмотреть файл

@ -28,6 +28,7 @@ namespace updating
std::future<std::optional<std::filesystem::path>> download_new_version(const new_version_download_info& new_version);
std::filesystem::path get_pending_updates_path();
std::future<nonstd::expected<github_version_info, std::wstring>> get_github_version_info_async(const bool prerelease = false);
void cleanup_updates();
// non-localized
constexpr inline std::wstring_view INSTALLER_FILENAME_PATTERN = L"powertoyssetup";

Просмотреть файл

@ -159,6 +159,10 @@ void ProcessNewVersionInfo(const github_version_info& version_info,
if (download_update)
{
Logger::trace(L"Downloading installer for a new version");
// Cleanup old updates before downloading the latest
updating::cleanup_updates();
if (download_new_version(new_version_info).get())
{
state.state = UpdateState::readyToInstall;

Просмотреть файл

@ -296,57 +296,6 @@ toast_notification_handler_result toast_notification_handler(const std::wstring_
}
}
void cleanup_updates()
{
auto state = UpdateState::read();
if (state.state != UpdateState::upToDate)
{
return;
}
auto update_dir = updating::get_pending_updates_path();
if (std::filesystem::exists(update_dir))
{
// Msi and exe files
for (const auto& entry : std::filesystem::directory_iterator(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"))
{
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::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;
@ -455,7 +404,11 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR l
modules();
std::thread{ [] {
cleanup_updates();
auto state = UpdateState::read();
if (state.state == UpdateState::upToDate)
{
updating::cleanup_updates();
}
} }.detach();
auto general_settings = load_general_settings();