Skip to content

Commit

Permalink
keep error converter function and rename it fs_error
Browse files Browse the repository at this point in the history
  • Loading branch information
TD-Sky committed Oct 30, 2023
1 parent c51aa78 commit a08118c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
34 changes: 17 additions & 17 deletions src/freedesktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ impl TrashContext {
debug!("The topdir was identical to the home topdir, so moving to the home trash.");
// Note that the following function creates the trash folder
// and its required subfolders in case they don't exist.
move_to_trash(path, &home_trash, topdir)?;
move_to_trash(path, &home_trash, topdir).map_err(|(p, e)| fs_error(p, e))?;
} else {
execute_on_mounted_trash_folders(uid, topdir, true, true, |trash_path| {
move_to_trash(&path, trash_path, topdir)
})?;
})
.map_err(|(p, e)| fs_error(p, e))?;
}
}
Ok(())
Expand Down Expand Up @@ -85,7 +86,8 @@ pub fn list() -> Result<Vec<TrashItem>, Error> {
execute_on_mounted_trash_folders(uid, &mount.mnt_dir, false, false, |trash_path| {
trash_folders.insert(trash_path);
Ok(())
})?;
})
.map_err(|(p, e)| fs_error(p, e))?;
}
if trash_folders.is_empty() {
warn!("No trash folder was found. The error when looking for the 'home trash' was: {:?}", home_error);
Expand Down Expand Up @@ -240,14 +242,14 @@ where
// that either there's a bug in this code or the target system didn't follow
// the specification.
let file = restorable_file_in_trash_from_info_file(info_file);
assert!(virtually_exists(&file).map_err(|e| (&file, e))?);
assert!(virtually_exists(&file).map_err(|e| fs_error(&file, e))?);
if file.is_dir() {
fs::remove_dir_all(&file).map_err(|e| (&file, e))?;
fs::remove_dir_all(&file).map_err(|e| fs_error(&file, e))?;
// TODO Update directory size cache if there's one.
} else {
fs::remove_file(&file).map_err(|e| (&file, e))?;
fs::remove_file(&file).map_err(|e| fs_error(&file, e))?;
}
fs::remove_file(info_file).map_err(|e| (info_file, e))?;
fs::remove_file(info_file).map_err(|e| fs_error(info_file, e))?;
}

Ok(())
Expand Down Expand Up @@ -277,12 +279,12 @@ where
// that either there's a bug in this code or the target system didn't follow
// the specification.
let file = restorable_file_in_trash_from_info_file(info_file);
assert!(virtually_exists(&file).map_err(|e| (&file, e))?);
assert!(virtually_exists(&file).map_err(|e| fs_error(&file, e))?);
// TODO add option to forcefully replace any target at the restore location
// if it already exists.
let original_path = item.original_path();
// Make sure the parent exists so that `create_dir` doesn't faile due to that.
fs::create_dir_all(&item.original_parent).map_err(|e| (&item.original_parent, e))?;
fs::create_dir_all(&item.original_parent).map_err(|e| fs_error(&item.original_parent, e))?;
let mut collision = false;
if file.is_dir() {
// NOTE create_dir_all succeeds when the path already exist but create_dir
Expand All @@ -291,7 +293,7 @@ where
if e.kind() == io::ErrorKind::AlreadyExists {
collision = true;
} else {
return Err((&original_path, e).into());
return Err(fs_error(&original_path, e));
}
}
} else {
Expand All @@ -300,16 +302,16 @@ where
if e.kind() == io::ErrorKind::AlreadyExists {
collision = true;
} else {
return Err((&original_path, e).into());
return Err(fs_error(&original_path, e));
}
}
}
if collision {
let remaining: Vec<_> = std::iter::once(item).chain(iter).collect();
return Err(Error::RestoreCollision { path: original_path, remaining_items: remaining });
}
fs::rename(&file, &original_path).map_err(|e| (&file, e))?;
fs::remove_file(info_file).map_err(|e| (info_file, e))?;
fs::rename(&file, &original_path).map_err(|e| fs_error(&file, e))?;
fs::remove_file(info_file).map_err(|e| fs_error(info_file, e))?;
}
Ok(())
}
Expand Down Expand Up @@ -1085,8 +1087,6 @@ mod tests {
}
}

impl<P: Into<PathBuf>> From<(P, io::Error)> for Error {
fn from((path, source): (P, io::Error)) -> Self {
Self::FileSystem { path: path.into(), source }
}
fn fs_error(path: impl Into<PathBuf>, source: io::Error) -> Error {
Error::FileSystem { path: path.into(), source }
}
10 changes: 1 addition & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,7 @@ impl fmt::Display for Error {
write!(f, "Error during a `trash` operation: {self:?}")
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))]
Self::FileSystem { path: _, source: e } => e.source(),
_ => None,
}
}
}
impl error::Error for Error {}
pub fn into_unknown<E: std::fmt::Display>(err: E) -> Error {
Error::Unknown { description: format!("{err}") }
}
Expand Down

0 comments on commit a08118c

Please sign in to comment.