From 1121bd3175562d7f682d32286a79a9f90a92f65b Mon Sep 17 00:00:00 2001 From: sreedev Date: Thu, 29 Dec 2022 22:04:41 -0500 Subject: [PATCH] bug fixes --- src/database.rs | 7 ++++--- src/output.rs | 42 ++++++++++++++++++++++-------------------- src/scanner.rs | 13 +++++++------ 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/database.rs b/src/database.rs index e7fb28a..e0e24fd 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,6 +1,6 @@ use anyhow::Result; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct File { pub path: String, pub hash: String, @@ -41,7 +41,7 @@ pub fn indexed_paths(connection: &sqlite::Connection) -> Result> { Ok(result) } -pub fn duplicate_hashes(connection: &sqlite::Connection) -> Result> { +pub fn duplicate_hashes(connection: &sqlite::Connection, path: &String) -> Result> { let query = format!( " SELECT a.* FROM files a @@ -50,8 +50,9 @@ pub fn duplicate_hashes(connection: &sqlite::Connection) -> Result> { GROUP BY hash HAVING count(*) > 1 ) b ON a.hash = b.hash + WHERE a.file_identifier LIKE \"{}%\" ORDER BY a.file_identifier - " + ", path ); let result: Vec = connection .prepare(query)? diff --git a/src/output.rs b/src/output.rs index ee4c634..4cd267a 100644 --- a/src/output.rs +++ b/src/output.rs @@ -1,10 +1,9 @@ use crate::database::File; -use itertools::Itertools; -use colored::Colorize; -use std::fs; use chrono::offset::Utc; use chrono::DateTime; +use colored::Colorize; use humansize::{format_size, DECIMAL}; +use std::{collections::HashMap, fs}; fn format_path(path: &String) -> String { let stringlen = path.len(); @@ -36,22 +35,25 @@ pub fn print(duplicates: Vec) { ); print_divider(); - duplicates - .into_iter() - .group_by(|record| record.hash.clone()) - .into_iter() - .for_each(|(_, group)| { - group - .into_iter() - .for_each(|file| { - println!( - "| {0: <16} | {1: <35} | {2: <16} | {3: <32} |", - &file.hash[0..16].red(), - format_path(&file.path).yellow(), - file_size(&file.path).blue(), - modified_time(&file.path).blue() - ); - }); - print_divider(); + let mut dup_index: HashMap> = HashMap::new(); + + duplicates.into_iter().for_each(|file| { + dup_index + .entry(file.hash.clone()) + .and_modify(|value| value.push(file.clone())) + .or_insert(vec![file]); + }); + + dup_index.into_iter().for_each(|(_, group)| { + group.into_iter().for_each(|file| { + println!( + "| {0: <16} | {1: <35} | {2: <16} | {3: <32} |", + &file.hash[0..16].red(), + format_path(&file.path).yellow(), + file_size(&file.path).blue(), + modified_time(&file.path).blue() + ); }); + print_divider(); + }); } diff --git a/src/scanner.rs b/src/scanner.rs index 1442cd2..c3d95f6 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -1,5 +1,3 @@ -use std::path::PathBuf; - use crate::database; use crate::{cli::App, database::File}; use anyhow::Result; @@ -7,11 +5,14 @@ use glob::glob; use itertools::Itertools; use rayon::prelude::*; use std::fs; +use std::path::PathBuf; pub fn duplicates(app_opts: App, connection: &sqlite::Connection) -> Result> { - let scan_results = scan(app_opts, connection)?; + let scan_results = scan(&app_opts, connection)?; + let base_path = get_directory(&app_opts)?; + index_files(scan_results, connection); - database::duplicate_hashes(connection) + database::duplicate_hashes(connection, &base_path) } fn get_directory(opts: &App) -> Result { @@ -55,8 +56,8 @@ fn is_indexed_file(path: &String, indexed: &Vec) -> bool { .contains(path) } -fn scan(app_opts: App, connection: &sqlite::Connection) -> Result> { - let directory = get_directory(&app_opts)?; +fn scan(app_opts: &App, connection: &sqlite::Connection) -> Result> { + let directory = get_directory(app_opts)?; let glob_patterns: Vec = get_glob_patterns(&app_opts, &directory); let indexed_paths = database::indexed_paths(connection)?; let files: Vec = glob_patterns