Skip to content

Commit 2bcfaad

Browse files
committed
refactor(client-cli): only remove contents of the ledger directory, not the directory itself
1 parent e574dc1 commit 2bcfaad

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

mithril-client-cli/src/commands/tools/snapshot_converter.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use clap::{Parser, ValueEnum};
1111
use mithril_client::MithrilResult;
1212

1313
use crate::utils::{
14-
copy_dir, ArchiveUnpacker, GitHubReleaseRetriever, HttpDownloader, ReqwestGitHubApiClient,
15-
ReqwestHttpDownloader,
14+
copy_dir, remove_dir_contents, ArchiveUnpacker, GitHubReleaseRetriever, HttpDownloader,
15+
ReqwestGitHubApiClient, ReqwestHttpDownloader,
1616
};
1717

1818
const GITHUB_ORGANIZATION: &str = "IntersectMBO";
@@ -403,15 +403,9 @@ impl SnapshotConverterCommand {
403403
let (slot_number, _) = filename
404404
.split_once('_')
405405
.ok_or_else(|| anyhow!("Invalid converted snapshot name format: {}", filename))?;
406-
remove_dir_all(&ledger_dir).with_context(|| {
406+
remove_dir_contents(&ledger_dir).with_context(|| {
407407
format!(
408-
"Failed to remove old ledger state snapshot directory: {}",
409-
ledger_dir.display()
410-
)
411-
})?;
412-
create_dir(&ledger_dir).with_context(|| {
413-
format!(
414-
"Failed to recreate ledger state snapshot directory: {}",
408+
"Failed to remove contents of ledger directory: {}",
415409
ledger_dir.display()
416410
)
417411
})?;

mithril-client-cli/src/utils/fs.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ fn copy_dir_contents(source_dir: &Path, target_dir: &Path) -> MithrilResult<()>
4848
Ok(())
4949
}
5050

51+
/// Removes all contents inside the given directory.
52+
pub fn remove_dir_contents(dir: &Path) -> MithrilResult<()> {
53+
if !dir.exists() {
54+
return Ok(());
55+
}
56+
57+
for entry in fs::read_dir(dir)? {
58+
let path = entry?.path();
59+
if path.is_dir() {
60+
fs::remove_dir_all(&path)
61+
.with_context(|| format!("Failed to remove subdirectory: {}", path.display()))?;
62+
} else {
63+
fs::remove_file(&path)
64+
.with_context(|| format!("Failed to remove file: {}", path.display()))?;
65+
}
66+
}
67+
68+
Ok(())
69+
}
70+
5171
#[cfg(test)]
5272
mod tests {
5373
use std::fs::File;
@@ -106,4 +126,20 @@ mod tests {
106126
** root.txt"
107127
);
108128
}
129+
130+
#[test]
131+
fn cleans_directory_without_deleting_it() {
132+
let dir = temp_dir_create!().join("dir_to_clean");
133+
fs::create_dir(&dir).unwrap();
134+
135+
File::create(dir.join("file1.txt")).unwrap();
136+
let sub_dir = dir.join("subdir");
137+
fs::create_dir(&sub_dir).unwrap();
138+
File::create(sub_dir.join("file2.txt")).unwrap();
139+
140+
remove_dir_contents(&dir).unwrap();
141+
142+
assert!(dir.exists());
143+
assert!(fs::read_dir(&dir).unwrap().next().is_none());
144+
}
109145
}

0 commit comments

Comments
 (0)