tmpfiles: age 0 cleans everything

An entry with the age 0 was read as having no age, so nothing in its
directory was ever cleaned:

    d /var/spool/work - - - 0

    tmpfiles --clean t.conf

In tmpfiles.d(5) age 0 means everything in the directory is removed
on each clean.  parse_age() returned 0 for both.

It now returns -1 for no age and for one it cannot read, and with
age 0 the timestamps are not looked at.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 18:02:36 +01:00
1 parent 60cd11ab23
commit bc2f2ba4bb
3 files changed
+29 -12

No files matched your search

+3
View File
@@ -263,6 +263,9 @@ All relevant changes are documented in this file.
file or in the files after it. Such an entry is now skipped with a warning
- Fix `tmpfiles --clean` cleaning other file systems mounted below the
directory of an entry, it now stays on the file system of the directory
- Fix age `0` in a tmpfiles entry, it was read as no age. As in
tmpfiles.d(5), `tmpfiles --clean` now empties the directory of such an
entry
- Fix a lost reload for a service paused mid-reload by a condition in
flux. A second reload arriving meanwhile re-parsed the unchanged .conf
and cleared the pending mark, so the service resumed without ever being
+15 -12
View File
@@ -270,7 +270,7 @@ static void write_arg(FILE *fp, char *arg)
/**
* Parse age string and return age in seconds.
* Returns 0 on invalid/empty age ("-" or NULL).
* Returns -1 on invalid/empty age ("-" or NULL).
* Examples: "10d" = 864000, "1w" = 604800, "2h" = 7200
*/
static time_t parse_age(const char *age)
@@ -278,13 +278,13 @@ static time_t parse_age(const char *age)
time_t val;
char *end;
if (!age || !strcmp(age, "-") || !strcmp(age, "0"))
return 0;
if (!age || !strcmp(age, "-"))
return -1;
errno = 0;
val = strtoul(age, &end, 10);
if (errno || end == age)
return 0;
return -1;
switch (*end) {
case 'w':
@@ -303,7 +303,7 @@ static time_t parse_age(const char *age)
case '\0':
break;
default:
return 0; /* unknown suffix */
return -1; /* unknown suffix */
}
return val;
@@ -414,15 +414,18 @@ static int do_clean(const char *fpath, const struct stat *sb, int tflag, struct
* - Files: keep if ANY of atime, ctime, mtime is recent
* - Directories: keep if ANY of atime, mtime is recent (ctime
* excluded because cleanup itself updates directory ctime)
* - Age 0 removes everything, whatever the timestamps
*/
if (clean_now - sb->st_mtime < clean_age)
return 0; /* mtime is too new, keep it */
if (clean_age) {
if (clean_now - sb->st_mtime < clean_age)
return 0; /* mtime is too new, keep it */
if (clean_now - sb->st_atime < clean_age)
return 0; /* atime is too new, keep it */
if (clean_now - sb->st_atime < clean_age)
return 0; /* atime is too new, keep it */
if (!is_dir && clean_now - sb->st_ctime < clean_age)
return 0; /* ctime is too new, keep it (files only) */
if (!is_dir && clean_now - sb->st_ctime < clean_age)
return 0; /* ctime is too new, keep it (files only) */
}
if (kept(fpath))
return 0;
@@ -874,7 +877,7 @@ static void tmpfiles(char *line)
time_t max_age = parse_age(age);
/* Only process if age is specified */
if (max_age > 0) {
if (max_age >= 0) {
switch (type[0]) {
case 'd':
case 'D':
+11
View File
@@ -53,3 +53,14 @@ CONF
assert_left $DIR/keep "all dir "
assert_left $DIR/keep/all "file "
assert_left $DIR/keep/dir ""
sep
say 'Age 0 removes everything, no age nothing'
run "mkdir -p $DIR/zero $DIR/none; touch $DIR/zero/file $DIR/none/file"
clean <<CONF
d $DIR/zero - - - 0
d $DIR/none - - - -
d $DIR/none
CONF
assert_left $DIR/zero ""
assert_left $DIR/none "file "