]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: make device_get_cached_sysattr_value() distinguish if we have looked up...
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 15 Sep 2021 06:21:44 +0000 (15:21 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 17 Sep 2021 03:38:51 +0000 (12:38 +0900)
src/libsystemd/sd-device/sd-device.c

index b9268dfcd1925a759d2317f9a2d5fb2dab0c736e..e06b6276dc6691c62e722b59a0a6d2d80b31a55e 100644 (file)
@@ -1968,18 +1968,19 @@ static int device_cache_sysattr_value(sd_device *device, const char *key, char *
         return 0;
 }
 
-static int device_get_cached_sysattr_value(sd_device *device, const char *_key, const char **_value) {
-        const char *key = NULL, *value;
+static int device_get_cached_sysattr_value(sd_device *device, const char *key, const char **ret_value) {
+        const char *k = NULL, *value;
 
         assert(device);
-        assert(_key);
-
-        value = hashmap_get2(device->sysattr_values, _key, (void **) &key);
-        if (!key)
-                return -ENOENT;
+        assert(key);
 
-        if (_value)
-                *_value = value;
+        value = hashmap_get2(device->sysattr_values, key, (void **) &k);
+        if (!k)
+                return -ENODATA; /* We have not read the attribute. */
+        if (!value)
+                return -ENOENT; /* We have looked up the attribute before and it did not exist. */
+        if (ret_value)
+                *ret_value = value;
         return 0;
 }
 
@@ -1987,7 +1988,7 @@ static int device_get_cached_sysattr_value(sd_device *device, const char *_key,
  * with a NULL value in the cache, otherwise the returned string is stored */
 _public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr, const char **ret_value) {
         _cleanup_free_ char *value = NULL;
-        const char *path, *syspath, *cached_value = NULL;
+        const char *path, *syspath;
         struct stat statbuf;
         int r;
 
@@ -1995,20 +1996,9 @@ _public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr,
         assert_return(sysattr, -EINVAL);
 
         /* look for possibly already cached result */
-        r = device_get_cached_sysattr_value(device, sysattr, &cached_value);
-        if (r != -ENOENT) {
-                if (r < 0)
-                        return r;
-
-                if (!cached_value)
-                        /* we looked up the sysattr before and it did not exist */
-                        return -ENOENT;
-
-                if (ret_value)
-                        *ret_value = cached_value;
-
-                return 0;
-        }
+        r = device_get_cached_sysattr_value(device, sysattr, ret_value);
+        if (r != -ENODATA)
+                return r;
 
         r = sd_device_get_syspath(device, &syspath);
         if (r < 0)