diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index f77c5708..dc8f728b 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -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` diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index 01e252e9..80c05acb 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -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`. diff --git a/src/tmpfiles.c b/src/tmpfiles.c index d721477d..0870e789 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -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; diff --git a/test/Makefile.am b/test/Makefile.am index f58d7e49..d2d8c250 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -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 diff --git a/test/tmpfiles-purge.sh b/test/tmpfiles-purge.sh new file mode 100755 index 00000000..46dd1feb --- /dev/null +++ b/test/tmpfiles-purge.sh @@ -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" </dev/null 2>&1; echo \$?")" = "1"