From 7f0a779c44ceaecfdf0222cc021b30a51cd2cab6 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Tue, 3 May 2022 14:19:24 +0800 Subject: [PATCH] blk/pmem: refactor pmem_check_file_type() using std::filesystem for better readability Signed-off-by: Kefu Chai --- src/blk/pmem/PMEMDevice.cc | 62 ++++++++++++++------------------------ 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/src/blk/pmem/PMEMDevice.cc b/src/blk/pmem/PMEMDevice.cc index fd9d3c46a30a..728c79a198d9 100644 --- a/src/blk/pmem/PMEMDevice.cc +++ b/src/blk/pmem/PMEMDevice.cc @@ -23,6 +23,10 @@ #include #include #include +#include +#include + +#include #include "PMEMDevice.h" #include "libpmem.h" @@ -66,51 +70,31 @@ int PMEMDevice::_lock() static int pmem_check_file_type(int fd, const char *pmem_file, uint64_t *total_size) { - int rc = 0; - struct stat file_stat; - - rc = ::fstat(fd, &file_stat); - if (rc) { - return -1; - } - - if ((file_stat.st_mode & S_IFCHR) != S_IFCHR) { - return -1; + namespace fs = std::filesystem; + if (!fs::is_character_file(pmem_file)) { + return -EINVAL; } - - char spath[PATH_MAX], npath[PATH_MAX]; - snprintf(spath, PATH_MAX, "/sys/dev/char/%d:%d/subsystem", - major(file_stat.st_rdev), minor(file_stat.st_rdev)); - - char *real_path = realpath(spath, npath); - if (!real_path) { - return -1; + struct stat file_stat; + if (::fstat(fd, &file_stat)) { + return -EINVAL; } - + fs::path char_dir = fmt::format("/sys/dev/char/{}:{}", + major(file_stat.st_rdev), + minor(file_stat.st_rdev)); // Need to check if it is a DAX device - char *base_name = strrchr(real_path, '/'); - if (!base_name || strcmp("dax", base_name + 1)) { - return -1; + if (auto subsys_path = char_dir / "subsystem"; + fs::read_symlink(subsys_path).filename().string() != "dax") { + return -EINVAL; } - - snprintf(spath, PATH_MAX, "/sys/dev/char/%d:%d/size", - major(file_stat.st_rdev), minor(file_stat.st_rdev)); - FILE *sfile = fopen(spath, "r"); - if (!sfile) { - return -1; + if (total_size == nullptr) { + return 0; } - - if (total_size != nullptr) { - rc = fscanf(sfile, "%lu", total_size); - if (rc < 0) { - rc = -1; - } else { - rc = 0; - } + if (std::ifstream size_file(char_dir / "size"); size_file) { + size_file >> *total_size; + return size_file ? 0 : -EINVAL; + } else { + return -EINVAL; } - - fclose(sfile); - return rc; } int PMEMDevice::open(const std::string& p) -- 2.47.3