Skip to content

Commit

Permalink
Changed DBFile ID attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
okynos committed Jan 4, 2025
1 parent 47f88f3 commit 26463c0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 83 deletions.
86 changes: 39 additions & 47 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
use crate::appconfig;
use crate::utils;
use crate::dbfile::*;
use crate::appconfig::AppConfig;

use rusqlite::{Connection, Error};
use rusqlite::{Connection, Error, params};
use rusqlite::Error::QueryReturnedNoRows;
use std::path::Path;
use log::*;
Expand Down Expand Up @@ -87,13 +88,11 @@ impl DB {
let connection = self.open();
let result = connection.execute(
"CREATE TABLE IF NOT EXISTS files (
dbid INTEGER PRIMARY KEY,
id TEXT PRIMARY KEY,
timestamp TEXT NOT NULL,
hash TEXT NOT NULL,
path TEXT NOT NULL UNIQUE,
size INTEGER,
PRIMARY KEY(dbid, id) )",
size INTEGER)",
(),
);
match result {
Expand All @@ -108,8 +107,8 @@ impl DB {
pub fn insert_file(&self, file: DBFile) {
let connection = self.open();
let result = connection.execute(
"INSERT INTO files (timestamp, hash, path, size) VALUES (?1, ?2, ?3, ?4)",
(file.timestamp, file.hash, file.path, file.size)
"INSERT INTO files (id, timestamp, hash, path, size) VALUES (?1, ?2, ?3, ?4, ?5)",
(file.id, file.timestamp, file.hash, file.path, file.size)
);
match result {
Ok(_) => debug!("Inserted new file in DB"),
Expand All @@ -126,12 +125,11 @@ impl DB {
"SELECT * FROM files WHERE path = ?1 LIMIT 1",
[path.clone()],
|row| Ok(DBFile {
dbid: row.get(0).unwrap(),
id: row.get(1).unwrap(),
timestamp: row.get(2).unwrap(),
hash: row.get(3).unwrap(),
path: row.get(4).unwrap(),
size: row.get(5).unwrap()
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()
})
);

Expand All @@ -154,18 +152,17 @@ impl DB {

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

pub fn get_file_by_id(&self, dbid: u64) -> DBFile {
pub fn get_file_by_id(&self, id: String) -> DBFile {
let connection = self.open();
let data = connection.query_row(
"SELECT * FROM files WHERE dbid = ?1 LIMIT 1",
[dbid],
"SELECT * FROM files WHERE id = ?1 LIMIT 1",
[id],
|row| Ok(DBFile {
dbid: row.get(0).unwrap(),
id: row.get(1).unwrap(),
timestamp: row.get(2).unwrap(),
hash: row.get(3).unwrap(),
path: row.get(4).unwrap(),
size: row.get(5).unwrap()
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();

Expand All @@ -175,32 +172,28 @@ impl DB {

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

pub fn update_file(&self, dbfile: DBFile, timestamp: Option<String>, hash: Option<String>, size: Option<u64>) {
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));

let timestamp_str = match timestamp {
Some(t) => format!("timestamp = '{}'", t),
None => String::new()
};
let hash_str = match hash {
Some(h) => format!("hash = '{}'", h),
None => String::new()
};
let size_str = match size {
Some(s) => format!("size = '{}'", s),
None => String::new()
};

let query = format!("UPDATE files SET {}, {}, {} WHERE dbid = {}",
timestamp_str, hash_str, size_str, dbfile.dbid);
let query = "UPDATE files SET timestamp = ?1, hash = ?2, size = ?3 WHERE id = ?4";

let mut statement = connection.prepare(&query).unwrap();
let result = statement.execute([]);
let result = statement.execute(params![
current_dbfile.timestamp,
current_dbfile.hash,
current_dbfile.size,
current_dbfile.id]);
match result {
Ok(_v) => debug!("File '{}', updated with new information.", dbfile.path),
Err(e) => error!("Cannot update file '{}' information, Error: {:?}", dbfile.path, e)
Ok(_v) => {
debug!("File '{}', updated with new information.", dbfile.path);
Some(current_dbfile)
},
Err(e) => {
error!("Cannot update file '{}' information, Error: {:?}", dbfile.path, e);
None
}
}

}

// ------------------------------------------------------------------------
Expand All @@ -211,12 +204,11 @@ impl DB {
"SELECT * from files").unwrap();
let files = query.query_map([], |row|{
Ok(DBFile {
dbid: row.get(0).unwrap(),
id: row.get(1).unwrap(),
timestamp: row.get(2).unwrap(),
hash: row.get(3).unwrap(),
path: row.get(4).unwrap(),
size: row.get(5).unwrap(),
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();

Expand Down
13 changes: 7 additions & 6 deletions src/dbfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub struct DBFileError {
}

pub struct DBFile {
pub dbid: u64,
pub id: String,
pub timestamp: String,
pub hash: String,
Expand Down Expand Up @@ -67,7 +66,6 @@ impl fmt::Debug for DBFileError {
impl fmt::Debug for DBFile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result{
f.debug_tuple("")
.field(&self.dbid)
.field(&self.id)
.field(&self.timestamp)
.field(&self.hash)
Expand All @@ -88,7 +86,7 @@ impl fmt::Display for DBFile {
// ----------------------------------------------------------------------------

impl DBFile {
pub fn new(cfg: AppConfig, path: &str) -> Self {
pub fn new(cfg: AppConfig, path: &str, id: Option<String>) -> Self {
let size = Path::new(path).metadata().unwrap().len();
let hash = match cfg.clone().checksum_method.as_str() {
"Partial" => hash::get_partial_checksum(
Expand All @@ -101,9 +99,13 @@ impl DBFile {
Sha256::new())
};

let target_id = match id {
Some(data) => data,
None => utils::get_uuid()
};

DBFile {
dbid: 0,
id: utils::get_uuid(),
id: target_id,
timestamp: utils::get_current_time_millis(),
hash,
path: String::from(path),
Expand All @@ -115,7 +117,6 @@ impl DBFile {

pub fn clone(&self) -> Self {
DBFile {
dbid: self.dbid,
id: self.id.clone(),
timestamp: self.timestamp.clone(),
hash: self.hash.clone(),
Expand Down
27 changes: 18 additions & 9 deletions src/hashevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ use reqwest::Client;
use std::time::Duration;

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

impl HashEvent {
pub fn new(dbfile: DBFile) -> Self {
pub fn new(previous_dbfile: DBFile, current_dbfile: DBFile) -> Self {
HashEvent {
dbfile
previous_dbfile,
current_dbfile
}
}

Expand Down Expand Up @@ -72,7 +76,7 @@ impl HashEvent {
}
// Elastic endpoint integration
} else {
let request_url = format!("{}/{}/_doc/{}", cfg.endpoint_address, index, self.dbfile.id);
let request_url = format!("{}/{}/_doc/{}", cfg.endpoint_address, index, self.current_dbfile.id);
let client = Client::builder()
.danger_accept_invalid_certs(cfg.insecure)
.timeout(Duration::from_secs(30))
Expand Down Expand Up @@ -113,11 +117,16 @@ impl HashEvent {

fn get_json(&self) -> serde_json::Value {
json!({
"dbfile.id": self.dbfile.id.clone(),
"dbfile.timestamp": self.dbfile.timestamp.clone(),
"dbfile.hash": self.dbfile.hash.clone(),
"dbfile.path": self.dbfile.path.clone(),
"dbfile.size": self.dbfile.size.clone()
"previous_dbfile.id": self.previous_dbfile.id.clone(),
"previous_dbfile.timestamp": self.previous_dbfile.timestamp.clone(),
"previous_dbfile.hash": self.previous_dbfile.hash.clone(),
"previous_dbfile.path": self.previous_dbfile.path.clone(),
"previous_dbfile.size": self.previous_dbfile.size.clone(),
"current_dbfile.id": self.current_dbfile.id.clone(),
"current_dbfile.timestamp": self.current_dbfile.timestamp.clone(),
"current_dbfile.hash": self.current_dbfile.hash.clone(),
"current_dbfile.path": self.current_dbfile.path.clone(),
"current_dbfile.size": self.current_dbfile.size.clone()
})
}
}
2 changes: 1 addition & 1 deletion src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub async fn monitor(
Ok(_d) => {
debug!("Monitoring '{}' path.", path);
debug!("Starting file scan to create hash database.");
scanner::first_scan(cfg.clone(), String::from(path));
scanner::first_scan(cfg.clone(), String::from(path)).await;
debug!("Path '{}' scanned all files are hashed in DB.", path);
},
Err(e) => warn!("Could not monitor given path '{}', description: {}", path, e)
Expand Down
33 changes: 13 additions & 20 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,28 @@

use crate::db;
use crate::dbfile::*;
use crate::utils;
use crate::appconfig::AppConfig;
use crate::hashevent::HashEvent;

use walkdir::WalkDir;
use log::*;

// Temporal
use tokio::runtime::Runtime;

pub fn scan_path(cfg: AppConfig, root: String) {
let db = db::DB::new();
for res in WalkDir::new(root) {
let entry = res.unwrap();
let metadata = entry.metadata().unwrap();
let path = entry.path();
if metadata.clone().is_file(){


let dbfile = DBFile::new(cfg.clone(), path.to_str().unwrap());
let dbfile = DBFile::new(cfg.clone(), path.to_str().unwrap(), None);
db.insert_file(dbfile);
}
}
}

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

pub fn check_changes(cfg: AppConfig, root: String) {
pub async fn check_changes(cfg: AppConfig, root: String) {
let db = db::DB::new();
for res in WalkDir::new(root) {
let entry = res.unwrap();
Expand All @@ -43,21 +37,20 @@ pub fn check_changes(cfg: AppConfig, root: String) {
let hash = dbfile.get_disk_hash(cfg.clone());
if dbfile.hash != hash {
debug!("The file '{}', has changed.", path.display());
db.update_file(
dbfile.clone(),
Some(utils::get_current_time_millis()),
Some(hash),
Some(metadata.len()));
let event = HashEvent::new(dbfile);
let rt = Runtime::new().unwrap();
rt.block_on(event.process(cfg.clone()));
// Trigger new event
let current_dbfile = db.update_file(cfg.clone(), dbfile.clone());
match current_dbfile {
Some(data) => {
let event = HashEvent::new(dbfile, data);
event.process(cfg.clone()).await;
},
None => debug!("Could not get current DBFile data.")
}
}
},
Err(e) => {
if e.kind() == "DBFileNotFoundError" {
debug!("New file '{}' found in directory.", path.display());
let dbfile = DBFile::new(cfg.clone(), path.to_str().unwrap());
let dbfile = DBFile::new(cfg.clone(), path.to_str().unwrap(), None);
db.insert_file(dbfile);
// Trigger new event
} else {
Expand All @@ -71,10 +64,10 @@ pub fn check_changes(cfg: AppConfig, root: String) {

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

pub fn first_scan(cfg: AppConfig, root: String) {
pub async fn first_scan(cfg: AppConfig, root: String) {
let db = db::DB::new();
if ! db.is_empty() {
check_changes(cfg, root);
check_changes(cfg, root).await;
} else {
scan_path(cfg, root);
}
Expand Down

0 comments on commit 26463c0

Please sign in to comment.