This commit is contained in:
2026-05-11 20:23:07 -05:00
parent 6956f67f32
commit 1b9814f5c6
2 changed files with 57 additions and 57 deletions
+3 -6
View File
@@ -30,8 +30,7 @@ mod tests {
operation_type: Close, operation_type: Close,
drawer_file: "blah.drawer".into(), drawer_file: "blah.drawer".into(),
key: Path("foo.key".into()), key: Path("foo.key".into()),
target_path: "blah".into(), target_path: Some("blah".into()),
target_inferred: false,
force: false, force: false,
} }
)) ))
@@ -45,8 +44,7 @@ mod tests {
operation_type: Open, operation_type: Open,
drawer_file: "blah.drawer".into(), drawer_file: "blah.drawer".into(),
key: Path("foo.key".into()), key: Path("foo.key".into()),
target_path: "blah".into(), target_path: Some("blah".into()),
target_inferred: false,
force: false, force: false,
} }
)) ))
@@ -60,8 +58,7 @@ mod tests {
operation_type: Open, operation_type: Open,
drawer_file: "blah.drawer".into(), drawer_file: "blah.drawer".into(),
key: Path("foo.key".into()), key: Path("foo.key".into()),
target_path: "blah".into(), target_path: None,
target_inferred: true,
force: false, force: false,
} }
)) ))
+54 -51
View File
@@ -22,9 +22,8 @@ pub struct DrawerOperation {
pub operation_type: OperationType, pub operation_type: OperationType,
pub drawer_file: PathBuf, pub drawer_file: PathBuf,
pub key: KeyType, pub key: KeyType,
pub target_path: PathBuf, pub target_path: Option<PathBuf>,
pub force: bool, pub force: bool,
pub target_inferred: bool,
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@@ -74,32 +73,22 @@ where
match cli.command { match cli.command {
Command::Open { drawer_file, target_path, key, force } => { Command::Open { drawer_file, target_path, key, force } => {
let key = resolve_key(key); let key = resolve_key(key);
let (target, target_inferred) = match target_path {
Some(p) => (p, false),
None => (PathBuf::from(drawer_file.file_stem().unwrap_or_default()), true),
};
Ok(DrawerOperation { Ok(DrawerOperation {
operation_type: OperationType::Open, operation_type: OperationType::Open,
drawer_file, drawer_file,
key, key,
target_path: target, target_path,
force, force,
target_inferred,
}) })
} }
Command::Close { drawer_file, target_path, key } => { Command::Close { drawer_file, target_path, key } => {
let key = resolve_key(key); let key = resolve_key(key);
let (target, target_inferred) = match target_path {
Some(p) => (p, false),
None => (PathBuf::from(drawer_file.file_stem().unwrap_or_default()), true),
};
Ok(DrawerOperation { Ok(DrawerOperation {
operation_type: OperationType::Close, operation_type: OperationType::Close,
drawer_file, drawer_file,
key, key,
target_path: target, target_path,
force: false, force: false,
target_inferred
}) })
} }
Command::Key { filename } => { Command::Key { filename } => {
@@ -107,9 +96,8 @@ where
operation_type: OperationType::Key, operation_type: OperationType::Key,
drawer_file: filename, drawer_file: filename,
key: KeyType::EnvVar, key: KeyType::EnvVar,
target_path: PathBuf::from("."), target_path: None,
force: false, force: false,
target_inferred: false,
}) })
} }
} }
@@ -138,6 +126,16 @@ fn single_file_entry(data: &[u8]) -> Option<PathBuf> {
} }
impl DrawerOperation { impl DrawerOperation {
// Returns the default target when no target_path is specified.
// For close and for directory drawers on open: the drawer file stem.
// For single-file drawers on open: the filename stored in the archive.
fn default_target(&self, single_file: Option<&std::path::Path>) -> PathBuf {
match single_file {
Some(p) => PathBuf::from(p.file_name().unwrap_or_default()),
None => PathBuf::from(self.drawer_file.file_stem().unwrap_or_default()),
}
}
pub fn perform(&self) -> Result<(), DrawerError> { pub fn perform(&self) -> Result<(), DrawerError> {
self.validate()?; self.validate()?;
match self.operation_type { match self.operation_type {
@@ -153,12 +151,26 @@ impl DrawerOperation {
let decrypted = self.decrypt(encrypted)?; let decrypted = self.decrypt(encrypted)?;
if let Some(entry_name) = single_file_entry(&decrypted) { let sf = single_file_entry(&decrypted);
let output = if self.target_inferred { let target = self.target_path.as_ref().cloned()
PathBuf::from(entry_name.file_name().unwrap_or_default()) .unwrap_or_else(|| self.default_target(sf.as_deref()));
} else {
self.target_path.clone() if self.target_path.is_none() {
}; println!("(assuming into {})", target.display());
}
if !self.force && target.exists() {
if target.is_file() {
return Err(DrawerError::TargetNotEmpty(target.clone()));
}
let mut entries = std::fs::read_dir(&target)
.map_err(|_| DrawerError::TargetInvalid(target.clone()))?;
if entries.next().is_some() {
return Err(DrawerError::TargetNotEmpty(target.clone()));
}
}
if sf.is_some() {
let cursor = std::io::Cursor::new(&decrypted); let cursor = std::io::Cursor::new(&decrypted);
let gz = GzDecoder::new(cursor); let gz = GzDecoder::new(cursor);
let mut archive = tar::Archive::new(gz); let mut archive = tar::Archive::new(gz);
@@ -167,13 +179,12 @@ impl DrawerOperation {
.next() .next()
.ok_or(DrawerError::UntarFailed)? .ok_or(DrawerError::UntarFailed)?
.map_err(|_| DrawerError::UntarFailed)?; .map_err(|_| DrawerError::UntarFailed)?;
entry.unpack(&output).map_err(|_| DrawerError::UntarFailed)?; entry.unpack(&target).map_err(|_| DrawerError::UntarFailed)?;
} else { } else {
let cursor = std::io::Cursor::new(decrypted); let cursor = std::io::Cursor::new(decrypted);
let gz = GzDecoder::new(cursor); let gz = GzDecoder::new(cursor);
let mut archive = tar::Archive::new(gz); let mut archive = tar::Archive::new(gz);
archive.unpack(&self.target_path) archive.unpack(&target).map_err(|_| DrawerError::UntarFailed)?;
.map_err(|_| DrawerError::UntarFailed)?;
} }
Ok(()) Ok(())
} }
@@ -202,16 +213,19 @@ impl DrawerOperation {
} }
fn perform_close(&self) -> Result<(), DrawerError> { fn perform_close(&self) -> Result<(), DrawerError> {
let target = self.target_path.as_ref().cloned()
.unwrap_or_else(|| self.default_target(None));
let mut buf: Vec<u8> = Vec::new(); let mut buf: Vec<u8> = Vec::new();
{ {
let gz = GzEncoder::new(&mut buf, Compression::default()); let gz = GzEncoder::new(&mut buf, Compression::default());
let mut archive = tar::Builder::new(gz); let mut archive = tar::Builder::new(gz);
if self.target_path.is_dir() { if target.is_dir() {
archive.append_dir_all(".", &self.target_path) archive.append_dir_all(".", &target)
.map_err(|_| DrawerError::TarFailed)?; .map_err(|_| DrawerError::TarFailed)?;
} else { } else {
let name = self.target_path.file_name().unwrap_or_default(); let name = target.file_name().unwrap_or_default();
archive.append_path_with_name(&self.target_path, name) archive.append_path_with_name(&target, name)
.map_err(|_| DrawerError::TarFailed)?; .map_err(|_| DrawerError::TarFailed)?;
} }
let gz = archive.into_inner() let gz = archive.into_inner()
@@ -299,22 +313,9 @@ impl DrawerOperation {
} }
fn validate_open(&self) -> Result<(), DrawerError> { fn validate_open(&self) -> Result<(), DrawerError> {
if self.target_inferred {
println!("(assuming into {})", self.target_path.display());
}
if !self.drawer_file.exists() { if !self.drawer_file.exists() {
return Err(DrawerError::DrawerFileNotFound(self.drawer_file.clone())); 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() {
return Err(DrawerError::TargetNotEmpty(self.target_path.clone()));
}
}
Ok(()) Ok(())
} }
@@ -328,19 +329,21 @@ impl DrawerOperation {
} }
fn validate_close(&self) -> Result<(), DrawerError> { fn validate_close(&self) -> Result<(), DrawerError> {
if self.target_inferred { let target = self.target_path.as_ref().cloned()
println!("(assuming from {})", self.target_path.display()); .unwrap_or_else(|| self.default_target(None));
if self.target_path.is_none() {
println!("(assuming from {})", target.display());
} }
let meta = std::fs::metadata(&self.target_path) let meta = std::fs::metadata(&target)
.map_err(|_| DrawerError::TargetInvalid(self.target_path.clone()))?; .map_err(|_| DrawerError::TargetInvalid(target.clone()))?;
if !meta.is_dir() && !meta.is_file() { if !meta.is_dir() && !meta.is_file() {
return Err(DrawerError::TargetInvalid(self.target_path.clone())); return Err(DrawerError::TargetInvalid(target.clone()));
} }
if meta.is_dir() && std::fs::read_dir(&self.target_path).is_err() { if meta.is_dir() && std::fs::read_dir(&target).is_err() {
return Err(DrawerError::TargetInvalid(self.target_path.clone())); return Err(DrawerError::TargetInvalid(target.clone()));
} }
let canon_target = self.target_path.canonicalize() let canon_target = target.canonicalize()
.unwrap_or_else(|_| self.target_path.clone()); .unwrap_or_else(|_| target.clone());
let drawer_parent = match self.drawer_file.parent() { let drawer_parent = match self.drawer_file.parent() {
None => Path::new("."), None => Path::new("."),
Some(p) if p == Path::new("") => { Some(p) if p == Path::new("") => {