sd-device: introduce device_new_from_stat_rdev()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 22 Aug 2018 05:00:53 +0000 (14:00 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 22 Aug 2018 19:57:39 +0000 (04:57 +0900)
src/libsystemd/sd-device/device-private.c
src/libsystemd/sd-device/device-private.h

index 2596410..1e41773 100644 (file)
@@ -843,6 +843,22 @@ int device_new_from_synthetic_event(sd_device **new_device, const char *syspath,
         return 0;
 }
 
+int device_new_from_stat_rdev(sd_device **ret, const struct stat *st) {
+        char type;
+
+        assert(ret);
+        assert(st);
+
+        if (S_ISBLK(st->st_mode))
+                type = 'b';
+        else if (S_ISCHR(st->st_mode))
+                type = 'c';
+        else
+                return -ENOTTY;
+
+        return sd_device_new_from_devnum(ret, type, st->st_rdev);
+}
+
 int device_copy_properties(sd_device *device_dst, sd_device *device_src) {
         const char *property, *value;
         int r;
index 22d7d80..7e4a78e 100644 (file)
@@ -3,12 +3,14 @@
 
 #include <inttypes.h>
 #include <stdbool.h>
+#include <sys/stat.h>
 #include <sys/types.h>
 
 #include "sd-device.h"
 
 int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len);
 int device_new_from_strv(sd_device **ret, char **strv);
+int device_new_from_stat_rdev(sd_device **ret, const struct stat *st);
 
 int device_get_id_filename(sd_device *device, const char **ret);