Skip to content

Commit

Permalink
fix: Properly reconstruct paths when restoring files on freedesktop i…
Browse files Browse the repository at this point in the history
…f those were relative (#47)

Previously it would be unable to reconstruct original paths if the trash
directory was on a mount point due to a 'split brain' of sorts.

When trashing files it would create original path information based
on them being relative to a mount point, but when restoring them
it would reconstruct them to be relative to the trash top level
directory.

Now the reconstruction happens against to mount point itself which makes
restoration match.
  • Loading branch information
Byron committed May 10, 2022
1 parent 90f0f9b commit dcda6df
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions src/freedesktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn list() -> Result<Vec<TrashItem>, Error> {
// trash folders when found one.
let uid = unsafe { libc::getuid() };
let mount_points = get_mount_points()?;
for mount in mount_points.into_iter() {
for mount in &mount_points {
execute_on_mounted_trash_folders(uid, &mount.mnt_dir, false, false, |trash_path| {
trash_folders.insert(trash_path);
Ok(())
Expand All @@ -94,7 +94,7 @@ pub fn list() -> Result<Vec<TrashItem>, Error> {
let mut result = Vec::new();
for folder in trash_folders.iter() {
// Read the info files for every file
let trash_folder_parent = trash_parent(folder);
let top_dir = get_topdir_for_path(folder, &mount_points);
let info_folder = folder.join("info");
if !info_folder.is_dir() {
warn!(
Expand Down Expand Up @@ -170,7 +170,7 @@ pub fn list() -> Result<Vec<TrashItem>, Error> {
if key == "Path" {
let mut value_path = Path::new(value).to_owned();
if value_path.is_relative() {
value_path = trash_folder_parent.join(value_path);
value_path = top_dir.join(value_path);
}
let full_path_utf8 = PathBuf::from(parse_uri_path(&value_path));
name = Some(full_path_utf8.file_name().unwrap().to_str().unwrap().to_owned());
Expand Down Expand Up @@ -213,19 +213,6 @@ pub fn list() -> Result<Vec<TrashItem>, Error> {
Ok(result)
}

fn trash_parent(trash_dir: &Path) -> &Path {
let parent = trash_dir.parent().unwrap();
if file_name(parent) == Some("share") && file_name(parent.parent().unwrap()) == Some(".local") {
parent.parent().unwrap().parent().unwrap().parent().unwrap()
} else {
parent
}
}

fn file_name(parent: &Path) -> Option<&str> {
parent.file_name().and_then(|f| f.to_str())
}

pub fn purge_all<I>(items: I) -> Result<(), Error>
where
I: IntoIterator<Item = TrashItem>,
Expand Down

0 comments on commit dcda6df

Please sign in to comment.