Skip to content

Commit 58ab3d8

Browse files
committed
Addressing issue with file_metatile_expire
1 parent 72ac94d commit 58ab3d8

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

src/store_file.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,24 +266,33 @@ static int file_metatile_expire(struct storage_backend * store, const char *xmlc
266266

267267
char name[PATH_MAX];
268268
struct stat s;
269-
static struct tm touchCalendar;
270269
struct utimbuf touchTime;
271270

271+
// Define time thresholds in seconds
272+
const time_t TWENTY_YEARS = 20LL * 365 * 24 * 60 * 60;
273+
const time_t TEN_YEARS = 10LL * 365 * 24 * 60 * 60;
274+
const time_t FALLBACK_TIME = 315558000; // Jan 1, 1980 (mod_tile's original fallback)
275+
272276
//TODO: deal with options
273277
xyz_to_meta(name, sizeof(name), store->storage_ctx, xmlconfig, x, y, z);
274278

275279
if (stat(name, &s) == 0) {// 0 is success
276280
// tile exists on disk; mark it as expired
281+
time_t now = time(NULL);
277282

278-
if (!gmtime_r(&(s.st_mtime), &touchCalendar)) {
279-
touchTime.modtime = 315558000;
280-
} else {
281-
if (touchCalendar.tm_year > 105) { // Tile hasn't already been marked as expired
282-
touchCalendar.tm_year -= 20; //Set back by 20 years, to keep the creation time as reference.
283-
touchTime.modtime = mktime(&touchCalendar);
283+
// If the file is newer than 10 years old, it hasn't been expired yet
284+
if (now >= s.st_mtime && (now - s.st_mtime) < TEN_YEARS) {
285+
286+
// Prevent underflow on systems where time_t is signed and small
287+
if (s.st_mtime > TWENTY_YEARS) {
288+
touchTime.modtime = s.st_mtime - TWENTY_YEARS; // Keep relative creation time
284289
} else {
285-
touchTime.modtime = s.st_mtime;
290+
touchTime.modtime = FALLBACK_TIME;
286291
}
292+
293+
} else {
294+
// Already marked as expired (or from the future), leave it alone
295+
touchTime.modtime = s.st_mtime;
287296
}
288297

289298
touchTime.actime = s.st_atime; // Don't modify atime, as that is used for tile cache purging

0 commit comments

Comments
 (0)