tmpfiles: keep what x and X entries name from being cleaned

The x and X entries were read and ignored, so a path that a package
had marked to be left alone was cleaned like the rest:

    d /var/tmp      - - - 30d
    x /var/tmp/keep

    tmpfiles --clean t.conf

This removed what was old in /var/tmp/keep.

Gather the x and X entries of all files in a pass before the cleaning,
they may come after the entry they apply to, or in another file.  x
keeps the path and what is in it, X the path alone.  A directory that
is not empty afterwards is no longer warned about.

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 9392f2082e
commit 60cd11ab23
3 files changed
+77 -7

No files matched your search

+2
View File
@@ -15,6 +15,8 @@ All relevant changes are documented in this file.
skipped unless the new `--boot` option is given. Finit gives it at
boot, and now also `--remove`, so `r`, `R`, and `D` entries take effect
at boot and no longer need a separate call
- tmpfiles: the `x` and `X` entries now keep paths from being cleaned by
`tmpfiles --clean`, before they were read and ignored
- tmpfiles: add the `a`, `a+`, `A`, and `A+` entries for POSIX ACLs, used
by for example the tpm2-tss package. Finit sets them itself, no need
for libacl or `setfacl`
+62 -7
View File
@@ -26,6 +26,7 @@
#include <dirent.h>
#include <endian.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <ftw.h>
#include <getopt.h>
#include <glob.h>
@@ -351,6 +352,49 @@ static int parse_gid(const char *group)
return val < 0 ? 0 : val;
}
/* What the x and X entries keep from being cleaned */
struct keep {
char *pattern;
int self; /* X, the path but not what is in it */
};
static struct keep *keeps;
static int num_keeps;
static int collect; /* Pass that only gathers the above */
static void keep_add(const char *pattern, int self)
{
struct keep *k;
k = realloc(keeps, (num_keeps + 1) * sizeof(*k));
if (!k)
return;
keeps = k;
k = &keeps[num_keeps];
k->pattern = strdup(pattern);
k->self = self;
if (!k->pattern)
return;
/* The paths from nftw() have no slash at the end */
for (size_t len = strlen(k->pattern); len > 1 && k->pattern[len - 1] == '/'; len--)
k->pattern[len - 1] = 0;
num_keeps++;
}
static int kept(const char *path)
{
for (int i = 0; i < num_keeps; i++) {
struct keep *k = &keeps[i];
if (!fnmatch(k->pattern, path, FNM_PATHNAME | (k->self ? 0 : FNM_LEADING_DIR)))
return 1;
}
return 0;
}
/* Globals for do_clean() callback - nftw doesn't support user data */
static time_t clean_age;
static time_t clean_now;
@@ -380,7 +424,10 @@ static int do_clean(const char *fpath, const struct stat *sb, int tflag, struct
if (!is_dir && clean_now - sb->st_ctime < clean_age)
return 0; /* ctime is too new, keep it (files only) */
if (remove(fpath) && errno != ENOENT && errno != EBUSY)
if (kept(fpath))
return 0;
if (remove(fpath) && errno != ENOENT && errno != EBUSY && errno != ENOTEMPTY)
warn("Failed cleaning %s", fpath);
return 0;
@@ -747,6 +794,11 @@ static void tmpfiles(char *line)
}
if (excluded(path))
return;
if (collect) {
if (type[0] == 'x' || type[0] == 'X')
keep_add(path, type[0] == 'X');
return;
}
token = strtok(NULL, "\t ");
if (token) {
@@ -805,7 +857,7 @@ static void tmpfiles(char *line)
break;
case 'X':
case 'x':
/* handled by clean_flag (--clean) */
/* see kept(), used with --clean */
break;
case 'Z':
case 'z':
@@ -833,10 +885,6 @@ static void tmpfiles(char *line)
clean_now = time(NULL);
nftw(path, do_clean, 20, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
break;
case 'x':
case 'X':
/* exclusion patterns - not yet implemented */
break;
default:
break;
}
@@ -1037,7 +1085,7 @@ static void tmpfiles(char *line)
break;
case 'X':
case 'x':
/* handled by clean_flag (--clean) */
/* see kept(), used with --clean */
break;
case 'Z':
opts = "-R";
@@ -1235,6 +1283,13 @@ int main(int argc, char *argv[])
return 1;
}
/* Cleaning needs every x and X entry, also those in files not yet read */
if (clean_flag) {
collect = quiet = 1;
process(argc - optind, &argv[optind]);
collect = quiet = 0;
}
/* Remove and clean first, or an r entry takes what an f entry above it made */
if (create_flag && (clean_flag || remove_flag)) {
create_flag = 0;
+13
View File
@@ -40,3 +40,16 @@ CONF
assert_left $DIR/young "file "
assert_left $DIR/old "mnt "
assert_left $DIR/old/mnt "file "
sep
say 'x keeps a path and what is in it, X only the path itself'
run "mkdir -p $DIR/keep/all $DIR/keep/dir; touch $DIR/keep/file $DIR/keep/all/file $DIR/keep/dir/file"
sleep 2
clean <<CONF
d $DIR/keep - - - 1s
x $DIR/keep/a*
X $DIR/keep/dir/
CONF
assert_left $DIR/keep "all dir "
assert_left $DIR/keep/all "file "
assert_left $DIR/keep/dir ""