Replace /proc/mounts grep with Mount_Info cache reload and query (!89)

Creating a grep process to check if a particular mount is still mounted
is an unnecessary overhead.  All that is needed is for the Mount_Info
module to refresh it's copy of /proc/mounts and query that.

To keep the code as simple as possible just reload the whole of the
Mount_Info module and query the mount cache to determine if the
particular block device is still mounted at this particular mount point.
This therefore re-reads /proc/mounts (necessary) and /proc/swaps and
/etc/fstab (unnecessary).  This is still much less overhead than
creating a separate grep process.

Closes !89 - Fix unmount error when unmounting below a bind mount point
This commit is contained in:
Mike Fleetwood 2021-09-05 10:10:59 +01:00
parent 6f811cfaca
commit ba1bafc5ac
3 changed files with 22 additions and 7 deletions

View file

@ -49,6 +49,7 @@ public:
static void load_cache();
static bool is_dev_mounted( const Glib::ustring & path );
static bool is_dev_mounted( const BlockSpecial & bs );
static bool is_dev_mounted_at(const Glib::ustring& path, const Glib::ustring& mountpoint);
static bool is_dev_mounted_readonly( const Glib::ustring & path );
static bool is_dev_mounted_readonly( const BlockSpecial & bs );
static std::vector<Glib::ustring> get_all_mountpoints();

View file

@ -136,6 +136,18 @@ const std::vector<Glib::ustring> & Mount_Info::get_fstab_mountpoints( const Glib
return find( fstab_info, path ).mountpoints;
}
// Return whether the device path, such as /dev/sda3, is mounted at mount point or not
bool Mount_Info::is_dev_mounted_at(const Glib::ustring& path, const Glib::ustring& mountpoint)
{
const std::vector<Glib::ustring>& mountpoints = get_mounted_mountpoints(path);
for (unsigned i = 0; i < mountpoints.size(); i++)
if (mountpoint == mountpoints[i])
return true;
return false;
}
// Private methods
void Mount_Info::read_mountpoints_from_file( const Glib::ustring & filename, MountMapping & map )

View file

@ -2847,14 +2847,16 @@ bool Win_GParted::unmount_partition( const Partition & partition, Glib::ustring
}
else
{
Glib::ustring checkmount = "grep -w " + Glib::shell_quote(fs_mountpoints[i]) + " /proc/mounts";
Glib::ustring cmd = "umount -v " + Glib::shell_quote( fs_mountpoints[i] );
Glib::ustring dummy;
Glib::ustring umount_error;
// Check mount point is still mounted
if (! Utils::execute_command(checkmount, dummy, umount_error))
// Unmounting below a duplicating bind mount, unmounts all copies
// in one go so check if the file system is still mounted at this
// mount point before trying to unmount it.
Mount_Info::load_cache();
if (Mount_Info::is_dev_mounted_at(partition.get_path(), fs_mountpoints[i]))
{
Glib::ustring cmd = "umount -v " + Glib::shell_quote(fs_mountpoints[i]);
Glib::ustring dummy;
Glib::ustring umount_error;
if (Utils::execute_command(cmd, dummy, umount_error))
umount_errors.push_back("# " + cmd + "\n" + umount_error);
}