mirror of
https://github.com/troglobit/finit.git
synced 2026-11-23 20:11:44 +00:00
tmpfiles: add --replace, files on the command line in place of one
A package script may run before the package's tmpfiles.d file is on
disk, and gives the entries on the command line or on standard input.
Then only those entries were read, so one that the admin has changed
in /etc/tmpfiles.d was run as the package has it.
With --replace=PATH all the tmpfiles.d files are read, and the ones on
the command line are read where the file PATH would be, as in
systemd-tmpfiles:
tmpfiles --create --replace=/usr/lib/tmpfiles.d/app.conf -
They are not read at all when a later directory has a file of that
name, as with a file that is installed.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
4 files changed
+98
-29
No files matched your search
@@ -61,6 +61,9 @@ All relevant changes are documented in this file.
|
|||||||
`tmpfiles --dry-run` tells which entries are skipped
|
`tmpfiles --dry-run` tells which entries are skipped
|
||||||
- tmpfiles: a file named `-` on the command line is read from standard
|
- tmpfiles: a file named `-` on the command line is read from standard
|
||||||
input
|
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
|
- tmpfiles: add `--prefix=PATH`, to only run the entries for paths below
|
||||||
it. `--prefix` and `--exclude-prefix` match whole path components,
|
it. `--prefix` and `--exclude-prefix` match whole path components,
|
||||||
`--exclude-prefix=/dev` no longer skips `/devices`
|
`--exclude-prefix=/dev` no longer skips `/devices`
|
||||||
|
|||||||
@@ -17,4 +17,4 @@ files or common packages already rely on, last what few will miss.
|
|||||||
2. Command line
|
2. Command line
|
||||||
---------------
|
---------------
|
||||||
|
|
||||||
Missing options: `--root`, `--replace`, and `--purge`.
|
Missing options: `--root` and `--purge`.
|
||||||
+76
-27
@@ -65,6 +65,9 @@ int boot_flag = 0;
|
|||||||
int dry_run = 0;
|
int dry_run = 0;
|
||||||
int cat_config = 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
|
#define MAX_EXCLUDES 32
|
||||||
static const char *exclude_prefixes[MAX_EXCLUDES];
|
static const char *exclude_prefixes[MAX_EXCLUDES];
|
||||||
static int num_excludes;
|
static int num_excludes;
|
||||||
@@ -2385,44 +2388,61 @@ static void process_file(const char *fn)
|
|||||||
fclose(fp);
|
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)
|
static int by_name(const void *a, const void *b)
|
||||||
{
|
{
|
||||||
return strcmp(basenm(*(char **)a), basenm(*(char **)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[])
|
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;
|
int flags = GLOB_NOESCAPE;
|
||||||
size_t i, j, cnt = 0;
|
size_t i, j, cnt = 0;
|
||||||
char **list;
|
char **list;
|
||||||
glob_t gl;
|
glob_t gl;
|
||||||
|
|
||||||
for (i = 0; i < NELEMS(dir); i++) {
|
if (num && !replace) {
|
||||||
glob(dir[i], flags, NULL, &gl);
|
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;
|
flags |= GLOB_APPEND;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2438,10 +2458,27 @@ static void process(int num, char *file[])
|
|||||||
if (j == gl.gl_pathc)
|
if (j == gl.gl_pathc)
|
||||||
list[cnt++] = gl.gl_pathv[i];
|
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);
|
qsort(list, cnt, sizeof(char *), by_name);
|
||||||
|
|
||||||
for (i = 0; i < cnt; i++)
|
for (i = 0; i < cnt; i++) {
|
||||||
|
if (list[i] != replace) {
|
||||||
process_file(list[i]);
|
process_file(list[i]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (int k = 0; k < num; k++)
|
||||||
|
process_file(file[k]);
|
||||||
|
}
|
||||||
|
|
||||||
free(list);
|
free(list);
|
||||||
globfree(&gl);
|
globfree(&gl);
|
||||||
@@ -2463,6 +2500,7 @@ static int usage(int rc)
|
|||||||
" --exclude-prefix=PFX Ignore rules for paths starting with PFX\n"
|
" --exclude-prefix=PFX Ignore rules for paths starting with PFX\n"
|
||||||
" --graceful Does nothing, an unknown user or group is never an error\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"
|
" --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"
|
" -r, --remove Remove files and directories marked for removal\n"
|
||||||
" -h, --help This help text\n"
|
" -h, --help This help text\n"
|
||||||
"\n"
|
"\n"
|
||||||
@@ -2480,6 +2518,7 @@ enum {
|
|||||||
OPT_PREFIX,
|
OPT_PREFIX,
|
||||||
OPT_CAT_CONFIG,
|
OPT_CAT_CONFIG,
|
||||||
OPT_GRACEFUL,
|
OPT_GRACEFUL,
|
||||||
|
OPT_REPLACE,
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
@@ -2495,6 +2534,7 @@ int main(int argc, char *argv[])
|
|||||||
{ "graceful", 0, NULL, OPT_GRACEFUL },
|
{ "graceful", 0, NULL, OPT_GRACEFUL },
|
||||||
{ "prefix", 1, NULL, OPT_PREFIX },
|
{ "prefix", 1, NULL, OPT_PREFIX },
|
||||||
{ "remove", 0, NULL, 'r' },
|
{ "remove", 0, NULL, 'r' },
|
||||||
|
{ "replace", 1, NULL, OPT_REPLACE },
|
||||||
{ "help", 0, NULL, 'h' },
|
{ "help", 0, NULL, 'h' },
|
||||||
{ NULL, 0, NULL, 0 }
|
{ NULL, 0, NULL, 0 }
|
||||||
};
|
};
|
||||||
@@ -2541,6 +2581,10 @@ int main(int argc, char *argv[])
|
|||||||
case OPT_GRACEFUL:
|
case OPT_GRACEFUL:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case OPT_REPLACE:
|
||||||
|
replace = optarg;
|
||||||
|
break;
|
||||||
|
|
||||||
case OPT_EXCLUDE_PREFIX:
|
case OPT_EXCLUDE_PREFIX:
|
||||||
add_exclude_prefix(optarg);
|
add_exclude_prefix(optarg);
|
||||||
break;
|
break;
|
||||||
@@ -2563,6 +2607,11 @@ int main(int argc, char *argv[])
|
|||||||
return 1;
|
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]);
|
process(argc - optind, &argv[optind]);
|
||||||
|
|
||||||
/* Remove and clean first, or an r entry takes what an f entry above it made */
|
/* Remove and clean first, or an r entry takes what an f entry above it made */
|
||||||
|
|||||||
+18
-1
@@ -6,13 +6,14 @@ set -eu
|
|||||||
TEST_DIR=$(dirname "$0")
|
TEST_DIR=$(dirname "$0")
|
||||||
|
|
||||||
DIR=/tmp/files
|
DIR=/tmp/files
|
||||||
|
CONF=/tmp/files.conf
|
||||||
LIB=/usr/lib/tmpfiles.d
|
LIB=/usr/lib/tmpfiles.d
|
||||||
ETC=/etc/tmpfiles.d
|
ETC=/etc/tmpfiles.d
|
||||||
|
|
||||||
test_teardown()
|
test_teardown()
|
||||||
{
|
{
|
||||||
say "Running 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
|
# shellcheck source=/dev/null
|
||||||
@@ -31,6 +32,7 @@ f $DIR/first - - - - one
|
|||||||
CONF
|
CONF
|
||||||
run "cat > $ETC/20-test.conf" <<CONF
|
run "cat > $ETC/20-test.conf" <<CONF
|
||||||
f $DIR/first - - - - two
|
f $DIR/first - - - - two
|
||||||
|
f $DIR/second - - - - two
|
||||||
w+ $DIR/first - - - - +three
|
w+ $DIR/first - - - - +three
|
||||||
w+ $DIR/first - - - - +four
|
w+ $DIR/first - - - - +four
|
||||||
CONF
|
CONF
|
||||||
@@ -46,6 +48,21 @@ assert "First by file name" "$(texec cat $DIR/first)" = "one+three+four"
|
|||||||
assert "Told about in a dry run" \
|
assert "Told about in a dry run" \
|
||||||
"$(texec sh -c "/libexec/finit/tmpfiles -n --create --prefix=$DIR 2>&1 | grep -c 'already has an entry'")" = "1"
|
"$(texec sh -c "/libexec/finit/tmpfiles -n --create --prefix=$DIR 2>&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" <<CONF
|
||||||
|
f $DIR/replace - - - - new
|
||||||
|
f $DIR/first - - - - new
|
||||||
|
f $DIR/second - - - - new
|
||||||
|
CONF
|
||||||
|
run "rm -rf $DIR; /libexec/finit/tmpfiles --create --prefix=$DIR --replace=$LIB/15-test.conf $CONF"
|
||||||
|
assert "Read" "$(texec cat $DIR/replace)" = "new"
|
||||||
|
assert "After the files before its name" "$(texec cat $DIR/first)" = "one+three+four"
|
||||||
|
assert "Before the ones after it" "$(texec cat $DIR/second)" = "new"
|
||||||
|
assert "With the other files" "$(texec cat $DIR/same)" = "etc"
|
||||||
|
run "rm -rf $DIR; /libexec/finit/tmpfiles --create --prefix=$DIR --replace=$LIB/50-test.conf $CONF"
|
||||||
|
assert "Not when /etc has that name" "$(texec sh -c "ls $DIR | grep -c replace")" = "0"
|
||||||
|
|
||||||
sep
|
sep
|
||||||
say 'A file named - is read from standard input'
|
say 'A file named - is read from standard input'
|
||||||
run "echo 'f $DIR/stdin' | /libexec/finit/tmpfiles --create -"
|
run "echo 'f $DIR/stdin' | /libexec/finit/tmpfiles --create -"
|
||||||
|
|||||||
Reference in new issue
Block a user