From d5097727cb4f2d36084ef7452106b6c63911265f Mon Sep 17 00:00:00 2001 From: grimhilt Date: Fri, 4 Aug 2023 15:25:51 +0200 Subject: [PATCH] create impl for blob --- src/commands/add.rs | 2 + src/commands/clone.rs | 4 +- src/commands/pull.rs | 5 +- src/commands/push.rs | 2 +- src/commands/push/deleted.rs | 4 +- src/commands/push/new.rs | 6 +- src/services/download_files.rs | 4 +- src/services/req_props.rs | 2 +- src/store/object.rs | 2 +- src/store/object/blob.rs | 107 +++++++++++++++++++++------------ src/store/object/tree.rs | 2 +- src/utils/remote.rs | 4 +- 12 files changed, 90 insertions(+), 54 deletions(-) diff --git a/src/commands/add.rs b/src/commands/add.rs index a180922..8559b85 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -13,6 +13,7 @@ pub struct AddArgs<'a> { // todo match deleted files // todo match weird reg expression +// todo -A == . pub fn add(args: AddArgs) { let mut index_file = store::index::open(); let mut added_files: Vec = vec![]; @@ -79,6 +80,7 @@ pub fn add(args: AddArgs) { } fn add_folder_content(path: PathBuf, added_files: &mut Vec) { + // todo check for changes let mut folders: Vec = vec![]; folders.push(path); diff --git a/src/commands/clone.rs b/src/commands/clone.rs index 71a8223..3e31133 100644 --- a/src/commands/clone.rs +++ b/src/commands/clone.rs @@ -10,7 +10,7 @@ use crate::utils::remote::{enumerate_remote, EnumerateOptions}; use crate::global::global::{DIR_PATH, set_dir_path}; use crate::services::api::ApiError; use crate::services::req_props::{ReqProps, ObjProps}; -use crate::store::object::{tree, blob}; +use crate::store::object::{tree, blob::Blob}; use crate::commands::config; use crate::commands::init; @@ -101,7 +101,7 @@ fn save_blob(obj: ObjProps) { let relative_s = &obj.clone().relative_s.unwrap(); let relative_p = PathBuf::from(&relative_s); let lastmodified = obj.clone().lastmodified.unwrap().timestamp_millis(); - if let Err(err) = blob::add(relative_p, &lastmodified.to_string(), false) { + if let Err(err) = Blob::new(relative_p).create(&lastmodified.to_string(), false) { eprintln!("err: saving ref of {} ({})", relative_s.clone(), err); } } diff --git a/src/commands/pull.rs b/src/commands/pull.rs index 2878c18..53849c0 100644 --- a/src/commands/pull.rs +++ b/src/commands/pull.rs @@ -3,7 +3,7 @@ use std::fs::DirBuilder; use crate::services::downloader::Downloader; use crate::services::req_props::ObjProps; -use crate::store::object::blob; +use crate::store::object::blob::Blob; use crate::store::object::tree; use crate::utils::api::get_api_props; use crate::utils::path; @@ -49,7 +49,8 @@ fn update_blob(obj: ObjProps) { let relative_s = &obj.clone().relative_s.unwrap(); let relative_p = PathBuf::from(&relative_s); let lastmodified = obj.clone().lastmodified.unwrap().timestamp_millis(); - if let Err(err) = blob::add(relative_p, &lastmodified.to_string(), false) { + // todo update function + if let Err(err) = Blob::new(relative_p).create(&lastmodified.to_string(), false) { eprintln!("err: saving ref of {} ({})", relative_s.clone(), err); } } diff --git a/src/commands/push.rs b/src/commands/push.rs index 8756535..45664e4 100644 --- a/src/commands/push.rs +++ b/src/commands/push.rs @@ -16,7 +16,7 @@ pub fn push() { None => { eprintln!("fatal: no remote set in configuration"); //std::process::exit(1); - String::from("") + String::new() } }; diff --git a/src/commands/push/deleted.rs b/src/commands/push/deleted.rs index 44c0eed..9b44ce0 100644 --- a/src/commands/push/deleted.rs +++ b/src/commands/push/deleted.rs @@ -3,7 +3,7 @@ use std::io; use crate::services::api::ApiError; use crate::services::delete_path::DeletePath; use crate::store::index; -use crate::store::object::blob; +use crate::store::object::blob::Blob; use crate::commands::status::LocalObj; use crate::commands::push::push_factory::{PushState, PushChange, PushFlowState}; @@ -42,7 +42,7 @@ impl PushChange for Deleted { // update tree // todo date - blob::rm(obj.path.clone())?; + Blob::new(obj.path.clone()).rm()?; // remove index index::rm_line(obj.path.to_str().unwrap())?; diff --git a/src/commands/push/new.rs b/src/commands/push/new.rs index c6eb863..4ac2173 100644 --- a/src/commands/push/new.rs +++ b/src/commands/push/new.rs @@ -4,7 +4,7 @@ use crate::services::api::ApiError; use crate::services::req_props::ReqProps; use crate::services::upload_file::UploadFile; use crate::store::index; -use crate::store::object::blob; +use crate::store::object::blob::Blob; use crate::commands::status::LocalObj; use crate::commands::push::push_factory::{PushState, PushChange, PushFlowState}; @@ -67,8 +67,8 @@ impl PushChange for New { let lastmodified = prop.lastmodified.unwrap().timestamp_millis(); - // update blob - blob::add(obj.path.clone(), &lastmodified.to_string(), true)?; + // create new blob + Blob::new(obj.path.clone()).create(&lastmodified.to_string(), true)?; // remove index index::rm_line(obj.path.to_str().unwrap())?; diff --git a/src/services/download_files.rs b/src/services/download_files.rs index 9df04a7..397fc87 100644 --- a/src/services/download_files.rs +++ b/src/services/download_files.rs @@ -16,7 +16,7 @@ impl DownloadFiles { pub fn new() -> Self { DownloadFiles { api_builder: ApiBuilder::new(), - relative_ps: String::from(""), + relative_ps: String::new(), } } @@ -74,7 +74,7 @@ impl DownloadFiles { if res.status().is_success() { let body = res.bytes().await.map_err(ApiError::EmptyError)?; match Self::write_file(p, &body.to_vec()) { - Err(_) => Err(ApiError::Unexpected(String::from(""))), + Err(_) => Err(ApiError::Unexpected(String::new())), Ok(_) => Ok(()), } } else { diff --git a/src/services/req_props.rs b/src/services/req_props.rs index 4087ee0..73f124e 100644 --- a/src/services/req_props.rs +++ b/src/services/req_props.rs @@ -195,7 +195,7 @@ impl ReqProps { let mut values: Vec = vec![]; let mut should_get = false; - let mut val: String = String::from(""); + let mut val: String = String::new(); let mut content = ObjProps::new(); for event in parser { diff --git a/src/store/object.rs b/src/store/object.rs index e37d741..5147c23 100644 --- a/src/store/object.rs +++ b/src/store/object.rs @@ -30,7 +30,7 @@ impl Object { if path == "" { return Object { path: PathBuf::from("/"), - hash: String::from(""), + hash: String::new(), obj_p: head::path(), ts: None, } diff --git a/src/store/object/blob.rs b/src/store/object/blob.rs index 11009ab..9693a82 100644 --- a/src/store/object/blob.rs +++ b/src/store/object/blob.rs @@ -1,52 +1,85 @@ use std::io; use std::path::PathBuf; use std::fs; +use crypto::sha1::Sha1; +use crypto::digest::Digest; use crate::utils::path; use crate::store::head; use crate::store::object::{update_dates, parse_path, add_node, create_obj, rm_node}; -pub fn add(path: PathBuf, date: &str, up_parent: bool) -> io::Result<()> { - let (line, hash, name) = parse_path(path.clone(), true); - // add blob reference to parent - if path.iter().count() == 1 { - head::add_line(line)?; - } else { - add_node(path.parent().unwrap(), &line)?; - } - - let mut content = name.clone().to_owned(); - content.push_str(" "); - content.push_str(date); - - // create blob object - create_obj(hash, &content)?; - - // update date for all parent - if up_parent { - update_dates(path, date)?; - } - - Ok(()) +pub struct Blob { + path: PathBuf, + hash: String, + obj_p: PathBuf, } -pub fn rm(path: PathBuf) -> io::Result<()> { - let (line, hash, _) = parse_path(path.clone(), true); +impl Blob { + pub fn new(path: PathBuf) -> Blob { + let mut hasher = Sha1::new(); + hasher.input_str(path.to_str().unwrap()); + let hash = hasher.result_str(); - // remove blob reference to parent - if path.iter().count() == 1 { - head::rm_line(&line)?; - } else { - rm_node(path.parent().unwrap(), &line)?; + let (dir, res) = hash.split_at(2); + + let mut obj_p = path::objects(); + obj_p.push(dir); + obj_p.push(res); + + Blob { + path, + hash, + obj_p, + } } - // remove blob object - let mut root = path::objects(); + fn get_line_filename(&mut self) -> (String, String) { + let file_name = self.path.file_name().unwrap().to_str().unwrap().to_owned(); + let mut line = String::from("blob"); + line.push_str(" "); + line.push_str(&self.hash); + line.push_str(" "); + line.push_str(&file_name); + (line, file_name) + } - let c = hash.clone(); - let (dir, rest) = c.split_at(2); - root.push(dir); - root.push(rest); - fs::remove_file(root)?; + pub fn create(&mut self, ts: &str, up_parent: bool) -> io::Result<()> { + let (line, file_name) = self.get_line_filename(); - Ok(()) + // add blob reference to parent + if self.path.iter().count() == 1 { + head::add_line(line)?; + } else { + add_node(self.path.parent().unwrap(), &line)?; + } + + // create blob object + let mut content = file_name.clone(); + content.push_str(" "); + content.push_str(ts); + // todo hash ts (bis) + create_obj(self.hash.clone(), &content)?; + + // update date for all parent + if up_parent { + update_dates(self.path.clone(), ts)?; + } + Ok(()) + } + + pub fn rm(&mut self) -> io::Result<()> { + let (line, _) = self.get_line_filename(); + + // remove blob reference to parent + if self.path.iter().count() == 1 { + head::rm_line(&line)?; + } else { + rm_node(self.path.parent().unwrap(), &line)?; + } + + // remove blob object + fs::remove_file(self.obj_p.clone())?; + + Ok(()) + + } } diff --git a/src/store/object/tree.rs b/src/store/object/tree.rs index 8ae05ec..1f2f449 100644 --- a/src/store/object/tree.rs +++ b/src/store/object/tree.rs @@ -78,7 +78,7 @@ pub fn read(tree: String) -> Option<(String, io::Lines>)> { Ok(mut reader) => { let name = match reader.next() { Some(Ok(line)) => line, - _ => String::from(""), + _ => String::new(), }; Some((name, reader)) }, diff --git a/src/utils/remote.rs b/src/utils/remote.rs index 828167d..b88a2a8 100644 --- a/src/utils/remote.rs +++ b/src/utils/remote.rs @@ -20,7 +20,7 @@ pub fn enumerate_remote( let relative_s = match folder.relative_s { Some(relative_s) => relative_s, - None => options.relative_s.clone().unwrap_or(String::from("")), + None => options.relative_s.clone().unwrap_or(String::new()) }; // request folder content @@ -88,6 +88,6 @@ pub fn enumerate_remote( } fn calc_depth(obj: &ObjProps) -> u16 { - obj.relative_s.clone().unwrap_or(String::from("")).split("/").count() as u16 + obj.relative_s.clone().unwrap_or(String::new()).split("/").count() as u16 }