mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-31 04:23:40 +00:00
refactor: add delete_dir utility and use across codebase
Adds a delete_dir helper that ignores NotFound errors (matching the existing delete_file pattern) and replaces the repeated metadata-check-then-remove_dir_all pattern throughout the codebase.
This commit is contained in:
@@ -323,9 +323,7 @@ async fn perform_backup(
|
|||||||
os_backup_file.save().await?;
|
os_backup_file.save().await?;
|
||||||
|
|
||||||
let luks_folder_old = backup_guard.path().join("luks.old");
|
let luks_folder_old = backup_guard.path().join("luks.old");
|
||||||
if tokio::fs::metadata(&luks_folder_old).await.is_ok() {
|
crate::util::io::delete_dir(&luks_folder_old).await?;
|
||||||
tokio::fs::remove_dir_all(&luks_folder_old).await?;
|
|
||||||
}
|
|
||||||
let luks_folder_bak = backup_guard.path().join("luks");
|
let luks_folder_bak = backup_guard.path().join("luks");
|
||||||
if tokio::fs::metadata(&luks_folder_bak).await.is_ok() {
|
if tokio::fs::metadata(&luks_folder_bak).await.is_ok() {
|
||||||
tokio::fs::rename(&luks_folder_bak, &luks_folder_old).await?;
|
tokio::fs::rename(&luks_folder_bak, &luks_folder_old).await?;
|
||||||
|
|||||||
@@ -53,9 +53,7 @@ impl<G: GenericMountGuard> BackupMountGuard<G> {
|
|||||||
})?,
|
})?,
|
||||||
)?
|
)?
|
||||||
} else {
|
} else {
|
||||||
if tokio::fs::metadata(&crypt_path).await.is_ok() {
|
crate::util::io::delete_dir(&crypt_path).await?;
|
||||||
tokio::fs::remove_dir_all(&crypt_path).await?;
|
|
||||||
}
|
|
||||||
Default::default()
|
Default::default()
|
||||||
};
|
};
|
||||||
let enc_key = if let (Some(hash), Some(wrapped_key)) = (
|
let enc_key = if let (Some(hash), Some(wrapped_key)) = (
|
||||||
|
|||||||
@@ -291,21 +291,15 @@ pub async fn init(
|
|||||||
|
|
||||||
init_tmp.start();
|
init_tmp.start();
|
||||||
let tmp_dir = Path::new(PACKAGE_DATA).join("tmp");
|
let tmp_dir = Path::new(PACKAGE_DATA).join("tmp");
|
||||||
if tokio::fs::metadata(&tmp_dir).await.is_ok() {
|
crate::util::io::delete_dir(&tmp_dir).await?;
|
||||||
tokio::fs::remove_dir_all(&tmp_dir).await?;
|
|
||||||
}
|
|
||||||
if tokio::fs::metadata(&tmp_dir).await.is_err() {
|
if tokio::fs::metadata(&tmp_dir).await.is_err() {
|
||||||
tokio::fs::create_dir_all(&tmp_dir).await?;
|
tokio::fs::create_dir_all(&tmp_dir).await?;
|
||||||
}
|
}
|
||||||
let tmp_var = Path::new(PACKAGE_DATA).join("tmp/var");
|
let tmp_var = Path::new(PACKAGE_DATA).join("tmp/var");
|
||||||
if tokio::fs::metadata(&tmp_var).await.is_ok() {
|
crate::util::io::delete_dir(&tmp_var).await?;
|
||||||
tokio::fs::remove_dir_all(&tmp_var).await?;
|
|
||||||
}
|
|
||||||
crate::disk::mount::util::bind(&tmp_var, "/var/tmp", false).await?;
|
crate::disk::mount::util::bind(&tmp_var, "/var/tmp", false).await?;
|
||||||
let downloading = Path::new(PACKAGE_DATA).join("archive/downloading");
|
let downloading = Path::new(PACKAGE_DATA).join("archive/downloading");
|
||||||
if tokio::fs::metadata(&downloading).await.is_ok() {
|
crate::util::io::delete_dir(&downloading).await?;
|
||||||
tokio::fs::remove_dir_all(&downloading).await?;
|
|
||||||
}
|
|
||||||
let tmp_docker = Path::new(PACKAGE_DATA).join("tmp").join(*CONTAINER_TOOL);
|
let tmp_docker = Path::new(PACKAGE_DATA).join("tmp").join(*CONTAINER_TOOL);
|
||||||
crate::disk::mount::util::bind(&tmp_docker, *CONTAINER_DATADIR, false).await?;
|
crate::disk::mount::util::bind(&tmp_docker, *CONTAINER_DATADIR, false).await?;
|
||||||
init_tmp.complete();
|
init_tmp.complete();
|
||||||
|
|||||||
@@ -101,13 +101,11 @@ pub async fn cleanup(ctx: &RpcContext, id: &PackageId, soft: bool) -> Result<(),
|
|||||||
|
|
||||||
if !soft {
|
if !soft {
|
||||||
let path = Path::new(DATA_DIR).join(PKG_VOLUME_DIR).join(&manifest.id);
|
let path = Path::new(DATA_DIR).join(PKG_VOLUME_DIR).join(&manifest.id);
|
||||||
if tokio::fs::metadata(&path).await.is_ok() {
|
crate::util::io::delete_dir(&path).await?;
|
||||||
tokio::fs::remove_dir_all(&path).await?;
|
#[cfg(not(feature = "dev"))]
|
||||||
}
|
{
|
||||||
let logs_dir = Path::new(PACKAGE_DATA).join("logs").join(&manifest.id);
|
let logs_dir = Path::new(PACKAGE_DATA).join("logs").join(&manifest.id);
|
||||||
if tokio::fs::metadata(&logs_dir).await.is_ok() {
|
crate::util::io::delete_dir(&logs_dir).await?;
|
||||||
#[cfg(not(feature = "dev"))]
|
|
||||||
tokio::fs::remove_dir_all(&logs_dir).await?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -738,9 +738,7 @@ async fn migrate(
|
|||||||
);
|
);
|
||||||
|
|
||||||
let tmpdir = Path::new(package_data_transfer_args.0).join("tmp");
|
let tmpdir = Path::new(package_data_transfer_args.0).join("tmp");
|
||||||
if tokio::fs::metadata(&tmpdir).await.is_ok() {
|
crate::util::io::delete_dir(&tmpdir).await?;
|
||||||
tokio::fs::remove_dir_all(&tmpdir).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let ordering = std::sync::atomic::Ordering::Relaxed;
|
let ordering = std::sync::atomic::Ordering::Relaxed;
|
||||||
|
|
||||||
|
|||||||
@@ -1047,6 +1047,20 @@ pub async fn delete_file(path: impl AsRef<Path>) -> Result<(), Error> {
|
|||||||
.with_ctx(|_| (ErrorKind::Filesystem, lazy_format!("delete {path:?}")))
|
.with_ctx(|_| (ErrorKind::Filesystem, lazy_format!("delete {path:?}")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn delete_dir(path: impl AsRef<Path>) -> Result<(), Error> {
|
||||||
|
let path = path.as_ref();
|
||||||
|
tokio::fs::remove_dir_all(path)
|
||||||
|
.await
|
||||||
|
.or_else(|e| {
|
||||||
|
if e.kind() == std::io::ErrorKind::NotFound {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.with_ctx(|_| (ErrorKind::Filesystem, lazy_format!("delete dir {path:?}")))
|
||||||
|
}
|
||||||
|
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all)]
|
||||||
pub async fn rename(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<(), Error> {
|
pub async fn rename(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<(), Error> {
|
||||||
let src = src.as_ref();
|
let src = src.as_ref();
|
||||||
|
|||||||
Reference in New Issue
Block a user