sd-device: introduce device_opendir()

This commit is contained in:
Yu Watanabe
2022-09-19 19:42:02 +09:00
parent a80ce209b9
commit db3049b6f0
2 changed files with 33 additions and 0 deletions
@@ -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);
+30
View 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;
}