From 6d27b49a7a767a9c8b31dc0f799da940ddb7232f Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Thu, 9 Apr 2020 20:52:20 +0200 Subject: Tidy up Drive and unmounting a little. --- src/lib/cross_linux.cc | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'src/lib/cross_linux.cc') diff --git a/src/lib/cross_linux.cc b/src/lib/cross_linux.cc index 14a7e5b47..d8a33482f 100644 --- a/src/lib/cross_linux.cc +++ b/src/lib/cross_linux.cc @@ -258,6 +258,8 @@ running_32_on_64 () return false; } + +static vector > get_mounts (string prefix) { @@ -278,8 +280,9 @@ get_mounts (string prefix) return mounts; } + vector -get_drives () +Drive::get () { vector drives; @@ -300,7 +303,6 @@ get_drives () if (size == 0) { continue; } - bool mounted = false; optional vendor; try { vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor"); @@ -311,19 +313,35 @@ get_drives () model = dcp::file_to_string("/sys/block/" + name + "/device/model"); boost::trim(*model); } catch (...) {} + vector mount_points; for (vector >::const_iterator j = mounted_devices.begin(); j != mounted_devices.end(); ++j) { if (boost::algorithm::starts_with(j->first, "/dev/" + name)) { - mounted = true; + mount_points.push_back (j->second); } } - drives.push_back(Drive("/dev/" + i->path().filename().string(), size, mounted, vendor, model)); - LOG_DISK("Block device %1 size %2 %3 vendor %4 model %5", name, size, mounted ? "mounted" : "not mounted", vendor.get_value_or("[none]"), model.get_value_or("[none]")); + drives.push_back(Drive("/dev/" + name, mount_points, size, vendor, model)); + LOG_DISK_NC(drives.back().log_summary()); } } return drives; } + +bool +Drive::unmount () +{ + BOOST_FOREACH (boost::filesystem::path i, _mount_points) { + int const r = umount(i.string().c_str()); + LOG_DISK("Tried to unmount %1 and got %2 and %3", i.string(), r, errno); + if (r == -1) { + return false; + } + } + return true; +} + + void unprivileged () { @@ -354,17 +372,3 @@ config_path () return p; } -bool -unmount_drive (string drive) -{ - vector > mounts = get_mounts (drive); - for (vector >::const_iterator i = mounts.begin(); i != mounts.end(); ++i) { - int const r = umount(i->second.c_str()); - LOG_DISK("Tried to unmount %1 and got %2 and %3", i->second, r, errno); - if (r == -1) { - return false; - } - } - return true; -} - -- cgit v1.2.3