From 355c8ae57d582974f4f6a4ba42f635f2253b84a7 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Fri, 6 Nov 2026 14:24:04 +0100 Subject: Add a wall clock helper so that statedumps can be written and re-read --- src/opentracker.c | 10 +++++++--- src/ot_fullscrape.c | 2 +- src/trackerlogic.c | 2 +- src/trackerlogic.h | 5 +++++ 4 files changed, 14 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/opentracker.c b/src/opentracker.c index e467ce6..123f20a 100644 --- a/src/opentracker.c +++ b/src/opentracker.c @@ -300,9 +300,13 @@ void load_state(const char *const state_filename) { FILE *state_filehandle; char inbuf[512]; ot_hash infohash; - uint64_t age, downcount; + uint64_t touched, downcount; int consumed; + /* The file records the minute a torrent was last touched, by the clock that + survives a restart */ + uint64_t now = (uint64_t)time_wall_minutes(); + state_filehandle = fopen(state_filename, "r"); if (state_filehandle == NULL) { @@ -318,12 +322,12 @@ void load_state(const char *const state_filename) { continue; i = 2 * sizeof(ot_hash); - if (inbuf[i++] != ':' || !(consumed = scan_uint(inbuf + i, &age, OT_TORRENT_TIMEOUT))) + if (inbuf[i++] != ':' || !(consumed = scan_uint(inbuf + i, &touched, now))) continue; i += consumed; if (inbuf[i++] != ':' || !(consumed = scan_uint(inbuf + i, &downcount, SIZE_MAX))) continue; - add_torrent_from_saved_state(infohash, (ot_time)age, (size_t)downcount); + add_torrent_from_saved_state(infohash, (ot_time)(now - touched), (size_t)downcount); } fclose(state_filehandle); diff --git a/src/ot_fullscrape.c b/src/ot_fullscrape.c index 85be65d..ef39a17 100644 --- a/src/ot_fullscrape.c +++ b/src/ot_fullscrape.c @@ -131,7 +131,7 @@ static void fullscrape_write_one( ot_tasktype mode, char **cursor, ot_torrent *t case TASK_FULLSCRAPE_TRACKERSTATE: hex_from_hash(r, *hash); r += 2 * sizeof(ot_hash); - fmt_append(&r, r_end, ":%zd:%zd\n", (ssize_t)time_since_minutes(torrent->peer_list6->base), down_count); + fmt_append(&r, r_end, ":%zd:%zd\n", (ssize_t)(time_wall_minutes() - time_since_minutes(torrent->peer_list6->base)), down_count); break; } *cursor = r; diff --git a/src/trackerlogic.c b/src/trackerlogic.c index 9fd6114..2313379 100644 --- a/src/trackerlogic.c +++ b/src/trackerlogic.c @@ -45,7 +45,7 @@ void add_torrent_from_saved_state(ot_hash const hash, ot_time age, size_t down_c ot_torrent *torrent; ot_vector *torrents_list; - if (age < 0 || age > OT_TORRENT_TIMEOUT) + if (age > OT_TORRENT_TIMEOUT) return; torrents_list = mutex_bucket_lock_by_hash(hash); diff --git a/src/trackerlogic.h b/src/trackerlogic.h index d03d3b1..241b6a7 100644 --- a/src/trackerlogic.h +++ b/src/trackerlogic.h @@ -114,6 +114,11 @@ static inline ot_time time_since_minutes(ot_time then) { /* Has a deadline the tracker set on itself gone by? */ static inline int time_is_past(ot_time deadline) { return time_now_seconds() > deadline; } + +/* The wall clock, which the state file uses and nothing else does: it has to + survive a restart, and the clock above counts from boot. */ +static inline ot_time time_wall_minutes(void) { return time(NULL) / 60; } + /* Cleared from the interrupt handler and read by every worker */ extern _Atomic int g_opentracker_running; -- cgit v1.3