Skip to content

Commit

Permalink
Add verification to the database when the setup fonction is called. I…
Browse files Browse the repository at this point in the history
…t's prevent error when user delete element directly on the trash folder
  • Loading branch information
AmineZouitine committed May 12, 2023
1 parent a1d7762 commit c76f491
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/data_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ use std::process::exit;
use crate::trash_item::TrashItem;
use crate::{database_errors::RmtDataBaseErrors, structure_manager};
use rusqlite::{params, types::FromSql, Connection, Row};
use std::path::{Path, MAIN_SEPARATOR};

// Create the database and the table to save information about deleted elements.
pub fn setup_data_base(is_test: bool) -> Connection {
let connection = create_database(is_test);
verification_database_item_exist_in_tash_folder(&connection, is_test);

connection
}

// Create the database and the table to save information about deleted elements. (if database doesn't already exist)
pub fn create_database(is_test: bool) -> Connection {
let connection = structure_manager::create_data_base_file(is_test);
let table_name = structure_manager::get_data_base_table_name(is_test);

Expand Down Expand Up @@ -36,6 +44,24 @@ pub fn setup_data_base(is_test: bool) -> Connection {
}
}

// Check if every element from the database exist on the trash
// It's prevent error if user delete (without using rmt --td or rm --tf) element direclty on the trash folder
pub fn verification_database_item_exist_in_tash_folder(connection: &Connection, is_test: bool) {
let trash_items = find_all_trash_items(connection, is_test);

trash_items.iter().for_each(|item| {
let path = format!(
"{}{}{}",
structure_manager::get_trash_directory_path(is_test),
MAIN_SEPARATOR,
item.hash
);
if !Path::new(&path).exists() {
delete_trash_item(connection, item.id, is_test);
}
})
}

fn get<T: FromSql>(row: &Row, index: usize) -> T {
match row.get(index) {
Ok(element) => element,
Expand Down

0 comments on commit c76f491

Please sign in to comment.