about summary refs log tree commit diff
path: root/tvix/store/src/fs/root_nodes.rs
blob: e672c6e647f320ce7ebb7dd75242d0ece1d9522e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::{ops::Deref, pin::Pin};

use futures::{Stream, StreamExt};
use nix_compat::store_path::StorePath;
use tonic::async_trait;
use tvix_castore::{proto::node::Node, Error};

use crate::pathinfoservice::PathInfoService;

/// Provides an interface for looking up root nodes  in tvix-castore by given
/// a lookup key (usually the basename), and optionally allow a listing.
///
#[async_trait]
pub trait RootNodes: Send + Sync {
    /// Looks up a root CA node based on the basename of the node in the root
    /// directory of the filesystem.
    async fn get_by_basename(&self, name: &[u8]) -> Result<Option<Node>, Error>;

    /// Lists all root CA nodes in the filesystem. An error can be returned
    /// in case listing is not allowed
    fn list(&self) -> Pin<Box<dyn Stream<Item = Result<Node, Error>> + Send>>;
}

/// Implements root node lookup for any [PathInfoService]. This represents a flat
/// directory structure like /nix/store where each entry in the root filesystem
/// directory corresponds to a CA node.
#[async_trait]
impl<T> RootNodes for T
where
    T: Deref<Target = dyn PathInfoService> + Send + Sync,
{
    async fn get_by_basename(&self, name: &[u8]) -> Result<Option<Node>, Error> {
        let Ok(store_path) = StorePath::from_bytes(name) else {
            return Ok(None);
        };

        Ok(self
            .deref()
            .get(*store_path.digest())
            .await?
            .map(|path_info| {
                path_info
                    .node
                    .expect("missing root node")
                    .node
                    .expect("empty node")
            }))
    }

    fn list(&self) -> Pin<Box<dyn Stream<Item = Result<Node, Error>> + Send>> {
        Box::pin(self.deref().list().map(|result| {
            result.map(|path_info| {
                path_info
                    .node
                    .expect("missing root node")
                    .node
                    .expect("empty node")
            })
        }))
    }
}