-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework the api now using libarchive, instead of shell
- fix the lzip file; - add uncompress_archive func; - add uncompress_archive_file func; - add uncompres_archive_file func; - add integration tests; - rework error enum; - add a binding script generator; - rework the example; Signed-off-by: asakiz <asakizin@gmail.com>
- Loading branch information
Showing
13 changed files
with
779 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// cargo-deps: bindgen = "0.51.1", pkg-config = "0.3.17" | ||
|
||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
let mut lib = pkg_config::Config::new() | ||
.atleast_version("3.2.2") | ||
.probe("libarchive") | ||
.expect("Fail to detect the libarchive library"); | ||
|
||
let include_path = lib | ||
.include_paths | ||
.pop() | ||
.unwrap_or(PathBuf::from("usr/include")); | ||
|
||
let bindings = bindgen::Builder::default() | ||
// Set rustfmt setting | ||
.rustfmt_configuration_file(Some(".rustfmt.toml".into())) | ||
|
||
// Set include path | ||
.header(format!("{}/archive.h", include_path.display())) | ||
.header(format!("{}/archive_entry.h", include_path.display())) | ||
|
||
// We need to add this as raw_line to pass cargo clippy warning about | ||
// convert to upper camel case | ||
.raw_line("#![allow(non_camel_case_types)]\n") | ||
|
||
// We need to add this as raw_line otherwise bindgen generates this as | ||
// u32, causing type mismatch | ||
.raw_line("pub const ARCHIVE_EOF: i32 = 1;") | ||
.raw_line("pub const ARCHIVE_OK: i32 = 0;") | ||
|
||
// Binding whitelist | ||
.whitelist_var("ARCHIVE_EXTRACT_TIME") | ||
.whitelist_var("ARCHIVE_EXTRACT_PERM") | ||
.whitelist_var("ARCHIVE_EXTRACT_ACL") | ||
.whitelist_var("ARCHIVE_EXTRACT_FFLAGS") | ||
.whitelist_var("ARCHIVE_EXTRACT_OWNER") | ||
.whitelist_var("ARCHIVE_EXTRACT_FFLAGS") | ||
.whitelist_var("ARCHIVE_EXTRACT_XATTR") | ||
.whitelist_function("archive_read_new") | ||
.whitelist_function("archive_read_support_filter_all") | ||
.whitelist_function("archive_read_support_format_all") | ||
.whitelist_function("archive_read_support_format_raw") | ||
.whitelist_function("archive_read_close") | ||
.whitelist_function("archive_read_free") | ||
.whitelist_function("archive_read_data_block") | ||
.whitelist_function("archive_read_next_header") | ||
.whitelist_function("archive_read_open") | ||
.whitelist_function("archive_write_disk_new") | ||
.whitelist_function("archive_write_disk_set_options") | ||
.whitelist_function("archive_write_disk_set_standard_lookup") | ||
.whitelist_function("archive_write_header") | ||
.whitelist_function("archive_write_finish_entry") | ||
.whitelist_function("archive_write_data_block") | ||
.whitelist_function("archive_write_close") | ||
.whitelist_function("archive_write_free") | ||
.whitelist_function("archive_entry_pathname") | ||
.whitelist_function("archive_entry_free") | ||
.whitelist_function("archive_entry_set_pathname") | ||
.whitelist_function("archive_entry_set_hardlink") | ||
.whitelist_function("archive_entry_hardlink") | ||
.whitelist_function("archive_set_error") | ||
.whitelist_function("archive_error_string") | ||
.generate() | ||
.expect("Unable to generate bindings"); | ||
|
||
bindings | ||
.write_to_file(PathBuf::from("src/ffi.rs")) | ||
.expect("Couldn't write bindings!"); | ||
|
||
println!("Sucessfully generated bindings for libarchive."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fn main() { | ||
// This forces the tests to run in a single thread. This is | ||
// required for use of the mocks as we run mocked binaries. | ||
pkg_config::Config::new() | ||
.atleast_version("3.2.2") | ||
.probe("libarchive") | ||
.expect("Fail to detect the libarchive library"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (C) 2019 O.S. Systems Sofware LTDA | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
use crate::ffi; | ||
use std::ffi::CStr; | ||
use thiserror::Error; | ||
|
||
pub type Result<T> = std::result::Result<T, Error>; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error("Extraction error: '{0}'")] | ||
ExtractionError(String), | ||
|
||
#[error("Nix error: '{0}'")] | ||
Nix(#[from] nix::Error), | ||
|
||
#[error("Io error: '{0}'")] | ||
Io(#[from] std::io::Error), | ||
|
||
#[error("Utf error: '{0}'")] | ||
Utf(#[from] std::str::Utf8Error), | ||
|
||
#[error("Try from int error: '{0}'")] | ||
TryInt(#[from] std::num::TryFromIntError), | ||
|
||
#[error("Error to create the archive struct, is null")] | ||
ArchiveNull, | ||
|
||
#[error("The entry is null, failed to set the pathname")] | ||
EntryNull, | ||
|
||
#[error("File not found")] | ||
FileNotFound, | ||
|
||
#[error("The end of file")] | ||
EndOfFile, | ||
} | ||
|
||
pub(crate) fn archive_result(value: i32, archive: *mut ffi::archive) -> Result<()> { | ||
if value != ffi::ARCHIVE_OK { | ||
return Err(Error::from(archive)); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[allow(clippy::not_unsafe_ptr_arg_deref)] | ||
impl From<*mut ffi::archive> for Error { | ||
fn from(input: *mut ffi::archive) -> Self { | ||
unsafe { | ||
let input = ffi::archive_error_string(input); | ||
Error::ExtractionError(CStr::from_ptr(input).to_string_lossy().to_string()) | ||
} | ||
} | ||
} |
Oops, something went wrong.