Skip to content

Commit

Permalink
Added new file and file removal detection on hash list
Browse files Browse the repository at this point in the history
  • Loading branch information
okynos committed Jan 15, 2025
1 parent 26463c0 commit 3c64af1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
21 changes: 21 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,27 @@ impl DB {

// ------------------------------------------------------------------------

pub fn get_file_list(&self, path: String) -> Vec<DBFile> {
let connection = self.open();
let mut list = Vec::new();
let mut statement = connection.prepare_cached("SELECT * FROM files WHERE path LIKE '?1%'").unwrap();
let file_iter = statement.query_map([path], |row| {
Ok(DBFile {
id: row.get(0).unwrap(),
timestamp: row.get(1).unwrap(),
hash: row.get(2).unwrap(),
path: row.get(3).unwrap(),
size: row.get(4).unwrap()
})
}).unwrap();
for file in file_iter {
list.push(file.unwrap())
}
list
}

// ------------------------------------------------------------------------

pub fn update_file(&self, cfg: AppConfig, dbfile: DBFile) -> Option<DBFile>{
let connection = self.open();
let current_dbfile = DBFile::new(cfg, &dbfile.path, Some(dbfile.id));
Expand Down
2 changes: 0 additions & 2 deletions src/hashevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ use reqwest::Client;
use std::time::Duration;

pub struct HashEvent {
//dbfile: DBFile,
previous_dbfile: DBFile,
current_dbfile: DBFile,
//operation: String
}

impl HashEvent {
Expand Down
32 changes: 25 additions & 7 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ use crate::db;
use crate::dbfile::*;
use crate::appconfig::AppConfig;
use crate::hashevent::HashEvent;
use crate::utils;

use walkdir::WalkDir;
use log::*;
use std::collections::HashSet;

pub fn scan_path(cfg: AppConfig, root: String) {
let db = db::DB::new();
Expand All @@ -23,7 +25,7 @@ pub fn scan_path(cfg: AppConfig, root: String) {

// ----------------------------------------------------------------------------

pub async fn check_changes(cfg: AppConfig, root: String) {
pub async fn check_path(cfg: AppConfig, root: String) {
let db = db::DB::new();
for res in WalkDir::new(root) {
let entry = res.unwrap();
Expand All @@ -43,7 +45,7 @@ pub async fn check_changes(cfg: AppConfig, root: String) {
let event = HashEvent::new(dbfile, data);
event.process(cfg.clone()).await;
},
None => debug!("Could not get current DBFile data.")
None => warn!("Could not update file information in database, file: '{}'", path.display())
}
}
},
Expand All @@ -52,9 +54,9 @@ pub async fn check_changes(cfg: AppConfig, root: String) {
debug!("New file '{}' found in directory.", path.display());
let dbfile = DBFile::new(cfg.clone(), path.to_str().unwrap(), None);
db.insert_file(dbfile);
// Trigger new event
// In this case we don't trigger an event due the watcher will trigger new file in monitoring path.
} else {
error!("Could not get file '{}' databse information, Error: {:?}", path.display(), e)
error!("Could not get file '{}' information from database, Error: {:?}", path.display(), e)
}
}
};
Expand All @@ -64,11 +66,27 @@ pub async fn check_changes(cfg: AppConfig, root: String) {

// ----------------------------------------------------------------------------

pub fn update_db(cfg: AppConfig, root: String) {
let db = db::DB::new();

let list = db.get_file_list(root.clone());
let path_list = utils::get_path_file_list(root);

//path_list.iter().filter()

let path_set: HashSet<_> = path_list.iter().collect();
let diff: Vec<_> = list.iter().filter(|item| !path_set.contains(&item.path)).collect();
println!("DIFF: {:?}", diff);
}

// ----------------------------------------------------------------------------

pub async fn first_scan(cfg: AppConfig, root: String) {
let db = db::DB::new();
if ! db.is_empty() {
check_changes(cfg, root).await;
} else {
if db.is_empty() {
scan_path(cfg, root);
} else {
check_path(cfg.clone(), root.clone()).await;
update_db(cfg, root);
}
}
11 changes: 11 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use log::{warn, debug, error};
// To manage maps
use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};
use walkdir::WalkDir;

// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -278,6 +279,16 @@ pub fn get_current_time_millis() -> String {

// ----------------------------------------------------------------------------

pub fn get_path_file_list(root: String) -> Vec<String> {
let mut list = Vec::new();
for result in WalkDir::new(root) {
list.push(String::from(result.unwrap().path().to_str().unwrap()))
}
list
}

// ----------------------------------------------------------------------------

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 3c64af1

Please sign in to comment.