DTPM: upload files as stream and directory support #2

Merged
ghe0 merged 5 commits from feat_dir_support_dtpm_config into staging 2025-04-21 14:41:01 +00:00
2 changed files with 31 additions and 10 deletions
Showing only changes of commit 64d7a1c9e1 - Show all commits

@ -205,13 +205,13 @@ impl DtpmConfig {
pub fn load_data(mut self) -> Result<Self> { pub fn load_data(mut self) -> Result<Self> {
for file_entry in self.filesystems.iter_mut() { for file_entry in self.filesystems.iter_mut() {
if let FileContent::Path(path) = &file_entry.content { if let FileContent::Path(content_path) = &file_entry.content {
if Path::new(path).is_dir() { if Path::new(content_path).is_dir() {
let compressed_data = compress_directory(path)?; let compressed_data = compress_directory(content_path)?;
file_entry.content = FileContent::Archive(compressed_data); file_entry.content = FileContent::Archive(compressed_data);
} else { } else {
let content = std::fs::read(path) let content = std::fs::read(content_path)
.unwrap_or_else(|_| panic!("Unable to read file {path}")); .unwrap_or_else(|_| panic!("Unable to read file {content_path}"));
let encoded = BASE64.encode(content); let encoded = BASE64.encode(content);
file_entry.content = FileContent::Data(encoded); file_entry.content = FileContent::Data(encoded);
} }
@ -223,9 +223,10 @@ impl DtpmConfig {
} }
pub fn compress_directory(input_dir: &str) -> Result<Vec<u8>> { pub fn compress_directory(input_dir: &str) -> Result<Vec<u8>> {
let path = format!("{}/", input_dir.split('/').next_back().unwrap_or("files"));
let mut tar_builder = Builder::new(Encoder::new(Vec::new(), 3)?); 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()?; tar_builder.finish()?;
@ -235,9 +236,30 @@ pub fn compress_directory(input_dir: &str) -> Result<Vec<u8>> {
Ok(compressed_data) Ok(compressed_data)
} }
pub fn decompress_directory(save_path: &str, archive_buff: Vec<u8>) -> Result<()> { pub fn decompress_directory(dst: &str, archive_buff: Vec<u8>) -> 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())?); 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(()) Ok(())
} }

@ -41,7 +41,6 @@ fn test_decompression_02() {
decompress_directory(&TEMP_DIR, archive_buff).unwrap(); decompress_directory(&TEMP_DIR, archive_buff).unwrap();
let path = format!("{}/{}", *TEMP_DIR, "fixtures/dtpm_config.yaml"); let path = format!("{}/{}", *TEMP_DIR, "tests/fixtures/dtpm_config.yaml");
dbg!(&path);
assert!(std::path::Path::new(&path).exists()); assert!(std::path::Path::new(&path).exists());
} }