chiark / gitweb /
libudev: enumerate - add lookup by property
authorKay Sievers <kay.sievers@vrfy.org>
Mon, 29 Dec 2008 06:42:19 +0000 (07:42 +0100)
committerKay Sievers <kay.sievers@vrfy.org>
Mon, 29 Dec 2008 06:42:19 +0000 (07:42 +0100)
udev/lib/exported_symbols
udev/lib/libudev-enumerate.c
udev/lib/libudev.h
udev/lib/test-libudev.c

index abb6ae3..a2f0ca7 100644 (file)
@@ -42,6 +42,7 @@ udev_enumerate_add_match_subsystem
 udev_enumerate_add_nomatch_subsystem
 udev_enumerate_add_match_sysattr
 udev_enumerate_add_nomatch_sysattr
+udev_enumerate_add_match_property
 udev_enumerate_scan_devices
 udev_enumerate_scan_subsystems
 udev_enumerate_add_syspath
index 618ffbc..abc89d5 100644 (file)
@@ -39,6 +39,7 @@ struct udev_enumerate {
        struct udev_list_node sysattr_nomatch_list;
        struct udev_list_node subsystem_match_list;
        struct udev_list_node subsystem_nomatch_list;
+       struct udev_list_node properties_match_list;
        struct udev_list_node devices_list;
        int devices_sorted;
 };
@@ -63,6 +64,7 @@ struct udev_enumerate *udev_enumerate_new(struct udev *udev)
        udev_list_init(&udev_enumerate->sysattr_nomatch_list);
        udev_list_init(&udev_enumerate->subsystem_match_list);
        udev_list_init(&udev_enumerate->subsystem_nomatch_list);
+       udev_list_init(&udev_enumerate->properties_match_list);
        return udev_enumerate;
 }
 
@@ -86,6 +88,7 @@ void udev_enumerate_unref(struct udev_enumerate *udev_enumerate)
        udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->sysattr_nomatch_list);
        udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->subsystem_match_list);
        udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->subsystem_nomatch_list);
+       udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->properties_match_list);
        free(udev_enumerate);
 }
 
@@ -178,6 +181,18 @@ exit:
        return match;
 }
 
+int udev_enumerate_add_match_property(struct udev_enumerate *udev_enumerate, const char *property, const char *value)
+{
+       if (udev_enumerate == NULL)
+               return -EINVAL;
+       if (property == NULL)
+               return 0;
+       if (udev_list_entry_add(udev_enumerate_get_udev(udev_enumerate),
+                               &udev_enumerate->properties_match_list, property, value, 1, 0) == NULL)
+               return -ENOMEM;
+       return 0;
+}
+
 static int match_sysattr(struct udev_enumerate *udev_enumerate, const char *syspath)
 {
        struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
@@ -204,6 +219,51 @@ static int match_sysattr(struct udev_enumerate *udev_enumerate, const char *sysp
        return 1;
 }
 
+static int match_property(struct udev_enumerate *udev_enumerate, const char *syspath)
+{
+       struct udev_device *dev;
+       struct udev_list_entry *list_entry;
+       int match = 0;
+
+       /* no match always matches */
+       if (udev_list_get_entry(&udev_enumerate->properties_match_list) == NULL)
+               return 1;
+
+       /* no device does not match */
+       dev = udev_device_new_from_syspath(udev_enumerate->udev, syspath);
+       if (dev == NULL)
+               return 0;
+
+       /* loop over matches */
+       udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->properties_match_list)) {
+               struct udev_list_entry *property_entry;
+
+               /* loop over device properties */
+               udev_list_entry_foreach(property_entry, udev_device_get_properties_list_entry(dev)) {
+                       if (fnmatch(udev_list_entry_get_name(list_entry), udev_list_entry_get_name(property_entry), 0) == 0) {
+                               const char *match_value;
+                               const char *dev_value;
+
+                               match_value = udev_list_entry_get_value(list_entry);
+                               dev_value = udev_list_entry_get_value(property_entry);
+                               if (match_value == NULL && dev_value == NULL) {
+                                       match = 1;
+                                       goto out;
+                               }
+                               if (match_value == NULL || dev_value == NULL)
+                                       continue;
+                               if (fnmatch(match_value, dev_value, 0) == 0) {
+                                       match = 1;
+                                       goto out;
+                               }
+                       }
+               }
+       }
+out:
+       udev_device_unref(dev);
+       return match;
+}
+
 static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate,
                                    const char *basedir, const char *subdir1, const char *subdir2)
 {
@@ -248,6 +308,8 @@ static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate,
                        continue;
                if (!match_sysattr(udev_enumerate, syspath))
                        continue;
+               if (!match_property(udev_enumerate, syspath))
+                       continue;
                udev_list_entry_add(udev, &udev_enumerate->devices_list, syspath, NULL, 1, 1);
        }
        closedir(dir);
index 64746ad..a9e399c 100644 (file)
@@ -101,6 +101,7 @@ extern int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumer
 extern int udev_enumerate_add_nomatch_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem);
 extern int udev_enumerate_add_match_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value);
 extern int udev_enumerate_add_nomatch_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value);
+extern int udev_enumerate_add_match_property(struct udev_enumerate *udev_enumerate, const char *property, const char *value);
 extern int udev_enumerate_add_syspath(struct udev_enumerate *udev_enumerate, const char *syspath);
 extern int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate);
 extern int udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate);
index 4c3bc16..5120626 100644 (file)
@@ -359,6 +359,15 @@ static int test_enumerate(struct udev *udev, const char *subsystem)
        udev_enumerate_scan_subsystems(udev_enumerate);
        test_enumerate_print_list(udev_enumerate);
        udev_enumerate_unref(udev_enumerate);
+
+       printf("enumerate 'property IF_FS_*=filesystem'\n");
+       udev_enumerate = udev_enumerate_new(udev);
+       if (udev_enumerate == NULL)
+               return -1;
+       udev_enumerate_add_match_property(udev_enumerate, "ID_FS*", "filesystem");
+       udev_enumerate_scan_devices(udev_enumerate);
+       test_enumerate_print_list(udev_enumerate);
+       udev_enumerate_unref(udev_enumerate);
        return 0;
 }