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 tar.gz where files are prefixed by "./" #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion examples/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Example updating an executable to the latest version released via GitHub
#[macro_use]
extern crate self_update;

fn run() -> Result<(), Box<::std::error::Error>> {
fn run() -> Result<(), Box<dyn ::std::error::Error>> {
let releases = self_update::backends::github::ReleaseList::configure()
.repo_owner("jaemk")
.repo_name("self_update")
Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl std::error::Error for Error {
"Self Update Error"
}

fn cause(&self) -> Option<&std::error::Error> {
fn cause(&self) -> Option<&dyn std::error::Error> {
use Error::*;
Some(match *self {
Io(ref e) => e,
Expand Down
36 changes: 26 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,10 @@ impl<'a> Extract<'a> {
let mut entry = archive
.entries()?
.filter_map(|e| e.ok())
.find(|e| e.path().ok().filter(|p| p == file_to_extract).is_some())
.find(|e|
e.path().ok().filter(|p|
p.strip_prefix("./").unwrap_or(p) == file_to_extract
).is_some())
.ok_or_else(|| {
Error::Update(format!(
"Could not find the required path in the archive: {:?}",
Expand Down Expand Up @@ -643,18 +646,19 @@ mod tests {
io::copy(&mut tar_writer.as_slice(), &mut e)
.expect("failed writing from tar archive to gz encoder");
e.finish().expect("gz finish fail");
archive_file.sync_all().expect("sync fs");

let out_tmp = TempDir::new("self_update_unpack_tar_gzip_outdir").expect("tempdir fail");
let out_path = out_tmp.path();
Extract::from_source(&archive_fp)
.extract_into(&out_path)
.expect("extract fail");

let out_file = out_path.join("inner_archive/temp.txt");
let out_file = out_path.join("inner_archive").join("temp.txt");
assert!(out_file.exists());
cmp_content(&out_file, "This is a test!");

let out_file = out_path.join("inner_archive/temp2.txt");
let out_file = out_path.join("inner_archive").join("temp2.txt");
assert!(out_file.exists());
cmp_content(&out_file, "This is a second test!");
}
Expand All @@ -667,6 +671,7 @@ mod tests {
let mut e = GzEncoder::new(&mut tmp_file, flate2::Compression::default());
e.write_all(b"This is a test!").expect("gz encode fail");
e.finish().expect("gz finish fail");
tmp_file.sync_all().expect("sync fs");

let out_tmp =
TempDir::new("self_update_unpack_file_plain_gzip_outdir").expect("tempdir fail");
Expand All @@ -679,8 +684,7 @@ mod tests {
cmp_content(out_file, "This is a test!");
}

#[test]
fn unpack_file_tar_gzip() {
fn unpack_file_tar_gzip_with_folder(archive_top_folder: &str, prefix_file_to_extract: &str) {
let tmp_dir = TempDir::new("self_update_unpack_file_tar_gzip_src").expect("tempdir fail");
let tmp_path = tmp_dir.path();

Expand All @@ -692,7 +696,7 @@ mod tests {
tmp_file.write_all(b"This is a test!").unwrap();

let mut ar = tar::Builder::new(vec![]);
ar.append_dir_all("inner_archive", &archive_src)
ar.append_dir_all(archive_top_folder, &archive_src)
.expect("tar append dir all fail");
let tar_writer = ar.into_inner().expect("failed getting tar writer");

Expand All @@ -702,26 +706,36 @@ mod tests {
io::copy(&mut tar_writer.as_slice(), &mut e)
.expect("failed writing from tar archive to gz encoder");
e.finish().expect("gz finish fail");
archive_file.sync_all().expect("sync fs");

let out_tmp =
TempDir::new("self_update_unpack_file_tar_gzip_outdir").expect("tempdir fail");
let out_path = out_tmp.path();
let file_to_extract = format!("{}temp.txt", prefix_file_to_extract);
//dbg!(&out_path, &file_to_extract);
Extract::from_source(&archive_fp)
.extract_file(&out_path, "inner_archive/temp.txt")
.extract_file(&out_path, &file_to_extract)
.expect("extract fail");
let out_file = out_path.join("inner_archive/temp.txt");
let out_file = out_path.join(&file_to_extract);
assert!(out_file.exists());
cmp_content(&out_file, "This is a test!");
}

#[test]
fn unpack_file_tar_gzip() {
for folder in &[("inner_archive", "inner_archive/"), ("", ""), ("./", "")] {
unpack_file_tar_gzip_with_folder(folder.0, folder.1)
}
}

#[test]
fn unpack_zip() {
let tmp_dir = TempDir::new("self_update_unpack_zip_src").expect("tempdir fail");
let tmp_path = tmp_dir.path();

let archive_path = tmp_path.join("archive.zip");
let archive_file = File::create(&archive_path).expect("create file fail");
let mut zip = zip::ZipWriter::new(archive_file);
let mut zip = zip::ZipWriter::new(&archive_file);
let options =
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored);
zip.start_file("zipped.txt", options)
Expand All @@ -733,6 +747,7 @@ mod tests {
zip.write_all(b"This is a second test!")
.expect("failed writing to second zip");
zip.finish().expect("failed finishing zip");
archive_file.sync_all().expect("sync fs");

let out_tmp = TempDir::new("self_update_unpack_zip_outdir").expect("tempdir fail");
let out_path = out_tmp.path();
Expand All @@ -755,7 +770,7 @@ mod tests {

let archive_path = tmp_path.join("archive.zip");
let archive_file = File::create(&archive_path).expect("create file fail");
let mut zip = zip::ZipWriter::new(archive_file);
let mut zip = zip::ZipWriter::new(&archive_file);
let options =
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored);
zip.start_file("zipped.txt", options)
Expand All @@ -767,6 +782,7 @@ mod tests {
zip.write_all(b"This is a second test!")
.expect("failed writing to second zip");
zip.finish().expect("failed finishing zip");
archive_file.sync_all().expect("sync fs");

let out_tmp = TempDir::new("self_update_unpack_zip_outdir").expect("tempdir fail");
let out_path = out_tmp.path();
Expand Down