From 64d7a1c9e16836615602636e346a356960dabd00 Mon Sep 17 00:00:00 2001 From: Noor Date: Tue, 8 Apr 2025 09:31:45 +0000 Subject: [PATCH] fix: decompression on enclave unpacking each file into enclave archive with top level directory --- src/sgx/types/dtpm.rs | 38 ++++++++++++++++++++++++++++++-------- tests/dtpm-config_test.rs | 3 +-- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/sgx/types/dtpm.rs b/src/sgx/types/dtpm.rs index 961d117..a50e95d 100644 --- a/src/sgx/types/dtpm.rs +++ b/src/sgx/types/dtpm.rs @@ -205,13 +205,13 @@ impl DtpmConfig { pub fn load_data(mut self) -> Result { for file_entry in self.filesystems.iter_mut() { - if let FileContent::Path(path) = &file_entry.content { - if Path::new(path).is_dir() { - let compressed_data = compress_directory(path)?; + if let FileContent::Path(content_path) = &file_entry.content { + if Path::new(content_path).is_dir() { + let compressed_data = compress_directory(content_path)?; file_entry.content = FileContent::Archive(compressed_data); } else { - let content = std::fs::read(path) - .unwrap_or_else(|_| panic!("Unable to read file {path}")); + let content = std::fs::read(content_path) + .unwrap_or_else(|_| panic!("Unable to read file {content_path}")); let encoded = BASE64.encode(content); file_entry.content = FileContent::Data(encoded); } @@ -223,9 +223,10 @@ impl DtpmConfig { } pub fn compress_directory(input_dir: &str) -> Result> { + let path = format!("{}/", input_dir.split('/').next_back().unwrap_or("files")); let mut tar_builder = Builder::new(Encoder::new(Vec::new(), 3)?); - tar_builder.append_dir_all(".", input_dir)?; + tar_builder.append_dir_all(path, input_dir)?; tar_builder.finish()?; @@ -235,9 +236,30 @@ pub fn compress_directory(input_dir: &str) -> Result> { Ok(compressed_data) } -pub fn decompress_directory(save_path: &str, archive_buff: Vec) -> Result<()> { +pub fn decompress_directory(dst: &str, archive_buff: Vec) -> Result<()> { + /* + for entry in Archive::new(Decoder::new(archive_buff.as_slice())?).entries()? { + let entry = entry?; + let path = entry.path(); + println!( + "Entry: {:?}; type {:?}; size: {}", + path?, + entry.header().entry_type(), + entry.size() + ); + } + */ let mut archive = Archive::new(Decoder::new(archive_buff.as_slice())?); - archive.unpack(save_path)?; + for entry in archive.entries()? { + let mut file = entry?; + let file_path = format!("{dst}/{}", &file.path()?.to_string_lossy()); + if file.header().entry_type() == tar::EntryType::Directory { + std::fs::create_dir_all(file_path)?; + } else { + file.unpack(file_path)?; + } + } + Ok(()) } diff --git a/tests/dtpm-config_test.rs b/tests/dtpm-config_test.rs index 3cbca31..ec6baa7 100644 --- a/tests/dtpm-config_test.rs +++ b/tests/dtpm-config_test.rs @@ -41,7 +41,6 @@ fn test_decompression_02() { decompress_directory(&TEMP_DIR, archive_buff).unwrap(); - let path = format!("{}/{}", *TEMP_DIR, "fixtures/dtpm_config.yaml"); - dbg!(&path); + let path = format!("{}/{}", *TEMP_DIR, "tests/fixtures/dtpm_config.yaml"); assert!(std::path::Path::new(&path).exists()); }