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
+99
-30
No files matched your search
+77
-28
@@ -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 */
|
||||
|
||||
Reference in new issue
Block a user