nix-super/nix-rust/src/c.rs

95 lines
2.4 KiB
Rust
Raw Normal View History

2019-09-18 01:31:51 +03:00
use super::{
error,
2019-12-13 19:29:16 +02:00
foreign::{self},
store::path,
store::StorePath,
2019-09-18 01:31:51 +03:00
util,
};
2019-09-16 00:09:30 +03:00
#[no_mangle]
2019-12-13 19:29:16 +02:00
pub unsafe extern "C" fn unpack_tarfile(
2019-09-16 00:09:30 +03:00
source: foreign::Source,
dest_dir: &str,
2019-12-13 19:29:16 +02:00
out: *mut Result<(), error::CppException>,
) {
out.write(
2019-12-13 19:11:37 +02:00
util::tarfile::unpack_tarfile(source, std::path::Path::new(dest_dir))
.map_err(|err| err.into()),
2019-12-13 19:29:16 +02:00
);
2019-09-16 00:09:30 +03:00
}
#[no_mangle]
pub unsafe extern "C" fn ffi_String_new(s: &str, out: *mut String) {
// FIXME: check whether 's' is valid UTF-8?
out.write(s.to_string())
}
2019-09-16 00:09:30 +03:00
#[no_mangle]
pub unsafe extern "C" fn ffi_String_drop(self_: *mut String) {
std::ptr::drop_in_place(self_);
}
2019-09-16 00:09:30 +03:00
#[no_mangle]
pub extern "C" fn ffi_StorePath_new(
path: &str,
store_dir: &str,
) -> Result<StorePath, error::CppException> {
StorePath::new(std::path::Path::new(path), store_dir).map_err(|err| err.into())
}
2019-09-16 00:09:30 +03:00
#[no_mangle]
pub extern "C" fn ffi_StorePath_new2(
hash: &[u8; crate::store::path::STORE_PATH_HASH_BYTES],
name: &str,
) -> Result<StorePath, error::CppException> {
StorePath::from_parts(*hash, name).map_err(|err| err.into())
}
2019-09-16 00:09:30 +03:00
#[no_mangle]
pub extern "C" fn ffi_StorePath_fromBaseName(
base_name: &str,
) -> Result<StorePath, error::CppException> {
StorePath::new_from_base_name(base_name).map_err(|err| err.into())
}
2019-09-16 00:09:30 +03:00
#[no_mangle]
pub unsafe extern "C" fn ffi_StorePath_drop(self_: *mut StorePath) {
std::ptr::drop_in_place(self_);
}
2019-09-16 00:09:30 +03:00
#[no_mangle]
pub extern "C" fn ffi_StorePath_to_string(self_: &StorePath) -> Vec<u8> {
let mut buf = vec![0; path::STORE_PATH_HASH_CHARS + 1 + self_.name.name().len()];
util::base32::encode_into(self_.hash.hash(), &mut buf[0..path::STORE_PATH_HASH_CHARS]);
buf[path::STORE_PATH_HASH_CHARS] = b'-';
buf[path::STORE_PATH_HASH_CHARS + 1..].clone_from_slice(self_.name.name().as_bytes());
buf
}
2019-09-16 00:09:30 +03:00
#[no_mangle]
pub extern "C" fn ffi_StorePath_less_than(a: &StorePath, b: &StorePath) -> bool {
a < b
}
2019-09-18 01:31:51 +03:00
#[no_mangle]
pub extern "C" fn ffi_StorePath_eq(a: &StorePath, b: &StorePath) -> bool {
a == b
}
2019-12-04 16:32:28 +02:00
#[no_mangle]
pub extern "C" fn ffi_StorePath_clone(self_: &StorePath) -> StorePath {
self_.clone()
}
2019-09-18 01:31:51 +03:00
#[no_mangle]
pub extern "C" fn ffi_StorePath_name(self_: &StorePath) -> &str {
self_.name.name()
}
#[no_mangle]
pub extern "C" fn ffi_StorePath_hash_data(
self_: &StorePath,
) -> &[u8; crate::store::path::STORE_PATH_HASH_BYTES] {
self_.hash.hash()
2019-09-16 00:09:30 +03:00
}