From 1e9df0347cd1298844222a43a6424400e7dc787b Mon Sep 17 00:00:00 2001 From: DarkKronicle Date: Sat, 13 Jan 2024 23:08:56 -0700 Subject: [PATCH] Fixing sometimes choosing incorrect mount point if substring of each other --- src/freedesktop.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/freedesktop.rs b/src/freedesktop.rs index 53c79d7..160717b 100644 --- a/src/freedesktop.rs +++ b/src/freedesktop.rs @@ -619,14 +619,20 @@ fn home_topdir(mnt_points: &[MountPoint]) -> Result { fn get_topdir_for_path<'a>(path: &Path, mnt_points: &'a [MountPoint]) -> &'a Path { let root: &'static Path = Path::new("/"); - let mut topdir = None; + let mut topdir: Option<&PathBuf> = None; + let mut path_length = 0; for mount_point in mnt_points { if mount_point.mnt_dir == root { continue; } if path.starts_with(&mount_point.mnt_dir) { - topdir = Some(&mount_point.mnt_dir); - break; + // If there is a /run mount and a /run/media/user mount, we want to prioritize the + // longest + let mount_length = &mount_point.mnt_dir.to_str().unwrap().len(); + if topdir == None || mount_length > &path_length { + path_length = *mount_length; + topdir = Some(&mount_point.mnt_dir); + } } } if let Some(t) = topdir {