chiark / gitweb /
remove unused variable
[elogind.git] / src / libudev / libudev-hwdb.c
index 92c58b61a2594a9592620ca496e655e4fd6057a5..ff34e5a2cd75a313395da9964216825477c948a9 100644 (file)
@@ -1,7 +1,7 @@
 /***
   This file is part of systemd.
 
-  Copyright 2012 Kay Sievers <kay.sievers@vrfy.org>
+  Copyright 2012 Kay Sievers <kay@vrfy.org>
   Copyright 2008 Alan Jenkins <alan.christopher.jenkins@googlemail.com>
 
   systemd is free software; you can redistribute it and/or modify it
@@ -35,7 +35,7 @@
  * SECTION:libudev-hwdb
  * @short_description: retrieve properties from the hardware database
  *
- * Libuded hardware database interface.
+ * Libudev hardware database interface.
  */
 
 /**
@@ -140,9 +140,13 @@ static const struct trie_node_f *node_lookup_f(struct udev_hwdb *hwdb, const str
 }
 
 static int hwdb_add_property(struct udev_hwdb *hwdb, const char *key, const char *value) {
-        /* TODO: add sub-matches (+) against DMI data */
+        /*
+         * Silently ignore all properties which do not start with a
+         * space; future extensions might use additional prefixes.
+         */
         if (key[0] != ' ')
                 return 0;
+
         if (udev_list_entry_add(&hwdb->properties_list, key+1, value) == NULL)
                 return -ENOMEM;
         return 0;
@@ -169,8 +173,8 @@ static int trie_fnmatch_f(struct udev_hwdb *hwdb, const struct trie_node_f *node
                 linebuf_rem_char(buf);
         }
 
-        if (node->values_count && fnmatch(linebuf_get(buf), search, 0) == 0)
-                for (i = 0; i < node->values_count; i++) {
+        if (le64toh(node->values_count) && fnmatch(linebuf_get(buf), search, 0) == 0)
+                for (i = 0; i < le64toh(node->values_count); i++) {
                         err = hwdb_add_property(hwdb, trie_string(hwdb, trie_node_values(hwdb, node)[i].key_off),
                                                 trie_string(hwdb, trie_node_values(hwdb, node)[i].value_off));
                         if (err < 0)
@@ -236,7 +240,7 @@ static int trie_search_f(struct udev_hwdb *hwdb, const char *search) {
                 if (search[i] == '\0') {
                         size_t n;
 
-                        for (n = 0; n < node->values_count; n++) {
+                        for (n = 0; n < le64toh(node->values_count); n++) {
                                 err = hwdb_add_property(hwdb, trie_string(hwdb, trie_node_values(hwdb, node)[n].key_off),
                                                         trie_string(hwdb, trie_node_values(hwdb, node)[n].value_off));
                                 if (err < 0)
@@ -273,38 +277,38 @@ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
 
         hwdb->f = fopen("/etc/udev/hwdb.bin", "re");
         if (!hwdb->f) {
-                log_debug("error reading /etc/udev/hwdb.bin: %m");
+                udev_dbg(udev, "error reading /etc/udev/hwdb.bin: %m");
                 udev_hwdb_unref(hwdb);
                 return NULL;
         }
 
         if (fstat(fileno(hwdb->f), &hwdb->st) < 0 ||
             (size_t)hwdb->st.st_size < offsetof(struct trie_header_f, strings_len) + 8) {
-                log_debug("error reading /etc/udev/hwdb.bin: %m");
+                udev_dbg(udev, "error reading /etc/udev/hwdb.bin: %m");
                 udev_hwdb_unref(hwdb);
                 return NULL;
         }
 
         hwdb->map = mmap(0, hwdb->st.st_size, PROT_READ, MAP_SHARED, fileno(hwdb->f), 0);
         if (hwdb->map == MAP_FAILED) {
-                log_debug("error mapping /etc/udev/hwdb.bin: %m");
+                udev_dbg(udev, "error mapping /etc/udev/hwdb.bin: %m");
                 udev_hwdb_unref(hwdb);
                 return NULL;
         }
 
         if (memcmp(hwdb->map, sig, sizeof(hwdb->head->signature)) != 0 ||
             (size_t)hwdb->st.st_size != le64toh(hwdb->head->file_size)) {
-                log_debug("error recognizing the format of /etc/udev/hwdb.bin");
+                udev_dbg(udev, "error recognizing the format of /etc/udev/hwdb.bin");
                 udev_hwdb_unref(hwdb);
                 return NULL;
         }
 
-        log_debug("=== trie on-disk ===\n");
-        log_debug("tool version:          %llu", (unsigned long long)le64toh(hwdb->head->tool_version));
-        log_debug("file size:        %8llu bytes\n", (unsigned long long)hwdb->st.st_size);
-        log_debug("header size       %8llu bytes\n", (unsigned long long)le64toh(hwdb->head->header_size));
-        log_debug("strings           %8llu bytes\n", (unsigned long long)le64toh(hwdb->head->strings_len));
-        log_debug("nodes             %8llu bytes\n", (unsigned long long)le64toh(hwdb->head->nodes_len));
+        udev_dbg(udev, "=== trie on-disk ===\n");
+        udev_dbg(udev, "tool version:          %"PRIu64, le64toh(hwdb->head->tool_version));
+        udev_dbg(udev, "file size:        %8llu bytes\n", (unsigned long long) hwdb->st.st_size);
+        udev_dbg(udev, "header size       %8"PRIu64" bytes\n", le64toh(hwdb->head->header_size));
+        udev_dbg(udev, "strings           %8"PRIu64" bytes\n", le64toh(hwdb->head->strings_len));
+        udev_dbg(udev, "nodes             %8"PRIu64" bytes\n", le64toh(hwdb->head->nodes_len));
         return hwdb;
 }
 
@@ -330,18 +334,18 @@ _public_ struct udev_hwdb *udev_hwdb_ref(struct udev_hwdb *hwdb) {
  * Drop a reference of a hwdb context. If the refcount reaches zero,
  * all resources of the hwdb context will be released.
  *
- * Returns: the passed hwdb context if it has still an active reference, or #NULL otherwise.
+ * Returns: #NULL
  **/
 _public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) {
         if (!hwdb)
                 return NULL;
         hwdb->refcount--;
         if (hwdb->refcount > 0)
-                return hwdb;
-        if (hwdb->f)
-                fclose(hwdb->f);
+                return NULL;
         if (hwdb->map)
                 munmap((void *)hwdb->map, hwdb->st.st_size);
+        if (hwdb->f)
+                fclose(hwdb->f);
         udev_list_cleanup(&hwdb->properties_list);
         free(hwdb);
         return NULL;
@@ -354,9 +358,9 @@ bool udev_hwdb_validate(struct udev_hwdb *hwdb) {
                 return false;
         if (!hwdb->f)
                 return false;
-        if (fstat(fileno(hwdb->f), &st) < 0)
+        if (stat("/etc/udev/hwdb.bin", &st) < 0)
                 return true;
-        if (ts_usec(&hwdb->st.st_mtim) != ts_usec(&st.st_mtim))
+        if (timespec_load(&hwdb->st.st_mtim) != timespec_load(&st.st_mtim))
                 return true;
         return false;
 }
@@ -377,11 +381,12 @@ bool udev_hwdb_validate(struct udev_hwdb *hwdb) {
 _public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned int flags) {
         int err;
 
-        if (!hwdb->f) {
+        if (!hwdb || !hwdb->f) {
                 errno = EINVAL;
                 return NULL;
         }
 
+        udev_list_cleanup(&hwdb->properties_list);
         err = trie_search_f(hwdb, modalias);
         if (err < 0) {
                 errno = -err;