diff --git a/src/Update/PowerToys.Update.cpp b/src/Update/PowerToys.Update.cpp index f55bb0c161..641e8a9929 100644 --- a/src/Update/PowerToys.Update.cpp +++ b/src/Update/PowerToys.Update.cpp @@ -49,26 +49,30 @@ std::optional CopySelfToTempDir() return std::move(dst_path); } -std::optional ObtainInstallerPath() +std::optional ObtainInstaller(bool& isUpToDate) { using namespace updating; + isUpToDate = false; + auto state = UpdateState::read(); + + const auto new_version_info = get_github_version_info_async().get(); + if (std::holds_alternative(*new_version_info)) + { + isUpToDate = true; + Logger::error("Invoked with -update_now argument, but no update was available"); + return std::nullopt; + } + if (state.state == UpdateState::readyToDownload || state.state == UpdateState::errorDownloading) { - const auto new_version_info = get_github_version_info_async().get(); if (!new_version_info) { Logger::error(L"Couldn't obtain github version info: {}", new_version_info.error()); return std::nullopt; } - if (!std::holds_alternative(*new_version_info)) - { - Logger::error("Invoked with -update_now argument, but no update was available"); - return std::nullopt; - } - auto downloaded_installer = download_new_version(std::get(*new_version_info)).get(); if (!downloaded_installer) { @@ -90,21 +94,18 @@ std::optional ObtainInstallerPath() return std::nullopt; } } - else + else if (state.state == UpdateState::upToDate) { - Logger::error("Invoked with -update_now argument, but update state was invalid"); + isUpToDate = true; return std::nullopt; } + + Logger::error("Invoked with -update_now argument, but update state was invalid"); + return std::nullopt; } -bool InstallNewVersionStage1() +bool InstallNewVersionStage1(fs::path installer) { - const auto installer = ObtainInstallerPath(); - if (!installer) - { - return false; - } - if (auto copy_in_temp = CopySelfToTempDir()) { // Detect if PT was running @@ -117,7 +118,7 @@ bool InstallNewVersionStage1() std::wstring arguments{ UPDATE_NOW_LAUNCH_STAGE2 }; arguments += L" \""; - arguments += installer->c_str(); + arguments += installer.c_str(); arguments += L"\" \""; arguments += get_module_folderpath(); arguments += L"\" "; @@ -235,13 +236,16 @@ int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) if (action == UPDATE_NOW_LAUNCH_STAGE1) { - const bool failed = !InstallNewVersionStage1(); + bool isUpToDate = false; + auto installerPath = ObtainInstaller(isUpToDate); + bool failed = !installerPath.has_value(); + failed = failed || !InstallNewVersionStage1(std::move(*installerPath)); if (failed) { UpdateState::store([&](UpdateState& state) { - state.downloadedInstallerFilename = {}; + state = {}; state.githubUpdateLastCheckedDate.emplace(timeutil::now()); - state.state = UpdateState::errorDownloading; + state.state = isUpToDate ? UpdateState::upToDate : UpdateState::errorDownloading; }); } return failed; @@ -253,7 +257,7 @@ int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) if (failed) { UpdateState::store([&](UpdateState& state) { - state.downloadedInstallerFilename = {}; + state = {}; state.githubUpdateLastCheckedDate.emplace(timeutil::now()); state.state = UpdateState::errorDownloading; }); diff --git a/src/common/updating/updateState.cpp b/src/common/updating/updateState.cpp index 3217321dca..e9bea79447 100644 --- a/src/common/updating/updateState.cpp +++ b/src/common/updating/updateState.cpp @@ -69,7 +69,10 @@ UpdateState UpdateState::read() { std::error_code _; fs::remove(filename, _); - return UpdateState{}; + UpdateState new_state; + json::to_file(filename, serialize(new_state)); + + return new_state; } }