diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index af07333c..95326228 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -22,6 +22,9 @@ All relevant changes are documented in this file. `%w`, `%W`, `%B`, `%M`, and `%A`. Before, an entry with a `%` in the path was skipped. **Note:** a literal `%` in such an argument, e.g. in a `w` entry for `core_pattern`, must now be written `%%` +- 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` - tmpfiles: the age of an entry can now have several units, as in `1d12h`, also `ms`, `us`, and `min`. New are the `~` prefix, to keep what is right in the directory, and a choice of the timestamps to go diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index f1b95bd6..bfcfbeae 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -22,10 +22,8 @@ files or common packages already rely on, last what few will miss. 2. Command line --------------- -Missing options: `--prefix`, `--root`, `--replace`, `--dry-run`, -`--purge`, `--graceful`, and `--cat-config`. Of these `--prefix` -is the useful one, for packages that run `tmpfiles --create` on only -their own paths. +Missing options: `--root`, `--replace`, `--dry-run`, `--purge`, +`--graceful`, and `--cat-config`. 3. Smaller things diff --git a/src/tmpfiles.c b/src/tmpfiles.c index 0f279188..f1fa53fb 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -73,10 +73,40 @@ static void add_exclude_prefix(const char *prefix) exclude_prefixes[num_excludes++] = prefix; } +static const char *prefixes[MAX_EXCLUDES]; +static int num_prefixes; + +static void add_prefix(const char *prefix) +{ + if (prefix[0] && num_prefixes < MAX_EXCLUDES) + prefixes[num_prefixes++] = prefix; +} + +/* Whole path components, /dev is not a prefix of /devices */ +static int below(const char *path, const char *prefix) +{ + size_t len = strlen(prefix); + + while (len > 1 && prefix[len - 1] == '/') + len--; + + return !strncmp(path, prefix, len) && (len == 1 || !path[len] || path[len] == '/'); +} + +/* Not below a --prefix, when there is one, or below an --exclude-prefix */ static int excluded(const char *path) { - for (int i = 0; i < num_excludes; i++) { - if (!strncmp(path, exclude_prefixes[i], strlen(exclude_prefixes[i]))) + int i; + + for (i = 0; i < num_prefixes; i++) { + if (below(path, prefixes[i])) + break; + } + if (num_prefixes && i == num_prefixes) + return 1; + + for (i = 0; i < num_excludes; i++) { + if (below(path, exclude_prefixes[i])) return 1; } @@ -1628,6 +1658,7 @@ static int usage(int rc) " -d, --debug Show developer debug messages\n" " -E Exclude /dev, /proc, /run, and /sys\n" " --exclude-prefix=PFX Ignore rules for paths starting with PFX\n" + " --prefix=PFX Only apply rules for paths starting with PFX\n" " -r, --remove Remove files and directories marked for removal\n" " -h, --help This help text\n" "\n" @@ -1641,6 +1672,7 @@ static int usage(int rc) enum { OPT_EXCLUDE_PREFIX = 256, OPT_BOOT, + OPT_PREFIX, }; int main(int argc, char *argv[]) @@ -1651,6 +1683,7 @@ int main(int argc, char *argv[]) { "create", 0, NULL, 'c' }, { "debug", 0, NULL, 'd' }, { "exclude-prefix", 1, NULL, OPT_EXCLUDE_PREFIX }, + { "prefix", 1, NULL, OPT_PREFIX }, { "remove", 0, NULL, 'r' }, { "help", 0, NULL, 'h' }, { NULL, 0, NULL, 0 } @@ -1683,6 +1716,10 @@ int main(int argc, char *argv[]) boot_flag = 1; break; + case OPT_PREFIX: + add_prefix(optarg); + break; + case OPT_EXCLUDE_PREFIX: add_exclude_prefix(optarg); break; diff --git a/test/tmpfiles-arg.sh b/test/tmpfiles-arg.sh index 03297d65..ef1676b2 100755 --- a/test/tmpfiles-arg.sh +++ b/test/tmpfiles-arg.sh @@ -30,3 +30,13 @@ assert "File content" "$(texec cat $DIR/text)" = "two words" say 'A lone - is no argument' assert "Empty file" "$(texec stat -c %s $DIR/dash)" = "0" + +sep +say 'With --prefix only the entries below it are run' +run "cat > $CONF" </dev/null" | tr '\n' ' ')" = "$DIR/in "