tmpfiles: add --purge and the $ modifier

When a package is removed, the state its tmpfiles.d file made is left
behind, there was no way to ask for it to be removed.

systemd-tmpfiles has --purge for that, which removes what the entries
marked with a $ after the type make:

    d$ /var/lib/app 0750 app app
    tmpfiles --purge /usr/lib/tmpfiles.d/app.conf

Add the same.  It only takes the entries of files on the command line,
never all of the tmpfiles.d files, and a path is removed before the
one above it.  A $ was until now read and ignored.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-21 19:23:50 +01:00
1 parent 575267f09b
commit 26568bc0fd
5 files changed
+104 -4

No files matched your search

+3
View File
@@ -64,6 +64,9 @@ All relevant changes are documented in this file.
- tmpfiles: add `--replace=PATH`, to read all files with the ones on the
command line in place of the file `PATH`, as in systemd-tmpfiles. For
a package script that runs before the package's own file is installed
- tmpfiles: add `--purge` and the `$` modifier, as in systemd-tmpfiles.
`tmpfiles --purge FILE` removes what the entries of `FILE` that are
marked with a `$`, as in `d$`, make. For when a package is removed
- tmpfiles: add `--prefix=PATH`, to only run the entries for paths below
it. `--prefix` and `--exclude-prefix` match whole path components,
`--exclude-prefix=/dev` no longer skips `/devices`
+1 -1
View File
@@ -17,4 +17,4 @@ files or common packages already rely on, last what few will miss.
2. Command line
---------------
Missing options: `--root` and `--purge`.
Missing option: `--root`.
+51 -3
View File
@@ -61,6 +61,7 @@ int debug;
int create_flag = 0;
int clean_flag = 0;
int remove_flag = 0;
int purge_flag = 0;
int boot_flag = 0;
int dry_run = 0;
int cat_config = 0;
@@ -1726,7 +1727,7 @@ struct entry {
struct perm perm;
char *age, *arg;
ssize_t arglen; /* Of an argument that was in Base64, else -1 */
int plus, kind;
int plus, kind, purge;
unsigned sum; /* Of the line, to tell if two are the same */
int done;
};
@@ -1833,6 +1834,8 @@ static int parse(char *line)
b64 = 1;
else if (*mod == '=')
e.kind = 1;
else if (*mod == '$')
e.purge = 1;
else if (*mod == '?' && e.type[0] == 'L')
e.type[0] = 'l'; /* What Finit has had as l */
else if (*mod == '^')
@@ -1958,6 +1961,19 @@ static int parse(char *line)
return 0;
}
static int purge_path(const char *path)
{
struct stat st;
if (lstat(path, &st))
return errno == ENOENT ? 0 : -1;
return S_ISDIR(st.st_mode) ? rmrf(path) : erase(path);
}
/* What the entries marked with a $ make, only that pass of --purge */
static int purging;
static void tmpfiles(struct entry *e)
{
char *type = e->type, *path = e->path, *age = e->age, *arg = e->arg;
@@ -1973,6 +1989,21 @@ static void tmpfiles(struct entry *e)
char buf[1024];
glob_t gl;
if (purging) {
if (!e->purge || !strchr("bcCdDefFlLpw", type[0]))
return;
if (dry_run)
printf("purge %s %s\n", type, path);
else if (!globs(e))
rc = purge_path(path);
else if (glob_do(path, purge_path) && errno != ENOENT)
rc = -1;
if (rc)
warn("Failed purging %s", path);
return;
}
if (dry_run) {
if (remove_flag && strchr("DrR", type[0]))
printf("remove %s %s\n", type, path);
@@ -2500,6 +2531,7 @@ static int usage(int rc)
" --exclude-prefix=PFX Ignore rules for paths starting with PFX\n"
" --graceful Does nothing, an unknown user or group is never an error\n"
" --prefix=PFX Only apply rules for paths starting with PFX\n"
" --purge Remove all that the entries marked with $ in CONFIGFILE make\n"
" --replace=PATH Read all files, with CONFIGFILE in place of PATH\n"
" -r, --remove Remove files and directories marked for removal\n"
" -h, --help This help text\n"
@@ -2519,6 +2551,7 @@ enum {
OPT_CAT_CONFIG,
OPT_GRACEFUL,
OPT_REPLACE,
OPT_PURGE,
};
int main(int argc, char *argv[])
@@ -2533,6 +2566,7 @@ int main(int argc, char *argv[])
{ "exclude-prefix", 1, NULL, OPT_EXCLUDE_PREFIX },
{ "graceful", 0, NULL, OPT_GRACEFUL },
{ "prefix", 1, NULL, OPT_PREFIX },
{ "purge", 0, NULL, OPT_PURGE },
{ "remove", 0, NULL, 'r' },
{ "replace", 1, NULL, OPT_REPLACE },
{ "help", 0, NULL, 'h' },
@@ -2585,6 +2619,10 @@ int main(int argc, char *argv[])
replace = optarg;
break;
case OPT_PURGE:
purge_flag = 1;
break;
case OPT_EXCLUDE_PREFIX:
add_exclude_prefix(optarg);
break;
@@ -2602,8 +2640,12 @@ int main(int argc, char *argv[])
}
}
if (!cat_config && create_flag + clean_flag + remove_flag == 0) {
fprintf(stderr, "You need to specify at least one of --clean, --create, or --remove.\n");
if (!cat_config && create_flag + clean_flag + remove_flag + purge_flag == 0) {
fprintf(stderr, "You need to specify at least one of --clean, --create, --remove, or --purge.\n");
return 1;
}
if (purge_flag && optind == argc) {
fprintf(stderr, "--purge needs a CONFIGFILE, it is not for all of the tmpfiles.d files.\n");
return 1;
}
@@ -2614,6 +2656,12 @@ int main(int argc, char *argv[])
process(argc - optind, &argv[optind]);
if (purge_flag) {
purging = 1;
run_entries(0);
purging = 0;
}
/* Remove and clean first, or an r entry takes what an f entry above it made */
if (clean_flag || remove_flag) {
int create = create_flag;
+2
View File
@@ -85,6 +85,7 @@ EXTRA_DIST += tmpfiles-acl.sh
EXTRA_DIST += tmpfiles-arg.sh
EXTRA_DIST += tmpfiles-attr.sh
EXTRA_DIST += tmpfiles-perm.sh
EXTRA_DIST += tmpfiles-purge.sh
EXTRA_DIST += tmpfiles-boot.sh
EXTRA_DIST += tmpfiles-clean.sh
EXTRA_DIST += tmpfiles-copy.sh
@@ -174,6 +175,7 @@ TESTS += tmpfiles-acl.sh
TESTS += tmpfiles-arg.sh
TESTS += tmpfiles-attr.sh
TESTS += tmpfiles-perm.sh
TESTS += tmpfiles-purge.sh
TESTS += tmpfiles-boot.sh
TESTS += tmpfiles-clean.sh
TESTS += tmpfiles-copy.sh
+47
View File
@@ -0,0 +1,47 @@
#!/bin/sh
# Verify tmpfiles --purge, which removes what the entries marked with a
# $ make, for when a package is uninstalled
set -eu
TEST_DIR=$(dirname "$0")
DIR=/tmp/purge
CONF=/tmp/purge.conf
test_teardown()
{
say "Running test teardown."
run "rm -rf $DIR $CONF"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
run "cat > $CONF" <<CONF
d $DIR
d\$ $DIR/state
f\$ $DIR/state/file - - - - text
f\$ $DIR/file
L\$ $DIR/link - - - - file
e\$ $DIR/glob-*
d $DIR/kept
f $DIR/kept/file
r\$ $DIR/other
CONF
run "/libexec/finit/tmpfiles --create $CONF"
run "mkdir $DIR/glob-1 $DIR/glob-2 $DIR/state/more; touch $DIR/other"
say 'Only tells what it would do with --dry-run'
out=$(texec /libexec/finit/tmpfiles --dry-run --purge "$CONF")
assert "Entries with a \$, a path before the one above it" "$(echo "$out" | tr '\n' ';')" \
= "purge f\$ $DIR/state/file;purge d\$ $DIR/state;purge f\$ $DIR/file;purge L\$ $DIR/link;purge e\$ $DIR/glob-*;"
sep
say 'Removes what the entries with a $ make, and nothing else'
run "/libexec/finit/tmpfiles --purge $CONF"
assert "What is left" "$(texec ls $DIR | tr '\n' ' ')" = "kept other "
assert "And in it" "$(texec ls $DIR/kept)" = "file"
sep
say 'Needs a file, it is not for all of the tmpfiles.d files'
assert "Refused without one" "$(texec sh -c "/libexec/finit/tmpfiles --purge >/dev/null 2>&1; echo \$?")" = "1"