about summary refs log tree commit diff
path: root/tvix/store/src/nar/renderer.rs
blob: c10f2ddf52fab747b8f3c16cc4a3969027cdc1a8 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use super::RenderError;
use crate::{
    blobservice::BlobService,
    directoryservice::DirectoryService,
    proto::{self, NamedNode},
    B3Digest,
};
use nix_compat::nar;
use std::io::{self, BufReader};
use tracing::warn;

/// A NAR renderer, using a blob_service, chunk_service and directory_service
/// to render a NAR to a writer.
#[derive(Clone)]
pub struct NARRenderer<BS: BlobService, DS: DirectoryService> {
    blob_service: BS,
    directory_service: DS,
}

impl<BS: BlobService, DS: DirectoryService> NARRenderer<BS, DS> {
    pub fn new(blob_service: BS, directory_service: DS) -> Self {
        Self {
            blob_service,
            directory_service,
        }
    }

    /// Consumes a [proto::node::Node] pointing to the root of a (store) path,
    /// and writes the contents in NAR serialization to the passed
    /// [std::io::Write].
    ///
    /// It uses the different clients in the struct to perform the necessary
    /// lookups as it traverses the structure.
    pub fn write_nar<W: std::io::Write>(
        &self,
        w: &mut W,
        proto_root_node: &proto::node::Node,
    ) -> Result<(), RenderError> {
        // Initialize NAR writer
        let nar_root_node = nar::writer::open(w).map_err(RenderError::NARWriterError)?;

        self.walk_node(nar_root_node, proto_root_node)
    }

    /// Process an intermediate node in the structure.
    /// This consumes the node.
    fn walk_node(
        &self,
        nar_node: nar::writer::Node,
        proto_node: &proto::node::Node,
    ) -> Result<(), RenderError> {
        match proto_node {
            proto::node::Node::Symlink(proto_symlink_node) => {
                nar_node
                    .symlink(&proto_symlink_node.target)
                    .map_err(RenderError::NARWriterError)?;
            }
            proto::node::Node::File(proto_file_node) => {
                let digest = B3Digest::from_vec(proto_file_node.digest.clone()).map_err(|_e| {
                    warn!(
                        file_node = ?proto_file_node,
                        "invalid digest length in file node",
                    );

                    RenderError::StoreError(crate::Error::StorageError(
                        "invalid digest len in file node".to_string(),
                    ))
                })?;

                let mut blob_reader = match self
                    .blob_service
                    .open_read(&digest)
                    .map_err(RenderError::StoreError)?
                {
                    Some(blob_reader) => Ok(BufReader::new(blob_reader)),
                    None => Err(RenderError::NARWriterError(io::Error::new(
                        io::ErrorKind::NotFound,
                        format!("blob with digest {} not found", &digest),
                    ))),
                }?;

                nar_node
                    .file(
                        proto_file_node.executable,
                        proto_file_node.size.into(),
                        &mut blob_reader,
                    )
                    .map_err(RenderError::NARWriterError)?;
            }
            proto::node::Node::Directory(proto_directory_node) => {
                let digest =
                    B3Digest::from_vec(proto_directory_node.digest.to_vec()).map_err(|_e| {
                        RenderError::StoreError(crate::Error::StorageError(
                            "invalid digest len in directory node".to_string(),
                        ))
                    })?;

                // look it up with the directory service
                let resp = self
                    .directory_service
                    .get(&digest)
                    .map_err(RenderError::StoreError)?;

                match resp {
                    // if it's None, that's an error!
                    None => {
                        return Err(RenderError::DirectoryNotFound(
                            digest,
                            proto_directory_node.name.to_owned(),
                        ))
                    }
                    Some(proto_directory) => {
                        // start a directory node
                        let mut nar_node_directory =
                            nar_node.directory().map_err(RenderError::NARWriterError)?;

                        // for each node in the directory, create a new entry with its name,
                        // and then invoke walk_node on that entry.
                        for proto_node in proto_directory.nodes() {
                            let child_node = nar_node_directory
                                .entry(proto_node.get_name())
                                .map_err(RenderError::NARWriterError)?;
                            self.walk_node(child_node, &proto_node)?;
                        }

                        // close the directory
                        nar_node_directory
                            .close()
                            .map_err(RenderError::NARWriterError)?;
                    }
                }
            }
        }
        Ok(())
    }
}