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,7 +100,8 @@ 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, _) => {
if *make_new {
let ok = if let Some(target) = target { let ok = if let Some(target) = target {
let t = target.join(dir); let t = target.join(dir);
if let Err(e) = fs::create_dir_all(&t) { if let Err(e) = fs::create_dir_all(&t) {
@ -116,6 +117,7 @@ pub fn apply_indexchanges_int(
fs::create_dir_all(&index.join(dir))?; fs::create_dir_all(&index.join(dir))?;
} }
} }
}
IndexChange::AddFile(file, index_file) => { IndexChange::AddFile(file, index_file) => {
gib_transferred += index_file.size as f64 / (1024 * 1024 * 1024) as f64; gib_transferred += index_file.size as f64 / (1024 * 1024 * 1024) as f64;
let ok = if let Some(target) = target { let ok = if let Some(target) = target {

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,10 +91,10 @@ 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((
@ -105,6 +105,8 @@ fn rec(
)) ))
}) })
.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,7 +184,11 @@ fn rec(
} }
} }
// combine everything // combine everything
let changes = [IndexChange::AddDir(rel_path.to_path_buf(), total_size)] let changes = [IndexChange::AddDir(
rel_path.to_path_buf(),
dir_is_new,
total_size,
)]
.into_iter() .into_iter()
.chain(removals.into_iter()) .chain(removals.into_iter())
.chain(ichanges.into_iter().flat_map(|(_, v)| v)) .chain(ichanges.into_iter().flat_map(|(_, v)| v))