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 gzip and xz extraction; verbose #26

Merged
merged 3 commits into from Aug 9, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -83,3 +83,5 @@ raw-cpuid = "2.0"
redox_syscall = "0.1"
tar = { git = "https://github.com/redox-os/tar-rs.git" }
termion = { git = "https://github.com/redox-os/termion.git", branch = "redox_termios" }
rust-lzma = "0.2"
tree_magic = "0.2"
35 changes: 28 additions & 7 deletions src/bin/tar.rs
@@ -1,13 +1,19 @@
#![deny(warnings)]

extern crate tar;
extern crate tree_magic;
extern crate lzma;
extern crate libflate;

use std::{env, process};
use std::io::{stdin, stdout, stderr, copy, Result, Read, Write};
use std::io::{stdin, stdout, stderr, copy, Error, ErrorKind, Result, Read, Write, BufReader};
use std::fs::{self, File};
use std::os::unix::fs::OpenOptionsExt;
use std::path::Path;

use tar::{Archive, Builder, EntryType};
use lzma::LzmaReader;
use libflate::gzip::Decoder as GzipDecoder;

fn create_inner<T: Write>(input: &str, ar: &mut Builder<T>) -> Result<()> {
if try!(fs::metadata(input)).is_dir() {
Expand Down Expand Up @@ -54,7 +60,7 @@ fn list(tar: &str) -> Result<()> {
}
}

fn extract_inner<T: Read>(ar: &mut Archive<T>) -> Result<()> {
fn extract_inner<T: Read>(ar: &mut Archive<T>, verbose: bool) -> Result<()> {
for entry_result in try!(ar.entries()) {
let mut entry = try!(entry_result);
match entry.header().entry_type() {
Expand Down Expand Up @@ -83,16 +89,30 @@ fn extract_inner<T: Read>(ar: &mut Archive<T>) -> Result<()> {
panic!("Unsupported entry type {:?}", other);
}
}

if verbose {
println!("{}", entry.path()?.display());
}
}

Ok(())
}

fn extract(tar: &str) -> Result<()> {
fn extract(tar: &str, verbose: bool) -> Result<()> {
if tar == "-" {
extract_inner(&mut Archive::new(stdin()))
extract_inner(&mut Archive::new(stdin()), verbose)
} else {
extract_inner(&mut Archive::new(try!(File::open(tar))))
let mime = tree_magic::from_filepath(Path::new(&tar));
let file = BufReader::new(File::open(tar)?);
if mime == "application/x-xz" {
extract_inner(&mut Archive::new(LzmaReader::new_decompressor(file)
.map_err(|e| Error::new(ErrorKind::Other, e))?), verbose)
} else if mime == "application/gzip" {
extract_inner(&mut Archive::new(GzipDecoder::new(file)
.map_err(|e| Error::new(ErrorKind::Other, e))?), verbose)
} else {
extract_inner(&mut Archive::new(file), verbose)
}
}
}

Expand Down Expand Up @@ -130,9 +150,10 @@ fn main() {
process::exit(1);
}
},
"x" | "xf" => {
"x" | "xf" | "xvf" => {
let tar = args.next().unwrap_or("-".to_string());
if let Err(err) = extract(&tar) {
let verbose = op.contains('v');
if let Err(err) = extract(&tar, verbose) {
write!(stderr(), "tar: extract: failed: {}\n", err).unwrap();
process::exit(1);
}
Expand Down