From 1fe37274c15c24112c5a09a7c0e967cd2484fa38 Mon Sep 17 00:00:00 2001 From: Jenna Johnson <jenna@a2-ai.com> Date: Mon, 1 Apr 2024 14:12:25 -0400 Subject: [PATCH] dvs add errors --- src/library/add.rs | 68 ++++++++++++++++------------ src/test_directory/test1.txt.dvsmeta | 2 +- src/test_directory/test2.txt.dvsmeta | 2 +- src/test_directory/test3.txt.dvsmeta | 2 +- 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/library/add.rs b/src/library/add.rs index 35981a0..3f73254 100644 --- a/src/library/add.rs +++ b/src/library/add.rs @@ -28,8 +28,8 @@ pub struct AddedFile { } pub fn dvs_add(files: &Vec<String>, message: &String) -> Result<Vec<AddedFile>> { - // Get git root - let git_dir = repo::get_nearest_repo_dir(&PathBuf::from(".")).with_context(|| "could not find git repo root - make sure you're in an active git repository")?; + // Get git root + let git_dir = repo::get_nearest_repo_dir(&PathBuf::from(".")).with_context(|| "could not find git repo root - make sure you're in an active git repository")?; // load the config let conf = config::read(&git_dir).with_context(|| "could not load configuration file - no dvs.yaml in directory - be sure to initiate devious")?; @@ -42,64 +42,44 @@ pub fn dvs_add(files: &Vec<String>, message: &String) -> Result<Vec<AddedFile>> if queued_paths.contains(&file) {continue} - // ensure file is inside of the git repo - let abs_path = match file.canonicalize() { - Ok(file) => file, - Err(_) => { // swallowing error here because the command can still run - println!("skipping {} - doesn't exist", file.display()); - continue; - } - }; - if abs_path.strip_prefix(&git_dir).unwrap() == abs_path { - println!("skipping {} - outside of git repository", file.display()); - continue; - } - - // skip directories - if file.is_dir() { - println!("skipping {} - is a directory", file.display()); - continue - } - - // all checks passed, finally add file to queued_paths queued_paths.push(file); } // for // add each file in queued_paths to storage let added_files = queued_paths.into_iter().map(|file| { - add(&file, &conf, &message) + add(&file, &git_dir, &conf, &message) }).collect::<Vec<AddedFile>>(); return Ok(added_files) } // run_add_cmd -fn add(local_path: &PathBuf, conf: &config::Config, message: &String) -> AddedFile { +fn add(local_path: &PathBuf, git_dir: &PathBuf, conf: &config::Config, message: &String) -> AddedFile { // set error to None by default let mut error: Option<String> = None; // get file hash let file_hash = hash::get_file_hash(&local_path); if file_hash.is_none() && error.is_none() { - error = Some(String::from("could not hash file")); + error = Some(String::from("hash not found")); } // get file size let file_size = get_file_size(&local_path); if file_size.is_none() && error.is_none() { - error = Some(String::from("unable to get size of file")); + error = Some(String::from("file size not found")); } // get user name let user_name = get_user_name(&local_path); if user_name.is_none() && error.is_none() { - error = Some(String::from("could not get name of file owner")); + error = Some(String::from("file owner not found")); } // check group let group: Option<Group> = match Group::from_name(&conf.group) { Ok(group) => Some(group), Err(_) => { - if error.is_none() {error = Some(String::from("group not found"));} + if error.is_none() {error = Some(String::from("group not found"))} None } }; @@ -108,11 +88,14 @@ fn add(local_path: &PathBuf, conf: &config::Config, message: &String) -> AddedFi let storage_dir_abs: Option<PathBuf> = match conf.storage_dir.canonicalize() { Ok(path) => Some(path), Err(_) => { - if error.is_none() {error = Some(String::from("could not find storage directory"))} + if error.is_none() {error = Some(String::from("storage directory not found"))} None } }; + // last few check for errors + if error.is_none() {error = get_other_errors(local_path, git_dir, conf, message)} + if error.is_some() { return AddedFile{ path: local_path.clone(), @@ -124,8 +107,11 @@ fn add(local_path: &PathBuf, conf: &config::Config, message: &String) -> AddedFi } // can safely unwrap storage_dir_abs and file_hash + let storage_dir_abs_value = storage_dir_abs.unwrap(); let file_hash_value = file_hash.clone().unwrap(); - let dest_path = hash::get_storage_path(&storage_dir_abs.unwrap(), &file_hash_value); + + // get storage path + let dest_path = hash::get_storage_path(&storage_dir_abs_value, &file_hash_value); // Copy the file to the storage directory if it's not already there let mut outcome: Outcome = Outcome::Success; @@ -171,6 +157,28 @@ fn add(local_path: &PathBuf, conf: &config::Config, message: &String) -> AddedFi } } +fn get_other_errors(local_path: &PathBuf, git_dir: &PathBuf, conf: &config::Config, message: &String) -> Option<String> { + // check if file exists + match local_path.canonicalize() { + Ok(local_path) => { // file exists + // if file is outside of git repository + if local_path.strip_prefix(&git_dir).unwrap() == local_path { + return Some(String::from("file not in git repository")); + } + } + Err(_) => { + return Some(String::from("file not found")) + } + }; + + if local_path.is_dir() { + return Some(String::from("path is a directory")) + } + + + None +} + fn get_file_size(local_path: &PathBuf) -> Option<u64> { match local_path.metadata() { diff --git a/src/test_directory/test1.txt.dvsmeta b/src/test_directory/test1.txt.dvsmeta index 898e2c3..2665cb3 100644 --- a/src/test_directory/test1.txt.dvsmeta +++ b/src/test_directory/test1.txt.dvsmeta @@ -1,7 +1,7 @@ { "file_hash": "7f08b8682ee8258389605201d65ed6a9104eed809c000d7975186bc4cd8a3efe", "file_size": 13, - "time_stamp": "2024-04-01 13:09:35.115181573 -04:00", + "time_stamp": "2024-04-01 14:11:08.866466477 -04:00", "message": "assembled data again", "saved_by": "jenna" } \ No newline at end of file diff --git a/src/test_directory/test2.txt.dvsmeta b/src/test_directory/test2.txt.dvsmeta index 378608b..e92f444 100644 --- a/src/test_directory/test2.txt.dvsmeta +++ b/src/test_directory/test2.txt.dvsmeta @@ -1,7 +1,7 @@ { "file_hash": "20fa09e17c49b68fd9606fa736b03b5115059cbbbf3090d89eb50e4da044f028", "file_size": 26, - "time_stamp": "2024-04-01 13:09:35.115493138 -04:00", + "time_stamp": "2024-04-01 14:11:08.866761522 -04:00", "message": "assembled data again", "saved_by": "jenna" } \ No newline at end of file diff --git a/src/test_directory/test3.txt.dvsmeta b/src/test_directory/test3.txt.dvsmeta index f54aa1d..c75c84b 100644 --- a/src/test_directory/test3.txt.dvsmeta +++ b/src/test_directory/test3.txt.dvsmeta @@ -1,7 +1,7 @@ { "file_hash": "8e287466df9f3e8b1a2bd177ba15efe111aae572ea8859bb24557cfb4418a5b4", "file_size": 13, - "time_stamp": "2024-04-01 13:09:35.115711679 -04:00", + "time_stamp": "2024-04-01 14:11:08.866981507 -04:00", "message": "assembled data again", "saved_by": "jenna" } \ No newline at end of file