]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/device: minor refactors
authorMike Yuan <me@yhndnzj.com>
Sun, 21 Jul 2024 15:01:22 +0000 (17:01 +0200)
committerMike Yuan <me@yhndnzj.com>
Sun, 21 Jul 2024 20:48:14 +0000 (22:48 +0200)
src/core/device.c

index d8567676a75fa00fdd0e59a6f45336762adabf95..f8ded6c838e8c6a8423f6710ca985484cc187169 100644 (file)
@@ -54,8 +54,6 @@ static int device_by_path(Manager *m, const char *path, Unit **ret) {
 }
 
 static void device_unset_sysfs(Device *d) {
-        Hashmap *devices;
-
         assert(d);
 
         if (!d->sysfs)
@@ -63,7 +61,7 @@ static void device_unset_sysfs(Device *d) {
 
         /* Remove this unit from the chain of devices which share the same sysfs path. */
 
-        devices = UNIT(d)->manager->devices_by_sysfs;
+        Hashmap *devices = ASSERT_PTR(UNIT(d)->manager->devices_by_sysfs);
 
         if (d->same_sysfs_prev)
                 /* If this is not the first unit, then simply remove this unit. */
@@ -84,36 +82,35 @@ static void device_unset_sysfs(Device *d) {
 }
 
 static int device_set_sysfs(Device *d, const char *sysfs) {
-        _cleanup_free_ char *copy = NULL;
-        Device *first;
+        Unit *u = UNIT(ASSERT_PTR(d));
         int r;
 
-        assert(d);
-
         if (streq_ptr(d->sysfs, sysfs))
                 return 0;
 
-        r = hashmap_ensure_allocated(&UNIT(d)->manager->devices_by_sysfs, &path_hash_ops);
+        Hashmap **devices = &u->manager->devices_by_sysfs;
+
+        r = hashmap_ensure_allocated(devices, &path_hash_ops);
         if (r < 0)
                 return r;
 
-        copy = strdup(sysfs);
+        _cleanup_free_ char *copy = strdup(sysfs);
         if (!copy)
                 return -ENOMEM;
 
         device_unset_sysfs(d);
 
-        first = hashmap_get(UNIT(d)->manager->devices_by_sysfs, sysfs);
+        Device *first = hashmap_get(*devices, sysfs);
         LIST_PREPEND(same_sysfs, first, d);
 
-        r = hashmap_replace(UNIT(d)->manager->devices_by_sysfs, copy, first);
+        r = hashmap_replace(*devices, copy, first);
         if (r < 0) {
                 LIST_REMOVE(same_sysfs, first, d);
                 return r;
         }
 
         d->sysfs = TAKE_PTR(copy);
-        unit_add_to_dbus_queue(UNIT(d));
+        unit_add_to_dbus_queue(u);
 
         return 0;
 }
@@ -350,13 +347,14 @@ static const struct {
 static int device_found_to_string_many(DeviceFound flags, char **ret) {
         _cleanup_free_ char *s = NULL;
 
+        assert(flags >= 0);
         assert(ret);
 
-        for (size_t i = 0; i < ELEMENTSOF(device_found_map); i++) {
-                if (!FLAGS_SET(flags, device_found_map[i].flag))
+        FOREACH_ELEMENT(i, device_found_map) {
+                if (!FLAGS_SET(flags, i->flag))
                         continue;
 
-                if (!strextend_with_separator(&s, ",", device_found_map[i].name))
+                if (!strextend_with_separator(&s, ",", i->name))
                         return -ENOMEM;
         }