diff --git a/src/main.rs b/src/main.rs index 1324cf0..54270d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ use std::path::PathBuf; use structopt::StructOpt; use xdg::BaseDirectories; +use crate::errors::Error; use crate::trashdir::TrashDir; lazy_static! { @@ -50,14 +51,14 @@ enum Command { /// -f to stay compatible with GNU rm #[structopt(long = "force", short = "f")] - force: bool, + _force: bool, }, #[structopt(name = "restore")] Restore, } -fn main() { +fn main() -> Result<(), Error> { env_logger::init(); let cmd = Command::from_args(); @@ -120,10 +121,12 @@ fn main() { Ok(i) if i < files.len() => { let info = files.get(i).unwrap(); println!("moving {:?} to {:?}", &info.deleted_path, &info.path); - fs::rename(&info.deleted_path, &info.path); + fs::rename(&info.deleted_path, &info.path)?; } _ => println!("Invalid number."), } } } + + Ok(()) } diff --git a/src/ops.rs b/src/ops.rs index 1b09fec..9b4999d 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -9,9 +9,6 @@ use crate::trashinfo::TrashInfo; pub fn empty(dry: bool, days: Option) -> Result<(), Error> { let home_trash = TrashDir::get_home_trash(); - let files_dir = home_trash.files_dir()?; - let info_dir = home_trash.info_dir()?; - let cutoff = if let Some(days) = days { Local::now() - Duration::days(days.into()) } else { @@ -20,11 +17,8 @@ pub fn empty(dry: bool, days: Option) -> Result<(), Error> { for file in home_trash.iter()? { let file = file?; - let mut ignore = false; // ignore files that were deleted after the cutoff (younger) - if file.deletion_date > cutoff { - ignore = true; - } + let ignore = file.deletion_date > cutoff; if !ignore { if dry { @@ -70,7 +64,10 @@ pub fn put(path: impl AsRef, recursive: bool) -> Result<(), Error> { trash_info.write(&trash_info_file)?; } - fs::rename(path, trash_file_path)?; + let result = fs::rename(&path, &trash_file_path); + if result.is_err() { + fs::copy(&path, &trash_file_path)?; + } Ok(()) } diff --git a/src/trashdir.rs b/src/trashdir.rs index 3e98a7e..830f4dd 100644 --- a/src/trashdir.rs +++ b/src/trashdir.rs @@ -69,7 +69,9 @@ impl Iterator for TrashDirIter { let deleted_path = if !name.ends_with(".trashinfo") { return self.next(); } else { - self.0.join("files").join(name.trim_right_matches(".trashinfo")) + self.0 + .join("files") + .join(name.trim_end_matches(".trashinfo")) }; Some(TrashInfo::from_files(entry.path(), deleted_path).map_err(Error::from)) } diff --git a/src/trashinfo.rs b/src/trashinfo.rs index 2b5dc28..e0bdec3 100644 --- a/src/trashinfo.rs +++ b/src/trashinfo.rs @@ -89,11 +89,11 @@ impl TrashInfo { } pub fn write(&self, mut out: impl Write) -> Result<(), io::Error> { - out.write(b"[Trash Info]\n")?; - write!(out, "Path={}\n", self.path.to_str().unwrap())?; - write!( + writeln!(out, "[Trash Info]")?; + writeln!(out, "Path={}", self.path.to_str().unwrap())?; + writeln!( out, - "DeletionDate={}\n", + "DeletionDate={}", self.deletion_date.format(DATE_FORMAT) )?; Ok(())