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

+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;