Rename crates nix- -> nix-bindings-
This way, the crates can be published without interfering with potential future non-bindings `nix-` crates, if Nix proper wants to have native rust code, for instance.
This commit is contained in:
parent
4b13929db3
commit
b3171585d1
30 changed files with 209 additions and 1853 deletions
18
rust/nix-bindings-store/Cargo.toml
Normal file
18
rust/nix-bindings-store/Cargo.toml
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
[package]
|
||||
name = "nix-bindings-store"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
build = "build.rs"
|
||||
license = "LGPL-2.1"
|
||||
|
||||
[lib]
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
nix-bindings-util = { path = "../nix-bindings-util" }
|
||||
nix-bindings-bindgen-raw = { path = "../nix-bindings-bindgen-raw" }
|
||||
lazy_static = "1.4"
|
||||
|
||||
[build-dependencies]
|
||||
pkg-config = "0.3"
|
||||
39
rust/nix-bindings-store/build.rs
Normal file
39
rust/nix-bindings-store/build.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
fn main() {
|
||||
// Get nix version
|
||||
let nix_version = pkg_config::probe_library("nix-store-c").unwrap().version;
|
||||
|
||||
// Generate version flags
|
||||
// Unfortunately, Rust doesn't give us a "greater than" operator in conditional
|
||||
// compilation, so we pre-evaluate the version comparisons here, making use
|
||||
// of the multi-valued nature of Rust cfgs.
|
||||
let relevant_versions = vec!["2.26"];
|
||||
let versions = relevant_versions
|
||||
.iter()
|
||||
.map(|v| format!("\"{}\"", v))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
|
||||
// Declare the known versions, so that Rust can warn about unknown versions
|
||||
// that aren't part of `relevant_versions` yet - feel free to add entries.
|
||||
println!(
|
||||
"cargo:rustc-check-cfg=cfg(nix_at_least,values({}))",
|
||||
versions
|
||||
);
|
||||
|
||||
let nix_version = nix_version.split('.').collect::<Vec<&str>>();
|
||||
let nix_version = (
|
||||
nix_version[0].parse::<u32>().unwrap(),
|
||||
nix_version[1].parse::<u32>().unwrap(),
|
||||
);
|
||||
|
||||
for version_str in relevant_versions {
|
||||
let version = version_str.split('.').collect::<Vec<&str>>();
|
||||
let version = (
|
||||
version[0].parse::<u32>().unwrap(),
|
||||
version[1].parse::<u32>().unwrap(),
|
||||
);
|
||||
if nix_version >= version {
|
||||
println!("cargo:rustc-cfg=nix_at_least=\"{}\"", version_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
rust/nix-bindings-store/src/lib.rs
Normal file
2
rust/nix-bindings-store/src/lib.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod path;
|
||||
pub mod store;
|
||||
87
rust/nix-bindings-store/src/path.rs
Normal file
87
rust/nix-bindings-store/src/path.rs
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
use std::ptr::NonNull;
|
||||
|
||||
use anyhow::Result;
|
||||
use nix_bindings_bindgen_raw as raw;
|
||||
use nix_bindings_util::{
|
||||
result_string_init,
|
||||
string_return::{callback_get_result_string, callback_get_result_string_data},
|
||||
};
|
||||
|
||||
pub struct StorePath {
|
||||
raw: NonNull<raw::StorePath>,
|
||||
}
|
||||
impl StorePath {
|
||||
/// Get the name of the store path.
|
||||
///
|
||||
/// For a store path like `/nix/store/abc1234...-foo-1.2`, this function will return `foo-1.2`.
|
||||
pub fn name(&self) -> Result<String> {
|
||||
unsafe {
|
||||
let mut r = result_string_init!();
|
||||
raw::store_path_name(
|
||||
self.as_ptr(),
|
||||
Some(callback_get_result_string),
|
||||
callback_get_result_string_data(&mut r),
|
||||
);
|
||||
r
|
||||
}
|
||||
}
|
||||
|
||||
/// This is a low level function that you shouldn't have to call unless you are developing the Nix bindings.
|
||||
///
|
||||
/// Construct a new `StorePath` by first cloning the C store path.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This does not take ownership of the C store path, so it should be a borrowed pointer, or you should free it.
|
||||
pub unsafe fn new_raw_clone(raw: NonNull<raw::StorePath>) -> Self {
|
||||
Self::new_raw(
|
||||
NonNull::new(raw::store_path_clone(raw.as_ptr()))
|
||||
.or_else(|| panic!("nix_store_path_clone returned a null pointer"))
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
/// This is a low level function that you shouldn't have to call unless you are developing the Nix bindings.
|
||||
///
|
||||
/// Takes ownership of a C `nix_store_path`. It will be freed when the `StorePath` is dropped.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The caller must ensure that the provided `NonNull<raw::StorePath>` is valid and that the ownership
|
||||
/// semantics are correctly followed. The `raw` pointer must not be used after being passed to this function.
|
||||
pub unsafe fn new_raw(raw: NonNull<raw::StorePath>) -> Self {
|
||||
StorePath { raw }
|
||||
}
|
||||
|
||||
/// This is a low level function that you shouldn't have to call unless you are developing the Nix bindings.
|
||||
///
|
||||
/// Get a pointer to the underlying Nix C API store path.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function is unsafe because it returns a raw pointer. The caller must ensure that the pointer is not used beyond the lifetime of this `StorePath`.
|
||||
pub unsafe fn as_ptr(&self) -> *mut raw::StorePath {
|
||||
self.raw.as_ptr()
|
||||
}
|
||||
}
|
||||
impl Drop for StorePath {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
raw::store_path_free(self.as_ptr());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
#[cfg(nix_at_least = "2.26" /* get_storedir */)]
|
||||
fn store_path_name() {
|
||||
let mut store = crate::store::Store::open(Some("dummy://"), []).unwrap();
|
||||
let store_dir = store.get_storedir().unwrap();
|
||||
let store_path_string =
|
||||
format!("{store_dir}/rdd4pnr4x9rqc9wgbibhngv217w2xvxl-bash-interactive-5.2p26");
|
||||
let store_path = store.parse_store_path(store_path_string.as_str()).unwrap();
|
||||
assert_eq!(store_path.name().unwrap(), "bash-interactive-5.2p26");
|
||||
}
|
||||
}
|
||||
341
rust/nix-bindings-store/src/store.rs
Normal file
341
rust/nix-bindings-store/src/store.rs
Normal file
|
|
@ -0,0 +1,341 @@
|
|||
use anyhow::{bail, Error, Result};
|
||||
use lazy_static::lazy_static;
|
||||
use nix_bindings_bindgen_raw as raw;
|
||||
use nix_bindings_util::context::Context;
|
||||
use nix_bindings_util::string_return::{callback_get_result_string, callback_get_result_string_data};
|
||||
use nix_bindings_util::{check_call, result_string_init};
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::{c_char, CString};
|
||||
use std::ptr::null_mut;
|
||||
use std::ptr::NonNull;
|
||||
use std::sync::{Arc, Mutex, Weak};
|
||||
|
||||
use crate::path::StorePath;
|
||||
|
||||
/* TODO make Nix itself thread safe */
|
||||
lazy_static! {
|
||||
static ref INIT: Result<()> = unsafe {
|
||||
check_call!(raw::libstore_init(&mut Context::new()))?;
|
||||
Ok(())
|
||||
};
|
||||
}
|
||||
|
||||
struct StoreRef {
|
||||
inner: NonNull<raw::Store>,
|
||||
}
|
||||
impl StoreRef {
|
||||
/// # Safety
|
||||
///
|
||||
/// The returned pointer is only valid as long as the `StoreRef` is alive.
|
||||
pub unsafe fn ptr(&self) -> *mut raw::Store {
|
||||
self.inner.as_ptr()
|
||||
}
|
||||
}
|
||||
impl Drop for StoreRef {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
raw::store_free(self.inner.as_ptr());
|
||||
}
|
||||
}
|
||||
}
|
||||
unsafe impl Send for StoreRef {}
|
||||
/// Unlike pointers in general, operations on raw::Store are thread safe and it is therefore safe to share them between threads.
|
||||
unsafe impl Sync for StoreRef {}
|
||||
|
||||
/// A [Weak] reference to a store.
|
||||
pub struct StoreWeak {
|
||||
inner: Weak<StoreRef>,
|
||||
}
|
||||
impl StoreWeak {
|
||||
/// Upgrade the weak reference to a proper [Store].
|
||||
///
|
||||
/// If no normal reference to the [Store] is around anymore elsewhere, this fails by returning `None`.
|
||||
pub fn upgrade(&self) -> Option<Store> {
|
||||
self.inner.upgrade().map(|inner| Store {
|
||||
inner,
|
||||
context: Context::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Protects against https://github.com/NixOS/nix/issues/11979 (unless different parameters are passed, in which case it's up to luck, but you do get your own parameters as you asked for).
|
||||
type StoreCacheMap = HashMap<(Option<String>, Vec<(String, String)>), StoreWeak>;
|
||||
|
||||
lazy_static! {
|
||||
static ref STORE_CACHE: Arc<Mutex<StoreCacheMap>> = Arc::new(Mutex::new(HashMap::new()));
|
||||
}
|
||||
|
||||
pub struct Store {
|
||||
inner: Arc<StoreRef>,
|
||||
/* An error context to reuse. This way we don't have to allocate them for each store operation. */
|
||||
context: Context,
|
||||
}
|
||||
impl Store {
|
||||
/// Open a store.
|
||||
///
|
||||
/// See [nix_c_raw::store_open] for more information.
|
||||
#[doc(alias = "nix_store_open")]
|
||||
pub fn open<'a, 'b>(
|
||||
url: Option<&str>,
|
||||
params: impl IntoIterator<Item = (&'a str, &'b str)>,
|
||||
) -> Result<Self> {
|
||||
let params = params
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.to_owned(), v.to_owned()))
|
||||
.collect::<Vec<(String, String)>>();
|
||||
let params2 = params.clone();
|
||||
let mut store_cache = STORE_CACHE
|
||||
.lock()
|
||||
.map_err(|_| Error::msg("Failed to lock store cache. This should never happen."))?;
|
||||
match store_cache.entry((url.map(Into::into), params)) {
|
||||
std::collections::hash_map::Entry::Occupied(mut e) => {
|
||||
if let Some(store) = e.get().upgrade() {
|
||||
Ok(store)
|
||||
} else {
|
||||
let store = Self::open_uncached(
|
||||
url,
|
||||
params2.iter().map(|(k, v)| (k.as_str(), v.as_str())),
|
||||
)?;
|
||||
e.insert(store.weak_ref());
|
||||
Ok(store)
|
||||
}
|
||||
}
|
||||
std::collections::hash_map::Entry::Vacant(e) => {
|
||||
let store = Self::open_uncached(
|
||||
url,
|
||||
params2.iter().map(|(k, v)| (k.as_str(), v.as_str())),
|
||||
)?;
|
||||
e.insert(store.weak_ref());
|
||||
Ok(store)
|
||||
}
|
||||
}
|
||||
}
|
||||
fn open_uncached<'a, 'b>(
|
||||
url: Option<&str>,
|
||||
params: impl IntoIterator<Item = (&'a str, &'b str)>,
|
||||
) -> Result<Self> {
|
||||
let x = INIT.as_ref();
|
||||
match x {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
// Couldn't just clone the error, so we have to print it here.
|
||||
bail!("nix_libstore_init error: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
let mut context: Context = Context::new();
|
||||
|
||||
let uri_cstring = match url {
|
||||
Some(url) => Some(CString::new(url)?),
|
||||
None => None,
|
||||
};
|
||||
let uri_ptr = uri_cstring
|
||||
.as_ref()
|
||||
.map(|s| s.as_ptr())
|
||||
.unwrap_or(null_mut());
|
||||
|
||||
// this intermediate value must be here and must not be moved
|
||||
// because it owns the data the `*const c_char` pointers point to.
|
||||
let params: Vec<(CString, CString)> = params
|
||||
.into_iter()
|
||||
.map(|(k, v)| Ok((CString::new(k)?, CString::new(v)?))) // to do. context
|
||||
.collect::<Result<_>>()?;
|
||||
// this intermediate value owns the data the `*mut *const c_char` pointer points to.
|
||||
let mut params: Vec<_> = params
|
||||
.iter()
|
||||
.map(|(k, v)| [k.as_ptr(), v.as_ptr()])
|
||||
.collect();
|
||||
// this intermediate value owns the data the `*mut *mut *const c_char` pointer points to.
|
||||
let mut params: Vec<*mut *const c_char> = params
|
||||
.iter_mut()
|
||||
.map(|t| t.as_mut_ptr())
|
||||
.chain(std::iter::once(null_mut())) // signal the end of the array
|
||||
.collect();
|
||||
|
||||
let store =
|
||||
unsafe { check_call!(raw::store_open(&mut context, uri_ptr, params.as_mut_ptr())) }?;
|
||||
if store.is_null() {
|
||||
panic!("nix_c_store_open returned a null pointer without an error");
|
||||
}
|
||||
let store = Store {
|
||||
inner: Arc::new(StoreRef {
|
||||
inner: NonNull::new(store).unwrap(),
|
||||
}),
|
||||
context,
|
||||
};
|
||||
Ok(store)
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
///
|
||||
/// The returned pointer is only valid as long as the `Store` is alive.
|
||||
pub unsafe fn raw_ptr(&self) -> *mut raw::Store {
|
||||
self.inner.ptr()
|
||||
}
|
||||
|
||||
#[doc(alias = "nix_store_get_uri")]
|
||||
pub fn get_uri(&mut self) -> Result<String> {
|
||||
let mut r = result_string_init!();
|
||||
unsafe {
|
||||
check_call!(raw::store_get_uri(
|
||||
&mut self.context,
|
||||
self.inner.ptr(),
|
||||
Some(callback_get_result_string),
|
||||
callback_get_result_string_data(&mut r)
|
||||
))
|
||||
}?;
|
||||
r
|
||||
}
|
||||
|
||||
#[cfg(nix_at_least = "2.26")]
|
||||
#[doc(alias = "nix_store_get_storedir")]
|
||||
pub fn get_storedir(&mut self) -> Result<String> {
|
||||
let mut r = result_string_init!();
|
||||
unsafe {
|
||||
check_call!(raw::store_get_storedir(
|
||||
&mut self.context,
|
||||
self.inner.ptr(),
|
||||
Some(callback_get_result_string),
|
||||
callback_get_result_string_data(&mut r)
|
||||
))
|
||||
}?;
|
||||
r
|
||||
}
|
||||
|
||||
#[doc(alias = "nix_store_parse_path")]
|
||||
pub fn parse_store_path(&mut self, path: &str) -> Result<StorePath> {
|
||||
let path = CString::new(path)?;
|
||||
unsafe {
|
||||
let store_path = check_call!(raw::store_parse_path(
|
||||
&mut self.context,
|
||||
self.inner.ptr(),
|
||||
path.as_ptr()
|
||||
))?;
|
||||
let store_path =
|
||||
NonNull::new(store_path).expect("nix_store_parse_path returned a null pointer");
|
||||
Ok(StorePath::new_raw(store_path))
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(alias = "nix_store_real_path")]
|
||||
pub fn real_path(&mut self, path: &StorePath) -> Result<String> {
|
||||
let mut r = result_string_init!();
|
||||
unsafe {
|
||||
check_call!(raw::store_real_path(
|
||||
&mut self.context,
|
||||
self.inner.ptr(),
|
||||
path.as_ptr(),
|
||||
Some(callback_get_result_string),
|
||||
callback_get_result_string_data(&mut r)
|
||||
))
|
||||
}?;
|
||||
r
|
||||
}
|
||||
|
||||
pub fn weak_ref(&self) -> StoreWeak {
|
||||
StoreWeak {
|
||||
inner: Arc::downgrade(&self.inner),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for Store {
|
||||
fn clone(&self) -> Self {
|
||||
Store {
|
||||
inner: self.inner.clone(),
|
||||
context: Context::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn none_works() {
|
||||
let res = Store::open(None, HashMap::new());
|
||||
res.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_works() {
|
||||
// This is not actually a given.
|
||||
// Maybe whatever is in NIX_REMOTE or nix.conf is really important.
|
||||
let res = Store::open(Some("auto"), HashMap::new());
|
||||
res.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_uri_fails() {
|
||||
let res = Store::open(Some("invalid://uri"), HashMap::new());
|
||||
assert!(res.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_uri() {
|
||||
let mut store = Store::open(None, HashMap::new()).unwrap();
|
||||
let uri = store.get_uri().unwrap();
|
||||
assert!(!uri.is_empty());
|
||||
// must be ascii
|
||||
assert!(uri.is_ascii());
|
||||
// usually something like "daemon", but that's not something we can check here.
|
||||
println!("uri: {}", uri);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore] // Needs network access
|
||||
fn get_uri_nixos_cache() {
|
||||
let mut store = Store::open(Some("https://cache.nixos.org/"), HashMap::new()).unwrap();
|
||||
let uri = store.get_uri().unwrap();
|
||||
assert_eq!(uri, "https://cache.nixos.org");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(nix_at_least = "2.26" /* get_storedir */)]
|
||||
fn parse_store_path_ok() {
|
||||
let mut store = crate::store::Store::open(Some("dummy://"), []).unwrap();
|
||||
let store_dir = store.get_storedir().unwrap();
|
||||
let store_path_string =
|
||||
format!("{store_dir}/rdd4pnr4x9rqc9wgbibhngv217w2xvxl-bash-interactive-5.2p26");
|
||||
let store_path = store.parse_store_path(store_path_string.as_str()).unwrap();
|
||||
let real_store_path = store.real_path(&store_path).unwrap();
|
||||
assert_eq!(store_path.name().unwrap(), "bash-interactive-5.2p26");
|
||||
assert_eq!(real_store_path, store_path_string);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_store_path_fail() {
|
||||
let mut store = crate::store::Store::open(Some("dummy://"), []).unwrap();
|
||||
let store_path_string = format!("bash-interactive-5.2p26");
|
||||
let r = store.parse_store_path(store_path_string.as_str());
|
||||
match r {
|
||||
Err(e) => {
|
||||
assert!(e.to_string().contains("bash-interactive-5.2p26"));
|
||||
}
|
||||
_ => panic!("Expected error"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn weak_ref() {
|
||||
let mut store = Store::open(None, HashMap::new()).unwrap();
|
||||
let uri = store.get_uri().unwrap();
|
||||
let weak = store.weak_ref();
|
||||
let mut store2 = weak.upgrade().unwrap();
|
||||
assert_eq!(store2.get_uri().unwrap(), uri);
|
||||
}
|
||||
#[test]
|
||||
fn weak_ref_gone() {
|
||||
let weak = {
|
||||
// Concurrent tests calling Store::open will keep the weak reference to auto alive,
|
||||
// so for this test we need to bypass the global cache.
|
||||
let store = Store::open_uncached(None, HashMap::new()).unwrap();
|
||||
store.weak_ref()
|
||||
};
|
||||
assert!(weak.upgrade().is_none());
|
||||
assert!(weak.inner.upgrade().is_none());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue