diff options
author | Tom Gundersen <teg@jklm.no> | 2014-11-11 21:42:04 +0100 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2014-12-11 13:54:18 +0100 |
commit | c335388a56173bfcd0e4f6c43742eeaf29cd457a (patch) | |
tree | 7056413298d876f10496230d2ded8079ddc1ea16 | |
parent | 5b6c2d709fe07e4235256875ff5acc76a66bf427 (diff) |
sd-device: add sd_device_get_property_{first,next}
-rw-r--r-- | src/libsystemd/sd-device/device-util.h | 5 | ||||
-rw-r--r-- | src/libsystemd/sd-device/sd-device.c | 38 | ||||
-rw-r--r-- | src/systemd/sd-device.h | 3 |
3 files changed, 46 insertions, 0 deletions
diff --git a/src/libsystemd/sd-device/device-util.h b/src/libsystemd/sd-device/device-util.h index 87cda99f9..6096f72a7 100644 --- a/src/libsystemd/sd-device/device-util.h +++ b/src/libsystemd/sd-device/device-util.h @@ -26,3 +26,8 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(sd_device*, sd_device_unref); #define _cleanup_device_unref_ _cleanup_(sd_device_unrefp) + +#define FOREACH_DEVICE_PROPERTY(device, key, value) \ + for (key = sd_device_get_property_first(device, &(value)); \ + key; \ + key = sd_device_get_property_next(device, &(value))) diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 70fed11c4..90ac5d694 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -41,6 +41,8 @@ struct sd_device { sd_device *parent; OrderedHashmap *properties; + Iterator properties_iterator; + bool properties_modified; Hashmap *sysattrs; Set *tags; @@ -142,6 +144,7 @@ static int device_add_property(sd_device *device, const char *_key, const char * r = ordered_hashmap_replace(device->properties, key, value); if (r < 0) return r; + device->properties_modified = true; key = NULL; value = NULL; @@ -150,6 +153,7 @@ static int device_add_property(sd_device *device, const char *_key, const char * _cleanup_free_ char *value = NULL; value = ordered_hashmap_remove2(device->properties, _key, (void**) &key); + device->properties_modified = true; } return 0; @@ -1243,6 +1247,40 @@ _public_ int sd_device_get_property_value(sd_device *device, const char *key, co return 0; } +_public_ const char *sd_device_get_property_first(sd_device *device, const char **_value) { + const char *key; + const char *value; + + assert_return(device, NULL); + + device->properties_modified = false; + device->properties_iterator = ITERATOR_FIRST; + + value = ordered_hashmap_iterate(device->properties, &device->properties_iterator, (const void**)&key); + + if (_value) + *_value = value; + + return key; +} + +_public_ const char *sd_device_get_property_next(sd_device *device, const char **_value) { + const char *key; + const char *value; + + assert_return(device, NULL); + + if (device->properties_modified) + return NULL; + + value = ordered_hashmap_iterate(device->properties, &device->properties_iterator, (const void**)&key); + + if (_value) + *_value = value; + + return key; +} + _public_ int sd_device_has_tag(sd_device *device, const char *tag, int *has_tag) { assert_return(device, -EINVAL); assert_return(tag, -EINVAL); diff --git a/src/systemd/sd-device.h b/src/systemd/sd-device.h index 15df05e84..b8997e1f2 100644 --- a/src/systemd/sd-device.h +++ b/src/systemd/sd-device.h @@ -54,6 +54,9 @@ int sd_device_get_property_value(sd_device *device, const char *key, const char int sd_device_has_tag(sd_device *device, const char *tag, int *has_tag); int sd_device_get_sysattr_value(sd_device *device, const char *sysattr, const char **_value); +const char *sd_device_get_property_first(sd_device *device, const char **value); +const char *sd_device_get_property_next(sd_device *device, const char **value); + _SD_END_DECLARATIONS; #endif |