]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
blockdev-util: add fd-based APIs for getting backing block device for file
authorLennart Poettering <lennart@poettering.net>
Mon, 8 Mar 2021 22:48:21 +0000 (23:48 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 8 Jul 2021 12:49:02 +0000 (14:49 +0200)
src/shared/blockdev-util.c
src/shared/blockdev-util.h

index 53df7d2f0d2de9dc155e411912f8c589fa4dc57c..db50505c9ae7f0d2b7d9e1395f6815a3f0182076 100644 (file)
@@ -182,26 +182,39 @@ int block_get_originating(dev_t dt, dev_t *ret) {
         return 1;
 }
 
-int get_block_device_harder(const char *path, dev_t *ret) {
+int get_block_device_harder_fd(int fd, dev_t *ret) {
         int r;
 
-        assert(path);
+        assert(fd >= 0);
         assert(ret);
 
         /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its
          * immediate parent, if there is one. */
 
-        r = get_block_device(path, ret);
+        r = get_block_device_fd(fd, ret);
         if (r <= 0)
                 return r;
 
         r = block_get_originating(*ret, ret);
         if (r < 0)
-                log_debug_errno(r, "Failed to chase block device '%s', ignoring: %m", path);
+                log_debug_errno(r, "Failed to chase block device, ignoring: %m");
 
         return 1;
 }
 
+int get_block_device_harder(const char *path, dev_t *ret) {
+        _cleanup_close_ int fd = -1;
+
+        assert(path);
+        assert(ret);
+
+        fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC);
+        if (fd < 0)
+                return -errno;
+
+        return get_block_device_harder_fd(fd, ret);
+}
+
 int lock_whole_block_device(dev_t devt, int operation) {
         _cleanup_free_ char *whole_node = NULL;
         _cleanup_close_ int lock_fd = -1;
index d36707cf632833dca8ce521382462974f65cb4d5..05501f26570af901d98e3442167977b766a29d29 100644 (file)
@@ -18,6 +18,7 @@ int block_get_originating(dev_t d, dev_t *ret);
 int get_block_device_fd(int fd, dev_t *ret);
 int get_block_device(const char *path, dev_t *dev);
 
+int get_block_device_harder_fd(int fd, dev_t *dev);
 int get_block_device_harder(const char *path, dev_t *dev);
 
 int lock_whole_block_device(dev_t devt, int operation);