Skip to content

Commit

Permalink
fix: Match against the full extension. (#58)
Browse files Browse the repository at this point in the history
* Update ext handling.

* Bump rust.
  • Loading branch information
milesj authored Mar 3, 2024
1 parent 087f020 commit ac8e328
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
14 changes: 7 additions & 7 deletions crates/archive/src/archive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::archive_error::ArchiveError;
use crate::join_file_name;
use crate::tree_differ::TreeDiffer;
use crate::{get_full_file_extension, join_file_name};
use rustc_hash::{FxHashMap, FxHashSet};
use starbase_utils::glob;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -162,7 +162,7 @@ impl<'owner> Archiver<'owner> {
/// Determine the packer to use based on the archive file extension,
/// then pack the archive using [`Archiver.pack`].
pub fn pack_from_ext(&self) -> ArchiveResult<()> {
match self.archive_file.extension().map(|e| e.to_str().unwrap()) {
match get_full_file_extension(self.archive_file).as_deref() {
Some("tar") => {
#[cfg(feature = "tar")]
self.pack(crate::tar::TarPacker::new)?;
Expand All @@ -174,7 +174,7 @@ impl<'owner> Archiver<'owner> {
}
.into());
}
Some("tgz" | "gz") => {
Some("tar.gz" | "tgz") => {
#[cfg(feature = "tar-gz")]
self.pack(crate::tar::TarPacker::new_gz)?;

Expand All @@ -185,7 +185,7 @@ impl<'owner> Archiver<'owner> {
}
.into());
}
Some("txz" | "xz") => {
Some("tar.xz" | "txz") => {
#[cfg(feature = "tar-xz")]
self.pack(crate::tar::TarPacker::new_xz)?;

Expand Down Expand Up @@ -271,7 +271,7 @@ impl<'owner> Archiver<'owner> {
/// Determine the unpacker to use based on the archive file extension,
/// then unpack the archive using [`Archiver.unpack`].
pub fn unpack_from_ext(&self) -> ArchiveResult<()> {
match self.archive_file.extension().map(|e| e.to_str().unwrap()) {
match get_full_file_extension(self.archive_file).as_deref() {
Some("tar") => {
#[cfg(feature = "tar")]
self.unpack(crate::tar::TarUnpacker::new)?;
Expand All @@ -283,7 +283,7 @@ impl<'owner> Archiver<'owner> {
}
.into());
}
Some("tgz" | "gz") => {
Some("tar.gz" | "tgz") => {
#[cfg(feature = "tar-gz")]
self.unpack(crate::tar::TarUnpacker::new_gz)?;

Expand All @@ -294,7 +294,7 @@ impl<'owner> Archiver<'owner> {
}
.into());
}
Some("txz" | "xz") => {
Some("tar.xz" | "txz") => {
#[cfg(feature = "tar-xz")]
self.unpack(crate::tar::TarUnpacker::new_xz)?;

Expand Down
20 changes: 17 additions & 3 deletions crates/archive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use archive::*;
pub use archive_error::*;
pub use tree_differ::*;

use starbase_utils::fs;
use std::path::Path;

/// Join a file name from a list of parts, removing any empty parts.
Expand All @@ -43,16 +44,29 @@ where
.join("/")
}

/// Extract the full extension from a file path, like `tar.gz`,
/// instead of just `gz`. If no file extension is found, returns `None`.`
pub fn get_full_file_extension(path: &Path) -> Option<String> {
let file_name = fs::file_name(path);

// Std lib `extension()` just returns the final part
if let Some(i) = file_name.find('.') {
return Some(file_name[i + 1..].to_owned());
}

None
}

/// Return true if the file path has a supported archive extension.
/// This does not check against feature flags!
pub fn is_supported_archive_extension(path: &Path) -> bool {
path.extension()
get_full_file_extension(path)
.map(|ext| {
ext == "tar"
|| ext == "tgz"
|| ext == "gz"
|| ext == "tar.gz"
|| ext == "txz"
|| ext == "xz"
|| ext == "tar.xz"
|| ext == "zstd"
|| ext == "zst"
|| ext == "zip"
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
profile = "default"
channel = "1.75.0"
channel = "1.76.0"

0 comments on commit ac8e328

Please sign in to comment.