summaryrefslogtreecommitdiff
path: root/udev
diff options
context:
space:
mode:
authorKay Sievers <kay.sievers@vrfy.org>2008-12-29 07:42:19 +0100
committerKay Sievers <kay.sievers@vrfy.org>2008-12-29 07:42:19 +0100
commitf089350234e39b868a5e3df71a8f8c036aaae4fd (patch)
tree275b34cfa62c11a1b9f4aaaf1ab81872b9d84982 /udev
parent109d114578a785c33fae01af2bea4ea88dda3def (diff)
libudev: enumerate - add lookup by property
Diffstat (limited to 'udev')
-rw-r--r--udev/lib/exported_symbols1
-rw-r--r--udev/lib/libudev-enumerate.c62
-rw-r--r--udev/lib/libudev.h1
-rw-r--r--udev/lib/test-libudev.c9
4 files changed, 73 insertions, 0 deletions
diff --git a/udev/lib/exported_symbols b/udev/lib/exported_symbols
index abb6ae3a57..a2f0ca7cb3 100644
--- a/udev/lib/exported_symbols
+++ b/udev/lib/exported_symbols
@@ -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
diff --git a/udev/lib/libudev-enumerate.c b/udev/lib/libudev-enumerate.c
index 618ffbc3e9..abc89d5cab 100644
--- a/udev/lib/libudev-enumerate.c
+++ b/udev/lib/libudev-enumerate.c
@@ -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);
diff --git a/udev/lib/libudev.h b/udev/lib/libudev.h
index 64746ad7c8..a9e399c4b4 100644
--- a/udev/lib/libudev.h
+++ b/udev/lib/libudev.h
@@ -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);
diff --git a/udev/lib/test-libudev.c b/udev/lib/test-libudev.c
index 4c3bc16aed..5120626520 100644
--- a/udev/lib/test-libudev.c
+++ b/udev/lib/test-libudev.c
@@ -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;
}