use std::path::{Component, Path, PathBuf};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ArchiveFormat {
Zip,
Tar,
TarGz,
}
impl ArchiveFormat {
pub const fn label(self) -> &'static str {
match self {
Self::Zip => "zip",
Self::Tar => "tar",
Self::TarGz => "tar.gz",
}
}
}
pub fn detect_format(path: &Path) -> Option<ArchiveFormat> {
let name = path.file_name()?.to_string_lossy().to_lowercase();
if name.ends_with(".tar.gz") || name.ends_with(".tgz") {
Some(ArchiveFormat::TarGz)
} else if name.ends_with(".tar") {
Some(ArchiveFormat::Tar)
} else if name.ends_with(".zip") {
Some(ArchiveFormat::Zip)
} else {
None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EntryKind {
File,
Dir,
Symlink,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArchiveEntry {
pub source: PathBuf,
pub name: PathBuf,
pub kind: EntryKind,
}
pub fn default_archive_name(selected: &[&str], cwd: &Path) -> String {
let stem = match selected {
[single] => (*single).to_string(),
_ => cwd
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_else(|| "archive".to_string()),
};
format!("{stem}.zip")
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EntryTarget {
Within(PathBuf),
ArchiveRoot,
Escaping,
}
pub fn classify_entry_path(raw: &Path) -> EntryTarget {
let mut safe = PathBuf::new();
for component in raw.components() {
match component {
Component::Normal(part) => safe.push(part),
Component::CurDir => {}
Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
return EntryTarget::Escaping
}
}
}
if safe.as_os_str().is_empty() {
return EntryTarget::ArchiveRoot;
}
EntryTarget::Within(safe)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detects_each_supported_format() {
assert_eq!(
detect_format(Path::new("/a/x.zip")),
Some(ArchiveFormat::Zip)
);
assert_eq!(
detect_format(Path::new("/a/x.tar")),
Some(ArchiveFormat::Tar)
);
assert_eq!(
detect_format(Path::new("/a/x.tar.gz")),
Some(ArchiveFormat::TarGz)
);
assert_eq!(
detect_format(Path::new("/a/x.tgz")),
Some(ArchiveFormat::TarGz)
);
}
#[test]
fn default_name_for_one_entry_is_its_own() {
assert_eq!(
default_archive_name(&["report.pdf"], Path::new("/home/u")),
"report.pdf.zip"
);
assert_eq!(
default_archive_name(&["src"], Path::new("/home/u")),
"src.zip"
);
}
#[test]
fn default_name_for_several_is_the_directory() {
assert_eq!(
default_archive_name(&["a", "b", "c"], Path::new("/home/u/photos")),
"photos.zip"
);
}
#[test]
fn default_name_at_the_root_is_generic() {
assert_eq!(
default_archive_name(&["a", "b"], Path::new("/")),
"archive.zip"
);
}
#[test]
fn tar_gz_wins_over_tar() {
assert_eq!(
detect_format(Path::new("bundle.tar.gz")),
Some(ArchiveFormat::TarGz)
);
}
#[test]
fn detection_ignores_case() {
assert_eq!(detect_format(Path::new("X.ZIP")), Some(ArchiveFormat::Zip));
assert_eq!(
detect_format(Path::new("X.Tar.GZ")),
Some(ArchiveFormat::TarGz)
);
}
#[test]
fn unsupported_names_are_not_archives() {
assert_eq!(detect_format(Path::new("notes.txt")), None);
assert_eq!(detect_format(Path::new("archive.rar")), None);
assert_eq!(detect_format(Path::new("blob.gz")), None);
assert_eq!(detect_format(Path::new("noextension")), None);
}
#[test]
fn ordinary_entry_paths_are_kept_relative() {
assert_eq!(
classify_entry_path(Path::new("src/main.rs")),
EntryTarget::Within(PathBuf::from("src/main.rs"))
);
assert_eq!(
classify_entry_path(Path::new("./src/./main.rs")),
EntryTarget::Within(PathBuf::from("src/main.rs"))
);
}
#[test]
fn the_archive_root_entry_is_not_an_escape() {
assert_eq!(
classify_entry_path(Path::new("./")),
EntryTarget::ArchiveRoot
);
assert_eq!(
classify_entry_path(Path::new(".")),
EntryTarget::ArchiveRoot
);
assert_eq!(classify_entry_path(Path::new("")), EntryTarget::ArchiveRoot);
}
#[test]
fn entries_escaping_upwards_are_refused() {
assert_eq!(
classify_entry_path(Path::new("../evil")),
EntryTarget::Escaping
);
assert_eq!(
classify_entry_path(Path::new("a/../../evil")),
EntryTarget::Escaping
);
assert_eq!(
classify_entry_path(Path::new("a/b/../c")),
EntryTarget::Escaping
);
}
#[test]
fn absolute_entries_are_refused() {
assert_eq!(
classify_entry_path(Path::new("/etc/passwd")),
EntryTarget::Escaping
);
assert_eq!(classify_entry_path(Path::new("/")), EntryTarget::Escaping);
}
}