-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcleanup.cpp
More file actions
280 lines (257 loc) · 8.11 KB
/
cleanup.cpp
File metadata and controls
280 lines (257 loc) · 8.11 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
hourly session-log cleanup worker (covers .tlog and .bin)
*/
#include "cleanup.h"
#include "keydb.h"
#include <algorithm>
#include <dirent.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <vector>
#include <string>
#include <tdb.h>
namespace {
// Per-port-pair on-disk quota: matches BinlogWriter::MAX_PER_PORT2_BYTES
// in binlog.h. Kept in sync there. Total .tlog + .bin under logs/<port2>/
// across all date dirs may not exceed this; oldest files are deleted
// first.
constexpr off_t MAX_PER_PORT2_BYTES = off_t(1024) * 1024 * 1024; // 1 GiB
struct PassCtx {
const char *base_dir;
time_t now;
};
/*
Predicate for "this is a session file we should age out under
log_retention_days". Covers both .tlog (raw MAVLink frames) and
.bin (ArduPilot dataflash logs) so the retention rule is uniform —
per spec, both file types share the entry's retention setting.
*/
static bool is_session_file(const char *name)
{
size_t n = strlen(name);
return (n > 5 && strcmp(name + n - 5, ".tlog") == 0) ||
(n > 4 && strcmp(name + n - 4, ".bin") == 0);
}
/*
Enforce the per-port2 disk quota by deleting oldest session files
until total bytes <= MAX_PER_PORT2_BYTES. Runs after the retention
pass so the operator's per-entry retention dictates *when* a file
can go; this dictates *whether one must go anyway* because we're
about to overflow. Walks every date dir under logs/<port2>/, sorts
files by mtime ascending, deletes from the head.
*/
static void enforce_port2_quota(uint32_t port2, const char *base_dir)
{
char port_dir[768];
snprintf(port_dir, sizeof(port_dir), "%s/%u", base_dir, port2);
DIR *d = opendir(port_dir);
if (d == nullptr) {
return;
}
struct Item {
std::string path;
off_t size;
time_t mtime;
std::string date_dir;
};
std::vector<Item> items;
off_t total = 0;
struct dirent *ent;
while ((ent = readdir(d)) != nullptr) {
if (ent->d_name[0] == '.') {
continue;
}
char date_dir[1024];
snprintf(date_dir, sizeof(date_dir), "%s/%s", port_dir, ent->d_name);
struct stat st;
if (stat(date_dir, &st) != 0 || !S_ISDIR(st.st_mode)) {
continue;
}
DIR *dd = opendir(date_dir);
if (dd == nullptr) {
continue;
}
struct dirent *fent;
while ((fent = readdir(dd)) != nullptr) {
if (fent->d_name[0] == '.' || !is_session_file(fent->d_name)) {
continue;
}
char fpath[1280];
snprintf(fpath, sizeof(fpath), "%s/%s", date_dir, fent->d_name);
struct stat fst;
if (stat(fpath, &fst) != 0) {
continue;
}
items.push_back({fpath, fst.st_size, fst.st_mtime, date_dir});
total += fst.st_size;
}
closedir(dd);
}
closedir(d);
if (total <= MAX_PER_PORT2_BYTES) {
return;
}
// Sort oldest-first and delete until under quota.
std::sort(items.begin(), items.end(),
[](const Item &a, const Item &b) { return a.mtime < b.mtime; });
for (const auto &it : items) {
if (total <= MAX_PER_PORT2_BYTES) {
break;
}
if (unlink(it.path.c_str()) == 0) {
::printf("log cleanup: removed %s for quota "
"(port2=%u total %lld > %lld)\n",
it.path.c_str(), unsigned(port2),
(long long)total, (long long)MAX_PER_PORT2_BYTES);
total -= it.size;
// Try rmdir on the date dir in case this was its last file;
// harmless if it isn't.
(void)rmdir(it.date_dir.c_str());
}
}
}
static void retention_pass(uint32_t port2, double retention_days,
const char *base_dir, time_t now)
{
if (retention_days <= 0.0) {
return; // 0 = keep forever (per-entry); quota pass still runs below
}
double cutoff_age_s = retention_days * 86400.0;
char port_dir[768];
snprintf(port_dir, sizeof(port_dir), "%s/%u", base_dir, port2);
DIR *d = opendir(port_dir);
if (d == nullptr) {
return;
}
struct dirent *ent;
while ((ent = readdir(d)) != nullptr) {
if (ent->d_name[0] == '.') {
continue;
}
char date_dir[1024];
snprintf(date_dir, sizeof(date_dir), "%s/%s", port_dir, ent->d_name);
struct stat st;
if (stat(date_dir, &st) != 0 || !S_ISDIR(st.st_mode)) {
continue;
}
DIR *dd = opendir(date_dir);
if (dd == nullptr) {
continue;
}
unsigned remaining = 0;
struct dirent *fent;
while ((fent = readdir(dd)) != nullptr) {
if (fent->d_name[0] == '.') {
continue;
}
char fpath[1280];
snprintf(fpath, sizeof(fpath), "%s/%s", date_dir, fent->d_name);
if (is_session_file(fent->d_name)) {
struct stat fst;
if (stat(fpath, &fst) == 0) {
double age = double(now - fst.st_mtime);
if (age > cutoff_age_s) {
if (unlink(fpath) == 0) {
::printf("log cleanup: removed %s (age %.0fs > %.0fs)\n",
fpath, age, cutoff_age_s);
continue;
}
}
}
}
remaining++;
}
closedir(dd);
if (remaining == 0) {
if (rmdir(date_dir) == 0) {
::printf("log cleanup: removed empty %s\n", date_dir);
}
}
}
closedir(d);
}
static void cleanup_for_port2(uint32_t port2, double retention_days,
const char *base_dir, time_t now)
{
// Two passes per port2:
// 1. retention_pass: per-entry "delete files older than the
// configured retention". Skipped when retention=0 (keep
// forever).
// 2. enforce_port2_quota: hard 1 GiB cap. Runs even if
// retention=0, so even a "keep forever" entry can't fill
// the disk.
retention_pass(port2, retention_days, base_dir, now);
enforce_port2_quota(port2, base_dir);
}
static int traverse_cb(struct tdb_context *db, TDB_DATA key, TDB_DATA data, void *ptr)
{
(void)db;
auto *ctx = static_cast<PassCtx *>(ptr);
if (key.dsize != sizeof(int) || data.dsize < KEYENTRY_MIN_SIZE) {
return 0;
}
int port2 = 0;
memcpy(&port2, key.dptr, sizeof(int));
if (port2 <= 0) {
return 0;
}
struct KeyEntry k {};
size_t copy = data.dsize < sizeof(KeyEntry) ? data.dsize : sizeof(KeyEntry);
memcpy(&k, data.dptr, copy);
if (k.magic != KEY_MAGIC) {
return 0;
}
cleanup_for_port2(uint32_t(port2), double(k.log_retention_days),
ctx->base_dir, ctx->now);
return 0;
}
static double cleanup_interval_seconds()
{
const char *env = getenv("SUPPORTPROXY_CLEANUP_INTERVAL");
if (env != nullptr && *env != '\0') {
char *endp = nullptr;
double v = strtod(env, &endp);
if (endp != env && v > 0.0) {
return v;
}
}
return 3600.0;
}
static void sleep_seconds(double s)
{
if (s <= 0.0) {
return;
}
struct timespec ts;
ts.tv_sec = time_t(s);
ts.tv_nsec = long((s - double(ts.tv_sec)) * 1e9);
nanosleep(&ts, nullptr);
}
} // namespace
void log_cleanup_once(const char *base_dir)
{
auto *db = db_open();
if (db == nullptr) {
return;
}
PassCtx ctx { base_dir, time(nullptr) };
tdb_traverse(db, traverse_cb, &ctx);
db_close(db);
}
void log_cleanup_loop(const char *base_dir)
{
// Run an immediate pass on startup so a fresh restart still cleans up.
log_cleanup_once(base_dir);
double interval = cleanup_interval_seconds();
while (true) {
sleep_seconds(interval);
log_cleanup_once(base_dir);
}
}