fix warnings

This commit is contained in:
Michael Zhang 2019-07-26 10:19:14 -05:00
parent 1835637071
commit f760162813
No known key found for this signature in database
GPG key ID: 5BAEFE5D04F0CE6C
4 changed files with 18 additions and 16 deletions

View file

@ -15,6 +15,7 @@ use std::path::PathBuf;
use structopt::StructOpt; use structopt::StructOpt;
use xdg::BaseDirectories; use xdg::BaseDirectories;
use crate::errors::Error;
use crate::trashdir::TrashDir; use crate::trashdir::TrashDir;
lazy_static! { lazy_static! {
@ -50,14 +51,14 @@ enum Command {
/// -f to stay compatible with GNU rm /// -f to stay compatible with GNU rm
#[structopt(long = "force", short = "f")] #[structopt(long = "force", short = "f")]
force: bool, _force: bool,
}, },
#[structopt(name = "restore")] #[structopt(name = "restore")]
Restore, Restore,
} }
fn main() { fn main() -> Result<(), Error> {
env_logger::init(); env_logger::init();
let cmd = Command::from_args(); let cmd = Command::from_args();
@ -120,10 +121,12 @@ fn main() {
Ok(i) if i < files.len() => { Ok(i) if i < files.len() => {
let info = files.get(i).unwrap(); let info = files.get(i).unwrap();
println!("moving {:?} to {:?}", &info.deleted_path, &info.path); 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."), _ => println!("Invalid number."),
} }
} }
} }
Ok(())
} }

View file

@ -9,9 +9,6 @@ use crate::trashinfo::TrashInfo;
pub fn empty(dry: bool, days: Option<u32>) -> Result<(), Error> { pub fn empty(dry: bool, days: Option<u32>) -> Result<(), Error> {
let home_trash = TrashDir::get_home_trash(); 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 { let cutoff = if let Some(days) = days {
Local::now() - Duration::days(days.into()) Local::now() - Duration::days(days.into())
} else { } else {
@ -20,11 +17,8 @@ pub fn empty(dry: bool, days: Option<u32>) -> Result<(), Error> {
for file in home_trash.iter()? { for file in home_trash.iter()? {
let file = file?; let file = file?;
let mut ignore = false;
// ignore files that were deleted after the cutoff (younger) // ignore files that were deleted after the cutoff (younger)
if file.deletion_date > cutoff { let ignore = file.deletion_date > cutoff;
ignore = true;
}
if !ignore { if !ignore {
if dry { if dry {
@ -70,7 +64,10 @@ pub fn put(path: impl AsRef<Path>, recursive: bool) -> Result<(), Error> {
trash_info.write(&trash_info_file)?; 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(()) Ok(())
} }

View file

@ -69,7 +69,9 @@ impl Iterator for TrashDirIter {
let deleted_path = if !name.ends_with(".trashinfo") { let deleted_path = if !name.ends_with(".trashinfo") {
return self.next(); return self.next();
} else { } 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)) Some(TrashInfo::from_files(entry.path(), deleted_path).map_err(Error::from))
} }

View file

@ -89,11 +89,11 @@ impl TrashInfo {
} }
pub fn write(&self, mut out: impl Write) -> Result<(), io::Error> { pub fn write(&self, mut out: impl Write) -> Result<(), io::Error> {
out.write(b"[Trash Info]\n")?; writeln!(out, "[Trash Info]")?;
write!(out, "Path={}\n", self.path.to_str().unwrap())?; writeln!(out, "Path={}", self.path.to_str().unwrap())?;
write!( writeln!(
out, out,
"DeletionDate={}\n", "DeletionDate={}",
self.deletion_date.format(DATE_FORMAT) self.deletion_date.format(DATE_FORMAT)
)?; )?;
Ok(()) Ok(())