From 06755358f8e8192d75be8dc250fe49066a8d75ac Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 9 Aug 2021 10:33:39 +0300 Subject: refactor: split long func into two Sponsored-by: author --- src/backup_progress.rs | 4 +++ src/backup_run.rs | 93 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 69 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/backup_progress.rs b/src/backup_progress.rs index 30b6228..741ae7c 100644 --- a/src/backup_progress.rs +++ b/src/backup_progress.rs @@ -80,6 +80,10 @@ impl BackupProgress { self.progress.inc(1); } + pub fn found_problems(&self, n: u64) { + self.progress.inc(n); + } + pub fn found_live_file(&self, filename: &Path) { self.progress.inc(1); if self.progress.length() < self.progress.position() { diff --git a/src/backup_run.rs b/src/backup_run.rs index 39ef217..e3bf4b2 100644 --- a/src/backup_run.rs +++ b/src/backup_run.rs @@ -131,44 +131,75 @@ impl<'a> BackupRun<'a> { let files_count = { let mut new = NascentGeneration::create(newpath)?; for root in &config.roots { - let iter = FsIterator::new(root, config.exclude_cache_tag_directories); - for entry in iter { - match entry { + match self.backup_one_root(config, old, &mut new, root).await { + Ok(mut o) => { + new_cachedir_tags.append(&mut o.new_cachedir_tags); + if !o.warnings.is_empty() { + warnings.append(&mut o.warnings); + self.found_problems(o.warnings.len() as u64); + } + } + Err(err) => { + debug!("ignoring backup error {}", err); + warnings.push(err.into()); + self.found_problem(); + } + } + } + new.file_count() + }; + self.finish(); + Ok(RootsBackupOutcome { + files_count, + warnings, + new_cachedir_tags, + }) + } + + async fn backup_one_root( + &self, + config: &ClientConfig, + old: &LocalGeneration, + new: &mut NascentGeneration, + root: &Path, + ) -> Result { + let mut warnings: Vec = vec![]; + let mut new_cachedir_tags = vec![]; + let iter = FsIterator::new(root, config.exclude_cache_tag_directories); + for entry in iter { + match entry { + Err(err) => { + debug!("ignoring backup error {}", err); + warnings.push(err.into()); + self.found_problem(); + } + Ok(entry) => { + let path = entry.inner.pathbuf(); + if entry.is_cachedir_tag && !old.is_cachedir_tag(&path)? { + new_cachedir_tags.push(path); + } + match self.backup(entry, old).await { Err(err) => { debug!("ignoring backup error {}", err); - warnings.push(err.into()); + warnings.push(err); self.found_problem(); } - Ok(entry) => { - let path = entry.inner.pathbuf(); - if entry.is_cachedir_tag && !old.is_cachedir_tag(&path)? { - new_cachedir_tags.push(path); - } - match self.backup(entry, old).await { - Err(err) => { - debug!("ignoring backup error {}", err); - warnings.push(err); - self.found_problem(); - } - Ok(o) => { - if let Err(err) = - new.insert(o.entry, &o.ids, o.reason, o.is_cachedir_tag) - { - debug!("ignoring backup error {}", err); - warnings.push(err.into()); - self.found_problem(); - } - } + Ok(o) => { + if let Err(err) = + new.insert(o.entry, &o.ids, o.reason, o.is_cachedir_tag) + { + debug!("ignoring backup error {}", err); + warnings.push(err.into()); + self.found_problem(); } } } } } - new.file_count() - }; - self.finish(); + } + Ok(RootsBackupOutcome { - files_count, + files_count: 0, // Caller will get file count from new. warnings, new_cachedir_tags, }) @@ -219,6 +250,12 @@ impl<'a> BackupRun<'a> { progress.found_problem(); } } + + fn found_problems(&self, n: u64) { + if let Some(progress) = &self.progress { + progress.found_problems(n); + } + } } async fn backup_file( -- cgit v1.2.1