Projects STRLCPY deduplicator Commits ef1e9a1f
🤬
  • replace path String in File type with PathBuf

  • Loading...
  • sreedev committed 1 year ago
    ef1e9a1f
    1 parent d06ca789
  • ■ ■ ■ ■ ■ ■
    src/file_manager.rs
    1 1  use anyhow::Result;
    2 2  use colored::Colorize;
     3 +use std::path::PathBuf;
    3 4   
    4 5  #[derive(Debug, Clone)]
    5 6  pub struct File {
    6  - pub path: String,
     7 + pub path: PathBuf,
    7 8   pub size: Option<u64>,
    8 9   pub hash: Option<String>,
    9 10  }
    skipped 1 lines
    11 12  pub fn delete_files(files: Vec<File>) -> Result<()> {
    12 13   files.into_iter().for_each(|file| {
    13 14   match std::fs::remove_file(file.path.clone()) {
    14  - Ok(_) => println!("{}: {}", "DELETED".green(), file.path),
    15  - Err(_) => println!("{}: {}", "FAILED".red(), file.path)
     15 + Ok(_) => println!("{}: {}", "DELETED".green(), file.path.display()),
     16 + Err(_) => println!("{}: {}", "FAILED".red(), file.path.display())
    16 17   }
    17 18   });
    18 19   
    skipped 3 lines
  • ■ ■ ■ ■ ■ ■
    src/output.rs
    skipped 8 lines
    9 9  use itertools::Itertools;
    10 10  use prettytable::{format, row, Table};
    11 11  use std::io::Write;
     12 +use std::path::Path;
    12 13  use std::{fs, io};
    13 14  use unicode_segmentation::UnicodeSegmentation;
    14 15   
    15  -fn format_path(path: &str, opts: &Params) -> Result<String> {
    16  - let display_path = path.replace(opts.get_directory()?.to_string_lossy().as_ref(), "");
     16 +fn format_path(path: &Path, opts: &Params) -> Result<String> {
     17 + let display_path = path
     18 + .to_string_lossy()
     19 + .replace(opts.get_directory()?.to_string_lossy().as_ref(), "");
    17 20   let display_range = if display_path.chars().count() > 32 {
    18 21   display_path
    19 22   .graphemes(true)
    skipped 14 lines
    34 37   Ok(format!("{:>12}", bytesize::ByteSize::b(file.size.unwrap())))
    35 38  }
    36 39   
    37  -fn modified_time(path: &String) -> Result<String> {
     40 +fn modified_time(path: &Path) -> Result<String> {
    38 41   let mdata = fs::metadata(path)?;
    39 42   let modified_time: DateTime<Utc> = mdata.modified()?.into();
    40 43   
    skipped 59 lines
    100 103   .clone()
    101 104   .enumerate()
    102 105   .for_each(|(index, file)| {
    103  - println!("{}: {}", index.to_string().blue(), file.path);
     106 + println!("{}: {}", index.to_string().blue(), file.path.display());
    104 107   });
    105 108   
    106 109   match scan_group_confirmation().unwrap() {
    skipped 78 lines
  • ■ ■ ■ ■ ■ ■
    src/scanner.rs
    skipped 6 lines
    7 7  use rayon::prelude::*;
    8 8  use std::hash::Hasher;
    9 9  use std::time::Duration;
    10  -use std::{fs, path::PathBuf};
     10 +use std::{
     11 + fs,
     12 + path::{Path, PathBuf},
     13 +};
    11 14   
    12 15  #[derive(Clone, Copy)]
    13 16  enum IndexCritera {
    skipped 43 lines
    57 60   .progress_with_style(ProgressStyle::with_template(
    58 61   "{spinner:.green} [processing mapped paths] [{wide_bar:.cyan/blue}] {pos}/{len} files",
    59 62   )?)
    60  - .map(|fpath| fpath.display().to_string())
    61 63   .map(|fpath| File {
    62 64   path: fpath.clone(),
    63 65   hash: None,
    skipped 46 lines
    110 112   Ok(store)
    111 113  }
    112 114   
    113  -fn incremental_hashing(filepath: &str) -> Result<String> {
     115 +fn incremental_hashing(filepath: &Path) -> Result<String> {
    114 116   let file = fs::File::open(filepath)?;
    115 117   let fmap = unsafe { Mmap::map(&file)? };
    116 118   let mut inchasher = fxhash::FxHasher::default();
    skipped 4 lines
    121 123   Ok(format!("{}", inchasher.finish()))
    122 124  }
    123 125   
    124  -fn standard_hashing(filepath: &str) -> Result<String> {
     126 +fn standard_hashing(filepath: &Path) -> Result<String> {
    125 127   let file = fs::read(filepath)?;
    126 128   Ok(hasher(&*file).to_string())
    127 129  }
    128 130   
    129  -fn hash_file(filepath: &str) -> Result<String> {
     131 +fn hash_file(filepath: &Path) -> Result<String> {
    130 132   let filemeta = fs::metadata(filepath)?;
    131 133   
    132 134   // NOTE: USE INCREMENTAL HASHING ONLY FOR FILES > 100MB
    skipped 6 lines
Please wait...
Page is in error, reload to recover