Skip to content

Commit

Permalink
Unpack tar archive, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
anti-social committed Apr 27, 2016
1 parent e0e9264 commit d042b2f
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 24 deletions.
39 changes: 39 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Expand Up @@ -25,6 +25,10 @@ regex = "0.1.41"
scan_dir = "0.3.1"
libmount = "0.1.1"
zip = "0.1"
tar = "0.4"
flate2 = "0.2"
bzip2 = "0.3"
xz2 = "0.1"

[[bin]]
name = "vagga"
Expand Down
78 changes: 54 additions & 24 deletions src/builder/commands/tarcmd.rs
@@ -1,12 +1,18 @@
use std::fs::Permissions;
use std::fs::{File, Permissions};
use std::fs::{create_dir_all, set_permissions};
use std::io::Read;
use std::os::unix::fs::PermissionsExt;
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};

use unshare::{Command, Stdio};
use libmount::BindMount;

use tar::Archive;
use flate2::read::GzDecoder;
use bzip2::read::BzDecoder;
use xz2::read::XzDecoder;

use container::mount::{unmount};
use builder::context::Context;
use builder::download::{maybe_download_and_check_hashsum};
Expand Down Expand Up @@ -38,31 +44,55 @@ pub fn unpack_file(_ctx: &mut Context, src: &Path, tgt: &Path,
-> Result<(), String>
{
info!("Unpacking {} -> {}", src.display(), tgt.display());
let mut cmd = Command::new("/vagga/bin/busybox");
cmd.stdin(Stdio::null())
.arg("tar")
.arg("-x")
.arg("-f").arg(src)
.arg("-C").arg(tgt);
for i in includes.iter() {
cmd.arg(i);
}
for i in excludes.iter() {
cmd.arg("--exclude").arg(i);
}
// let mut cmd = Command::new("/vagga/bin/busybox");
// cmd.stdin(Stdio::null())
// .arg("tar")
// .arg("-x")
// .arg("-f").arg(src)
// .arg("-C").arg(tgt);
// for i in includes.iter() {
// cmd.arg(i);
// }
// for i in excludes.iter() {
// cmd.arg("--exclude").arg(i);
// }

match src.extension().and_then(|x| x.to_str()) {
Some("gz")|Some("tgz") => { cmd.arg("-z"); }
Some("bz")|Some("tbz") => { cmd.arg("-j"); }
Some("xz")|Some("txz") => { cmd.arg("-J"); }
_ => {}
let file = try_msg!(File::open(src), "Cannot open file: {err}");
let (gz, bz, xz, t);
let mut tar: &Archive<Read> = match src.extension().and_then(|x| x.to_str()) {
Some("gz")|Some("tgz") => {
let fin = try_msg!(GzDecoder::new(file), "Cannot decode file: {err}");
gz = Archive::new(fin);
&gz
// cmd.arg("-z");
}
Some("bz")|Some("tbz") => {
let fin = BzDecoder::new(file);
bz = Archive::new(fin);
&bz
// cmd.arg("-j");
}
Some("xz")|Some("txz") => {
let fin = XzDecoder::new(file);
xz = Archive::new(fin);
&xz
// cmd.arg("-J");
}
_ => {
t = Archive::new(file);
&t
}
};
info!("Running: {:?}", cmd);
match cmd.status() {
Ok(st) if st.success() => Ok(()),
Ok(val) => Err(format!("Tar exited with status: {}", val)),
Err(e) => Err(format!("Error running tar: {}", e)),
}

try_msg!(tar.unpack(tgt), "Cannot unpack archive: {err}");
Ok(())

// info!("Running: {:?}", cmd);
// match cmd.status() {
// Ok(st) if st.success() => Ok(()),
// Ok(val) => Err(format!("Tar exited with status: {}", val)),
// Err(e) => Err(format!("Error running tar: {}", e)),
// }
}

pub fn tar_command(ctx: &mut Context, tar: &Tar) -> Result<(), String>
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Expand Up @@ -17,6 +17,10 @@ extern crate signal;
extern crate regex;
extern crate scan_dir;
extern crate zip;
extern crate tar;
extern crate flate2;
extern crate bzip2;
extern crate xz2;
#[macro_use] extern crate matches;
#[macro_use] extern crate mopa;
#[macro_use] extern crate log;
Expand Down
15 changes: 15 additions & 0 deletions tests/generic.bats
Expand Up @@ -194,6 +194,21 @@ setup() {
[[ $link = ".roots/vagga.f4d65ae1/root" ]]
}

@test "generic: unpack tar.gz" {
# curl -o test-file.zip http://files.zerogw.com/test-files/test-file.zip
# unzip test-file.zip
# tar -c -v -z -f test-file.tar.gz install.sh dir
# rm -rf install.sh dir

run vagga _build tar-local
printf "%s\n" "${lines[@]}"
# link=$(readlink .vagga/tar-local)
# [[ $link = ".roots/tar-local.f4d65ae1/root" ]]
[[ $(cat .vagga/tar-local/root/test/1/dir/file.txt) = "Hello" ]]
[[ $(cat .vagga/tar-local/root/test/1/dir/file2.txt) = "2" ]]
[[ -x .vagga/tar-local/root/test/1/install.sh ]]
}

@test "generic: unpack zip archive" {
curl -o test-file.zip http://files.zerogw.com/test-files/test-file.zip
hash=($(sha256sum test-file.zip))
Expand Down
10 changes: 10 additions & 0 deletions tests/generic/vagga.yaml
Expand Up @@ -39,6 +39,16 @@ containers:
- !Alpine v3.1
- !Sh env

tar-local:
setup:
- !Alpine v3.3
- !EnsureDir /root/test/1/dir
- !Text
/root/test/1/dir/file.txt: Hello world!
- !Tar
url: ./test-file.tar.gz
path: /root/test/1

unzip-local:
setup:
- !Alpine v3.3
Expand Down

0 comments on commit d042b2f

Please sign in to comment.