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, root_nodes};
5use snix_castore::{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    uid_gid_override: Option<(u32, u32)>,
21    show_xattr: bool,
22) -> SnixStoreFs<BS, DS, RootNodesWrapper<PS>>
23where
24    BS: BlobService + Send + Clone + 'static,
25    DS: DirectoryService + Send + Clone + 'static,
26    PS: PathInfoService + Send + Sync + Clone + 'static,
27{
28    SnixStoreFs::new(
29        blob_service,
30        directory_service,
31        RootNodesWrapper(path_info_service),
32        list_root,
33        uid_gid_override,
34        show_xattr,
35    )
36}
37
38/// Wrapper to satisfy Rust's orphan rules for trait implementations, as
39/// RootNodes is coming from the [snix-castore] crate.
40#[doc(hidden)]
41#[derive(Clone, Debug)]
42pub struct RootNodesWrapper<T>(pub(crate) T);
43
44/// Implements root node lookup for any [PathInfoService]. This represents a flat
45/// directory structure like /nix/store where each entry in the root filesystem
46/// directory corresponds to a CA node.
47#[cfg(any(feature = "fuse", feature = "virtiofs"))]
48#[async_trait]
49impl<T> RootNodes for RootNodesWrapper<T>
50where
51    T: PathInfoService,
52{
53    async fn get_by_basename(
54        &self,
55        name: &PathComponent,
56    ) -> Result<Option<Node>, root_nodes::Error> {
57        let Ok(store_path) = StorePathRef::from_bytes(name.as_ref()) else {
58            return Ok(None);
59        };
60
61        Ok(self
62            .0
63            .get(*store_path.digest())
64            .await?
65            .map(|path_info| path_info.node))
66    }
67
68    fn list(&self) -> BoxStream<'static, Result<(PathComponent, Node), root_nodes::Error>> {
69        self.0
70            .list()
71            .map_ok(|path_info| {
72                let name = path_info
73                    .store_path
74                    .to_string()
75                    .as_str()
76                    .try_into()
77                    .expect("Snix bug: StorePath must be PathComponent");
78                (name, path_info.node)
79            })
80            .boxed()
81    }
82}