Skip to content

Commit 759aaeb

Browse files
committed
feat(client-lib): allow unexpected downloaded file verifier to fully work on non existing dir
As the download step may have not been able to create them because of a failure.
1 parent de586a5 commit 759aaeb

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

mithril-client/src/utils/unexpected_downloaded_file_verifier.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,21 @@ impl ExpectedFilesAfterDownload {
120120
/// *Note: removed directories names are suffixed with a "/"*
121121
pub async fn remove_unexpected_files(self) -> StdResult<Option<Vec<String>>> {
122122
tokio::task::spawn_blocking(move || {
123-
let unexpected_entries_in_immutable_dir: Vec<_> =
124-
std::fs::read_dir(self.target_cardano_db_dir.join(IMMUTABLE_DIR))
123+
let immutable_files_dir = self.target_cardano_db_dir.join(IMMUTABLE_DIR);
124+
let unexpected_entries_in_immutable_dir = if immutable_files_dir.exists() {
125+
std::fs::read_dir(&immutable_files_dir)
125126
.with_context(|| BASE_ERROR)?
126127
.flatten()
127128
.filter(|entry| {
128129
!self
129130
.expected_filenames_in_immutable_dir
130131
.contains(&entry.file_name())
131132
})
132-
.collect();
133+
.collect()
134+
} else {
135+
// The immutable dir can be missing if the download was interrupted
136+
Vec::new()
137+
};
133138
let mut removed_entries = Vec::new();
134139

135140
for unexpected_entry in &unexpected_entries_in_immutable_dir {
@@ -371,6 +376,21 @@ mod tests {
371376

372377
use super::*;
373378

379+
#[tokio::test]
380+
async fn when_immutable_dir_does_not_exist_do_nothing_and_return_none() {
381+
let temp_dir = temp_dir_create!();
382+
383+
let existing_before = ExpectedFilesAfterDownload {
384+
target_cardano_db_dir: temp_dir.clone(),
385+
expected_filenames_in_immutable_dir: HashSet::new(),
386+
logger: TestLogger::stdout(),
387+
};
388+
389+
let removed_entries = existing_before.remove_unexpected_files().await.unwrap();
390+
assert_eq!(removed_entries, None);
391+
assert_dir_eq!(&temp_dir, "");
392+
}
393+
374394
#[tokio::test]
375395
async fn when_dir_empty_do_nothing_and_return_none() {
376396
let temp_dir = temp_dir_create!();

0 commit comments

Comments
 (0)