summaryrefslogtreecommitdiff
path: root/src/libsystemd/sd-hwdb
diff options
context:
space:
mode:
authorNick Rosbrook <nick.rosbrook@canonical.com>2022-05-24 13:08:06 -0400
committerNick Rosbrook <nick.rosbrook@canonical.com>2022-05-27 09:40:54 -0400
commit60f0ba75569312825ff14680d05a4b4f95842951 (patch)
treefa8a13019740da0d1f1fd22411c6bbd27460a9e0 /src/libsystemd/sd-hwdb
parent9745b51c73c78a63003b4cb6e0714829144d297c (diff)
downloadsystemd-60f0ba75569312825ff14680d05a4b4f95842951.tar.gz
sd-hwdb: add sd_hwdb_new_from_path
The existing sd_hwdb_new function always initializes the hwdb from the first successful hwdb.bin it finds from hwdb_bin_paths. This means there is currently no way to initialize a hwdb from an explicit path, which would be useful for systemd-hwdb query. Add sd_hwdb_new_from_path to allow a sd_hwdb to be initialized from a custom path outside of hwdb_bin_paths.
Diffstat (limited to 'src/libsystemd/sd-hwdb')
-rw-r--r--src/libsystemd/sd-hwdb/sd-hwdb.c45
1 files changed, 31 insertions, 14 deletions
diff --git a/src/libsystemd/sd-hwdb/sd-hwdb.c b/src/libsystemd/sd-hwdb/sd-hwdb.c
index 748cf26934..f73d1fc21e 100644
--- a/src/libsystemd/sd-hwdb/sd-hwdb.c
+++ b/src/libsystemd/sd-hwdb/sd-hwdb.c
@@ -281,9 +281,9 @@ static int trie_search_f(sd_hwdb *hwdb, const char *search) {
return 0;
}
-_public_ int sd_hwdb_new(sd_hwdb **ret) {
+static int hwdb_new(const char *path, sd_hwdb **ret) {
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
- const char *hwdb_bin_path;
+ const char *hwdb_bin_path = NULL;
const char sig[] = HWDB_SIG;
assert_return(ret, -EINVAL);
@@ -294,19 +294,26 @@ _public_ int sd_hwdb_new(sd_hwdb **ret) {
hwdb->n_ref = 1;
- /* find hwdb.bin in hwdb_bin_paths */
- NULSTR_FOREACH(hwdb_bin_path, hwdb_bin_paths) {
- log_debug("Trying to open \"%s\"...", hwdb_bin_path);
- hwdb->f = fopen(hwdb_bin_path, "re");
- if (hwdb->f)
- break;
- if (errno != ENOENT)
- return log_debug_errno(errno, "Failed to open %s: %m", hwdb_bin_path);
- }
+ /* find hwdb.bin in hwdb_bin_paths, or from an explicit path if provided */
+ if (!isempty(path)) {
+ log_debug("Trying to open \"%s\"...", path);
+ hwdb->f = fopen(path, "re");
+ if (!hwdb->f)
+ return log_debug_errno(errno, "Failed to open %s: %m", path);
+ } else {
+ NULSTR_FOREACH(hwdb_bin_path, hwdb_bin_paths) {
+ log_debug("Trying to open \"%s\"...", hwdb_bin_path);
+ hwdb->f = fopen(hwdb_bin_path, "re");
+ if (hwdb->f)
+ break;
+ if (errno != ENOENT)
+ return log_debug_errno(errno, "Failed to open %s: %m", hwdb_bin_path);
+ }
- if (!hwdb->f)
- return log_debug_errno(SYNTHETIC_ERRNO(ENOENT),
- "hwdb.bin does not exist, please run 'systemd-hwdb update'");
+ if (!hwdb->f)
+ return log_debug_errno(SYNTHETIC_ERRNO(ENOENT),
+ "hwdb.bin does not exist, please run 'systemd-hwdb update'");
+ }
if (fstat(fileno(hwdb->f), &hwdb->st) < 0)
return log_debug_errno(errno, "Failed to stat %s: %m", hwdb_bin_path);
@@ -339,6 +346,16 @@ _public_ int sd_hwdb_new(sd_hwdb **ret) {
return 0;
}
+_public_ int sd_hwdb_new_from_path(const char *path, sd_hwdb **ret) {
+ assert_return(!isempty(path), -EINVAL);
+
+ return hwdb_new(path, ret);
+}
+
+_public_ int sd_hwdb_new(sd_hwdb **ret) {
+ return hwdb_new(NULL, ret);
+}
+
static sd_hwdb *hwdb_free(sd_hwdb *hwdb) {
assert(hwdb);