analyze: add --root option for cat-config

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-04-27 08:55:16 +02:00
parent 3c51c62616
commit 46d8646a9f
4 changed files with 29 additions and 7 deletions

View file

@ -367,6 +367,13 @@ NAutoVTs=8
generators enabled will generally result in some warnings.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--root=<replaceable>PATH</replaceable></option></term>
<listitem><para>With <command>cat-files</command>, show config files underneath
the specified root path <replaceable>PATH</replaceable>.</para></listitem>
</varlistentry>
<xi:include href="user-system-options.xml" xpointer="host" />
<xi:include href="user-system-options.xml" xpointer="machine" />

View file

@ -72,6 +72,7 @@ static const char *arg_host = NULL;
static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
static bool arg_man = true;
static bool arg_generators = false;
static const char *arg_root = NULL;
struct boot_times {
usec_t firmware_time;
@ -1329,7 +1330,7 @@ static int cat_config(int argc, char *argv[], void *userdata) {
return -EINVAL;
}
r = conf_files_cat(*arg);
r = conf_files_cat(arg_root, *arg);
if (r < 0)
return r;
}
@ -1688,6 +1689,7 @@ static int parse_argv(int argc, char *argv[]) {
ARG_VERSION = 0x100,
ARG_ORDER,
ARG_REQUIRE,
ARG_ROOT,
ARG_SYSTEM,
ARG_USER,
ARG_GLOBAL,
@ -1704,6 +1706,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "version", no_argument, NULL, ARG_VERSION },
{ "order", no_argument, NULL, ARG_ORDER },
{ "require", no_argument, NULL, ARG_REQUIRE },
{ "root", required_argument, NULL, ARG_ROOT },
{ "system", no_argument, NULL, ARG_SYSTEM },
{ "user", no_argument, NULL, ARG_USER },
{ "global", no_argument, NULL, ARG_GLOBAL },
@ -1732,6 +1735,10 @@ static int parse_argv(int argc, char *argv[]) {
case ARG_VERSION:
return version();
case ARG_ROOT:
arg_root = optarg;
break;
case ARG_SYSTEM:
arg_scope = UNIT_FILE_SYSTEM;
break;
@ -1825,6 +1832,11 @@ static int parse_argv(int argc, char *argv[]) {
return -EINVAL;
}
if (arg_root && !streq_ptr(argv[optind], "cat-config")) {
log_error("Option --root is only supported for cat-config right now.");
return -EINVAL;
}
return 1; /* work to do */
}

View file

@ -294,8 +294,9 @@ int conf_files_list_with_replacement(
return 0;
}
int conf_files_cat(const char *name) {
int conf_files_cat(const char *root, const char *name) {
_cleanup_strv_free_ char **dirs = NULL, **files = NULL;
_cleanup_free_ char *path = NULL;
const char *dir;
char **t;
int r;
@ -307,19 +308,21 @@ int conf_files_cat(const char *name) {
return log_error("Failed to build directory list: %m");
}
r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dirs);
r = conf_files_list_strv(&files, ".conf", root, 0, (const char* const*) dirs);
if (r < 0)
return log_error_errno(r, "Failed to query file list: %m");
name = strjoina("/etc/", name);
path = path_join(root, "/etc", name);
if (!path)
return log_oom();
if (DEBUG_LOGGING) {
log_debug("Looking for configuration in:");
log_debug(" %s", name);
log_debug(" %s", path);
STRV_FOREACH(t, dirs)
log_debug(" %s/*.conf", *t);
}
/* show */
return cat_files(name, files, CAT_FLAGS_MAIN_FILE_OPTIONAL);
return cat_files(path, files, CAT_FLAGS_MAIN_FILE_OPTIONAL);
}

View file

@ -23,4 +23,4 @@ int conf_files_list_with_replacement(
const char *replacement,
char ***files,
char **replace_file);
int conf_files_cat(const char *name);
int conf_files_cat(const char *root, const char *name);