From d0ee95c4cd7a51e3c331cf8010e88d096d1d2a67 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 2 Feb 2020 07:22:17 +1000 Subject: [PATCH] send removed files to the trash The way the trash crate implements deletion on a Mac is ugly, and we may need to look into alternatives. --- rslib/Cargo.toml | 1 + rslib/src/media/files.rs | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/rslib/Cargo.toml b/rslib/Cargo.toml index b18299111..198c75955 100644 --- a/rslib/Cargo.toml +++ b/rslib/Cargo.toml @@ -29,6 +29,7 @@ env_logger = "0.7.1" zip = "0.5.4" log = "0.4.8" serde_tuple = "0.4.0" +trash = "1.0.0" [build-dependencies] prost-build = "0.5.0" diff --git a/rslib/src/media/files.rs b/rslib/src/media/files.rs index aec113554..c5523f5f1 100644 --- a/rslib/src/media/files.rs +++ b/rslib/src/media/files.rs @@ -10,6 +10,7 @@ use std::borrow::Cow; use std::io::Read; use std::path::Path; use std::{fs, io, time}; +use trash::remove_all; use unicode_normalization::{is_nfc, UnicodeNormalization}; /// The maximum length we allow a filename to be. When combined @@ -234,16 +235,20 @@ pub(super) fn mtime_as_i64>(path: P) -> io::Result { .as_secs() as i64) } -pub(super) fn remove_files(media_folder: &Path, files: &[&String]) -> Result<()> { - for &file in files { - debug!("removing {}", file); - let path = media_folder.join(file.as_str()); - fs::remove_file(path).map_err(|e| AnkiError::IOError { - info: format!("Error removing {}: {}", file, e), - })?; +pub(super) fn remove_files(media_folder: &Path, files: &[S]) -> Result<()> +where + S: AsRef + std::fmt::Debug, +{ + if files.is_empty() { + return Ok(()); } - Ok(()) + let paths = files.iter().map(|f| media_folder.join(f.as_ref())); + + debug!("removing {:?}", files); + remove_all(paths).map_err(|e| AnkiError::IOError { + info: format!("removing files failed: {:?}", e), + }) } pub(super) struct AddedFile { @@ -308,7 +313,7 @@ pub(super) fn data_for_file(media_folder: &Path, fname: &str) -> Result