diff --git a/src/commands/init.rs b/src/commands/init.rs index 45b9110..ea96338 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -13,15 +13,15 @@ pub fn init() { }; // check if dir is empty - if let Ok(entries) = read_folder(path.clone()) { - if entries.len() != 0 { - eprintln!("fatal: destination path '{}' already exists and is not an empty directory.", path.display()); - std::process::exit(1); - } - } else { - eprintln!("fatal: cannot open the destination directory"); - std::process::exit(1); - } + // if let Ok(entries) = read_folder(path.clone()) { + // if entries.len() != 0 { + // eprintln!("fatal: destination path '{}' already exists and is not an empty directory.", path.display()); + // std::process::exit(1); + // } + // } else { + // eprintln!("fatal: cannot open the destination directory"); + // std::process::exit(1); + // } let builder = DirBuilder::new(); diff --git a/src/commands/pull.rs b/src/commands/pull.rs index f20fc3d..3a664a0 100644 --- a/src/commands/pull.rs +++ b/src/commands/pull.rs @@ -16,10 +16,10 @@ pub fn pull() { .strip_prefix(path::repo_root()).unwrap().to_path_buf(); let (folders, files) = get_diff(relative_p); - let ref_p = path::nextsync(); + let root = path::repo_root(); for folder in folders { - let p = ref_p.clone().join(PathBuf::from(folder.relative_s.unwrap())); + let p = root.clone().join(PathBuf::from(folder.relative_s.unwrap())); if !p.exists() { // create folder if let Err(err) = DirBuilder::new().recursive(true).create(p.clone()) { @@ -27,9 +27,9 @@ pub fn pull() { } // add tree - let path_folder = p.strip_prefix(ref_p.clone()).unwrap(); + let path_folder = p.strip_prefix(root.clone()).unwrap(); let lastmodified = folder.lastmodified.unwrap().timestamp_millis(); - if let Err(err) = Tree::from_path(path_folder.clone()).create(&lastmodified.to_string(), false) { + if let Err(err) = Tree::from_path(path_folder).create(&lastmodified.to_string(), false) { eprintln!("err: saving ref of {} ({})", path_folder.display(), err); } } @@ -39,13 +39,11 @@ pub fn pull() { .set_api_props(get_api_props()) .set_files(files) .should_log() - .download(ref_p.clone(), Some(&update_blob)); + .download(root, Some(&update_blob)); // todo look if need to download or update } fn update_blob(obj: ObjProps) { - // todo update blob - return; let relative_s = &obj.clone().relative_s.unwrap(); let relative_p = PathBuf::from(&relative_s); let lastmodified = obj.clone().lastmodified.unwrap().timestamp_millis(); diff --git a/src/commands/push.rs b/src/commands/push.rs index dc60572..6eb2213 100644 --- a/src/commands/push.rs +++ b/src/commands/push.rs @@ -39,11 +39,9 @@ pub fn push() { let mut whitelist: Option = None; for obj in staged_objs { - dbg!(obj.clone()); if obj.otype == String::from("tree") { let push_factory = PushFactory.new_dir(obj.clone()); let res = push_factory.can_push(&mut whitelist); - dbg!(&res); match res { PushState::Valid => { match push_factory.push() { diff --git a/src/commands/remote_diff.rs b/src/commands/remote_diff.rs index 400651a..9dbe88e 100644 --- a/src/commands/remote_diff.rs +++ b/src/commands/remote_diff.rs @@ -24,7 +24,7 @@ pub fn remote_diff() { pub fn get_diff(path: PathBuf) -> (Vec, Vec) { - let depth = "2"; // todo opti + let depth = "6"; // todo opti let api_props = get_api_props(); enumerate_remote( @@ -55,6 +55,7 @@ fn req(api_props: &ApiProps, depth: &str, relative_s: &str) -> Result SIZE_TO_STREAM { - true - } else { - false - } + size > SIZE_TO_STREAM } else { false } diff --git a/src/store/object/tree.rs b/src/store/object/tree.rs index db36f78..6c0568c 100644 --- a/src/store/object/tree.rs +++ b/src/store/object/tree.rs @@ -111,6 +111,4 @@ impl Tree { Ok(()) } - - // create } diff --git a/src/utils/remote.rs b/src/utils/remote.rs index b89d272..d4bbb1e 100644 --- a/src/utils/remote.rs +++ b/src/utils/remote.rs @@ -20,6 +20,7 @@ pub fn enumerate_remote( let mut deleted: Vec = vec![]; let mut files: Vec = vec![]; let mut objs_hashmap: HashMap> = HashMap::new(); + objs_hashmap.insert( options.relative_s.clone().unwrap_or(String::new()), Vec::new()); @@ -53,7 +54,11 @@ pub fn enumerate_remote( }; // separate folders and files in response - let d = options.depth.clone().unwrap_or("0".to_owned()).parse::().unwrap(); + let d = options.depth.clone() + .unwrap_or("0".to_owned()) + .parse::() + .unwrap(); + // first element is not used as it is the fetched folder if let Some(should_skip_fct) = should_skip.clone() { iter_with_skip_fct( @@ -82,6 +87,7 @@ pub fn enumerate_remote( &mut all_folders); } } + // go through all folders not checked for deletion before // as they were empty if let Some(_) = should_skip.clone() { @@ -90,18 +96,13 @@ pub fn enumerate_remote( objs_hashmap.remove(&key); } } - dbg!(deleted); - dbg!(objs_hashmap); (all_folders, files) } fn calc_depth(obj: &ObjProps) -> u16 { - calc_depth_string(obj.relative_s.clone().unwrap_or(String::new())) -} - -fn calc_depth_string(s: String) -> u16 { - s.split("/").count() as u16 + let path = obj.relative_s.clone().unwrap_or(String::new()); + path.split("/").count() as u16 } fn iter_with_skip_fct( @@ -121,7 +122,6 @@ fn iter_with_skip_fct( let current_depth = calc_depth(object); if object.is_dir() { - // add folder to parent folder only if exists let mut r_path = PathBuf::from(object.relative_s.clone().unwrap()); r_path.pop(); diff --git a/tests/pull.rs b/tests/pull.rs index 8470563..a06846f 100644 --- a/tests/pull.rs +++ b/tests/pull.rs @@ -1,9 +1,9 @@ mod utils; -use utils::{utils::*, status_utils::*, server::ServerTest, client::ClientTest}; +use utils::{utils::*, server::ServerTest, client::ClientTest}; #[cfg(test)] -mod push_tests { +mod pull_tests { use super::*; #[test] @@ -23,4 +23,23 @@ mod push_tests { server.clean(); } + #[test] + fn simple_pull_directory() { + let id = get_random_test_id(); + let mut server = ServerTest::new(id.clone()); + server.init(); + let mut client = ClientTest::new(id).init(); + + let _ = server.add_dir("dir"); + let _ = server.add_file("dir/file1", "foo"); + + client.run_cmd_ok("pull"); + + // tests + assert!(client.has_file("dir/file1", "foo")); + + client.clean(); + server.clean(); + } + } diff --git a/tests/utils/files_utils.rs b/tests/utils/files_utils.rs index f619307..730b73f 100644 --- a/tests/utils/files_utils.rs +++ b/tests/utils/files_utils.rs @@ -6,7 +6,7 @@ pub fn has_files(full_path: PathBuf, file: &str, content: &str, test_id: String) { if !full_path.exists() { println!("id: {}", test_id.clone()); - eprintln!("File '{}' doesn't exists on the server", file); + eprintln!("File '{}' doesn't exists", file); return false; } diff --git a/tests/utils/server.rs b/tests/utils/server.rs index af7b989..bf96373 100644 --- a/tests/utils/server.rs +++ b/tests/utils/server.rs @@ -28,9 +28,9 @@ impl ServerTest { } pub fn init(&mut self) -> &mut ServerTest{ - self.add_dir(self.test_id.clone()); + self.add_dir(&self.test_id.clone()); self.volume = self.volume.join(self.test_id.clone()); - self.sync_test(); + self.sync_root(); self } @@ -40,7 +40,7 @@ impl ServerTest { self } - pub fn add_dir(&mut self, path: String) -> &mut ServerTest { + pub fn add_dir(&mut self, path: &str) -> &mut ServerTest { let mut full_path = self.volume.clone(); full_path.push(path); @@ -55,6 +55,11 @@ impl ServerTest { Err(e) => eprintln!("Error creating directory: {}", e), } + // do not sync test directory when creating it + if !path.ends_with("_nextsync") + { + self.sync_test(); + } self } @@ -73,6 +78,7 @@ impl ServerTest { full_path.push(path); let _ = fs::remove_dir_all(&full_path); + self.sync_test(); self } @@ -89,17 +95,12 @@ impl ServerTest { // perform the occ files:scan command inside the nextcloud docker container let nextcloud_docker = "master-nextcloud-1"; - let mut args = String::from("exec -ti --user www-data"); - args.push_str(nextcloud_docker); - args.push_str("/var/www/html/occ files:scan --path=/"); - args.push_str(&self.user); - args.push_str("files/"); - args.push_str(path); + let args = format!("exec -t --user www-data {} /var/www/html/occ files:scan --path=/{}/files/{}", nextcloud_docker, &self.user, path); let _output = Command::new("docker") .args(args.split(" ")) .output() - .expect("Could not execute nextsync command"); + .expect("Could not execute docker exec command"); self }