snix_store/pathinfoservice/fs/
mod.rs

1use futures::stream::BoxStream;
2use futures::{StreamExt, TryStreamExt};
3use nix_compat::store_path::StorePathRef;
4use snix_castore::fs::{RootNodes, SnixStoreFs};
5use snix_castore::{Error, Node, PathComponent};
6use snix_castore::{blobservice::BlobService, directoryservice::DirectoryService};
7use tonic::async_trait;
8
9use super::PathInfoService;
10
11/// Helper to construct a [SnixStoreFs] from a [BlobService], [DirectoryService]
12/// and [PathInfoService].
13/// This avoids users to have to interact with the wrapper struct directly, as
14/// it leaks into the type signature of SnixStoreFS.
15pub fn make_fs<BS, DS, PS>(
16    blob_service: BS,
17    directory_service: DS,
18    path_info_service: PS,
19    list_root: bool,
20    show_xattr: bool,
21) -> SnixStoreFs<BS, DS, RootNodesWrapper<PS>>
22where
23    BS: BlobService + Send + Clone + 'static,
24    DS: DirectoryService + Send + Clone + 'static,
25    PS: PathInfoService + Send + Sync + Clone + 'static,
26{
27    SnixStoreFs::new(
28        blob_service,
29        directory_service,
30        RootNodesWrapper(path_info_service),
31        list_root,
32        show_xattr,
33    )
34}
35
36/// Wrapper to satisfy Rust's orphan rules for trait implementations, as
37/// RootNodes is coming from the [snix-castore] crate.
38#[doc(hidden)]
39#[derive(Clone, Debug)]
40pub struct RootNodesWrapper<T>(pub(crate) T);
41
42/// Implements root node lookup for any [PathInfoService]. This represents a flat
43/// directory structure like /nix/store where each entry in the root filesystem
44/// directory corresponds to a CA node.
45#[cfg(any(feature = "fuse", feature = "virtiofs"))]
46#[async_trait]
47impl<T> RootNodes for RootNodesWrapper<T>
48where
49    T: PathInfoService,
50{
51    async fn get_by_basename(&self, name: &PathComponent) -> Result<Option<Node>, Error> {
52        let Ok(store_path) = StorePathRef::from_bytes(name.as_ref()) else {
53            return Ok(None);
54        };
55
56        Ok(self
57            .0
58            .get(*store_path.digest())
59            .await?
60            .map(|path_info| path_info.node))
61    }
62
63    fn list(&self) -> BoxStream<'static, Result<(PathComponent, Node), Error>> {
64        self.0
65            .list()
66            .map_ok(|path_info| {
67                let name = path_info
68                    .store_path
69                    .to_string()
70                    .as_str()
71                    .try_into()
72                    .expect("Snix bug: StorePath must be PathComponent");
73                (name, path_info.node)
74            })
75            .boxed()
76    }
77}