From 5746e5772a828c34c57885c4065efbba39c4fa6f Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Thu, 14 Apr 2022 12:28:32 -0700 Subject: [PATCH] [omicron-package] Ignore some more NotFound errors. --- package/src/bin/omicron-package.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/package/src/bin/omicron-package.rs b/package/src/bin/omicron-package.rs index 8d8a50e10d..560b5a7c7c 100644 --- a/package/src/bin/omicron-package.rs +++ b/package/src/bin/omicron-package.rs @@ -402,6 +402,16 @@ fn uninstall_all_packages(config: &Config) { .unwrap(); } +fn remove_file_unless_already_removed>(path: P) -> Result<()> { + if let Err(e) = std::fs::remove_file(path.as_ref()) { + match e.kind() { + std::io::ErrorKind::NotFound => {} + _ => bail!(e), + } + } + Ok(()) +} + fn remove_all_unless_already_removed>(path: P) -> Result<()> { if let Err(e) = std::fs::remove_dir_all(path.as_ref()) { match e.kind() { @@ -414,13 +424,18 @@ fn remove_all_unless_already_removed>(path: P) -> Result<()> { fn remove_all_except_databases>(path: P) -> Result<()> { const TO_KEEP: [&str; 2] = ["clickhouse", "cockroachdb"]; - for entry in path.as_ref().read_dir()? { + let dir = match path.as_ref().read_dir() { + Ok(dir) => dir, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()), + Err(e) => bail!(e), + }; + for entry in dir { let entry = entry?; if !TO_KEEP.contains(&&*(entry.file_name().to_string_lossy())) { if entry.metadata()?.is_dir() { - std::fs::remove_dir_all(entry.path())?; + remove_all_unless_already_removed(entry.path())?; } else { - std::fs::remove_file(entry.path())?; + remove_file_unless_already_removed(entry.path())?; } } }