blockdev-util: split-out fd_get_devnum()
No functional changes, just preparation for later commits.
This commit is contained in:
+44
-28
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user