tmpfiles: add --prefix, to only run entries below a path

A package that ships a tmpfiles.d file wants its own paths created
when it is installed, without running every other entry on the
system:

    tmpfiles --create --prefix=/var/lib/app

Add the option, it can be given more than once.  It and
--exclude-prefix compare whole path components now, before
--exclude-prefix=/dev also left out /devices.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 18:35:36 +01:00
1 parent fb3b13a245
commit 0a2beb9b6d
4 files changed
+54 -6

No files matched your search

+3
View File
@@ -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
+2 -4
View File
@@ -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
+39 -2
View File
@@ -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;
+10
View File
@@ -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" <<CONF
f $DIR/in/file
f $DIR/out/file
f $DIR/input
CONF
run "/libexec/finit/tmpfiles --create --prefix=$DIR/in $CONF"
assert "Only below the prefix" "$(texec sh -c "ls -d $DIR/in* $DIR/out* 2>/dev/null" | tr '\n' ' ')" = "$DIR/in "