ksud: fix incorrecy RAII

This commit is contained in:
tiann
2023-02-02 19:21:37 +08:00
parent dc902b16d4
commit 0bc36b3299
3 changed files with 11 additions and 2 deletions

View File

@@ -505,7 +505,9 @@ fn do_install_module(zip: String) -> Result<()> {
// mount the modules_update.img to mountpoint // mount the modules_update.img to mountpoint
println!("- Mounting image"); println!("- Mounting image");
mount::AutoMountExt4::try_new(tmp_module_img, module_update_tmp_dir)?; let _dontdrop = mount::AutoMountExt4::try_new(tmp_module_img, module_update_tmp_dir)?;
info!("mounted {} to {}", tmp_module_img, module_update_tmp_dir);
setsyscon(module_update_tmp_dir)?; setsyscon(module_update_tmp_dir)?;
@@ -529,8 +531,10 @@ fn do_install_module(zip: String) -> Result<()> {
exec_install_script(&zip)?; exec_install_script(&zip)?;
info!("rename {tmp_module_img} to {}", defs::MODULE_UPDATE_IMG);
// all done, rename the tmp image to modules_update.img // all done, rename the tmp image to modules_update.img
if std::fs::rename(tmp_module_img, defs::MODULE_UPDATE_IMG).is_err() { if std::fs::rename(tmp_module_img, defs::MODULE_UPDATE_IMG).is_err() {
warn!("Rename image failed, try copy it.");
std::fs::copy(tmp_module_img, defs::MODULE_UPDATE_IMG) std::fs::copy(tmp_module_img, defs::MODULE_UPDATE_IMG)
.with_context(|| "Failed to copy image.".to_string())?; .with_context(|| "Failed to copy image.".to_string())?;
let _ = std::fs::remove_file(tmp_module_img); let _ = std::fs::remove_file(tmp_module_img);
@@ -538,6 +542,8 @@ fn do_install_module(zip: String) -> Result<()> {
mark_update()?; mark_update()?;
info!("Module install successfully!");
Ok(()) Ok(())
} }
@@ -583,7 +589,7 @@ where
ensure_clean_dir(update_dir)?; ensure_clean_dir(update_dir)?;
// mount the modules_update img // mount the modules_update img
mount::AutoMountExt4::try_new(defs::MODULE_UPDATE_TMP_IMG, update_dir)?; let _dontdrop = mount::AutoMountExt4::try_new(defs::MODULE_UPDATE_TMP_IMG, update_dir)?;
// call the operation func // call the operation func
let result = func(id, update_dir); let result = func(id, update_dir);

View File

@@ -77,6 +77,7 @@ impl AutoMountExt4 {
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
impl Drop for AutoMountExt4 { impl Drop for AutoMountExt4 {
fn drop(&mut self) { fn drop(&mut self) {
log::info!("AutoMountExt4 drop: {}", self.mnt);
let _ = self.umount(); let _ = self.umount();
} }
} }

View File

@@ -8,7 +8,9 @@ use std::{
pub fn ensure_clean_dir(dir: &str) -> Result<()> { pub fn ensure_clean_dir(dir: &str) -> Result<()> {
let path = Path::new(dir); let path = Path::new(dir);
log::debug!("ensure_clean_dir: {}", path.display());
if path.exists() { if path.exists() {
log::debug!("ensure_clean_dir: {} exists, remove it", path.display());
std::fs::remove_dir_all(path)?; std::fs::remove_dir_all(path)?;
} }
Ok(std::fs::create_dir_all(path)?) Ok(std::fs::create_dir_all(path)?)