diff --git a/src/apply_indexchanges.rs b/src/apply_indexchanges.rs index d204700..7d13e31 100755 --- a/src/apply_indexchanges.rs +++ b/src/apply_indexchanges.rs @@ -100,20 +100,22 @@ pub fn apply_indexchanges_int( ); for (i, change) in changes.iter().enumerate() { match change { - IndexChange::AddDir(dir, _) => { - let ok = if let Some(target) = target { - let t = target.join(dir); - if let Err(e) = fs::create_dir_all(&t) { - eprintln!("\n[warn] couldn't create directory {t:?}: {e}"); - false + IndexChange::AddDir(dir, make_new, _) => { + if *make_new { + let ok = if let Some(target) = target { + let t = target.join(dir); + if let Err(e) = fs::create_dir_all(&t) { + eprintln!("\n[warn] couldn't create directory {t:?}: {e}"); + false + } else { + true + } } else { true + }; + if ok { + fs::create_dir_all(&index.join(dir))?; } - } else { - true - }; - if ok { - fs::create_dir_all(&index.join(dir))?; } } IndexChange::AddFile(file, index_file) => { diff --git a/src/indexchanges.rs b/src/indexchanges.rs index 326ab91..217912b 100755 --- a/src/indexchanges.rs +++ b/src/indexchanges.rs @@ -5,7 +5,7 @@ use crate::indexfile::IndexFile; #[derive(Debug)] pub enum IndexChange { /// Ensure a directory with this path exists (at least if all its parent directories exist). - AddDir(PathBuf, u64), + AddDir(PathBuf, bool, u64), /// Add or update a file AddFile(PathBuf, IndexFile), /// Remove a file diff --git a/src/main.rs b/src/main.rs index 5abd729..5f641c7 100755 --- a/src/main.rs +++ b/src/main.rs @@ -108,14 +108,15 @@ fn main() { } fn show_change(change: &IndexChange, rev: bool) { match change { - IndexChange::AddDir(v, s) => { + IndexChange::AddDir(v, new, s) => { let mut path_str = v.display().to_string(); if !path_str.ends_with(['/', '\\']) { path_str.push('/'); } eprintln!( - "{}>> {} [{:.2} GiB]", + " {}{} {} [{:.2} GiB]", if rev { "^" } else { "v" }, + if *new { ">>" } else { "> " }, path_str, *s as f64 / (1024 * 1024 * 1024) as f64 ); @@ -138,7 +139,7 @@ fn main() { eprintln!(" - - - - -"); let add_dir_count = changes .iter() - .filter(|c| matches!(c, IndexChange::AddDir(..))) + .filter(|c| matches!(c, IndexChange::AddDir(_, true, _))) .count(); eprintln!( " {}>> add directory | {add_dir_count}x", diff --git a/src/update_index.rs b/src/update_index.rs index ba001c5..03fe28d 100755 --- a/src/update_index.rs +++ b/src/update_index.rs @@ -91,20 +91,22 @@ fn rec( let mut total_size = 0; // used to find removals let index_rel_path = index_files.join(rel_path); - let mut index_entries = match fs::read_dir(&index_rel_path) { - Err(_) => HashMap::new(), - Ok(e) => e - .into_iter() - .filter_map(|v| v.ok()) - .map(|v| { - Ok(( - v.file_name(), - v.file_type() - .map_err(|e| ("getting file type".to_owned(), v.path(), e))? - .is_dir(), - )) - }) - .collect::>()?, + let (mut index_entries, dir_is_new) = match fs::read_dir(&index_rel_path) { + Err(_) => (HashMap::new(), true), + Ok(e) => ( + e.into_iter() + .filter_map(|v| v.ok()) + .map(|v| { + Ok(( + v.file_name(), + v.file_type() + .map_err(|e| ("getting file type".to_owned(), v.path(), e))? + .is_dir(), + )) + }) + .collect::>()?, + false, + ), }; // compare source files with index let source_files_path = source.join(rel_path); @@ -182,10 +184,14 @@ fn rec( } } // combine everything - let changes = [IndexChange::AddDir(rel_path.to_path_buf(), total_size)] - .into_iter() - .chain(removals.into_iter()) - .chain(ichanges.into_iter().flat_map(|(_, v)| v)) - .collect(); + let changes = [IndexChange::AddDir( + rel_path.to_path_buf(), + dir_is_new, + total_size, + )] + .into_iter() + .chain(removals.into_iter()) + .chain(ichanges.into_iter().flat_map(|(_, v)| v)) + .collect(); Ok((total_size, changes)) }