hwdb: implement --root option for systemd-hwdb query

Currently, the systemd-hwdb --root flag only has an effect for the
'update' verb. It would be useful to be able to use the --root option
for the 'query' verb too (e.g. for testing a hwdb.bin created with
systemd-hwdb update --root <path>).

Use sd_hwdb_new_from_path to initialize the hwdb if --root is passed to
systemd-hwdb query.

Note that this functionality was not added to 'udevadm hwdb' since that
command is deprecated.
This commit is contained in:
Nick Rosbrook
2022-05-27 09:40:54 -04:00
parent 60f0ba7556
commit beff73f94e
4 changed files with 19 additions and 6 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ static const char *arg_root = NULL;
static bool arg_strict = false;
static int verb_query(int argc, char *argv[], void *userdata) {
return hwdb_query(argv[1]);
return hwdb_query(argv[1], arg_root);
}
static int verb_update(int argc, char *argv[], void *userdata) {
+16 -3
View File
@@ -650,14 +650,27 @@ int hwdb_update(const char *root, const char *hwdb_bin_dir, bool strict, bool co
return r;
}
int hwdb_query(const char *modalias) {
int hwdb_query(const char *modalias, const char *root) {
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
const char *key, *value;
const char *key, *value, *p;
int r;
assert(modalias);
r = sd_hwdb_new(&hwdb);
if (!isempty(root))
NULSTR_FOREACH(p, hwdb_bin_paths) {
_cleanup_free_ char *hwdb_bin = NULL;
hwdb_bin = path_join(root, p);
if (!hwdb_bin)
return -ENOMEM;
r = sd_hwdb_new_from_path(hwdb_bin, &hwdb);
if (r >= 0)
break;
}
else
r = sd_hwdb_new(&hwdb);
if (r < 0)
return r;
+1 -1
View File
@@ -7,4 +7,4 @@
bool hwdb_validate(sd_hwdb *hwdb);
int hwdb_update(const char *root, const char *hwdb_bin_dir, bool strict, bool compat);
int hwdb_query(const char *modalias);
int hwdb_query(const char *modalias, const char *root);
+1 -1
View File
@@ -95,7 +95,7 @@ int hwdb_main(int argc, char *argv[], void *userdata) {
}
if (arg_test)
return hwdb_query(arg_test);
return hwdb_query(arg_test, NULL);
return 0;
}