-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcleanup.cpp
More file actions
168 lines (152 loc) · 4.24 KB
/
cleanup.cpp
File metadata and controls
168 lines (152 loc) · 4.24 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
/*
hourly tlog cleanup worker
*/
#include "cleanup.h"
#include "keydb.h"
#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 <tdb.h>
namespace {
struct PassCtx {
const char *base_dir;
time_t now;
};
static bool ends_with_tlog(const char *name)
{
size_t n = strlen(name);
return n > 5 && strcmp(name + n - 5, ".tlog") == 0;
}
static void cleanup_for_port2(uint32_t port2, double retention_days,
const char *base_dir, time_t now)
{
if (retention_days <= 0.0) {
return; // 0 = keep forever
}
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) {
// missing dir is fine: nothing has been logged for this port yet
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 (ends_with_tlog(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("tlog 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("tlog cleanup: removed empty %s\n", date_dir);
}
}
}
closedir(d);
}
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.tlog_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 tlog_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 tlog_cleanup_loop(const char *base_dir)
{
// Run an immediate pass on startup so a fresh restart still cleans up.
tlog_cleanup_once(base_dir);
double interval = cleanup_interval_seconds();
while (true) {
sleep_seconds(interval);
tlog_cleanup_once(base_dir);
}
}