chiark / gitweb /
hwdb: add --device=<device-id> and --filter=<key name glob>
authorKay Sievers <kay@vrfy.org>
Mon, 1 Jul 2013 20:34:43 +0000 (22:34 +0200)
committerKay Sievers <kay@vrfy.org>
Mon, 1 Jul 2013 20:34:43 +0000 (22:34 +0200)
src/libudev/libudev-device.c
src/libudev/libudev.h
src/udev/udev-builtin-hwdb.c
src/udev/udev-builtin-net_id.c
src/udev/udev.h

index 6bb2e41..eb43668 100644 (file)
@@ -780,7 +780,7 @@ _public_ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char
  *
  * Returns: a new udev device, or #NULL, if it does not exist
  **/
-_public_ struct udev_device *udev_device_new_from_device_id(struct udev *udev, char *id)
+_public_ struct udev_device *udev_device_new_from_device_id(struct udev *udev, const char *id)
 {
         char type;
         int maj, min;
index 61567b1..b9b8f13 100644 (file)
@@ -81,7 +81,7 @@ struct udev *udev_device_get_udev(struct udev_device *udev_device);
 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath);
 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum);
 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname);
-struct udev_device *udev_device_new_from_device_id(struct udev *udev, char *id);
+struct udev_device *udev_device_new_from_device_id(struct udev *udev, const char *id);
 struct udev_device *udev_device_new_from_environment(struct udev *udev);
 /* udev_device_get_parent_*() does not take a reference on the returned device, it is automatically unref'd with the parent */
 struct udev_device *udev_device_get_parent(struct udev_device *udev_device);
index 0b35d79..9895f65 100644 (file)
 #include <inttypes.h>
 #include <ctype.h>
 #include <stdlib.h>
+#include <fnmatch.h>
 #include <getopt.h>
 
 #include "udev.h"
 
 static struct udev_hwdb *hwdb;
 
-int udev_builtin_hwdb_lookup(struct udev_device *dev, const char *modalias, bool test) {
+int udev_builtin_hwdb_lookup(struct udev_device *dev,
+                             const char *modalias, const char *filter, bool test) {
         struct udev_list_entry *entry;
         int n = 0;
 
@@ -37,6 +39,9 @@ int udev_builtin_hwdb_lookup(struct udev_device *dev, const char *modalias, bool
                 return -ENOENT;
 
         udev_list_entry_foreach(entry, udev_hwdb_get_properties_list_entry(hwdb, modalias, 0)) {
+                if (filter && fnmatch(filter, udev_list_entry_get_name(entry), FNM_NOESCAPE) != 0)
+                        continue;
+
                 if (udev_builtin_add_property(dev, test,
                                               udev_list_entry_get_name(entry),
                                               udev_list_entry_get_value(entry)) < 0)
@@ -66,12 +71,13 @@ static const char *modalias_usb(struct udev_device *dev, char *s, size_t size) {
         return s;
 }
 
-static int udev_builtin_hwdb_search(struct udev_device *dev, const char *subsystem, bool test) {
+static int udev_builtin_hwdb_search(struct udev_device *dev, struct udev_device *srcdev,
+                                    const char *subsystem, const char *filter, bool test) {
         struct udev_device *d;
         char s[16];
         int n = 0;
 
-        for (d = dev; d; d = udev_device_get_parent(d)) {
+        for (d = srcdev; d; d = udev_device_get_parent(d)) {
                 const char *dsubsys;
                 const char *modalias = NULL;
 
@@ -85,14 +91,15 @@ static int udev_builtin_hwdb_search(struct udev_device *dev, const char *subsyst
 
                 /* the usb_device does not have a modalias, compose one */
                 if (streq(dsubsys, "usb"))
-                        modalias = modalias_usb(dev, s, sizeof(s));
+                        modalias = modalias_usb(d, s, sizeof(s));
 
                 if (!modalias)
                         modalias = udev_device_get_property_value(d, "MODALIAS");
 
                 if (!modalias)
                         continue;
-                n = udev_builtin_hwdb_lookup(dev, modalias, test);
+
+                n = udev_builtin_hwdb_lookup(dev, modalias, filter, test);
                 if (n > 0)
                         break;
         }
@@ -102,10 +109,15 @@ static int udev_builtin_hwdb_search(struct udev_device *dev, const char *subsyst
 
 static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool test) {
         static const struct option options[] = {
+                { "filter", required_argument, NULL, 'f' },
+                { "device", required_argument, NULL, 'd' },
                 { "subsystem", required_argument, NULL, 's' },
                 {}
         };
+        const char *filter = NULL;
+        const char *device = NULL;
         const char *subsystem = NULL;
+        struct udev_device *srcdev;
 
         if (!hwdb)
                 return EXIT_FAILURE;
@@ -118,13 +130,29 @@ static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool te
                         break;
 
                 switch (option) {
+                case 'f':
+                        filter = optarg;
+                        break;
+
+                case 'd':
+                        device = optarg;
+                        break;
+
                 case 's':
                         subsystem = optarg;
                         break;
                 }
         }
 
-        if (udev_builtin_hwdb_search(dev, subsystem, test) < 0)
+        /* read data from another device than the device we will store the data */
+        if (device) {
+                srcdev = udev_device_new_from_device_id(udev_device_get_udev(dev), device);
+                if (!srcdev)
+                        return EXIT_FAILURE;
+        } else
+                srcdev = dev;
+
+        if (udev_builtin_hwdb_search(dev, srcdev, subsystem, filter, test) < 0)
                 return EXIT_FAILURE;
         return EXIT_SUCCESS;
 }
index 5b732bb..e1d8087 100644 (file)
@@ -396,7 +396,7 @@ static int ieee_oui(struct udev_device *dev, struct netnames *names, bool test)
         snprintf(str, sizeof(str), "OUI:%02X%02X%02X%02X%02X%02X",
                  names->mac[0], names->mac[1], names->mac[2],
                  names->mac[3], names->mac[4], names->mac[5]);
-        udev_builtin_hwdb_lookup(dev, str, test);
+        udev_builtin_hwdb_lookup(dev, str, NULL, test);
         return 0;
 }
 
index caec5f0..d5f8cd7 100644 (file)
@@ -190,7 +190,7 @@ int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const c
 void udev_builtin_list(struct udev *udev);
 bool udev_builtin_validate(struct udev *udev);
 int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val);
-int udev_builtin_hwdb_lookup(struct udev_device *dev, const char *modalias, bool test);
+int udev_builtin_hwdb_lookup(struct udev_device *dev, const char *modalias, const char *filter, bool test);
 
 /* udev logging */
 void udev_main_log(struct udev *udev, int priority,