Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support snappy format #215

Merged
merged 1 commit into from
Dec 8, 2021
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
support snappy format
  • Loading branch information
figsoda committed Dec 7, 2021
commit 52a8acf2e12872b976a876ba99d968504a8f2c80
7 changes: 7 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 @@ -22,6 +22,7 @@ libc = "0.2.103"
linked-hash-map = "0.5.4"
lzzzz = "0.8.0"
once_cell = "1.8.0"
snap = "1.0.5"
tar = "0.4.37"
walkdir = "2.3.2"
xz2 = "0.1.6"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ ouch c file.txt file.zip

# Supported formats

| Format | `.tar` | `.zip` | `.bz`, `.bz2` | `.gz` | `.lz4` | `.xz`, `.lzma` | `.zst` |
|:---------:|:------:|:------:|:-------------:|:-----:|:------:|:---------------------:|:------:|
| Supported | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Format | `.tar` | `.zip` | `.bz`, `.bz2` | `.gz` | `.lz4` | `.xz`, `.lzma` | `.sz` | `.zst` |
|:---------:|:------:|:------:|:-------------:|:-----:|:------:|:--------------:|:-----:|:-------|
| Supported | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |

And the aliases: `tgz`, `tbz`, `tbz2`, `tlz4`, `txz`, `tlz`, `tlzma`, `tzst`.
And the aliases: `tgz`, `tbz`, `tbz2`, `tlz4`, `txz`, `tlzma`, `tsz`, `tzst`.

Formats can be chained:

Expand Down
9 changes: 6 additions & 3 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ fn compress_files(files: Vec<PathBuf>, formats: Vec<Extension>, output_file: fs:
Bzip => Box::new(bzip2::write::BzEncoder::new(encoder, Default::default())),
Lz4 => Box::new(lzzzz::lz4f::WriteCompressor::new(encoder, Default::default())?),
Lzma => Box::new(xz2::write::XzEncoder::new(encoder, 6)),
Snappy => Box::new(snap::write::FrameEncoder::new(encoder)),
Zstd => {
let zstd_encoder = zstd::stream::write::Encoder::new(encoder, Default::default());
// Safety:
Expand All @@ -321,7 +322,7 @@ fn compress_files(files: Vec<PathBuf>, formats: Vec<Extension>, output_file: fs:
}

match formats[0].compression_formats[0] {
Gzip | Bzip | Lz4 | Lzma | Zstd => {
Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => {
let _progress = Progress::new_accessible_aware(
total_input_size,
precise,
Expand Down Expand Up @@ -454,6 +455,7 @@ fn decompress_file(
Bzip => Box::new(bzip2::read::BzDecoder::new(decoder)),
Lz4 => Box::new(lzzzz::lz4f::ReadDecompressor::new(decoder)?),
Lzma => Box::new(xz2::read::XzDecoder::new(decoder)),
Snappy => Box::new(snap::read::FrameDecoder::new(decoder)),
Zstd => Box::new(zstd::stream::Decoder::new(decoder)?),
Tar | Zip => unreachable!(),
};
Expand All @@ -466,7 +468,7 @@ fn decompress_file(

let files_unpacked;
match formats[0].compression_formats[0] {
Gzip | Bzip | Lz4 | Lzma | Zstd => {
Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => {
reader = chain_reader_decoder(&formats[0].compression_formats[0], reader)?;

let writer = utils::create_or_ask_overwrite(&output_file_path, question_policy)?;
Expand Down Expand Up @@ -580,6 +582,7 @@ fn list_archive_contents(
Bzip => Box::new(bzip2::read::BzDecoder::new(decoder)),
Lz4 => Box::new(lzzzz::lz4f::ReadDecompressor::new(decoder)?),
Lzma => Box::new(xz2::read::XzDecoder::new(decoder)),
Snappy => Box::new(snap::read::FrameDecoder::new(decoder)),
Zstd => Box::new(zstd::stream::Decoder::new(decoder)?),
Tar | Zip => unreachable!(),
};
Expand All @@ -604,7 +607,7 @@ fn list_archive_contents(

crate::archive::zip::list_archive(zip_archive)?
}
Gzip | Bzip | Lz4 | Lzma | Zstd => {
Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => {
panic!("Not an archive! This should never happen, if it does, something is wrong with `CompressionFormat::is_archive()`. Please report this error!");
}
};
Expand Down
10 changes: 8 additions & 2 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ pub enum CompressionFormat {
Lz4,
/// .xz .lzma
Lzma,
/// tar, tgz, tbz, tbz2, txz, tlz, tlz4, tlzma, tzst
/// .sz
Snappy,
/// tar, tgz, tbz, tbz2, txz, tlz4, tlzma, tsz, tzst
Tar,
/// .zst
Zstd,
Expand All @@ -75,6 +77,7 @@ impl CompressionFormat {
Bzip => false,
Lz4 => false,
Lzma => false,
Snappy => false,
Zstd => false,
}
}
Expand All @@ -91,6 +94,7 @@ impl fmt::Display for CompressionFormat {
Zstd => ".zst",
Lz4 => ".lz4",
Lzma => ".lz",
Snappy => ".sz",
Tar => ".tar",
Zip => ".zip",
}
Expand Down Expand Up @@ -121,13 +125,15 @@ pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<Exten
"tgz" => &[Tar, Gzip],
"tbz" | "tbz2" => &[Tar, Bzip],
"tlz4" => &[Tar, Lz4],
"txz" | "tlz" | "tlzma" => &[Tar, Lzma],
"txz" | "tlzma" => &[Tar, Lzma],
"tsz" => &[Tar, Snappy],
"tzst" => &[Tar, Zstd],
"zip" => &[Zip],
"bz" | "bz2" => &[Bzip],
"gz" => &[Gzip],
"lz4" => &[Lz4],
"xz" | "lzma" => &[Lzma],
"sz" => &[Snappy],
"zst" => &[Zstd],
_ => break,
};
Expand Down
5 changes: 5 additions & 0 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ pub fn try_infer_extension(path: &Path) -> Option<Extension> {
fn is_lz4(buf: &[u8]) -> bool {
buf.starts_with(&[0x04, 0x22, 0x4D, 0x18])
}
fn is_sz(buf: &[u8]) -> bool {
buf.starts_with(&[0xFF, 0x06, 0x00, 0x00, 0x73, 0x4E, 0x61, 0x50, 0x70, 0x59])
}
fn is_zst(buf: &[u8]) -> bool {
buf.starts_with(&[0x28, 0xB5, 0x2F, 0xFD])
}
Expand Down Expand Up @@ -113,6 +116,8 @@ pub fn try_infer_extension(path: &Path) -> Option<Extension> {
Some(Extension::new(&[Lzma], "xz"))
} else if is_lz4(&buf) {
Some(Extension::new(&[Lz4], "lz4"))
} else if is_sz(&buf) {
Some(Extension::new(&[Snappy], "sz"))
} else if is_zst(&buf) {
Some(Extension::new(&[Zstd], "zst"))
} else {
Expand Down
3 changes: 2 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ enum DirectoryExtension {
Tbz,
Tgz,
Tlz4,
Tlz,
Tlzma,
Tsz,
Txz,
Zip,
}
Expand All @@ -35,6 +35,7 @@ enum FileExtension {
Gz,
Lz4,
Lzma,
Sz,
Xz,
}

Expand Down
3 changes: 1 addition & 2 deletions tests/mime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn sanity_check_through_mime() {
write_random_content(test_file, &mut SmallRng::from_entropy());

let formats = [
"tar", "zip", "tar.gz", "tgz", "tbz", "tbz2", "txz", "tlz", "tlzma", "tzst", "tar.bz", "tar.bz2", "tar.lzma",
"tar", "zip", "tar.gz", "tgz", "tbz", "tbz2", "txz", "tlzma", "tzst", "tar.bz", "tar.bz2", "tar.lzma",
"tar.xz", "tar.zst",
];

Expand All @@ -30,7 +30,6 @@ fn sanity_check_through_mime() {
"application/x-bzip2",
"application/x-xz",
"application/x-xz",
"application/x-xz",
"application/zstd",
"application/x-bzip2",
"application/x-bzip2",
Expand Down