diff --git a/src/file_manager.rs b/src/file_manager.rs index 25d5b8d..7a28424 100644 --- a/src/file_manager.rs +++ b/src/file_manager.rs @@ -1,9 +1,10 @@ use anyhow::Result; use colored::Colorize; +use std::path::PathBuf; #[derive(Debug, Clone)] pub struct File { - pub path: String, + pub path: PathBuf, pub size: Option, pub hash: Option, } @@ -11,8 +12,8 @@ pub struct File { pub fn delete_files(files: Vec) -> Result<()> { files.into_iter().for_each(|file| { match std::fs::remove_file(file.path.clone()) { - Ok(_) => println!("{}: {}", "DELETED".green(), file.path), - Err(_) => println!("{}: {}", "FAILED".red(), file.path) + Ok(_) => println!("{}: {}", "DELETED".green(), file.path.display()), + Err(_) => println!("{}: {}", "FAILED".red(), file.path.display()) } }); diff --git a/src/output.rs b/src/output.rs index 8767bfb..0e2201a 100644 --- a/src/output.rs +++ b/src/output.rs @@ -9,11 +9,14 @@ use indicatif::{ProgressBar, ProgressIterator, ProgressStyle}; use itertools::Itertools; use prettytable::{format, row, Table}; use std::io::Write; +use std::path::Path; use std::{fs, io}; use unicode_segmentation::UnicodeSegmentation; -fn format_path(path: &str, opts: &Params) -> Result { - let display_path = path.replace(opts.get_directory()?.to_string_lossy().as_ref(), ""); +fn format_path(path: &Path, opts: &Params) -> Result { + let display_path = path + .to_string_lossy() + .replace(opts.get_directory()?.to_string_lossy().as_ref(), ""); let display_range = if display_path.chars().count() > 32 { display_path .graphemes(true) @@ -34,7 +37,7 @@ fn file_size(file: &File) -> Result { Ok(format!("{:>12}", bytesize::ByteSize::b(file.size.unwrap()))) } -fn modified_time(path: &String) -> Result { +fn modified_time(path: &Path) -> Result { let mdata = fs::metadata(path)?; let modified_time: DateTime = mdata.modified()?.into(); @@ -100,7 +103,7 @@ fn process_group_action(duplicates: &Vec, dup_index: usize, dup_size: usiz .clone() .enumerate() .for_each(|(index, file)| { - println!("{}: {}", index.to_string().blue(), file.path); + println!("{}: {}", index.to_string().blue(), file.path.display()); }); match scan_group_confirmation().unwrap() { diff --git a/src/scanner.rs b/src/scanner.rs index 35a32ca..2138b1c 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -7,7 +7,10 @@ use memmap2::Mmap; use rayon::prelude::*; use std::hash::Hasher; use std::time::Duration; -use std::{fs, path::PathBuf}; +use std::{ + fs, + path::{Path, PathBuf}, +}; #[derive(Clone, Copy)] enum IndexCritera { @@ -57,7 +60,6 @@ fn scan(app_opts: &Params) -> Result> { .progress_with_style(ProgressStyle::with_template( "{spinner:.green} [processing mapped paths] [{wide_bar:.cyan/blue}] {pos}/{len} files", )?) - .map(|fpath| fpath.display().to_string()) .map(|fpath| File { path: fpath.clone(), hash: None, @@ -110,7 +112,7 @@ fn index_files( Ok(store) } -fn incremental_hashing(filepath: &str) -> Result { +fn incremental_hashing(filepath: &Path) -> Result { let file = fs::File::open(filepath)?; let fmap = unsafe { Mmap::map(&file)? }; let mut inchasher = fxhash::FxHasher::default(); @@ -121,12 +123,12 @@ fn incremental_hashing(filepath: &str) -> Result { Ok(format!("{}", inchasher.finish())) } -fn standard_hashing(filepath: &str) -> Result { +fn standard_hashing(filepath: &Path) -> Result { let file = fs::read(filepath)?; Ok(hasher(&*file).to_string()) } -fn hash_file(filepath: &str) -> Result { +fn hash_file(filepath: &Path) -> Result { let filemeta = fs::metadata(filepath)?; // NOTE: USE INCREMENTAL HASHING ONLY FOR FILES > 100MB