From ff80e7ff5f89624fc7ce5e10158a20ddfef8c9f2 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Fri, 21 Dec 2018 11:19:38 -0600 Subject: [PATCH] common/blkdev: fix BlkDev::get_devid when we got a devname, not fd Signed-off-by: Sage Weil --- src/common/blkdev.cc | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/common/blkdev.cc b/src/common/blkdev.cc index 48ddbc8781d14..be5a223419a23 100644 --- a/src/common/blkdev.cc +++ b/src/common/blkdev.cc @@ -82,14 +82,20 @@ BlkDev::BlkDev(const std::string& devname) : devname(devname) {} -int BlkDev::get_devid(dev_t *id) const { +int BlkDev::get_devid(dev_t *id) const +{ struct stat st; - - int r = fstat(fd, &st); - - if (r < 0) + int r; + if (fd >= 0) { + r = fstat(fd, &st); + } else { + char path[PATH_MAX]; + snprintf(path, sizeof(path), "/dev/%s", devname.c_str()); + stat(path, &st); + } + if (r < 0) { return -errno; - + } *id = S_ISBLK(st.st_mode) ? st.st_rdev : st.st_dev; return 0; } -- 2.39.5