mirror of
https://github.com/sreedevk/deduplicator.git
synced 2026-09-01 13:06:43 +00:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
96fe667d3f | ||
|
|
c32e64450f | ||
|
|
85acbded46 | ||
|
|
f12cecc9ee | ||
|
|
05f513735e | ||
|
|
552f6c73f2 | ||
|
|
138b66038f | ||
|
|
d8e1de169d | ||
|
|
bc170f139e | ||
|
|
c76ad81a55 | ||
|
|
254e61cabe | ||
|
|
a76163f24f | ||
|
|
27fef21be0 | ||
|
|
8b00faf075 | ||
|
|
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]]
|
[[package]]
|
||||||
name = "deduplicator"
|
name = "deduplicator"
|
||||||
version = "0.0.1"
|
version = "0.0.5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "deduplicator"
|
name = "deduplicator"
|
||||||
version = "0.0.2"
|
version = "0.0.5"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "find,filter,delete Duplicates"
|
description = "find,filter,delete Duplicates"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
authors = ["Sreedev Kodichath <sreedevpadmakumar@gmail.com>", "Valentin Bersier <vbersier@gmail.com>", "Dhruva Sagar <dhruva.sagar@gmail.com>"]
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crossterm::event::{self, KeyCode, KeyEvent};
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use crossterm::event::{self, KeyCode, KeyEvent};
|
||||||
|
|
||||||
use super::events;
|
use super::events;
|
||||||
|
|
||||||
pub struct EventHandler;
|
pub struct EventHandler;
|
||||||
@@ -21,8 +22,7 @@ impl EventHandler {
|
|||||||
fn handle_keypress(keyevent: KeyEvent) -> Result<events::Event> {
|
fn handle_keypress(keyevent: KeyEvent) -> Result<events::Event> {
|
||||||
match keyevent.code {
|
match keyevent.code {
|
||||||
KeyCode::Char('q') => Ok(events::Event::Exit),
|
KeyCode::Char('q') => Ok(events::Event::Exit),
|
||||||
_ => Ok(events::Event::Noop)
|
_ => Ok(events::Event::Noop),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
pub enum Event {
|
pub enum Event {
|
||||||
Exit,
|
Exit,
|
||||||
Noop
|
Noop,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,13 @@
|
|||||||
mod event_handler;
|
mod event_handler;
|
||||||
mod events;
|
mod events;
|
||||||
mod ui;
|
|
||||||
mod formatter;
|
mod formatter;
|
||||||
|
mod ui;
|
||||||
|
|
||||||
|
use std::{io, thread, time::Duration};
|
||||||
|
|
||||||
use crate::database;
|
|
||||||
use crate::output;
|
|
||||||
use crate::params::Params;
|
|
||||||
use crate::scanner;
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use crossterm::{event, execute, terminal};
|
use crossterm::{event, execute, terminal};
|
||||||
use event_handler::EventHandler;
|
use event_handler::EventHandler;
|
||||||
use std::io;
|
|
||||||
use std::thread;
|
|
||||||
use std::time::Duration;
|
|
||||||
use tui::{
|
use tui::{
|
||||||
backend::CrosstermBackend,
|
backend::CrosstermBackend,
|
||||||
widgets::{Block, Borders, Widget},
|
widgets::{Block, Borders, Widget},
|
||||||
@@ -20,19 +15,24 @@ use tui::{
|
|||||||
};
|
};
|
||||||
use ui::Ui;
|
use ui::Ui;
|
||||||
|
|
||||||
|
use crate::database;
|
||||||
|
use crate::output;
|
||||||
|
use crate::params::Params;
|
||||||
|
use crate::scanner;
|
||||||
|
|
||||||
pub struct App;
|
pub struct App;
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn init(app_args: &Params) -> Result<()> {
|
pub fn init(app_args: &Params) -> Result<()> {
|
||||||
// let mut term = Self::init_terminal()?;
|
// let mut term = Self::init_terminal()?;
|
||||||
|
|
||||||
let connection = database::get_connection(&app_args)?;
|
let connection = database::get_connection(app_args)?;
|
||||||
let duplicates = scanner::duplicates(&app_args, &connection)?;
|
let duplicates = scanner::duplicates(app_args, &connection)?;
|
||||||
|
|
||||||
// Self::init_render_loop(&mut term)?;
|
// Self::init_render_loop(&mut term)?;
|
||||||
// Self::cleanup(&mut term)?;
|
// Self::cleanup(&mut term)?;
|
||||||
|
|
||||||
output::print(duplicates, &app_args); /* TODO: APP TUI INIT FUNCTION */
|
output::print(duplicates, app_args); /* TODO: APP TUI INIT FUNCTION */
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,6 +56,8 @@ impl App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn init_render_loop(term: &mut Terminal<CrosstermBackend<io::Stdout>>) -> Result<()> {
|
fn init_render_loop(term: &mut Terminal<CrosstermBackend<io::Stdout>>) -> Result<()> {
|
||||||
|
// this could be simplified with a `while Self::render_cycle(term).is_ok() {}` in the current state, but maybe
|
||||||
|
// it's good to keep it to handle errors in the future
|
||||||
loop {
|
loop {
|
||||||
match Self::render_cycle(term) {
|
match Self::render_cycle(term) {
|
||||||
Ok(_) => continue,
|
Ok(_) => continue,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use anyhow::Result;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
use tui::{
|
use tui::{
|
||||||
backend::{Backend, CrosstermBackend},
|
backend::{Backend, CrosstermBackend},
|
||||||
layout::{Constraint, Direction, Layout, Rect},
|
layout::{Constraint, Direction, Layout, Rect},
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
|
use std::env::temp_dir;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
use crate::params::Params;
|
use crate::params::Params;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -7,13 +10,18 @@ pub struct File {
|
|||||||
pub hash: String,
|
pub hash: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_connection(args: &Params) -> Result<sqlite::Connection, sqlite::Error> {
|
fn db_connection_url(args: &Params) -> String {
|
||||||
let connection_url = match args.nocache {
|
match args.nocache {
|
||||||
false => "/tmp/deduplicator.db",
|
true => String::from(":memory:"),
|
||||||
true => ":memory:"
|
false => {
|
||||||
};
|
let temp_dir_path = temp_dir();
|
||||||
|
format!("{}/deduplicator.db", temp_dir_path.display())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sqlite::open(connection_url).and_then(|conn| {
|
pub fn get_connection(args: &Params) -> Result<sqlite::Connection, sqlite::Error> {
|
||||||
|
sqlite::open(db_connection_url(args)).and_then(|conn| {
|
||||||
setup(&conn).ok();
|
setup(&conn).ok();
|
||||||
Ok(conn)
|
Ok(conn)
|
||||||
})
|
})
|
||||||
@@ -30,31 +38,28 @@ pub fn put(file: &File, connection: &sqlite::Connection) -> Result<()> {
|
|||||||
"INSERT INTO files (file_identifier, hash) VALUES (\"{}\", \"{}\")",
|
"INSERT INTO files (file_identifier, hash) VALUES (\"{}\", \"{}\")",
|
||||||
file.path, file.hash
|
file.path, file.hash
|
||||||
);
|
);
|
||||||
let result = connection.execute(query)?;
|
connection.execute(query)?;
|
||||||
|
Ok(())
|
||||||
Ok(result)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn indexed_paths(connection: &sqlite::Connection) -> Result<Vec<File>> {
|
pub fn indexed_paths(connection: &sqlite::Connection) -> Result<Vec<File>> {
|
||||||
let query = format!(
|
let query = "SELECT * FROM files";
|
||||||
"SELECT * FROM files"
|
|
||||||
);
|
|
||||||
|
|
||||||
let result: Vec<File> = connection
|
let result: Vec<File> = connection
|
||||||
.prepare(query)?
|
.prepare(query)?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|row_result| row_result.unwrap())
|
.filter_map(|row_result| row_result.ok())
|
||||||
.map(|row| {
|
.map(|row| {
|
||||||
let path = row.read::<&str, _>("file_identifier").to_string();
|
let path = row.read::<&str, _>("file_identifier").to_string();
|
||||||
let hash = row.read::<i64, _>("hash").to_string();
|
let hash = row.read::<i64, _>("hash").to_string();
|
||||||
File { path, hash }
|
File { path, hash }
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn duplicate_hashes(connection: &sqlite::Connection, path: &String) -> Result<Vec<File>> {
|
pub fn duplicate_hashes(connection: &sqlite::Connection, path: &str) -> Result<Vec<File>> {
|
||||||
let query = format!(
|
let query = format!(
|
||||||
"
|
"
|
||||||
SELECT a.* FROM files a
|
SELECT a.* FROM files a
|
||||||
@@ -65,13 +70,14 @@ pub fn duplicate_hashes(connection: &sqlite::Connection, path: &String) -> Resul
|
|||||||
ON a.hash = b.hash
|
ON a.hash = b.hash
|
||||||
WHERE a.file_identifier LIKE \"{}%\"
|
WHERE a.file_identifier LIKE \"{}%\"
|
||||||
ORDER BY a.file_identifier
|
ORDER BY a.file_identifier
|
||||||
", path
|
",
|
||||||
|
path
|
||||||
);
|
);
|
||||||
|
|
||||||
let result: Vec<File> = connection
|
let result: Vec<File> = connection
|
||||||
.prepare(query)?
|
.prepare(query)?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|row_result| row_result.unwrap())
|
.filter_map(|row_result| row_result.ok())
|
||||||
.map(|row| {
|
.map(|row| {
|
||||||
let path = row.read::<&str, _>("file_identifier").to_string();
|
let path = row.read::<&str, _>("file_identifier").to_string();
|
||||||
let hash = row.read::<i64, _>("hash").to_string();
|
let hash = row.read::<i64, _>("hash").to_string();
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
mod params;
|
#![allow(unused)] // TODO: remove this once TUI is implemented
|
||||||
|
mod app;
|
||||||
mod database;
|
mod database;
|
||||||
mod output;
|
mod output;
|
||||||
|
mod params;
|
||||||
mod scanner;
|
mod scanner;
|
||||||
mod app;
|
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Parser;
|
|
||||||
use app::App;
|
use app::App;
|
||||||
|
use clap::Parser;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
|
|||||||
@@ -1,33 +1,40 @@
|
|||||||
use crate::database::File;
|
use std::{collections::HashMap, fs};
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
use chrono::offset::Utc;
|
use chrono::offset::Utc;
|
||||||
use chrono::DateTime;
|
use chrono::DateTime;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use humansize::{format_size, DECIMAL};
|
use humansize::{format_size, DECIMAL};
|
||||||
use std::{collections::HashMap, fs};
|
|
||||||
|
use crate::database::File;
|
||||||
use crate::params::Params;
|
use crate::params::Params;
|
||||||
|
|
||||||
fn format_path(path: &String, opts: &Params) -> String {
|
fn format_path(path: &str, opts: &Params) -> Result<String> {
|
||||||
let display_path = path.replace(&opts.get_directory().unwrap(), "");
|
let display_path = path.replace(&opts.get_directory()?, "");
|
||||||
let display_range = if display_path.len() > 32 {
|
let text_vec = display_path.chars().collect::<Vec<_>>();
|
||||||
&display_path[(display_path.len() - 32)..]
|
|
||||||
|
let display_range = if text_vec.len() > 32 {
|
||||||
|
text_vec[(display_path.len() - 32)..]
|
||||||
|
.iter()
|
||||||
|
.collect::<String>()
|
||||||
} else {
|
} else {
|
||||||
&display_path[..]
|
display_path
|
||||||
};
|
};
|
||||||
|
|
||||||
format!("...{}", display_range)
|
Ok(format!("...{}", display_range))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn file_size(path: &String) -> String {
|
fn file_size(path: &String) -> Result<String> {
|
||||||
let mdata = fs::metadata(path).unwrap();
|
let mdata = fs::metadata(path)?;
|
||||||
let formatted_size = format_size(mdata.len(), DECIMAL);
|
let formatted_size = format_size(mdata.len(), DECIMAL);
|
||||||
format!("{}", formatted_size)
|
Ok(formatted_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn modified_time(path: &String) -> String {
|
fn modified_time(path: &String) -> Result<String> {
|
||||||
let mdata = fs::metadata(path).unwrap();
|
let mdata = fs::metadata(path)?;
|
||||||
let modified_time: DateTime<Utc> = mdata.modified().unwrap().into();
|
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() {
|
fn print_divider() {
|
||||||
@@ -48,17 +55,17 @@ pub fn print(duplicates: Vec<File>, opts: &Params) {
|
|||||||
dup_index
|
dup_index
|
||||||
.entry(file.hash.clone())
|
.entry(file.hash.clone())
|
||||||
.and_modify(|value| value.push(file.clone()))
|
.and_modify(|value| value.push(file.clone()))
|
||||||
.or_insert(vec![file]);
|
.or_insert_with(|| vec![file]);
|
||||||
});
|
});
|
||||||
|
|
||||||
dup_index.into_iter().for_each(|(_, group)| {
|
dup_index.iter().for_each(|(_, group)| {
|
||||||
group.into_iter().for_each(|file| {
|
group.iter().for_each(|file| {
|
||||||
println!(
|
println!(
|
||||||
"| {0: <16} | {1: <35} | {2: <16} | {3: <32} |",
|
"| {0: <16} | {1: <35} | {2: <16} | {3: <32} |",
|
||||||
file.hash.red(),
|
file.hash.red(),
|
||||||
format_path(&file.path, opts).yellow(),
|
format_path(&file.path, opts).unwrap_or_default().yellow(),
|
||||||
file_size(&file.path).blue(),
|
file_size(&file.path).unwrap_or_default().blue(),
|
||||||
modified_time(&file.path).blue()
|
modified_time(&file.path).unwrap_or_default().blue()
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
print_divider();
|
print_divider();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::path::PathBuf;
|
use std::{fs, path::PathBuf};
|
||||||
|
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use anyhow::Result;
|
|
||||||
use std::fs;
|
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(author, version, about, long_about = None)]
|
#[command(author, version, about, long_about = None)]
|
||||||
@@ -19,20 +19,17 @@ pub struct Params {
|
|||||||
|
|
||||||
impl Params {
|
impl Params {
|
||||||
pub fn get_directory(&self) -> Result<String> {
|
pub fn get_directory(&self) -> Result<String> {
|
||||||
let dir_string: String = self
|
let dir_pathbuf: PathBuf = self
|
||||||
.dir
|
.dir
|
||||||
.clone()
|
.clone()
|
||||||
.unwrap_or(std::env::current_dir()?)
|
.unwrap_or(std::env::current_dir()?)
|
||||||
.as_os_str()
|
.as_os_str()
|
||||||
.to_str()
|
.into();
|
||||||
.unwrap()
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
let dir_pathbuf = PathBuf::from(&dir_string);
|
let dir = fs::canonicalize(dir_pathbuf)?
|
||||||
let dir = fs::canonicalize(&dir_pathbuf)?
|
|
||||||
.as_os_str()
|
.as_os_str()
|
||||||
.to_str()
|
.to_str()
|
||||||
.unwrap()
|
.ok_or_else(|| anyhow!("Invalid directory"))?
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
Ok(dir)
|
Ok(dir)
|
||||||
|
|||||||
@@ -1,55 +1,61 @@
|
|||||||
use crate::database;
|
use std::{fs, path::PathBuf};
|
||||||
use crate::{params::Params, database::File};
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use fxhash::hash32 as hasher;
|
||||||
use glob::glob;
|
use glob::glob;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use std::fs;
|
|
||||||
use std::path::PathBuf;
|
use crate::{
|
||||||
use fxhash::hash32 as hasher;
|
database::{self, File},
|
||||||
|
params::Params,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn duplicates(app_opts: &Params, connection: &sqlite::Connection) -> Result<Vec<File>> {
|
pub fn duplicates(app_opts: &Params, connection: &sqlite::Connection) -> Result<Vec<File>> {
|
||||||
let scan_results = scan(app_opts, connection)?;
|
let scan_results = scan(app_opts, connection)?;
|
||||||
let base_path = app_opts.get_directory()?;
|
let base_path = app_opts.get_directory()?;
|
||||||
|
|
||||||
index_files(scan_results, connection);
|
index_files(scan_results, connection)?;
|
||||||
database::duplicate_hashes(connection, &base_path)
|
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
|
opts.types
|
||||||
.clone()
|
.clone()
|
||||||
.unwrap_or(String::from("*"))
|
.unwrap_or_else(|| String::from("*"))
|
||||||
.split(",")
|
.split(',')
|
||||||
.map(|filetype| format!("*.{}", filetype))
|
.map(|filetype| format!("*.{}", filetype))
|
||||||
.map(|filetype| {
|
.map(|filetype| {
|
||||||
vec![directory.clone(), String::from("**"), filetype]
|
vec![directory.to_owned(), String::from("**"), filetype]
|
||||||
.iter()
|
.iter()
|
||||||
.collect()
|
.collect()
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_indexed_file(path: &String, indexed: &Vec<File>) -> bool {
|
fn is_indexed_file(path: impl Into<String>, indexed: &[File]) -> bool {
|
||||||
indexed
|
indexed
|
||||||
.into_iter()
|
.iter()
|
||||||
.map(|file| file.path.clone())
|
.map(|file| file.path.clone())
|
||||||
.contains(path)
|
.contains(&path.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scan(app_opts: &Params, connection: &sqlite::Connection) -> Result<Vec<String>> {
|
fn scan(app_opts: &Params, connection: &sqlite::Connection) -> Result<Vec<String>> {
|
||||||
let directory = app_opts.get_directory()?;
|
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 indexed_paths = database::indexed_paths(connection)?;
|
||||||
let files: Vec<String> = glob_patterns
|
let files: Vec<String> = glob_patterns
|
||||||
.into_par_iter()
|
.par_iter()
|
||||||
.map(|glob_pattern| glob(&glob_pattern.as_os_str().to_str().unwrap()))
|
.filter_map(|glob_pattern| glob(glob_pattern.as_os_str().to_str()?).ok())
|
||||||
.map(|glob_result| glob_result.unwrap())
|
|
||||||
.flat_map(|file_vec| {
|
.flat_map(|file_vec| {
|
||||||
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(|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::<Vec<String>>()
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
@@ -57,18 +63,18 @@ fn scan(app_opts: &Params, connection: &sqlite::Connection) -> Result<Vec<String
|
|||||||
Ok(files)
|
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
|
let hashed: Vec<File> = files
|
||||||
.into_par_iter()
|
.into_par_iter()
|
||||||
.map(|file| {
|
.filter_map(|file| {
|
||||||
let hash = hash_file(&file).unwrap();
|
let hash = hash_file(&file).ok()?;
|
||||||
database::File { path: file, hash }
|
Some(database::File { path: file, hash })
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
hashed.into_iter().for_each(|file| {
|
hashed
|
||||||
database::put(&file, connection).unwrap();
|
.iter()
|
||||||
});
|
.try_for_each(|file| database::put(file, connection))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hash_file(filepath: &str) -> Result<String> {
|
pub fn hash_file(filepath: &str) -> Result<String> {
|
||||||
|
|||||||
Reference in New Issue
Block a user