replace path String in File type with PathBuf

This commit is contained in:
sreedev
2023-01-26 00:04:17 -05:00
parent d06ca7897d
commit ef1e9a1fce
3 changed files with 18 additions and 12 deletions

View File

@@ -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<u64>,
pub hash: Option<String>,
}
@@ -11,8 +12,8 @@ pub struct File {
pub fn delete_files(files: Vec<File>) -> 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())
}
});

View File

@@ -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<String> {
let display_path = path.replace(opts.get_directory()?.to_string_lossy().as_ref(), "");
fn format_path(path: &Path, opts: &Params) -> Result<String> {
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<String> {
Ok(format!("{:>12}", bytesize::ByteSize::b(file.size.unwrap())))
}
fn modified_time(path: &String) -> Result<String> {
fn modified_time(path: &Path) -> Result<String> {
let mdata = fs::metadata(path)?;
let modified_time: DateTime<Utc> = mdata.modified()?.into();
@@ -100,7 +103,7 @@ fn process_group_action(duplicates: &Vec<File>, 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() {

View File

@@ -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<Vec<File>> {
.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<String> {
fn incremental_hashing(filepath: &Path) -> Result<String> {
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<String> {
Ok(format!("{}", inchasher.finish()))
}
fn standard_hashing(filepath: &str) -> Result<String> {
fn standard_hashing(filepath: &Path) -> Result<String> {
let file = fs::read(filepath)?;
Ok(hasher(&*file).to_string())
}
fn hash_file(filepath: &str) -> Result<String> {
fn hash_file(filepath: &Path) -> Result<String> {
let filemeta = fs::metadata(filepath)?;
// NOTE: USE INCREMENTAL HASHING ONLY FOR FILES > 100MB