]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: introduce device_opendir()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 29 Aug 2022 20:23:05 +0000 (05:23 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 19 Sep 2022 10:42:02 +0000 (19:42 +0900)
src/libsystemd/sd-device/device-private.h
src/libsystemd/sd-device/sd-device.c

index 724061a28cd2f328fec815766f77a5fdd0ac7612..d53479e8c97ae87d6ee50af3b5fd6db91a146cb9 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 #pragma once
 
+#include <dirent.h>
 #include <inttypes.h>
 #include <stdbool.h>
 #include <sys/stat.h>
@@ -14,6 +15,8 @@ int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum);
 int device_new_from_nulstr(sd_device **ret, char *nulstr, size_t len);
 int device_new_from_strv(sd_device **ret, char **strv);
 
+int device_opendir(sd_device *device, const char *subdir, DIR **ret);
+
 int device_get_property_bool(sd_device *device, const char *key);
 int device_get_sysattr_unsigned(sd_device *device, const char *sysattr, unsigned *ret_value);
 int device_get_sysattr_bool(sd_device *device, const char *sysattr);
index fd2bc1932342a8e70e073f010e4f8584a15fe08b..7a5403be6fcd71a52540e4bb0ec6851d11bc43d7 100644 (file)
@@ -2472,3 +2472,33 @@ _public_ int sd_device_open(sd_device *device, int flags) {
 
         return TAKE_FD(fd2);
 }
+
+int device_opendir(sd_device *device, const char *subdir, DIR **ret) {
+        _cleanup_closedir_ DIR *d = NULL;
+        _cleanup_free_ char *path = NULL;
+        const char *syspath;
+        int r;
+
+        assert(device);
+        assert(ret);
+
+        r = sd_device_get_syspath(device, &syspath);
+        if (r < 0)
+                return r;
+
+        if (subdir) {
+                if (!path_is_safe(subdir))
+                        return -EINVAL;
+
+                path = path_join(syspath, subdir);
+                if (!path)
+                        return -ENOMEM;
+        }
+
+        d = opendir(path ?: syspath);
+        if (!d)
+                return -errno;
+
+        *ret = TAKE_PTR(d);
+        return 0;
+}