Repository navigation
Expand file tree
/
Copy pathtimekeep_core.cpp
More file actions
164 lines (145 loc) · 4.96 KB
/
Copy pathtimekeep_core.cpp
File metadata and controls
164 lines (145 loc) · 4.96 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// SPDX-License-Identifier: Apache-2.0
#include "timekeep_core.h"
#include <dirent.h>
#include <fcntl.h>
#include <linux/rtc.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <cctype>
#include <ctime>
#include <fstream>
#include <sstream>
namespace timekeep {
namespace {
std::optional<std::string> ReadFile(const std::string& path) {
std::ifstream in(path);
if (!in) return std::nullopt;
std::stringstream ss;
ss << in.rdbuf();
return ss.str();
}
} // namespace
std::optional<int64_t> ParseEpoch(const std::string& text) {
size_t i = 0;
int64_t v = 0;
while (i < text.size() && std::isdigit(static_cast<unsigned char>(text[i]))) {
if (v > (INT64_MAX - 9) / 10) return std::nullopt; // overflow: not a time
v = v * 10 + (text[i] - '0');
++i;
}
if (i == 0) return std::nullopt;
for (; i < text.size(); ++i) {
if (!std::isspace(static_cast<unsigned char>(text[i]))) return std::nullopt;
}
return v;
}
int64_t BuildTime(const std::string& prop) {
std::optional<int64_t> b = ParseEpoch(prop);
if (!b || *b < kFloor) return kFloor;
return *b;
}
bool Plausible(std::optional<int64_t> v, int64_t build) {
return v && *v >= build && *v <= build + kSpan;
}
const char* SourceName(Source s) {
switch (s) {
case Source::kRtc:
return "rtc";
case Source::kVrtc:
return "vrtc";
case Source::kSaved:
return "saved";
}
return "?";
}
RestoreDecision DecideRestore(const RestoreInput& in) {
if (Plausible(in.now, in.build)) return {RestoreDecision::Kind::kAlreadySet};
if (Plausible(in.rtc, in.build)) return {RestoreDecision::Kind::kSet, *in.rtc, Source::kRtc};
if (Plausible(in.vrtc, in.build)) {
return {RestoreDecision::Kind::kSet, *in.vrtc, Source::kVrtc};
}
if (Plausible(in.saved, in.build)) {
return {RestoreDecision::Kind::kSet, *in.saved, Source::kSaved};
}
return {RestoreDecision::Kind::kNoRecord};
}
bool ShouldSave(int64_t now, int64_t build) {
return Plausible(now, build);
}
std::optional<int64_t> ReadVrtc(const std::string& class_dir) {
DIR* d = opendir(class_dir.c_str());
if (!d) return std::nullopt;
std::optional<int64_t> kept;
while (dirent* e = readdir(d)) {
const std::string name = e->d_name;
if (name.rfind("rtc", 0) != 0) continue;
const std::string dir = class_dir + "/" + name;
std::optional<std::string> n = ReadFile(dir + "/name");
if (!n) continue;
// name reads "<driver> <device>", e.g. "meson-vrtc c81000a8.rtc"
const std::string word = n->substr(0, n->find_first_of(" \n"));
if (word != "meson-vrtc") continue;
std::optional<std::string> s = ReadFile(dir + "/since_epoch");
if (s) kept = ParseEpoch(*s);
break;
}
closedir(d);
return kept;
}
std::optional<int64_t> ReadSaved(const std::string& file) {
std::optional<std::string> s = ReadFile(file);
if (!s) return std::nullopt;
return ParseEpoch(*s);
}
bool WriteSaved(const std::string& file, int64_t now) {
const std::string dir = file.substr(0, file.find_last_of('/') + 1);
const int dfd = open(dir.empty() ? "." : dir.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
if (dfd < 0) return false;
// One writer at a time: the lock is released when dfd closes.
if (TEMP_FAILURE_RETRY(flock(dfd, LOCK_EX)) != 0) {
close(dfd);
return false;
}
const std::string tmp = file + ".tmp";
const int fd = open(tmp.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0660);
if (fd < 0) {
close(dfd);
return false;
}
const std::string text = std::to_string(now) + "\n";
// The record exists for power cuts, so it must be on disk before it replaces the
// old one: data first (fsync), then the rename, then the directory entry.
bool ok = write(fd, text.data(), text.size()) == static_cast<ssize_t>(text.size());
ok = ok && fsync(fd) == 0;
close(fd);
ok = ok && rename(tmp.c_str(), file.c_str()) == 0;
ok = ok && fsync(dfd) == 0;
close(dfd);
return ok;
}
int64_t SaveDelayMs(int64_t now_ms, std::optional<int64_t> last_ms, int64_t min_gap_ms) {
if (!last_ms) return 0;
const int64_t since = now_ms - *last_ms;
if (since < 0 || since >= min_gap_ms) return 0;
return min_gap_ms - since;
}
std::optional<int64_t> ReadRtcDevice(const std::string& dev) {
const int fd = open(dev.c_str(), O_RDONLY | O_CLOEXEC);
if (fd < 0) return std::nullopt;
rtc_time rt = {};
const int r = ioctl(fd, RTC_RD_TIME, &rt);
close(fd);
if (r != 0) return std::nullopt;
tm t = {};
t.tm_sec = rt.tm_sec;
t.tm_min = rt.tm_min;
t.tm_hour = rt.tm_hour;
t.tm_mday = rt.tm_mday;
t.tm_mon = rt.tm_mon;
t.tm_year = rt.tm_year;
const time_t v = timegm(&t);
if (v == static_cast<time_t>(-1)) return std::nullopt;
return static_cast<int64_t>(v);
}
} // namespace timekeep