Browse Source

feat: 删除磁盘镜像以及删除sysroot (#76)

Signed-off-by: longjin <longjin@dragonos.org>
LoGin 5 months ago
parent
commit
c99db05566

+ 23 - 1
dadk/src/actions/rootfs/disk_img.rs

@@ -5,9 +5,12 @@ use anyhow::{anyhow, Result};
 use dadk_config::rootfs::{fstype::FsType, partition::PartitionType};
 use dadk_config::rootfs::{fstype::FsType, partition::PartitionType};
 
 
 use super::loopdev::LoopDeviceBuilder;
 use super::loopdev::LoopDeviceBuilder;
-pub(super) fn create(ctx: &DADKExecContext) -> Result<()> {
+pub(super) fn create(ctx: &DADKExecContext, skip_if_exists: bool) -> Result<()> {
     let disk_image_path = ctx.disk_image_path();
     let disk_image_path = ctx.disk_image_path();
     if disk_image_path.exists() {
     if disk_image_path.exists() {
+        if skip_if_exists {
+            return Ok(());
+        }
         return Err(anyhow!(
         return Err(anyhow!(
             "Disk image already exists: {}",
             "Disk image already exists: {}",
             disk_image_path.display()
             disk_image_path.display()
@@ -34,6 +37,24 @@ pub(super) fn create(ctx: &DADKExecContext) -> Result<()> {
     r
     r
 }
 }
 
 
+pub(super) fn delete(ctx: &DADKExecContext, skip_if_not_exists: bool) -> Result<()> {
+    let disk_image_path = ctx.disk_image_path();
+    if !disk_image_path.exists() {
+        if skip_if_not_exists {
+            return Ok(());
+        }
+        return Err(anyhow!(
+            "Disk image does not exist: {}",
+            disk_image_path.display()
+        ));
+    }
+    disk_path_safety_check(&disk_image_path)?;
+
+    std::fs::remove_file(&disk_image_path)
+        .map_err(|e| anyhow!("Failed to remove disk image: {}", e))?;
+    Ok(())
+}
+
 pub fn mount(ctx: &DADKExecContext) -> Result<()> {
 pub fn mount(ctx: &DADKExecContext) -> Result<()> {
     let disk_image_path = ctx.disk_image_path();
     let disk_image_path = ctx.disk_image_path();
     if !disk_image_path.exists() {
     if !disk_image_path.exists() {
@@ -154,6 +175,7 @@ pub fn umount(ctx: &DADKExecContext) -> Result<()> {
         }
         }
         log::info!("Loop device detached: {:?}", loop_dev_path);
         log::info!("Loop device detached: {:?}", loop_dev_path);
     }
     }
+
     Ok(())
     Ok(())
 }
 }
 
 

+ 4 - 3
dadk/src/actions/rootfs/mod.rs

@@ -3,12 +3,13 @@ use anyhow::Result;
 
 
 mod disk_img;
 mod disk_img;
 mod loopdev;
 mod loopdev;
+mod sysroot;
 
 
 pub(super) fn run(ctx: &DADKExecContext, rootfs_cmd: &RootFSCommand) -> Result<()> {
 pub(super) fn run(ctx: &DADKExecContext, rootfs_cmd: &RootFSCommand) -> Result<()> {
     match rootfs_cmd {
     match rootfs_cmd {
-        RootFSCommand::Create => disk_img::create(ctx),
-        RootFSCommand::Delete => todo!(),
-        RootFSCommand::DeleteSysroot => todo!(),
+        RootFSCommand::Create => disk_img::create(ctx, false),
+        RootFSCommand::Delete => disk_img::delete(ctx, false),
+        RootFSCommand::DeleteSysroot => sysroot::delete(ctx),
         RootFSCommand::Mount => disk_img::mount(ctx),
         RootFSCommand::Mount => disk_img::mount(ctx),
         RootFSCommand::Umount => disk_img::umount(ctx),
         RootFSCommand::Umount => disk_img::umount(ctx),
     }
     }

+ 26 - 0
dadk/src/actions/rootfs/sysroot.rs

@@ -0,0 +1,26 @@
+use anyhow::{anyhow, Result};
+
+use crate::context::DADKExecContext;
+
+pub(super) fn delete(ctx: &DADKExecContext) -> Result<()> {
+    let sysroot_dir = ctx.sysroot_dir()?;
+    // 检查 sysroot_dir 是否存在
+    if !sysroot_dir.exists() {
+        return Err(anyhow!("Sysroot directory does not exist"));
+    }
+
+    // 检查 sysroot_dir 是否是一个目录
+    if !sysroot_dir.is_dir() {
+        return Err(anyhow!("Sysroot path is not a directory"));
+    }
+
+    // 检查 sysroot_dir 是否是当前工作目录的子目录
+    if !sysroot_dir.starts_with(&ctx.workdir()) {
+        return Err(anyhow!(
+            "Sysroot directory must be a subdirectory of the current working directory"
+        ));
+    }
+
+    std::fs::remove_dir_all(sysroot_dir)?;
+    Ok(())
+}