From c986c09b8c262726403baef72ae035583a0db483 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 17 Dec 2024 09:54:28 +0100 Subject: [PATCH] feat: Store.parse_store_path() (cherry picked from commit 6b92d4164b94d5030929dfb56577b1fd8d62e067) --- rust/nix-store/src/path.rs | 14 +++++++++++++ rust/nix-store/src/store.rs | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/rust/nix-store/src/path.rs b/rust/nix-store/src/path.rs index a0cb301..e6b9ceb 100644 --- a/rust/nix-store/src/path.rs +++ b/rust/nix-store/src/path.rs @@ -71,3 +71,17 @@ impl Drop for StorePath { } } } + +#[cfg(test)] +mod tests { + #[test] + #[cfg(nix_at_least = "2.26" /* get_storedir */)] + fn store_path_name() { + let mut store = crate::store::Store::open("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"); + } +} diff --git a/rust/nix-store/src/store.rs b/rust/nix-store/src/store.rs index da4cbc9..dc6c9cc 100644 --- a/rust/nix-store/src/store.rs +++ b/rust/nix-store/src/store.rs @@ -10,6 +10,8 @@ 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 { @@ -187,6 +189,21 @@ impl Store { r } + #[doc(alias = "nix_store_parse_path")] + pub fn parse_store_path(&mut self, path: &str) -> Result { + 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)) + } + } + pub fn weak_ref(&self) -> StoreWeak { StoreWeak { inner: Arc::downgrade(&self.inner), @@ -240,6 +257,30 @@ mod tests { 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("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"); + } + + #[test] + fn parse_store_path_fail() { + let mut store = crate::store::Store::open("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("auto", HashMap::new()).unwrap();