diff --git a/src/commands/clone.rs b/src/commands/clone.rs index 28fcccf..e22f58f 100644 --- a/src/commands/clone.rs +++ b/src/commands/clone.rs @@ -7,7 +7,7 @@ use crate::global::global::{DIR_PATH, set_dir_path}; use crate::services::api::ApiError; use crate::services::req_props::{ReqProps, ObjProps}; use crate::services::download_files::DownloadFiles; -use crate::store::object::{self, add_blob, add_tree}; +use crate::store::object::{tree, blob}; use crate::commands::init; pub fn clone(remote: Values<'_>) { @@ -93,7 +93,7 @@ pub fn clone(remote: Values<'_>) { // add tree let path_folder = p.strip_prefix(ref_path.clone()).unwrap(); let lastmodified = folder.lastmodified.unwrap().timestamp_millis(); - if let Err(err) = add_tree(&path_folder, &lastmodified.to_string()) { + if let Err(err) = tree::add(&path_folder, &lastmodified.to_string()) { eprintln!("err: saving ref of {} ({})", path_folder.display(), err); } } @@ -125,7 +125,7 @@ fn download_files(ref_p: PathBuf, files: Vec, api_props: &ApiProps) { Ok(()) => { let relative_p = Path::new(&relative_s); let lastmodified = obj.clone().lastmodified.unwrap().timestamp_millis(); - if let Err(err) = add_blob(relative_p, &lastmodified.to_string()) { + if let Err(err) = blob::add(relative_p, &lastmodified.to_string()) { eprintln!("err: saving ref of {} ({})", relative_s.clone(), err); } }, diff --git a/src/commands/push.rs b/src/commands/push.rs index f0d3428..48211fd 100644 --- a/src/commands/push.rs +++ b/src/commands/push.rs @@ -4,7 +4,7 @@ use crate::services::upload_file::UploadFile; use crate::services::delete_path::DeletePath; use crate::services::req_props::{ReqProps, ObjProps}; use crate::store::index; -use crate::store::object::{add_blob, rm_blob}; +use crate::store::object::blob; use crate::commands::{status, config}; use crate::commands::status::{State, LocalObj}; use crate::commands::push::push_factory::{PushFactory, PushState}; diff --git a/src/commands/push/push_factory.rs b/src/commands/push/push_factory.rs index 34cd76e..247ac04 100644 --- a/src/commands/push/push_factory.rs +++ b/src/commands/push/push_factory.rs @@ -1,6 +1,7 @@ use std::path::Path; use crate::commands::status::{State, LocalObj}; use crate::services::api::ApiError; +use crate::store::object; use crate::services::req_props::{ObjProps, ReqProps}; use crate::commands::push::new::New; //use crate::commands::push::new_dir::NewDir; @@ -45,7 +46,7 @@ pub trait PushChange { .getlastmodified() .send_req_single(); - let file_infos = match res { + let obj_data = match res { Ok(obj) => Ok(Some(obj)), Err(ApiError::IncorrectRequest(err)) => { if err.status() == 404 { @@ -57,16 +58,21 @@ pub trait PushChange { Err(_) => Err(()), }; - let infos = match file_infos { + let obj_data = match obj_data { Ok(Some(info)) => info, Ok(None) => return PushFlowState::NotOnRemote, Err(_) => return PushFlowState::Error, }; // check if remote is newest - // set timestamp from remote stuff - // get from file - todo!() + let last_sync_ts = object::get_timestamp(obj.path.to_str().unwrap().to_string()).unwrap(); + let remote_ts = obj_data.lastmodified.unwrap().timestamp_millis(); + + if last_sync_ts < remote_ts { + PushFlowState::RemoteIsNewer + } else { + PushFlowState::LocalIsNewer + } } } diff --git a/src/commands/status.rs b/src/commands/status.rs index ca5784b..58a1a35 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -6,7 +6,8 @@ use crypto::digest::Digest; use crypto::sha1::Sha1; use colored::Colorize; use crate::utils; -use crate::store::{self, object}; +use crate::store::object::tree; +use crate::store::index; #[derive(PartialEq)] enum RemoveSide { @@ -68,7 +69,7 @@ fn get_staged(objs: &mut Vec) -> Vec { let mut indexes = HashSet::new(); let mut staged_objs: Vec = vec![]; - if let Ok(entries) = store::index::read_line() { + if let Ok(entries) = index::read_line() { for entry in entries { indexes.insert(entry.unwrap()); } @@ -121,7 +122,7 @@ fn get_diff() -> (Vec, Vec) { let obj_path = root.clone().join(cur_path.clone()); if obj_path.is_dir() { - if let Some((_, lines)) = object::read_tree(cur_obj.clone()) { + if let Some((_, lines)) = tree::read(cur_obj.clone()) { add_to_hashmap(lines, &mut hashes, cur_path.clone()); } @@ -171,7 +172,7 @@ fn add_to_hashmap(lines: Lines>, hashes: &mut HashMap 5 { - let (ftype, hash, name) = object::parse_line(ip); + let (ftype, hash, name) = tree::parse_line(ip); let mut p = path.clone(); p.push(name.clone()); hashes.insert(String::from(hash), LocalObj{ diff --git a/src/store/object.rs b/src/store/object.rs index 4189c35..5b43dd1 100644 --- a/src/store/object.rs +++ b/src/store/object.rs @@ -110,7 +110,7 @@ fn create_obj(name: String, content: &str) -> io::Result<()> { Ok(()) } -pub fn get_timestamp(path_s: String) -> Option { +pub fn get_timestamp(path_s: String) -> Option { let mut obj_p = match path::objects() { Some(path) => path, None => todo!(), @@ -126,7 +126,7 @@ pub fn get_timestamp(path_s: String) -> Option { Some(Ok(line)) => { let mut data = line.rsplit(' '); if data.clone().count() >= 2 { - Some(String::from(data.next().unwrap())) + Some(data.next().unwrap().parse::().unwrap()) } else { None }