mirror of
https://github.com/NixOS/nix.git
synced 2025-11-12 21:46:01 +01:00
Merge branch 'libarchive' of https://github.com/yorickvP/nix
This commit is contained in:
commit
ee235e764c
17 changed files with 128 additions and 182 deletions
|
|
@ -1,22 +1,4 @@
|
|||
use super::{
|
||||
error,
|
||||
foreign::{self},
|
||||
store::path,
|
||||
store::StorePath,
|
||||
util,
|
||||
};
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn unpack_tarfile(
|
||||
source: foreign::Source,
|
||||
dest_dir: &str,
|
||||
out: *mut Result<(), error::CppException>,
|
||||
) {
|
||||
out.write(
|
||||
util::tarfile::unpack_tarfile(source, std::path::Path::new(dest_dir))
|
||||
.map_err(|err| err.into()),
|
||||
);
|
||||
}
|
||||
use super::{error, store::path, store::StorePath, util};
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ffi_String_new(s: &str, out: *mut String) {
|
||||
|
|
|
|||
|
|
@ -1,14 +0,0 @@
|
|||
/// A wrapper around Nix's Source class that provides the Read trait.
|
||||
#[repr(C)]
|
||||
pub struct Source {
|
||||
fun: extern "C" fn(this: *mut libc::c_void, data: &mut [u8]) -> usize,
|
||||
this: *mut libc::c_void,
|
||||
}
|
||||
|
||||
impl std::io::Read for Source {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::result::Result<usize, std::io::Error> {
|
||||
let n = (self.fun)(self.this, buf);
|
||||
assert!(n <= buf.len());
|
||||
Ok(n)
|
||||
}
|
||||
}
|
||||
|
|
@ -12,7 +12,6 @@ extern crate proptest;
|
|||
#[cfg(not(test))]
|
||||
mod c;
|
||||
mod error;
|
||||
mod foreign;
|
||||
#[cfg(unused)]
|
||||
mod nar;
|
||||
mod store;
|
||||
|
|
|
|||
|
|
@ -1,2 +1 @@
|
|||
pub mod base32;
|
||||
pub mod tarfile;
|
||||
|
|
|
|||
|
|
@ -1,56 +0,0 @@
|
|||
use crate::{foreign::Source, Error};
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
use std::path::{Component, Path};
|
||||
use tar::Archive;
|
||||
|
||||
pub fn unpack_tarfile(source: Source, dest_dir: &Path) -> Result<(), Error> {
|
||||
fs::create_dir_all(dest_dir)?;
|
||||
|
||||
let mut tar = Archive::new(source);
|
||||
|
||||
for file in tar.entries()? {
|
||||
let mut file = file?;
|
||||
|
||||
let path = file.path()?;
|
||||
|
||||
for i in path.components() {
|
||||
if let Component::Prefix(_) | Component::RootDir | Component::ParentDir = i {
|
||||
return Err(Error::BadTarFileMemberName(
|
||||
file.path()?.to_str().unwrap().to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let dest_file = dest_dir.join(path);
|
||||
|
||||
fs::create_dir_all(dest_file.parent().unwrap())?;
|
||||
|
||||
match file.header().entry_type() {
|
||||
tar::EntryType::Directory => {
|
||||
fs::create_dir(dest_file)?;
|
||||
}
|
||||
tar::EntryType::Regular => {
|
||||
let mode = if file.header().mode()? & (libc::S_IXUSR as u32) == 0 {
|
||||
0o666
|
||||
} else {
|
||||
0o777
|
||||
};
|
||||
let mut f = fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.mode(mode)
|
||||
.open(dest_file)?;
|
||||
io::copy(&mut file, &mut f)?;
|
||||
}
|
||||
tar::EntryType::Symlink => {
|
||||
std::os::unix::fs::symlink(file.header().link_name()?.unwrap(), dest_file)?;
|
||||
}
|
||||
tar::EntryType::XGlobalHeader | tar::EntryType::XHeader => {}
|
||||
t => return Err(Error::Misc(format!("unsupported tar entry type '{:?}'", t))),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue