mirror of
https://github.com/sreedevk/deduplicator.git
synced 2026-08-26 18:15:33 +00:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e158a8267a | ||
|
|
e6f93ce3d6 | ||
|
|
011da05c59 | ||
|
|
6087abe960 | ||
|
|
5eba7cdc30 | ||
|
|
5a65550e58 | ||
|
|
0b5effd06e | ||
|
|
cc243f413a | ||
|
|
ad467d70a8 | ||
|
|
4490ea4c69 | ||
|
|
8ed0b13ff0 | ||
|
|
be7e8d38ae | ||
|
|
c9694dea09 | ||
|
|
3a7ac7a332 | ||
|
|
51521f1641 | ||
|
|
b23db06847 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -269,7 +269,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "deduplicator"
|
||||
version = "0.0.1"
|
||||
version = "0.0.4"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"chrono",
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
[package]
|
||||
name = "deduplicator"
|
||||
version = "0.0.2"
|
||||
version = "0.0.4"
|
||||
edition = "2021"
|
||||
description = "find,filter,delete Duplicates"
|
||||
license = "MIT"
|
||||
authors = ["Sreedev Kodichath <sreedevpadmakumar@gmail.com>", "Valentin Bersier <vbersier@gmail.com>"]
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
|
||||
@@ -1,33 +1,40 @@
|
||||
use crate::database::File;
|
||||
use std::{collections::HashMap, fs};
|
||||
|
||||
use anyhow::Result;
|
||||
use chrono::offset::Utc;
|
||||
use chrono::DateTime;
|
||||
use colored::Colorize;
|
||||
use humansize::{format_size, DECIMAL};
|
||||
use std::{collections::HashMap, fs};
|
||||
|
||||
use crate::database::File;
|
||||
use crate::params::Params;
|
||||
|
||||
fn format_path(path: &String, opts: &Params) -> String {
|
||||
let display_path = path.replace(&opts.get_directory().unwrap(), "");
|
||||
let display_range = if display_path.len() > 32 {
|
||||
&display_path[(display_path.len() - 32)..]
|
||||
fn format_path(path: &str, opts: &Params) -> Result<String> {
|
||||
let display_path = path.replace(&opts.get_directory()?, "");
|
||||
let text_vec = display_path.chars().collect::<Vec<_>>();
|
||||
|
||||
let display_range = if text_vec.len() > 32 {
|
||||
text_vec[(display_path.len() - 32)..]
|
||||
.iter()
|
||||
.collect::<String>()
|
||||
} else {
|
||||
&display_path[..]
|
||||
display_path
|
||||
};
|
||||
|
||||
format!("...{}", display_range)
|
||||
|
||||
Ok(format!("...{}", display_range))
|
||||
}
|
||||
|
||||
fn file_size(path: &String) -> String {
|
||||
let mdata = fs::metadata(path).unwrap();
|
||||
fn file_size(path: &String) -> Result<String> {
|
||||
let mdata = fs::metadata(path)?;
|
||||
let formatted_size = format_size(mdata.len(), DECIMAL);
|
||||
format!("{}", formatted_size)
|
||||
Ok(formatted_size)
|
||||
}
|
||||
|
||||
fn modified_time(path: &String) -> String {
|
||||
let mdata = fs::metadata(path).unwrap();
|
||||
let modified_time: DateTime<Utc> = mdata.modified().unwrap().into();
|
||||
fn modified_time(path: &String) -> Result<String> {
|
||||
let mdata = fs::metadata(path)?;
|
||||
let modified_time: DateTime<Utc> = mdata.modified()?.into();
|
||||
|
||||
modified_time.format("%Y-%m-%d %H:%M:%S").to_string()
|
||||
Ok(modified_time.format("%Y-%m-%d %H:%M:%S").to_string())
|
||||
}
|
||||
|
||||
fn print_divider() {
|
||||
@@ -48,17 +55,17 @@ pub fn print(duplicates: Vec<File>, opts: &Params) {
|
||||
dup_index
|
||||
.entry(file.hash.clone())
|
||||
.and_modify(|value| value.push(file.clone()))
|
||||
.or_insert(vec![file]);
|
||||
.or_insert_with(|| vec![file]);
|
||||
});
|
||||
|
||||
dup_index.into_iter().for_each(|(_, group)| {
|
||||
group.into_iter().for_each(|file| {
|
||||
dup_index.iter().for_each(|(_, group)| {
|
||||
group.iter().for_each(|file| {
|
||||
println!(
|
||||
"| {0: <16} | {1: <35} | {2: <16} | {3: <32} |",
|
||||
file.hash.red(),
|
||||
format_path(&file.path, opts).yellow(),
|
||||
file_size(&file.path).blue(),
|
||||
modified_time(&file.path).blue()
|
||||
format_path(&file.path, opts).unwrap_or_default().yellow(),
|
||||
file_size(&file.path).unwrap_or_default().blue(),
|
||||
modified_time(&file.path).unwrap_or_default().blue()
|
||||
);
|
||||
});
|
||||
print_divider();
|
||||
|
||||
@@ -1,55 +1,61 @@
|
||||
use crate::database;
|
||||
use crate::{params::Params, database::File};
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
use anyhow::Result;
|
||||
use fxhash::hash32 as hasher;
|
||||
use glob::glob;
|
||||
use itertools::Itertools;
|
||||
use rayon::prelude::*;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use fxhash::hash32 as hasher;
|
||||
|
||||
use crate::{
|
||||
database::{self, File},
|
||||
params::Params,
|
||||
};
|
||||
|
||||
pub fn duplicates(app_opts: &Params, connection: &sqlite::Connection) -> Result<Vec<File>> {
|
||||
let scan_results = scan(app_opts, connection)?;
|
||||
let base_path = app_opts.get_directory()?;
|
||||
|
||||
index_files(scan_results, connection);
|
||||
index_files(scan_results, connection)?;
|
||||
database::duplicate_hashes(connection, &base_path)
|
||||
}
|
||||
|
||||
fn get_glob_patterns(opts: &Params, directory: &String) -> Vec<PathBuf> {
|
||||
fn get_glob_patterns(opts: &Params, directory: &str) -> Vec<PathBuf> {
|
||||
opts.types
|
||||
.clone()
|
||||
.unwrap_or(String::from("*"))
|
||||
.split(",")
|
||||
.unwrap_or_else(|| String::from("*"))
|
||||
.split(',')
|
||||
.map(|filetype| format!("*.{}", filetype))
|
||||
.map(|filetype| {
|
||||
vec![directory.clone(), String::from("**"), filetype]
|
||||
vec![directory.to_owned(), String::from("**"), filetype]
|
||||
.iter()
|
||||
.collect()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn is_indexed_file(path: &String, indexed: &Vec<File>) -> bool {
|
||||
fn is_indexed_file(path: impl Into<String>, indexed: &[File]) -> bool {
|
||||
indexed
|
||||
.into_iter()
|
||||
.iter()
|
||||
.map(|file| file.path.clone())
|
||||
.contains(path)
|
||||
.contains(&path.into())
|
||||
}
|
||||
|
||||
fn scan(app_opts: &Params, connection: &sqlite::Connection) -> Result<Vec<String>> {
|
||||
let directory = app_opts.get_directory()?;
|
||||
let glob_patterns: Vec<PathBuf> = get_glob_patterns(&app_opts, &directory);
|
||||
let glob_patterns: Vec<PathBuf> = get_glob_patterns(app_opts, &directory);
|
||||
let indexed_paths = database::indexed_paths(connection)?;
|
||||
let files: Vec<String> = glob_patterns
|
||||
.into_par_iter()
|
||||
.map(|glob_pattern| glob(&glob_pattern.as_os_str().to_str().unwrap()))
|
||||
.map(|glob_result| glob_result.unwrap())
|
||||
.par_iter()
|
||||
.filter_map(|glob_pattern| glob(glob_pattern.as_os_str().to_str()?).ok())
|
||||
.flat_map(|file_vec| {
|
||||
file_vec
|
||||
.map(|x| x.unwrap().as_os_str().to_str().unwrap().to_string())
|
||||
.filter_map(|x| Some(x.ok()?.as_os_str().to_str()?.to_string()))
|
||||
.filter(|fpath| !is_indexed_file(fpath, &indexed_paths))
|
||||
.filter(|glob_result| fs::metadata(glob_result).unwrap().is_file())
|
||||
.filter(|glob_result| {
|
||||
fs::metadata(glob_result)
|
||||
.map(|f| f.is_file())
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
})
|
||||
.collect();
|
||||
@@ -57,18 +63,18 @@ fn scan(app_opts: &Params, connection: &sqlite::Connection) -> Result<Vec<String
|
||||
Ok(files)
|
||||
}
|
||||
|
||||
fn index_files(files: Vec<String>, connection: &sqlite::Connection) {
|
||||
fn index_files(files: Vec<String>, connection: &sqlite::Connection) -> Result<()> {
|
||||
let hashed: Vec<File> = files
|
||||
.into_par_iter()
|
||||
.map(|file| {
|
||||
let hash = hash_file(&file).unwrap();
|
||||
database::File { path: file, hash }
|
||||
.filter_map(|file| {
|
||||
let hash = hash_file(&file).ok()?;
|
||||
Some(database::File { path: file, hash })
|
||||
})
|
||||
.collect();
|
||||
|
||||
hashed.into_iter().for_each(|file| {
|
||||
database::put(&file, connection).unwrap();
|
||||
});
|
||||
hashed
|
||||
.iter()
|
||||
.try_for_each(|file| database::put(file, connection))
|
||||
}
|
||||
|
||||
pub fn hash_file(filepath: &str) -> Result<String> {
|
||||
|
||||
Reference in New Issue
Block a user