summaryrefslogtreecommitdiffstats
path: root/src/basic/blockdev-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2019-12-12 14:18:26 +0100
committerYu Watanabe <watanabe.yu+github@gmail.com>2019-12-13 10:38:35 +0100
commitdb8728a60c73076ebea7fa8eec7ae844f20bc2e9 (patch)
tree5f583e35e14a4fc9d355d628b2752d3842b75517 /src/basic/blockdev-util.c
parentMerge pull request #13915 from ddstreet/ipv6_mtu (diff)
downloadsystemd-db8728a60c73076ebea7fa8eec7ae844f20bc2e9.tar.xz
systemd-db8728a60c73076ebea7fa8eec7ae844f20bc2e9.zip
blockdev-util: rework get_block_device()
Let's open the specified path once, and use the same fd for all lookups. Also, don't check for btrfs twice. The behaviour remains unmodified.
Diffstat (limited to '')
-rw-r--r--src/basic/blockdev-util.c32
1 files changed, 18 insertions, 14 deletions
diff --git a/src/basic/blockdev-util.c b/src/basic/blockdev-util.c
index 5372c26be6..7d94c55a6d 100644
--- a/src/basic/blockdev-util.c
+++ b/src/basic/blockdev-util.c
@@ -54,32 +54,36 @@ int block_get_whole_disk(dev_t d, dev_t *ret) {
return 1;
}
-int get_block_device(const char *path, dev_t *dev) {
+int get_block_device(const char *path, dev_t *ret) {
+ _cleanup_close_ int fd = -1;
struct stat st;
- struct statfs sfs;
+ int r;
assert(path);
- assert(dev);
+ assert(ret);
- /* Gets the block device directly backing a file system. If
- * the block device is encrypted, returns the device mapper
- * block device. */
+ /* Gets the block device directly backing a file system. If the block device is encrypted, returns
+ * the device mapper block device. */
+
+ fd = open(path, O_NOFOLLOW|O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
- if (lstat(path, &st))
+ if (fstat(fd, &st))
return -errno;
if (major(st.st_dev) != 0) {
- *dev = st.st_dev;
+ *ret = st.st_dev;
return 1;
}
- if (statfs(path, &sfs) < 0)
- return -errno;
-
- if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC))
- return btrfs_get_block_device(path, dev);
+ r = btrfs_get_block_device_fd(fd, ret);
+ if (r > 0)
+ return 1;
+ if (r != -ENOTTY) /* not btrfs */
+ return r;
- *dev = 0;
+ *ret = 0;
return 0;
}