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() {
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) => {

View File

@ -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

View File

@ -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",

View File

@ -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::<Result<_, (String, PathBuf, io::Error)>>()?,
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::<Result<_, (String, PathBuf, io::Error)>>()?,
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))
}