From: Yu Watanabe Date: Sun, 18 Sep 2022 07:15:54 +0000 (+0900) Subject: blockdev-util: split-out fd_get_devnum() X-Git-Tag: v252-rc1~49^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7e4a0387c4cb02a13d3221035f7c159e71d4b587;p=thirdparty%2Fsystemd.git blockdev-util: split-out fd_get_devnum() No functional changes, just preparation for later commits. --- diff --git a/src/shared/blockdev-util.c b/src/shared/blockdev-util.c index 4402b7a97c0..b90a5223ccf 100644 --- a/src/shared/blockdev-util.c +++ b/src/shared/blockdev-util.c @@ -20,6 +20,46 @@ #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); }