From 995dd0385e455e5263e7eead5277eb16e882d89b Mon Sep 17 00:00:00 2001 From: Jaegeuk Kim Date: Tue, 5 Dec 2023 14:22:31 -0800 Subject: [PATCH] Use target path when handling dm-bow Refactor getting sysfs directory. Bug: 312817490 Change-Id: Icc12002d3f5a2e24f86ce88e5e602cf7a71b0f84 Signed-off-by: Jaegeuk Kim --- fs_mgr/fs_mgr.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/fs_mgr/fs_mgr.cpp b/fs_mgr/fs_mgr.cpp index 35c8c6396..a94a27447 100644 --- a/fs_mgr/fs_mgr.cpp +++ b/fs_mgr/fs_mgr.cpp @@ -1244,17 +1244,27 @@ class CheckpointManager { }; std::string fs_mgr_find_bow_device(const std::string& block_device) { - if (block_device.substr(0, 5) != "/dev/") { - LOG(ERROR) << "Expected block device, got " << block_device; - return std::string(); + // handle symlink such as "/dev/block/mapper/userdata" + std::string real_path; + if (!android::base::Realpath(block_device, &real_path)) { + real_path = block_device; } - std::string sys_dir = std::string("/sys/") + block_device.substr(5); - + struct stat st; + if (stat(real_path.c_str(), &st) < 0) { + PLOG(ERROR) << "stat failed: " << real_path; + return std::string(); + } + if (!S_ISBLK(st.st_mode)) { + PLOG(ERROR) << real_path << " is not block device"; + return std::string(); + } + std::string sys_dir = android::base::StringPrintf("/sys/dev/block/%u:%u", major(st.st_rdev), + minor(st.st_rdev)); for (;;) { std::string name; if (!android::base::ReadFileToString(sys_dir + "/dm/name", &name)) { - PLOG(ERROR) << block_device << " is not dm device"; + PLOG(ERROR) << real_path << " is not dm device"; return std::string(); }