Fix OSDK original directory not restored if bundle validation fails

This commit is contained in:
Ruize Tang
2024-12-09 16:56:25 +08:00
committed by Tate, Hongliang Tian
parent 858e95ed4d
commit 7601509e6e
3 changed files with 42 additions and 4 deletions

View File

@ -22,6 +22,7 @@ use crate::{
}, },
error::Errno, error::Errno,
error_msg, error_msg,
util::DirGuard,
}; };
/// The osdk bundle artifact that stores as `bundle` directory. /// The osdk bundle artifact that stores as `bundle` directory.
@ -84,8 +85,7 @@ impl Bundle {
let manifest_file_content = std::fs::read_to_string(manifest_file_path).ok()?; let manifest_file_content = std::fs::read_to_string(manifest_file_path).ok()?;
let manifest: BundleManifest = toml::from_str(&manifest_file_content).ok()?; let manifest: BundleManifest = toml::from_str(&manifest_file_content).ok()?;
let original_dir = std::env::current_dir().unwrap(); let _dir_guard = DirGuard::change_dir(&path);
std::env::set_current_dir(&path).unwrap();
if let Some(aster_bin) = &manifest.aster_bin { if let Some(aster_bin) = &manifest.aster_bin {
if !aster_bin.validate() { if !aster_bin.validate() {
@ -103,8 +103,6 @@ impl Bundle {
} }
} }
std::env::set_current_dir(original_dir).unwrap();
Some(Self { Some(Self {
manifest, manifest,
path: path.as_ref().to_path_buf(), path: path.as_ref().to_path_buf(),

View File

@ -239,6 +239,7 @@ fn build_kernel_elf(
} }
info!("Building kernel ELF using command: {:#?}", command); info!("Building kernel ELF using command: {:#?}", command);
info!("Building directory: {:?}", std::env::current_dir().unwrap());
let status = command.status().unwrap(); let status = command.status().unwrap();
if !status.success() { if !status.success() {

View File

@ -251,3 +251,42 @@ pub fn trace_panic_from_log(qemu_log: File, bin_path: PathBuf) {
addr2line_proc.kill().unwrap(); addr2line_proc.kill().unwrap();
addr2line_proc.wait().unwrap(); addr2line_proc.wait().unwrap();
} }
/// A guard that ensures the current working directory is restored
/// to its original state when the guard goes out of scope.
pub struct DirGuard(PathBuf);
impl DirGuard {
/// Creates a new `DirGuard` that restores the provided directory
/// when it goes out of scope.
///
/// # Arguments
///
/// * `original_dir` - The directory to restore when the guard is dropped.
pub fn new(original_dir: PathBuf) -> Self {
Self(original_dir)
}
/// Creates a new `DirGuard` using the current working directory as the original directory.
pub fn from_current_dir() -> Self {
Self::new(std::env::current_dir().unwrap())
}
/// Stores the current directory as the original directory and
/// changes the working directory to the specified `new_dir`.
///
/// # Arguments
///
/// * `new_dir` - The directory to switch to.
pub fn change_dir(new_dir: impl AsRef<Path>) -> Self {
let original_dir_guard = DirGuard::from_current_dir();
std::env::set_current_dir(new_dir.as_ref()).unwrap();
original_dir_guard
}
}
impl Drop for DirGuard {
fn drop(&mut self) {
std::env::set_current_dir(&self.0).unwrap();
}
}