forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_state.cpp
More file actions
68 lines (58 loc) · 2.2 KB
/
update_state.cpp
File metadata and controls
68 lines (58 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "pch.h"
#include "update_state.h"
#include <common/json.h>
#include <common/timeutil.h>
#include <common/settings_helpers.h>
namespace
{
const wchar_t PERSISTENT_STATE_FILENAME[] = L"\\update_state.json";
const wchar_t UPDATE_STATE_MUTEX[] = L"PTUpdateStateMutex";
}
UpdateState deserialize(const json::JsonObject& json)
{
UpdateState result;
result.github_update_last_checked_date = timeutil::from_string(json.GetNamedString(L"github_update_last_checked_date", L"invalid").c_str());
result.pending_update = json.GetNamedBoolean(L"pending_update", false);
result.pending_installer_filename = json.GetNamedString(L"pending_installer_filename", L"");
return result;
}
json::JsonObject serialize(const UpdateState& state)
{
json::JsonObject json;
if (state.github_update_last_checked_date.has_value())
{
json.SetNamedValue(L"github_update_last_checked_date", json::value(timeutil::to_string(*state.github_update_last_checked_date)));
}
json.SetNamedValue(L"pending_update", json::value(state.pending_update));
json.SetNamedValue(L"pending_installer_filename", json::value(state.pending_installer_filename));
return json;
}
UpdateState UpdateState::read()
{
const auto file_name = PTSettingsHelper::get_root_save_folder_location() + PERSISTENT_STATE_FILENAME;
std::optional<json::JsonObject> json;
{
wil::unique_mutex_nothrow mutex{ CreateMutexW(nullptr, FALSE, UPDATE_STATE_MUTEX) };
auto lock = mutex.acquire();
json = json::from_file(file_name);
}
return json ? deserialize(*json) : UpdateState{};
}
void UpdateState::store(std::function<void(UpdateState&)> state_modifier)
{
const auto file_name = PTSettingsHelper::get_root_save_folder_location() + PERSISTENT_STATE_FILENAME;
std::optional<json::JsonObject> json;
{
wil::unique_mutex_nothrow mutex{ CreateMutexW(nullptr, FALSE, UPDATE_STATE_MUTEX) };
auto lock = mutex.acquire();
json = json::from_file(file_name);
UpdateState state;
if (json)
{
state = deserialize(*json);
}
state_modifier(state);
json.emplace(serialize(state));
json::to_file(file_name, *json);
}
}