14 Commits
0.0.4 ... 0.0.5

Author SHA1 Message Date
sreedev
96fe667d3f version 0.0.5 2023-01-09 11:08:20 -05:00
sreedev
c32e64450f bug fix: connection result issue 2023-01-09 11:04:29 -05:00
Sreedev Kodichath
85acbded46 Merge pull request #13 from beeb/panics
Fix some more unhandled errors
2023-01-09 11:03:12 -05:00
Sreedev Kodichath
f12cecc9ee Merge branch 'main' into panics 2023-01-09 11:02:56 -05:00
sreedev
05f513735e tempdir fixes 2023-01-09 11:01:52 -05:00
Valentin Bersier
552f6c73f2 Merge branch 'main' into panics 2023-01-09 16:59:42 +01:00
Valentin Bersier
138b66038f refactor: various clippy fixes 2023-01-09 16:57:43 +01:00
Sreedev Kodichath
d8e1de169d Merge pull request #12 from dhruvasagar/fix/temp_file_path
Fix Temporary File Path #6
2023-01-09 10:48:28 -05:00
Valentin Bersier
bc170f139e style: remove trailing spaces 2023-01-09 16:46:12 +01:00
Valentin Bersier
c76ad81a55 refactor: do no unwrap in database.rs 2023-01-09 16:41:55 +01:00
Valentin Bersier
254e61cabe fix: clippy warnings 2023-01-09 16:27:52 +01:00
Valentin Bersier
a76163f24f style: format 2023-01-09 16:21:13 +01:00
Valentin Bersier
27fef21be0 fix: no unwrap in params.rs 2023-01-09 16:17:50 +01:00
Dhruva Sagar
8b00faf075 Fix Temporary File Path #6
Better cross platform support
2023-01-09 10:53:20 +05:30
9 changed files with 80 additions and 73 deletions

2
Cargo.lock generated
View File

@@ -269,7 +269,7 @@ dependencies = [
[[package]] [[package]]
name = "deduplicator" name = "deduplicator"
version = "0.0.4" version = "0.0.5"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"chrono", "chrono",

View File

@@ -1,10 +1,10 @@
[package] [package]
name = "deduplicator" name = "deduplicator"
version = "0.0.4" 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>"] 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

View File

@@ -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),
} }
} }
} }

View File

@@ -1,4 +1,4 @@
pub enum Event { pub enum Event {
Exit, Exit,
Noop Noop,
} }

View File

@@ -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,

View File

@@ -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},

View File

@@ -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();

View File

@@ -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<()> {

View File

@@ -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)