This commit is contained in:
2026-05-11 20:04:46 -05:00
parent b5986887be
commit 6956f67f32
3 changed files with 102 additions and 30 deletions
+50 -9
View File
@@ -122,6 +122,21 @@ fn resolve_key(key: Option<PathBuf>) -> KeyType {
EnvVar
}
fn single_file_entry(data: &[u8]) -> Option<PathBuf> {
let cursor = std::io::Cursor::new(data);
let gz = GzDecoder::new(cursor);
let mut archive = tar::Archive::new(gz);
let mut entries = archive.entries().ok()?;
let entry = entries.next()?.ok()?;
if !entry.header().entry_type().is_file() {
return None;
}
if entries.next().is_some() {
return None;
}
Some(entry.path().ok()?.into_owned())
}
impl DrawerOperation {
pub fn perform(&self) -> Result<(), DrawerError> {
self.validate()?;
@@ -138,11 +153,28 @@ impl DrawerOperation {
let decrypted = self.decrypt(encrypted)?;
let cursor = std::io::Cursor::new(decrypted);
let gz = GzDecoder::new(cursor);
let mut archive = tar::Archive::new(gz);
archive.unpack(&self.target_path)
.map_err(|_| DrawerError::UntarFailed)?;
if let Some(entry_name) = single_file_entry(&decrypted) {
let output = if self.target_inferred {
PathBuf::from(entry_name.file_name().unwrap_or_default())
} else {
self.target_path.clone()
};
let cursor = std::io::Cursor::new(&decrypted);
let gz = GzDecoder::new(cursor);
let mut archive = tar::Archive::new(gz);
let mut entry = archive.entries()
.map_err(|_| DrawerError::UntarFailed)?
.next()
.ok_or(DrawerError::UntarFailed)?
.map_err(|_| DrawerError::UntarFailed)?;
entry.unpack(&output).map_err(|_| DrawerError::UntarFailed)?;
} else {
let cursor = std::io::Cursor::new(decrypted);
let gz = GzDecoder::new(cursor);
let mut archive = tar::Archive::new(gz);
archive.unpack(&self.target_path)
.map_err(|_| DrawerError::UntarFailed)?;
}
Ok(())
}
@@ -174,8 +206,14 @@ impl DrawerOperation {
{
let gz = GzEncoder::new(&mut buf, Compression::default());
let mut archive = tar::Builder::new(gz);
archive.append_dir_all(".", &self.target_path)
.map_err(|_| DrawerError::TarFailed)?;
if self.target_path.is_dir() {
archive.append_dir_all(".", &self.target_path)
.map_err(|_| DrawerError::TarFailed)?;
} else {
let name = self.target_path.file_name().unwrap_or_default();
archive.append_path_with_name(&self.target_path, name)
.map_err(|_| DrawerError::TarFailed)?;
}
let gz = archive.into_inner()
.map_err(|_| DrawerError::TarFailed)?;
gz.finish()
@@ -268,6 +306,9 @@ impl DrawerOperation {
return Err(DrawerError::DrawerFileNotFound(self.drawer_file.clone()));
}
if !self.force && self.target_path.exists() {
if self.target_path.is_file() {
return Err(DrawerError::TargetNotEmpty(self.target_path.clone()));
}
let mut entries = std::fs::read_dir(&self.target_path)
.map_err(|_| DrawerError::TargetInvalid(self.target_path.clone()))?;
if entries.next().is_some() {
@@ -292,10 +333,10 @@ impl DrawerOperation {
}
let meta = std::fs::metadata(&self.target_path)
.map_err(|_| DrawerError::TargetInvalid(self.target_path.clone()))?;
if !meta.is_dir() {
if !meta.is_dir() && !meta.is_file() {
return Err(DrawerError::TargetInvalid(self.target_path.clone()));
}
if std::fs::read_dir(&self.target_path).is_err() {
if meta.is_dir() && std::fs::read_dir(&self.target_path).is_err() {
return Err(DrawerError::TargetInvalid(self.target_path.clone()));
}
let canon_target = self.target_path.canonicalize()