diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 68d141f3..f77c5708 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -61,6 +61,9 @@ All relevant changes are documented in this file. `tmpfiles --dry-run` tells which entries are skipped - tmpfiles: a file named `-` on the command line is read from standard input +- 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 `--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 6345e4b3..01e252e9 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`, `--replace`, and `--purge`. +Missing options: `--root` and `--purge`. diff --git a/src/tmpfiles.c b/src/tmpfiles.c index 3d681edf..d721477d 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -65,6 +65,9 @@ int boot_flag = 0; int dry_run = 0; int cat_config = 0; +/* File the ones on the command line are read in place of, --replace */ +static char *replace; + #define MAX_EXCLUDES 32 static const char *exclude_prefixes[MAX_EXCLUDES]; static int num_excludes; @@ -2385,44 +2388,61 @@ static void process_file(const char *fn) fclose(fp); } +/* + * Only the three last directories are defined in tmpfiles.d(5) as + * system search paths. Finit adds two more before that, for its own + * files and for the configure prefix. + */ +static const char *dirs[] = { + FINIT_TMPFILES, + TMPFILES_PATH_, + "/usr/lib/tmpfiles.d", + "/run/tmpfiles.d", + "/etc/tmpfiles.d", /* local admin overrides */ +}; + +/* Which of dirs[] a file is in, a later one wins, or -1 */ +static int dir_prio(const char *path) +{ + size_t len = basenm(path) - path; + + for (size_t i = 0; len && i < NELEMS(dirs); i++) { + if (strlen(dirs[i]) == len - 1 && !strncmp(path, dirs[i], len - 1)) + return i; + } + + return -1; +} + static int by_name(const void *a, const void *b) { return strcmp(basenm(*(char **)a), basenm(*(char **)b)); } +/* + * The files on the command line, or with none, or with --replace, the + * ones in dirs[]. A file there is read in place of one of the same + * name in a directory before its own, and what is left is read in + * order of the file names, whatever directory they are in. + */ static void process(int num, char *file[]) { - /* If config files specified on command line, process only those */ - if (num) { - for (int i = 0; i < num; i++) - process_file(file[i]); - return; - } - - /* - * No config files specified, process standard tmpfiles.d directories. - * - * Only the three last tmpfiles.d/ directories are defined in - * tmpfiles.d(5) as system search paths. Finit adds two more - * before that, for its own files and for the configure prefix. - * A file overrides one of the same name in a directory before - * its own, and what is left is read in order of the file names, - * whatever directory they are in. - */ - char *dir[] = { - FINIT_TMPFILES "/*.conf", - TMPFILES_PATH_ "/*.conf", - "/usr/lib/tmpfiles.d/*.conf", - "/run/tmpfiles.d/*.conf", - "/etc/tmpfiles.d/*.conf", /* local admin overrides */ - }; int flags = GLOB_NOESCAPE; size_t i, j, cnt = 0; char **list; glob_t gl; - for (i = 0; i < NELEMS(dir); i++) { - glob(dir[i], flags, NULL, &gl); + if (num && !replace) { + for (int k = 0; k < num; k++) + process_file(file[k]); + return; + } + + for (i = 0; i < NELEMS(dirs); i++) { + char pattern[PATH_MAX]; + + snprintf(pattern, sizeof(pattern), "%s/*.conf", dirs[i]); + glob(pattern, flags, NULL, &gl); flags |= GLOB_APPEND; } @@ -2438,10 +2458,27 @@ static void process(int num, char *file[]) if (j == gl.gl_pathc) list[cnt++] = gl.gl_pathv[i]; } + + /* Not when a file of that name in a later directory is read in its place */ + for (i = 0; replace && i < cnt; i++) { + if (strcmp(basenm(list[i]), basenm(replace))) + continue; + if (dir_prio(replace) >= dir_prio(list[i])) + list[i] = replace; + break; + } + if (replace && i == cnt) + list[cnt++] = replace; qsort(list, cnt, sizeof(char *), by_name); - for (i = 0; i < cnt; i++) - process_file(list[i]); + for (i = 0; i < cnt; i++) { + if (list[i] != replace) { + process_file(list[i]); + continue; + } + for (int k = 0; k < num; k++) + process_file(file[k]); + } free(list); globfree(&gl); @@ -2463,6 +2500,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" + " --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" "\n" @@ -2480,6 +2518,7 @@ enum { OPT_PREFIX, OPT_CAT_CONFIG, OPT_GRACEFUL, + OPT_REPLACE, }; int main(int argc, char *argv[]) @@ -2495,6 +2534,7 @@ int main(int argc, char *argv[]) { "graceful", 0, NULL, OPT_GRACEFUL }, { "prefix", 1, NULL, OPT_PREFIX }, { "remove", 0, NULL, 'r' }, + { "replace", 1, NULL, OPT_REPLACE }, { "help", 0, NULL, 'h' }, { NULL, 0, NULL, 0 } }; @@ -2541,6 +2581,10 @@ int main(int argc, char *argv[]) case OPT_GRACEFUL: break; + case OPT_REPLACE: + replace = optarg; + break; + case OPT_EXCLUDE_PREFIX: add_exclude_prefix(optarg); break; @@ -2563,6 +2607,11 @@ int main(int argc, char *argv[]) return 1; } + if (replace && (replace[0] != '/' || fnmatch("*.conf", replace, 0) || optind == argc)) { + fprintf(stderr, "--replace takes the absolute path of a .conf file, and needs a CONFIGFILE.\n"); + return 1; + } + process(argc - optind, &argv[optind]); /* Remove and clean first, or an r entry takes what an f entry above it made */ diff --git a/test/tmpfiles-files.sh b/test/tmpfiles-files.sh index 1c81024c..fcf691d2 100755 --- a/test/tmpfiles-files.sh +++ b/test/tmpfiles-files.sh @@ -6,13 +6,14 @@ set -eu TEST_DIR=$(dirname "$0") DIR=/tmp/files +CONF=/tmp/files.conf LIB=/usr/lib/tmpfiles.d ETC=/etc/tmpfiles.d test_teardown() { say "Running test teardown." - run "rm -rf $DIR $LIB/[0-9]0-test.conf $ETC/[0-9]0-test.conf" + run "rm -rf $DIR $CONF $LIB/[0-9]0-test.conf $ETC/[0-9]0-test.conf" } # shellcheck source=/dev/null @@ -31,6 +32,7 @@ f $DIR/first - - - - one CONF run "cat > $ETC/20-test.conf" <&1 | grep -c 'already has an entry'")" = "1" +sep +say 'With --replace a file on the command line is read as if it was the one named' +run "cat > $CONF" <