]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd/sd-device/device-enumerator.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / libsystemd / sd-device / device-enumerator.c
index d59da7d88f380e7f6776fe15cfa204e5001416b7..9379209a583abc87d3acefb4ee742cc3842dd264 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include "util.h"
-#include "prioq.h"
-#include "strv.h"
-#include "set.h"
-
 #include "sd-device.h"
 
-#include "device-util.h"
 #include "device-enumerator-private.h"
+#include "device-util.h"
+#include "prioq.h"
+#include "set.h"
+#include "string-util.h"
+#include "strv.h"
+#include "util.h"
 
 #define DEVICE_ENUMERATE_MAX_DEPTH 256
 
@@ -137,7 +137,6 @@ _public_ int sd_device_enumerator_add_match_sysattr(sd_device_enumerator *enumer
 
         assert_return(enumerator, -EINVAL);
         assert_return(_sysattr, -EINVAL);
-        assert_return(_value, -EINVAL);
 
         if (match)
                 hashmap = &enumerator->match_sysattr;
@@ -152,9 +151,11 @@ _public_ int sd_device_enumerator_add_match_sysattr(sd_device_enumerator *enumer
         if (!sysattr)
                 return -ENOMEM;
 
-        value = strdup(_value);
-        if (!value)
-                return -ENOMEM;
+        if (_value) {
+                value = strdup(_value);
+                if (!value)
+                        return -ENOMEM;
+        }
 
         r = hashmap_put(*hashmap, sysattr, value);
         if (r < 0)
@@ -174,7 +175,6 @@ _public_ int sd_device_enumerator_add_match_property(sd_device_enumerator *enume
 
         assert_return(enumerator, -EINVAL);
         assert_return(_property, -EINVAL);
-        assert_return(_value, -EINVAL);
 
         r = hashmap_ensure_allocated(&enumerator->match_property, NULL);
         if (r < 0)
@@ -184,9 +184,11 @@ _public_ int sd_device_enumerator_add_match_property(sd_device_enumerator *enume
         if (!property)
                 return -ENOMEM;
 
-        value = strdup(_value);
-        if (!value)
-                return -ENOMEM;
+        if (_value) {
+                value = strdup(_value);
+                if (!value)
+                        return -ENOMEM;
+        }
 
         r = hashmap_put(enumerator->match_property, property, value);
         if (r < 0)
@@ -273,7 +275,7 @@ int device_enumerator_add_match_is_initialized(sd_device_enumerator *enumerator)
 static int device_compare(const void *_a, const void *_b) {
         sd_device *a = (sd_device *)_a, *b = (sd_device *)_b;
         const char *devpath_a, *devpath_b, *sound_a;
-        bool delay_a = false, delay_b = false;
+        bool delay_a, delay_b;
 
         assert_se(sd_device_get_devpath(a, &devpath_a) >= 0);
         assert_se(sd_device_get_devpath(b, &devpath_b) >= 0);
@@ -312,17 +314,10 @@ static int device_compare(const void *_a, const void *_b) {
         }
 
         /* md and dm devices are enumerated after all other devices */
-        if (strstr(devpath_a, "/block/md") || strstr(devpath_a, "/block/dm-"))
-                delay_a = true;
-
-        if (strstr(devpath_b, "/block/md") || strstr(devpath_b, "/block/dm-"))
-                delay_b = true;
-
-        if (delay_a && !delay_b)
-                return 1;
-
-        if (!delay_a && delay_b)
-                return -1;
+        delay_a = strstr(devpath_a, "/block/md") || strstr(devpath_a, "/block/dm-");
+        delay_b = strstr(devpath_b, "/block/md") || strstr(devpath_b, "/block/dm-");
+        if (delay_a != delay_b)
+                return delay_a - delay_b;
 
         return strcmp(devpath_a, devpath_b);
 }
@@ -374,11 +369,11 @@ static bool match_sysattr(sd_device_enumerator *enumerator, sd_device *device) {
         assert(enumerator);
         assert(device);
 
-        HASHMAP_FOREACH_KEY(sysattr, value, enumerator->nomatch_sysattr, i)
+        HASHMAP_FOREACH_KEY(value, sysattr, enumerator->nomatch_sysattr, i)
                 if (match_sysattr_value(device, sysattr, value))
                         return false;
 
-        HASHMAP_FOREACH_KEY(sysattr, value, enumerator->match_sysattr, i)
+        HASHMAP_FOREACH_KEY(value, sysattr, enumerator->match_sysattr, i)
                 if (!match_sysattr_value(device, sysattr, value))
                         return false;
 
@@ -396,7 +391,7 @@ static bool match_property(sd_device_enumerator *enumerator, sd_device *device)
         if (hashmap_isempty(enumerator->match_property))
                 return true;
 
-        HASHMAP_FOREACH_KEY(property, value, enumerator->match_property, i) {
+        HASHMAP_FOREACH_KEY(value, property, enumerator->match_property, i) {
                 const char *property_dev, *value_dev;
 
                 FOREACH_DEVICE_PROPERTY(device, property_dev, value_dev) {
@@ -724,6 +719,8 @@ static int parent_add_child(sd_device_enumerator *enumerator, const char *path)
                 return r;
 
         r = sd_device_get_subsystem(device, &subsystem);
+        if (r == -ENOENT)
+                return 0;
         if (r < 0)
                 return r;
 
@@ -771,9 +768,9 @@ static int parent_crawl_children(sd_device_enumerator *enumerator, const char *p
                 if (dent->d_type != DT_DIR)
                         continue;
 
-                k = asprintf(&child, "%s/%s", path, dent->d_name);
-                if (k < 0)
-                        return -errno;
+                child = strjoin(path, "/", dent->d_name, NULL);
+                if (!child)
+                        return -ENOMEM;
 
                 k = parent_add_child(enumerator, child);
                 if (k < 0)
@@ -815,10 +812,8 @@ static int enumerator_scan_devices_all(sd_device_enumerator *enumerator) {
         if (access("/sys/subsystem", F_OK) >= 0) {
                 /* we have /subsystem/, forget all the old stuff */
                 r = enumerator_scan_dir(enumerator, "subsystem", "devices", NULL);
-                if (r < 0) {
-                        log_debug("device-enumerator: failed to scan /sys/subsystem: %s", strerror(-r));
-                        return r;
-                }
+                if (r < 0)
+                        return log_debug_errno(r, "device-enumerator: failed to scan /sys/subsystem: %m");
         } else {
                 int k;