]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
blockdev-util: split-out fd_get_devnum()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 18 Sep 2022 07:15:54 +0000 (16:15 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 28 Sep 2022 08:41:09 +0000 (17:41 +0900)
No functional changes, just preparation for later commits.

src/shared/blockdev-util.c

index 4402b7a97c05551a4454bf20c9d1279190ad6588..b90a5223ccf3661c3a2797c268ce939e5fa71f7b 100644 (file)
 #include "missing_magic.h"
 #include "parse-util.h"
 
+static int fd_get_devnum(int fd, bool backing, dev_t *ret) {
+        struct stat st;
+        dev_t devnum;
+        int r;
+
+        assert(fd >= 0);
+        assert(ret);
+
+        if (fstat(fd, &st) < 0)
+                return -errno;
+
+        if (S_ISBLK(st.st_mode))
+                devnum = st.st_rdev;
+        else if (!backing)
+                return -ENOTBLK;
+        else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
+                return -ENOTBLK;
+        else if (major(st.st_dev) != 0)
+                devnum = st.st_dev;
+        else {
+                _cleanup_close_ int regfd = -1;
+
+                /* If major(st.st_dev) is zero, this might mean we are backed by btrfs, which needs special
+                 * handing, to get the backing device node. */
+
+                regfd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
+                if (regfd < 0)
+                        return regfd;
+
+                r = btrfs_get_block_device_fd(regfd, &devnum);
+                if (r == -ENOTTY) /* not btrfs */
+                        return -ENOTBLK;
+                if (r < 0)
+                        return r;
+        }
+
+        *ret = devnum;
+        return 0;
+}
+
 int block_device_is_whole_disk(sd_device *dev) {
         const char *s;
         int r;
@@ -445,38 +485,14 @@ int path_is_encrypted(const char *path) {
 
 int fd_get_whole_disk(int fd, bool backing, dev_t *ret) {
         dev_t devt;
-        struct stat st;
         int r;
 
+        assert(fd >= 0);
         assert(ret);
 
-        if (fstat(fd, &st) < 0)
-                return -errno;
-
-        if (S_ISBLK(st.st_mode))
-                devt = st.st_rdev;
-        else if (!backing)
-                return -ENOTBLK;
-        else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
-                return -ENOTBLK;
-        else if (major(st.st_dev) != 0)
-                devt = st.st_dev;
-        else {
-                _cleanup_close_ int regfd = -1;
-
-                /* If major(st.st_dev) is zero, this might mean we are backed by btrfs, which needs special
-                 * handing, to get the backing device node. */
-
-                regfd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
-                if (regfd < 0)
-                        return regfd;
-
-                r = btrfs_get_block_device_fd(regfd, &devt);
-                if (r == -ENOTTY)
-                        return -ENOTBLK;
-                if (r < 0)
-                        return r;
-        }
+        r = fd_get_devnum(fd, backing, &devt);
+        if (r < 0)
+                return r;
 
         return block_get_whole_disk(devt, ret);
 }