mirror of
https://github.com/NixOS/nix
synced 2025-06-25 06:31:14 +02:00
Add some Rust code
This commit is contained in:
parent
abb8ef619b
commit
11da5b2816
10 changed files with 194 additions and 3 deletions
59
nix-rust/Cargo.lock
generated
Normal file
59
nix-rust/Cargo.lock
generated
Normal file
|
@ -0,0 +1,59 @@
|
|||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "filetime"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.50"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "nix-rust"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tar 0.4.22 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.1.51"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "tar"
|
||||
version = "0.4.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"filetime 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"xattr 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "xattr"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[metadata]
|
||||
"checksum cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "11d43355396e872eefb45ce6342e4374ed7bc2b3a502d1b28e36d6e23c05d1f4"
|
||||
"checksum filetime 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a2df5c1a8c4be27e7707789dc42ae65976e60b394afd293d1419ab915833e646"
|
||||
"checksum libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)" = "aab692d7759f5cd8c859e169db98ae5b52c924add2af5fbbca11d12fefb567c1"
|
||||
"checksum redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)" = "423e376fffca3dfa06c9e9790a9ccd282fafb3cc6e6397d01dbf64f9bacc6b85"
|
||||
"checksum tar 0.4.22 (registry+https://github.com/rust-lang/crates.io-index)" = "c2167ff53da2a661702b3299f71a91b61b1dffef36b4b2884b1f9c67254c0133"
|
||||
"checksum xattr 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "244c3741f4240ef46274860397c7c74e50eb23624996930e484c16679633a54c"
|
12
nix-rust/Cargo.toml
Normal file
12
nix-rust/Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "nix-rust"
|
||||
version = "0.1.0"
|
||||
authors = ["Eelco Dolstra <edolstra@gmail.com>"]
|
||||
|
||||
[lib]
|
||||
name = "nixrust"
|
||||
crate-type = ["staticlib"]
|
||||
|
||||
[dependencies]
|
||||
tar = "0.4"
|
||||
libc = "0.2"
|
7
nix-rust/local.mk
Normal file
7
nix-rust/local.mk
Normal file
|
@ -0,0 +1,7 @@
|
|||
libnixrust_PATH := $(d)/target/release/libnixrust.a
|
||||
libnixrust_INSTALL_PATH := $(libnixrust_PATH)
|
||||
libnixrust_LDFLAGS_USE := -L$(d)/target/release -lnixrust -ldl
|
||||
libnixrust_LDFLAGS_USE_INSTALLED := $(libnixrust_LDFLAGS_USE)
|
||||
|
||||
$(d)/target/release/libnixrust.a: $(wildcard $(d)/src/*.rs) $(d)/Cargo.toml
|
||||
$(trace-gen) cd nix-rust && RUSTC_BOOTSTRAP=1 cargo build --release && touch target/release/libnixrust.a
|
48
nix-rust/src/lib.rs
Normal file
48
nix-rust/src/lib.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
extern crate libc;
|
||||
extern crate tar;
|
||||
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
use std::path::Path;
|
||||
use tar::Archive;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn unpack_tarfile(data: &[u8], dest_dir: &str) -> bool {
|
||||
// FIXME: handle errors.
|
||||
|
||||
let dest_dir = Path::new(dest_dir);
|
||||
|
||||
let mut tar = Archive::new(data);
|
||||
|
||||
for file in tar.entries().unwrap() {
|
||||
let mut file = file.unwrap();
|
||||
|
||||
let dest_file = dest_dir.join(file.header().path().unwrap());
|
||||
|
||||
fs::create_dir_all(dest_file.parent().unwrap()).unwrap();
|
||||
|
||||
match file.header().entry_type() {
|
||||
tar::EntryType::Directory => {
|
||||
fs::create_dir(dest_file).unwrap();
|
||||
}
|
||||
tar::EntryType::Regular => {
|
||||
let mode = if file.header().mode().unwrap() & libc::S_IXUSR == 0 {
|
||||
0o666
|
||||
} else {
|
||||
0o777
|
||||
};
|
||||
let mut f = fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.mode(mode)
|
||||
.open(dest_file)
|
||||
.unwrap();
|
||||
io::copy(&mut file, &mut f).unwrap();
|
||||
}
|
||||
t => panic!("Unsupported tar entry type '{:?}'.", t),
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue