diff --git a/src/operation/mod.rs b/src/operation/mod.rs index 7bbc54c..d48b72b 100644 --- a/src/operation/mod.rs +++ b/src/operation/mod.rs @@ -683,7 +683,7 @@ impl Operation { path.strip_prefix(relative_root).map_err(err_str)?.to_str() { if path.is_file() { - let mut file = fs::File::open(&path).map_err(err_str)?; + let mut file = fs::File::open(path).map_err(err_str)?; let metadata = file.metadata().map_err(err_str)?; let total = metadata.len(); if total >= 4 * 1024 * 1024 * 1024 { @@ -800,7 +800,7 @@ impl Operation { op_sel.selected.push(new_dir.clone()); let controller = controller.clone(); - let mime = mime_for_path(&path); + let mime = mime_for_path(path); match mime.essence_str() { "application/gzip" | "application/x-compressed-tar" => { OpReader::new(path, controller) @@ -960,7 +960,7 @@ mod tests { }; use cosmic::iced::futures::{channel::mpsc, StreamExt}; - use log::{debug, trace}; + use log::debug; use test_log::test; use tokio::sync; @@ -968,8 +968,8 @@ mod tests { use crate::{ app::{ test_utils::{ - empty_fs, filter_dirs, filter_files, read_dir_sorted, simple_fs, NAME_LEN, - NUM_DIRS, NUM_FILES, NUM_HIDDEN, NUM_NESTED, + empty_fs, filter_dirs, filter_files, simple_fs, NAME_LEN, NUM_DIRS, NUM_FILES, + NUM_HIDDEN, NUM_NESTED, }, DialogPage, Message, }, diff --git a/src/operation/recursive.rs b/src/operation/recursive.rs index b6f64a4..4a1a28e 100644 --- a/src/operation/recursive.rs +++ b/src/operation/recursive.rs @@ -12,12 +12,18 @@ use super::{copy_unique_path, Controller, OperationSelection, ReplaceResult}; pub struct Context { buf: Vec, controller: Controller, - on_progress: Box, - on_replace: Box ReplaceResult + 'static>, + on_progress: Box, + on_replace: Box, pub(crate) op_sel: OperationSelection, replace_result_opt: Option, } +pub trait OnProgress: Fn(&Op, &Progress) + 'static {} +impl OnProgress for F where F: Fn(&Op, &Progress) + 'static {} + +pub trait OnReplace: Fn(&Op) -> ReplaceResult + 'static {} +impl OnReplace for F where F: Fn(&Op) -> ReplaceResult + 'static {} + impl Context { pub fn new(controller: Controller) -> Self { Self { @@ -67,7 +73,7 @@ impl Context { OpKind::Symlink { target } } else { //TODO: present dialog and allow continue - return Err(format!("{} is not a known file type", from.display()).into()); + return Err(format!("{} is not a known file type", from.display())); }; let to = if from == from_parent { // When copying a file, from matches from_parent, and to_parent must be used @@ -130,12 +136,12 @@ impl Context { Ok(true) } - pub fn on_progress(mut self, f: F) -> Self { + pub fn on_progress(mut self, f: F) -> Self { self.on_progress = Box::new(f); self } - pub fn on_replace ReplaceResult + 'static>(mut self, f: F) -> Self { + pub fn on_replace(mut self, f: F) -> Self { self.on_replace = Box::new(f); self } @@ -153,9 +159,7 @@ impl Context { Ok(ControlFlow::Continue(op.to.clone())) } ReplaceResult::KeepBoth => match op.to.parent() { - Some(to_parent) => Ok(ControlFlow::Continue(copy_unique_path( - &op.from, &to_parent, - ))), + Some(to_parent) => Ok(ControlFlow::Continue(copy_unique_path(&op.from, to_parent))), None => Err(format!("failed to get parent of {:?}", op.to).into()), }, ReplaceResult::Skip(apply_to_all) => { @@ -216,7 +220,7 @@ impl Op { let metadata = from_file.metadata()?; // Remove `to` if overwriting and it is an existing file if self.to.is_file() { - match ctx.replace(&self)? { + match ctx.replace(self)? { ControlFlow::Continue(to) => { self.to = to; } @@ -226,7 +230,7 @@ impl Op { } } progress.total_bytes = Some(metadata.len()); - (ctx.on_progress)(&self, &progress); + (ctx.on_progress)(self, &progress); // This is atomic and ensures `to` is not created by any other process let mut to_file = fs::OpenOptions::new() .create_new(true) @@ -242,14 +246,14 @@ impl Op { } to_file.write_all(&ctx.buf[..count])?; progress.current_bytes += count as u64; - (ctx.on_progress)(&self, &progress); + (ctx.on_progress)(self, &progress); } to_file.sync_all()?; } OpKind::Move => { // Remove `to` if overwriting and it is an existing file if self.to.is_file() { - match ctx.replace(&self)? { + match ctx.replace(self)? { ControlFlow::Continue(to) => { self.to = to; } @@ -289,7 +293,7 @@ impl Op { OpKind::Symlink { ref target } => { // Remove `to` if overwriting and it is an existing file if self.to.is_file() { - match ctx.replace(&self)? { + match ctx.replace(self)? { ControlFlow::Continue(to) => { self.to = to; } @@ -298,8 +302,18 @@ impl Op { } } } - //TODO: use OS-specific function - fs::soft_link(&target, &self.to)?; + #[cfg(unix)] + { + std::os::unix::fs::symlink(target, &self.to)?; + } + #[cfg(windows)] + { + if target.is_dir() { + std::os::windows::fs::symlink_dir(target, &self.to)?; + } else { + std::os::windows::fs::symlink_file(target, &self.to)?; + } + } } } Ok(true) diff --git a/src/thumbnailer.rs b/src/thumbnailer.rs index ac2df57..36dbd0a 100644 --- a/src/thumbnailer.rs +++ b/src/thumbnailer.rs @@ -144,9 +144,7 @@ impl ThumbnailerCache { } pub fn get(&self, key: &Mime) -> Vec { - self.cache - .get(&key) - .map_or_else(|| Vec::new(), |x| x.clone()) + self.cache.get(key).map_or_else(Vec::new, |x| x.clone()) } }