fix display for AddDir + fix AddDir counter

This commit is contained in:
Mark 2024-12-24 02:02:51 +01:00
parent 6172ffe248
commit 209821820b
4 changed files with 43 additions and 34 deletions

View File

@ -100,20 +100,22 @@ pub fn apply_indexchanges_int(
); );
for (i, change) in changes.iter().enumerate() { for (i, change) in changes.iter().enumerate() {
match change { match change {
IndexChange::AddDir(dir, _) => { IndexChange::AddDir(dir, make_new, _) => {
let ok = if let Some(target) = target { if *make_new {
let t = target.join(dir); let ok = if let Some(target) = target {
if let Err(e) = fs::create_dir_all(&t) { let t = target.join(dir);
eprintln!("\n[warn] couldn't create directory {t:?}: {e}"); if let Err(e) = fs::create_dir_all(&t) {
false eprintln!("\n[warn] couldn't create directory {t:?}: {e}");
false
} else {
true
}
} else { } else {
true 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) => { IndexChange::AddFile(file, index_file) => {

View File

@ -5,7 +5,7 @@ use crate::indexfile::IndexFile;
#[derive(Debug)] #[derive(Debug)]
pub enum IndexChange { pub enum IndexChange {
/// Ensure a directory with this path exists (at least if all its parent directories exist). /// 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 /// Add or update a file
AddFile(PathBuf, IndexFile), AddFile(PathBuf, IndexFile),
/// Remove a file /// Remove a file

View File

@ -108,14 +108,15 @@ fn main() {
} }
fn show_change(change: &IndexChange, rev: bool) { fn show_change(change: &IndexChange, rev: bool) {
match change { match change {
IndexChange::AddDir(v, s) => { IndexChange::AddDir(v, new, s) => {
let mut path_str = v.display().to_string(); let mut path_str = v.display().to_string();
if !path_str.ends_with(['/', '\\']) { if !path_str.ends_with(['/', '\\']) {
path_str.push('/'); path_str.push('/');
} }
eprintln!( eprintln!(
"{}>> {} [{:.2} GiB]", " {}{} {} [{:.2} GiB]",
if rev { "^" } else { "v" }, if rev { "^" } else { "v" },
if *new { ">>" } else { "> " },
path_str, path_str,
*s as f64 / (1024 * 1024 * 1024) as f64 *s as f64 / (1024 * 1024 * 1024) as f64
); );
@ -138,7 +139,7 @@ fn main() {
eprintln!(" - - - - -"); eprintln!(" - - - - -");
let add_dir_count = changes let add_dir_count = changes
.iter() .iter()
.filter(|c| matches!(c, IndexChange::AddDir(..))) .filter(|c| matches!(c, IndexChange::AddDir(_, true, _)))
.count(); .count();
eprintln!( eprintln!(
" {}>> add directory | {add_dir_count}x", " {}>> add directory | {add_dir_count}x",

View File

@ -91,20 +91,22 @@ fn rec(
let mut total_size = 0; let mut total_size = 0;
// used to find removals // used to find removals
let index_rel_path = index_files.join(rel_path); let index_rel_path = index_files.join(rel_path);
let mut index_entries = match fs::read_dir(&index_rel_path) { let (mut index_entries, dir_is_new) = match fs::read_dir(&index_rel_path) {
Err(_) => HashMap::new(), Err(_) => (HashMap::new(), true),
Ok(e) => e Ok(e) => (
.into_iter() e.into_iter()
.filter_map(|v| v.ok()) .filter_map(|v| v.ok())
.map(|v| { .map(|v| {
Ok(( Ok((
v.file_name(), v.file_name(),
v.file_type() v.file_type()
.map_err(|e| ("getting file type".to_owned(), v.path(), e))? .map_err(|e| ("getting file type".to_owned(), v.path(), e))?
.is_dir(), .is_dir(),
)) ))
}) })
.collect::<Result<_, (String, PathBuf, io::Error)>>()?, .collect::<Result<_, (String, PathBuf, io::Error)>>()?,
false,
),
}; };
// compare source files with index // compare source files with index
let source_files_path = source.join(rel_path); let source_files_path = source.join(rel_path);
@ -182,10 +184,14 @@ fn rec(
} }
} }
// combine everything // combine everything
let changes = [IndexChange::AddDir(rel_path.to_path_buf(), total_size)] let changes = [IndexChange::AddDir(
.into_iter() rel_path.to_path_buf(),
.chain(removals.into_iter()) dir_is_new,
.chain(ichanges.into_iter().flat_map(|(_, v)| v)) total_size,
.collect(); )]
.into_iter()
.chain(removals.into_iter())
.chain(ichanges.into_iter().flat_map(|(_, v)| v))
.collect();
Ok((total_size, changes)) Ok((total_size, changes))
} }