PowerToys/src/runner/update_utils.cpp
Andrey Nekrasov c1b1fe6371
Fix compilation errors for VS 16.8.1 (#8024)
* fix compilation issues for VS 16.8.* release

* - VS 16.8 changed its template instantiation model, so now the compiler checks the syntax used in template even when it's not instantiated. we were relying on that behavior to declare the list of required localized strings from a shared header => replacing that template with a macro.

* - fix 3 C++ `const auto&` bugs the new compiler was able to uncover

* - enabled /Zc:twoPhase- for bootstrapper and PowerToysSetupCustomActions to workaround 10.0.17134.0 headers' issue.

- PowerToysSetupCustomActions.vcxproj now uses 10.0.17134.0 instead of "latest" SDK

* - PowerToysSetupCustomActions project leverages `goto` operator's power extensively, and the new compiler was able to uncover new uninitialized variables behavior, restructured declarations and changed `NULL`s to `nullptr`s.

* - Fix unit FZ unit test compiler error

* - format FZS unit test source file

* fixup fz tests

* use /Zc:twoPhase- for common-md as well
2020-11-13 15:57:01 +03:00

113 lines
2.9 KiB
C++

#include "pch.h"
#include <common/common.h>
#include "Generated Files/resource.h"
#include "action_runner_utils.h"
#include "update_state.h"
#include "update_utils.h"
#include <common/timeutil.h>
#include <common/updating/updating.h>
#include <runner/general_settings.h>
extern "C" IMAGE_DOS_HEADER __ImageBase;
auto Strings = create_notifications_strings();
bool start_msi_uninstallation_sequence()
{
const auto package_path = updating::get_msi_package_path();
if (package_path.empty())
{
// No MSI version detected
return true;
}
if (!updating::offer_msi_uninstallation(Strings))
{
// User declined to uninstall or opted for "Don't show again"
return false;
}
auto sei = launch_action_runner(L"-uninstall_msi");
WaitForSingleObject(sei.hProcess, INFINITE);
DWORD exit_code = 0;
GetExitCodeProcess(sei.hProcess, &exit_code);
CloseHandle(sei.hProcess);
return exit_code == 0;
}
void github_update_worker()
{
const int64_t update_check_period_minutes = 60 * 24;
for (;;)
{
auto state = UpdateState::read();
int64_t sleep_minutes_till_next_update = 0;
if (state.github_update_last_checked_date.has_value())
{
int64_t last_checked_minutes_ago = timeutil::diff::in_minutes(timeutil::now(), *state.github_update_last_checked_date);
if (last_checked_minutes_ago < 0)
{
last_checked_minutes_ago = update_check_period_minutes;
}
sleep_minutes_till_next_update = max(0, update_check_period_minutes - last_checked_minutes_ago);
}
std::this_thread::sleep_for(std::chrono::minutes(sleep_minutes_till_next_update));
const bool download_updates_automatically = get_general_settings().downloadUpdatesAutomatically;
try
{
updating::try_autoupdate(download_updates_automatically, Strings).get();
}
catch (...)
{
// Couldn't autoupdate
}
UpdateState::store([](UpdateState& state) {
state.github_update_last_checked_date.emplace(timeutil::now());
});
}
}
std::wstring check_for_updates()
{
try
{
return updating::check_new_version_available(Strings).get();
}
catch (...)
{
// Couldn't autoupdate
return std::wstring();
}
}
bool launch_pending_update()
{
try
{
auto update_state = UpdateState::read();
if (update_state.pending_update)
{
UpdateState::store([](UpdateState& state) {
state.pending_update = false;
state.pending_installer_filename = {};
});
std::wstring args{ UPDATE_NOW_LAUNCH_STAGE1_START_PT_CMDARG };
args += L' ';
args += update_state.pending_installer_filename;
launch_action_runner(args.c_str());
return true;
}
}
catch (...)
{
}
return false;
}