Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dunce = "1.0"
filepath = "0.2"
flate2 = "1.1"
goblin = "0.10"
lzma-rs = "0.3"
postcard = { version = "1.1", features = ["alloc"] }
semver = { version = "1.0", features = ["serde"] }
tar = "0.4"
Expand Down
9 changes: 9 additions & 0 deletions lib/result.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::Error as IoError;
use std::path::PathBuf;

use lzma_rs::error::Error as LzmaError;
use postcard::Error as PostcardError;
use serde_json::Error as JsonError;
use thiserror::Error;
Expand Down Expand Up @@ -32,6 +33,8 @@ pub enum RokitError {
Postcard(Box<PostcardError>),
#[error("Zip file error: {0}")]
Zip(Box<ZipError>),
#[error("LZMA error: {0}")]
Lzma(Box<LzmaError>),
#[error("GitHub error: {0}")]
GitHub(Box<GithubError>),
}
Expand Down Expand Up @@ -82,6 +85,12 @@ impl From<ZipError> for RokitError {
}
}

impl From<LzmaError> for RokitError {
fn from(err: LzmaError) -> Self {
RokitError::Lzma(err.into())
}
}

impl From<GithubError> for RokitError {
fn from(err: GithubError) -> Self {
RokitError::GitHub(err.into())
Expand Down
23 changes: 23 additions & 0 deletions lib/sources/artifact/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::util::split_filename_and_extensions;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ArtifactFormat {
TarGz,
TarXz,
Tar,
Zip,
Gz,
Expand All @@ -20,6 +21,7 @@ impl ArtifactFormat {
Self::Zip => "zip",
Self::Tar => "tar",
Self::TarGz => "tar.gz",
Self::TarXz => "tar.xz",
Self::Gz => "gz",
}
}
Expand All @@ -30,11 +32,17 @@ impl ArtifactFormat {
[.., ext] if ext.eq_ignore_ascii_case("zip") => Some(Self::Zip),
[.., ext] if ext.eq_ignore_ascii_case("tar") => Some(Self::Tar),
[.., ext] if ext.eq_ignore_ascii_case("tgz") => Some(Self::TarGz),
[.., ext] if ext.eq_ignore_ascii_case("txz") => Some(Self::TarXz),
[.., ext1, ext2]
if ext1.eq_ignore_ascii_case("tar") && ext2.eq_ignore_ascii_case("gz") =>
{
Some(Self::TarGz)
}
[.., ext1, ext2]
if ext1.eq_ignore_ascii_case("tar") && ext2.eq_ignore_ascii_case("xz") =>
{
Some(Self::TarXz)
}
[.., ext] if ext.eq_ignore_ascii_case("gz") => Some(Self::Gz),
_ => None,
}
Expand All @@ -56,6 +64,7 @@ impl FromStr for ArtifactFormat {
"zip" => Ok(Self::Zip),
"tar" => Ok(Self::Tar),
"tar.gz" | "tgz" => Ok(Self::TarGz),
"tar.xz" | "txz" => Ok(Self::TarXz),
_ => Err(format!("unknown artifact format '{l}'")),
}
}
Expand All @@ -82,6 +91,8 @@ mod tests {
assert_eq!(format_from_str("file.tar"), Some(ArtifactFormat::Tar));
assert_eq!(format_from_str("file.tar.gz"), Some(ArtifactFormat::TarGz));
assert_eq!(format_from_str("file.tgz"), Some(ArtifactFormat::TarGz));
assert_eq!(format_from_str("file.tar.xz"), Some(ArtifactFormat::TarXz));
assert_eq!(format_from_str("file.txz"), Some(ArtifactFormat::TarXz));
assert_eq!(format_from_str("file.gz"), Some(ArtifactFormat::Gz));
assert_eq!(
format_from_str("file.with.many.extensions.tar.gz.zip"),
Expand All @@ -95,6 +106,10 @@ mod tests {
format_from_str("file.with.many.extensions.tar.gz"),
Some(ArtifactFormat::TarGz)
);
assert_eq!(
format_from_str("file.with.many.extensions.tar.xz"),
Some(ArtifactFormat::TarXz)
);
}

#[test]
Expand Down Expand Up @@ -145,6 +160,12 @@ mod tests {
assert_eq!(format_from_str("file.gz"), Some(ArtifactFormat::Gz));
assert_eq!(format_from_str("file.GZ"), Some(ArtifactFormat::Gz));
assert_eq!(format_from_str("file.Gz"), Some(ArtifactFormat::Gz));
assert_eq!(format_from_str("file.tar.xz"), Some(ArtifactFormat::TarXz));
assert_eq!(format_from_str("file.TAR.XZ"), Some(ArtifactFormat::TarXz));
assert_eq!(format_from_str("file.Tar.Xz"), Some(ArtifactFormat::TarXz));
assert_eq!(format_from_str("file.txz"), Some(ArtifactFormat::TarXz));
assert_eq!(format_from_str("file.TXZ"), Some(ArtifactFormat::TarXz));
assert_eq!(format_from_str("file.Txz"), Some(ArtifactFormat::TarXz));
}

#[test]
Expand All @@ -153,6 +174,7 @@ mod tests {
"tool-v1.0.0-x86_64-linux.zip",
"tool-v1.0.0-x86_64-linux.tar",
"tool-v1.0.0-x86_64-linux.tar.gz",
"tool-v1.0.0-x86_64-linux.tar.xz",
"tool-v1.0.0-x86_64-linux.gz",
"tool-v1.0.0-x86_64-linux",
"tool-v1.0.0-x86_64-linux.elf",
Expand All @@ -171,6 +193,7 @@ mod tests {
None,
None,
Some(ArtifactFormat::TarGz),
Some(ArtifactFormat::TarXz),
Some(ArtifactFormat::Tar),
Some(ArtifactFormat::Zip),
Some(ArtifactFormat::Gz),
Expand Down
6 changes: 5 additions & 1 deletion lib/sources/artifact/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
};

use super::{
decompression::decompress_gzip,
decompression::{decompress_gzip, decompress_xz},
extraction::{extract_tar_file, extract_zip_file},
github::models::GithubAsset,
ExtractError,
Expand Down Expand Up @@ -85,6 +85,10 @@ impl Artifact {
let tar = decompress_gzip(&contents).await?;
extract_tar_file(&tar, &file_name).await
}
ArtifactFormat::TarXz => {
let tar = decompress_xz(&contents).await?;
extract_tar_file(&tar, &file_name).await
}
ArtifactFormat::Gz => decompress_gzip(&contents).await.map(Some),
};

Expand Down
18 changes: 17 additions & 1 deletion lib/sources/artifact/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::Path;

const ALLOWED_EXTENSION_NAMES: [&str; 4] = ["zip", "tar", "gz", "tgz"];
const ALLOWED_EXTENSION_NAMES: [&str; 6] = ["zip", "tar", "gz", "tgz", "xz", "txz"];
const ALLOWED_EXTENSION_COUNT: usize = 2;

pub(super) fn split_filename_and_extensions(name: &str) -> (&str, Vec<&str>) {
Expand Down Expand Up @@ -52,6 +52,14 @@ mod tests {
split_filename_and_extensions("file.tar.gz"),
("file", vec!["tar", "gz"])
);
assert_eq!(
split_filename_and_extensions("file.tar.xz"),
("file", vec!["tar", "xz"])
);
assert_eq!(
split_filename_and_extensions("file.txz"),
("file", vec!["txz"])
);
assert_eq!(
split_filename_and_extensions("file.with.many.extensions.tar.gz.zip"),
("file.with.many.extensions.tar", vec!["gz", "zip"])
Expand All @@ -64,6 +72,10 @@ mod tests {
split_filename_and_extensions("file.with.many.extensions.tar.gz"),
("file.with.many.extensions", vec!["tar", "gz"])
);
assert_eq!(
split_filename_and_extensions("file.with.many.extensions.tar.xz"),
("file.with.many.extensions", vec!["tar", "xz"])
);
}

#[test]
Expand All @@ -84,5 +96,9 @@ mod tests {
split_filename_and_extensions("sentry-cli-linux-i686-2.32.1.tgz"),
("sentry-cli-linux-i686-2.32.1", vec!["tgz"])
);
assert_eq!(
split_filename_and_extensions("asphalt-x86_64-apple-darwin.tar.xz"),
("asphalt-x86_64-apple-darwin", vec!["tar", "xz"])
);
}
}
21 changes: 21 additions & 0 deletions lib/sources/decompression.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::Read;

use flate2::read::GzDecoder;
use lzma_rs::xz_decompress;
use tokio::{task::spawn_blocking, time::Instant};

use crate::result::RokitResult;
Expand All @@ -26,3 +27,23 @@ pub async fn decompress_gzip(gz_contents: impl AsRef<[u8]>) -> RokitResult<Vec<u
})
.await?
}

pub async fn decompress_xz(xz_contents: impl AsRef<[u8]>) -> RokitResult<Vec<u8>> {
let xz_contents = xz_contents.as_ref().to_vec();
let num_kilobytes = xz_contents.len() / 1024;
let start = Instant::now();

// using spawn_blocking for the same reason as gzip
spawn_blocking(move || {
let mut contents = Vec::new();
xz_decompress(&mut xz_contents.as_slice(), &mut contents)?;

tracing::trace!(
num_kilobytes,
elapsed = ?start.elapsed(),
"decompressed xz"
);
Ok(contents)
})
.await?
}