]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd/sd-hwdb/hwdb-util.c
util-lib: split out all temporary file related calls into tmpfiles-util.c
[thirdparty/systemd.git] / src / libsystemd / sd-hwdb / hwdb-util.c
index 2b6f7fa4a48d7fb33401007b3e8e4f536763ad95..c5c329f2ac6c7538172a7541059188cff40b70db 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <ctype.h>
+#include <stdio.h>
 
 #include "alloc-util.h"
 #include "conf-files.h"
@@ -15,6 +16,7 @@
 #include "strbuf.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 
 static const char *default_hwdb_bin_dir = "/etc/udev";
 static const char * const conf_file_dirs[] = {
@@ -132,7 +134,7 @@ static int trie_values_cmp(const struct trie_value_entry *a, const struct trie_v
 static int trie_node_add_value(struct trie *trie, struct trie_node *node,
                                const char *key, const char *value,
                                const char *filename, uint16_t file_priority, uint32_t line_number, bool compat) {
-        ssize_t k, v, fn;
+        ssize_t k, v, fn = 0;
         struct trie_value_entry *val;
 
         k = strbuf_add_string(trie->strings, key, strlen(key));
@@ -172,11 +174,13 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
                 return -ENOMEM;
         trie->values_count++;
         node->values = val;
-        node->values[node->values_count].key_off = k;
-        node->values[node->values_count].value_off = v;
-        node->values[node->values_count].filename_off = fn;
-        node->values[node->values_count].file_priority = file_priority;
-        node->values[node->values_count].line_number = line_number;
+        node->values[node->values_count] = (struct trie_value_entry) {
+                .key_off = k,
+                .value_off = v,
+                .filename_off = fn,
+                .file_priority = file_priority,
+                .line_number = line_number,
+        };
         node->values_count++;
         typesafe_qsort_r(node->values, node->values_count, trie_values_cmp, trie);
         return 0;
@@ -202,16 +206,18 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
                                 continue;
 
                         /* split node */
-                        new_child = new0(struct trie_node, 1);
+                        new_child = new(struct trie_node, 1);
                         if (!new_child)
                                 return -ENOMEM;
 
                         /* move values from parent to child */
-                        new_child->prefix_off = node->prefix_off + p+1;
-                        new_child->children = node->children;
-                        new_child->children_count = node->children_count;
-                        new_child->values = node->values;
-                        new_child->values_count = node->values_count;
+                        *new_child = (struct trie_node) {
+                                .prefix_off = node->prefix_off + p+1,
+                                .children = node->children,
+                                .children_count = node->children_count,
+                                .values = node->values,
+                                .values_count = node->values_count,
+                        };
 
                         /* update parent; use strdup() because the source gets realloc()d */
                         s = strndup(trie->strings->buf + node->prefix_off, p);
@@ -222,11 +228,9 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
                         if (off < 0)
                                 return off;
 
-                        node->prefix_off = off;
-                        node->children = NULL;
-                        node->children_count = 0;
-                        node->values = NULL;
-                        node->values_count = 0;
+                        *node = (struct trie_node) {
+                                .prefix_off = off,
+                        };
                         r = node_add_child(trie, node, new_child, c);
                         if (r < 0)
                                 return r;
@@ -246,7 +250,7 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
                         ssize_t off;
 
                         /* new child */
-                        new_child = new0(struct trie_node, 1);
+                        new_child = new(struct trie_node, 1);
                         if (!new_child)
                                 return -ENOMEM;
 
@@ -254,7 +258,10 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
                         if (off < 0)
                                 return off;
 
-                        new_child->prefix_off = off;
+                        *new_child = (struct trie_node) {
+                                .prefix_off = off,
+                        };
+
                         r = node_add_child(trie, node, new_child, c);
                         if (r < 0)
                                 return r;
@@ -466,7 +473,6 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
                 HW_DATA,
         } state = HW_NONE;
         _cleanup_fclose_ FILE *f = NULL;
-        char line[LINE_MAX];
         _cleanup_strv_free_ char **match_list = NULL;
         uint32_t line_number = 0;
         char *match = NULL;
@@ -476,10 +482,17 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
         if (!f)
                 return -errno;
 
-        while (fgets(line, sizeof(line), f)) {
+        for (;;) {
+                _cleanup_free_ char *line = NULL;
                 size_t len;
                 char *pos;
 
+                r = read_line(f, LONG_LINE_MAX, &line);
+                if (r < 0)
+                        return r;
+                if (r == 0)
+                        break;
+
                 ++line_number;
 
                 /* comment line */
@@ -654,3 +667,20 @@ int hwdb_update(const char *root, const char *hwdb_bin_dir, bool strict, bool co
 
         return r;
 }
+
+int hwdb_query(const char *modalias) {
+        _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
+        const char *key, *value;
+        int r;
+
+        assert(modalias);
+
+        r = sd_hwdb_new(&hwdb);
+        if (r < 0)
+                return r;
+
+        SD_HWDB_FOREACH_PROPERTY(hwdb, modalias, key, value)
+                printf("%s=%s\n", key, value);
+
+        return 0;
+}