Skip to content

Commit

Permalink
fix: remove tar command deps. close #21
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 20, 2022
1 parent 87be27d commit 26bf31f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 76 deletions.
18 changes: 0 additions & 18 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ sha2 = "0.10.2"
libflate = "1"
url = "2.2.2"
zip = "0.5.13"
which = "4.2.4"
semver = "1.0.6"
wait-timeout = "0.1.5"
thiserror = "1.0.30"
Expand Down
64 changes: 7 additions & 57 deletions src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ use std::fs::{self, File};
use std::io;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::Command as ChildProcess;

use eyre::Report;
use libflate::gzip::Decoder as GzDecoder;
use tar::Archive;
use which::which;

fn extract_tar_gz(
src_filepath: &Path,
Expand All @@ -20,62 +18,14 @@ fn extract_tar_gz(
) -> Result<PathBuf, Report> {
let output_file_path = dest_dir.join(extract_file_name);

// use tar command
// wait for fix: https://github.com/alexcrichton/tar-rs/issues/286
if let Ok(tar_command_path) = which("tar") {
let unpack_exe_file_path = {
let mut unpack = dest_dir.to_path_buf();
for p in extract_file_in_tarball_file_path.split('/') {
if p.is_empty() {
continue;
}
unpack = dest_dir.join(p).to_path_buf();
}

unpack = unpack.join(extract_file_name);

unpack
};
fs::create_dir_all(dest_dir).map_err(|e| {
eyre::format_err!("can not create folder '{}': {}", dest_dir.display(), e)
})?;

match ChildProcess::new(tar_command_path)
.current_dir(dest_dir)
.arg("-f")
.arg(format!("{}", src_filepath.display()))
.arg("-zx")
.spawn()
{
Ok(mut child) => match child.wait() {
Ok(state) => {
if state.success() {
// Rename it to the target folder if the executable file is not locate in root tarball
if extract_file_in_tarball_file_path != "/" {
fs::rename(&unpack_exe_file_path, &output_file_path)?;
}
Ok(output_file_path)
} else {
Err(eyre::format_err!(
"unpack file and exit code: {}",
state.code().unwrap_or(1),
))
}
}
Err(e) => Err(eyre::format_err!("{}", e)),
},
Err(e) => Err(eyre::format_err!("{}", e)),
}
} else {
extract_archive(
GzDecoder::new(File::open(&src_filepath)?)?,
extract_file_name,
extract_file_in_tarball_file_path,
&output_file_path,
)?;
extract_archive(
GzDecoder::new(File::open(&src_filepath)?)?,
extract_file_name,
extract_file_in_tarball_file_path,
&output_file_path,
)?;

Ok(output_file_path)
}
Ok(output_file_path)
}

fn extract_tar(
Expand Down

0 comments on commit 26bf31f

Please sign in to comment.