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

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