mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-26 02:43:24 +00:00
Fix OSDK original directory not restored if bundle validation fails
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
858e95ed4d
commit
7601509e6e
@ -22,6 +22,7 @@ use crate::{
|
||||
},
|
||||
error::Errno,
|
||||
error_msg,
|
||||
util::DirGuard,
|
||||
};
|
||||
|
||||
/// 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: BundleManifest = toml::from_str(&manifest_file_content).ok()?;
|
||||
|
||||
let original_dir = std::env::current_dir().unwrap();
|
||||
std::env::set_current_dir(&path).unwrap();
|
||||
let _dir_guard = DirGuard::change_dir(&path);
|
||||
|
||||
if let Some(aster_bin) = &manifest.aster_bin {
|
||||
if !aster_bin.validate() {
|
||||
@ -103,8 +103,6 @@ impl Bundle {
|
||||
}
|
||||
}
|
||||
|
||||
std::env::set_current_dir(original_dir).unwrap();
|
||||
|
||||
Some(Self {
|
||||
manifest,
|
||||
path: path.as_ref().to_path_buf(),
|
||||
|
@ -239,6 +239,7 @@ fn build_kernel_elf(
|
||||
}
|
||||
|
||||
info!("Building kernel ELF using command: {:#?}", command);
|
||||
info!("Building directory: {:?}", std::env::current_dir().unwrap());
|
||||
|
||||
let status = command.status().unwrap();
|
||||
if !status.success() {
|
||||
|
@ -251,3 +251,42 @@ pub fn trace_panic_from_log(qemu_log: File, bin_path: PathBuf) {
|
||||
addr2line_proc.kill().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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user