chiark / gitweb /
libudev: import hwdb and export lookup interface
[elogind.git] / src / udev / udevadm-hwdb.c
index 52fe1d4ebc5c5fb45dfa2526a334dc0b0142e741..5587094e630533ca0deaaea475416ebf8831541b 100644 (file)
@@ -27,7 +27,7 @@
 #include "conf-files.h"
 
 #include "udev.h"
-#include "udev-hwdb.h"
+#include "libudev-hwdb-def.h"
 
 /*
  * Generic udev properties, key/value database based on modalias strings.
@@ -35,7 +35,7 @@
  */
 
 static const char * const conf_file_dirs[] = {
-        SYSCONFDIR "/udev/hwdb.d",
+        "/etc/udev/hwdb.d",
         UDEVLIBEXECDIR "/hwdb.d",
         NULL
 };
@@ -135,19 +135,28 @@ static int trie_values_cmp(const void *v1, const void *v2, void *arg) {
 
 static int trie_node_add_value(struct trie *trie, struct trie_node *node,
                           const char *key, const char *value) {
-        size_t k, v;
+        ssize_t k, v;
         struct trie_value_entry *val;
-        struct trie_value_entry search;
 
         k = strbuf_add_string(trie->strings, key, strlen(key));
+        if (k < 0)
+                return k;
         v = strbuf_add_string(trie->strings, value, strlen(value));
+        if (v < 0)
+                return v;
 
-        /* replace existing earlier key with new value */
-        search.value_off = k;
-        val = xbsearch_r(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp, trie);
-        if (val) {
-                val->value_off = v;
-                return 0;
+        if (node->values_count) {
+                struct trie_value_entry search = {
+                        .key_off = k,
+                        .value_off = v,
+                };
+
+                val = xbsearch_r(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp, trie);
+                if (val) {
+                        /* replace existing earlier key with new value */
+                        val->value_off = v;
+                        return 0;
+                }
         }
 
         /* extend array, add new entry, sort for bisection */
@@ -282,7 +291,7 @@ static int64_t trie_store_nodes(struct trie_f *trie, struct trie_node *node) {
                 .children_count = node->children_count,
                 .values_count = htole64(node->values_count),
         };
-        struct trie_child_entry_f *children;
+        struct trie_child_entry_f *children = NULL;
         int64_t node_off;
 
         if (node->children_count) {
@@ -381,12 +390,15 @@ static int trie_store(struct trie *trie, const char *filename) {
         }
 
         log_debug("=== trie on-disk ===\n");
-        log_debug("size:             %8zi bytes\n", size);
+        log_debug("size:             %8llu bytes\n", (unsigned long long)size);
         log_debug("header:           %8zu bytes\n", sizeof(struct trie_header_f));
-        log_debug("nodes:            %8zu bytes (%8zi)\n", t.nodes_count * sizeof(struct trie_node_f), t.nodes_count);
-        log_debug("child pointers:   %8zu bytes (%8zi)\n", t.children_count * sizeof(struct trie_child_entry_f), t.children_count);
-        log_debug("value pointers:   %8zu bytes (%8zi)\n", t.values_count * sizeof(struct trie_value_entry_f), t.values_count);
-        log_debug("string store:     %8zu bytes\n", trie->strings->len);
+        log_debug("nodes:            %8llu bytes (%8llu)\n",
+                  (unsigned long long)t.nodes_count * sizeof(struct trie_node_f), (unsigned long long)t.nodes_count);
+        log_debug("child pointers:   %8llu bytes (%8llu)\n",
+                  (unsigned long long)t.children_count * sizeof(struct trie_child_entry_f), (unsigned long long)t.children_count);
+        log_debug("value pointers:   %8llu bytes (%8llu)\n",
+                  (unsigned long long)t.values_count * sizeof(struct trie_value_entry_f), (unsigned long long)t.values_count);
+        log_debug("string store:     %8llu bytes\n", (unsigned long long)trie->strings->len);
         log_debug("strings start:    %8llu\n", (unsigned long long) t.strings_off);
 out:
         free(filename_tmp);
@@ -397,12 +409,14 @@ static int import_file(struct trie *trie, const char *filename) {
         FILE *f;
         char line[LINE_MAX];
         char match[LINE_MAX];
+        char cond[LINE_MAX];
 
         f = fopen(filename, "re");
         if (f == NULL)
                 return -errno;
 
         match[0] = '\0';
+        cond[0] = '\0';
         while (fgets(line, sizeof(line), f)) {
                 size_t len;
 
@@ -412,6 +426,7 @@ static int import_file(struct trie *trie, const char *filename) {
                 /* new line, new record */
                 if (line[0] == '\n') {
                         match[0] = '\0';
+                        cond[0] = '\0';
                         continue;
                 }
 
@@ -424,9 +439,19 @@ static int import_file(struct trie *trie, const char *filename) {
                 /* start of new record */
                 if (match[0] == '\0') {
                         strcpy(match, line);
+                        cond[0] = '\0';
                         continue;
                 }
 
+                if (line[0] == '+') {
+                        strcpy(cond, line);
+                        continue;
+                }
+
+                /* TODO: support +; skip the entire record until we support it */
+                if (cond[0] != '\0')
+                        continue;
+
                 /* value lines */
                 if (line[0] == ' ') {
                         char *value;
@@ -526,13 +551,12 @@ static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
         log_debug("strings incoming: %8zu bytes (%8zu)\n", trie->strings->in_len, trie->strings->in_count);
         log_debug("strings dedup'ed: %8zu bytes (%8zu)\n", trie->strings->dedup_len, trie->strings->dedup_count);
 
-        mkdir_parents(SYSCONFDIR "/udev/hwdb.bin", 0755);
-        err = trie_store(trie, SYSCONFDIR "/udev/hwdb.bin");
+        mkdir_parents("/etc/udev/hwdb.bin", 0755);
+        err = trie_store(trie, "/etc/udev/hwdb.bin");
         if (err < 0) {
-                log_error("Failure writing hardware database '%s': %s", SYSCONFDIR "/udev/hwdb.bin", strerror(-err));
+                log_error("Failure writing hardware database '%s': %s", "/etc/udev/hwdb.bin", strerror(-err));
                 rc = EXIT_FAILURE;
         }
-
 out:
         if (trie->root)
                 trie_node_cleanup(trie->root);