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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#![allow(clippy::derive_partial_eq_without_eq)]
// https://github.com/hyperium/tonic/issues/1056
use std::collections::HashSet;
use thiserror::Error;
use prost::Message;
use nix_compat::store_path::{ParseStorePathError, StorePath};
tonic::include_proto!("tvix.store.v1");
#[cfg(feature = "reflection")]
/// Compiled file descriptors for implementing [gRPC
/// reflection](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md) with e.g.
/// [`tonic_reflection`](https://docs.rs/tonic-reflection).
pub const FILE_DESCRIPTOR_SET: &[u8] = tonic::include_file_descriptor_set!("tvix.store.v1");
/// Errors that can occur during the validation of Directory messages.
#[derive(Debug, PartialEq, Eq, Error)]
pub enum ValidateDirectoryError {
/// Elements are not in sorted order
#[error("{0} is not sorted")]
WrongSorting(String),
/// Multiple elements with the same name encountered
#[error("{0} is a duplicate name")]
DuplicateName(String),
/// Invalid name encountered
#[error("Invalid name in {0}")]
InvalidName(String),
/// Invalid digest length encountered
#[error("Invalid Digest length: {0}")]
InvalidDigestLen(usize),
}
/// Errors that can occur during the validation of PathInfo messages.
#[derive(Debug, Error, PartialEq)]
pub enum ValidatePathInfoError {
/// No node present
#[error("No node present")]
NoNodePresent(),
/// Invalid node name encountered.
#[error("Failed to parse {0} as NixPath: {1}")]
InvalidNodeName(String, ParseStorePathError),
/// The digest the (root) node refers to has invalid length.
#[error("Invalid Digest length: {0}")]
InvalidDigestLen(usize),
/// The number of references in the narinfo.reference_names field does not match
/// the number of references in the .references field.
#[error("Inconsistent Number of References: {0} (references) vs {0} (narinfo)")]
InconsistentNumberOfReferences(usize, usize),
}
/// Checks a Node name for validity as an intermediate node, and returns an
/// error that's generated from the supplied constructor.
///
/// We disallow slashes, null bytes, '.', '..' and the empty string.
fn validate_node_name<E>(name: &str, err: fn(String) -> E) -> Result<(), E> {
if name.is_empty() || name == ".." || name == "." || name.contains('\x00') || name.contains('/')
{
return Err(err(name.to_string()));
}
Ok(())
}
/// Checks a digest for validity.
/// Digests are 32 bytes long, as we store blake3 digests.
fn validate_digest<E>(digest: &Vec<u8>, err: fn(usize) -> E) -> Result<(), E> {
if digest.len() != 32 {
return Err(err(digest.len()));
}
Ok(())
}
/// Parses a root node name.
///
/// On success, this returns the parsed [StorePath].
/// On error, it returns an error generated from the supplied constructor.
fn parse_node_name_root<E>(
name: &str,
err: fn(String, ParseStorePathError) -> E,
) -> Result<StorePath, E> {
match StorePath::from_string(name) {
Ok(np) => Ok(np),
Err(e) => Err(err(name.to_string(), e)),
}
}
impl PathInfo {
/// validate performs some checks on the PathInfo struct,
/// Returning either a [StorePath] of the root node, or a
/// [ValidatePathInfoError].
pub fn validate(&self) -> Result<StorePath, ValidatePathInfoError> {
// If there is a narinfo field populated, ensure the number of references there
// matches PathInfo.references count.
if let Some(narinfo) = &self.narinfo {
if narinfo.reference_names.len() != self.references.len() {
return Err(ValidatePathInfoError::InconsistentNumberOfReferences(
narinfo.reference_names.len(),
self.references.len(),
));
}
}
// FUTUREWORK: parse references in reference_names. ensure they start
// with storeDir, and use the same digest as in self.references.
// Ensure there is a (root) node present, and it properly parses to a [StorePath].
let root_nix_path = match &self.node {
None => {
return Err(ValidatePathInfoError::NoNodePresent());
}
Some(Node { node }) => match node {
None => {
return Err(ValidatePathInfoError::NoNodePresent());
}
Some(node::Node::Directory(directory_node)) => {
// ensure the digest has the appropriate size.
validate_digest(
&directory_node.digest,
ValidatePathInfoError::InvalidDigestLen,
)?;
// parse the name
parse_node_name_root(
&directory_node.name,
ValidatePathInfoError::InvalidNodeName,
)?
}
Some(node::Node::File(file_node)) => {
// ensure the digest has the appropriate size.
validate_digest(&file_node.digest, ValidatePathInfoError::InvalidDigestLen)?;
// parse the name
parse_node_name_root(&file_node.name, ValidatePathInfoError::InvalidNodeName)?
}
Some(node::Node::Symlink(symlink_node)) => {
// parse the name
parse_node_name_root(
&symlink_node.name,
ValidatePathInfoError::InvalidNodeName,
)?
}
},
};
// return the root nix path
Ok(root_nix_path)
}
}
/// NamedNode is implemented for [FileNode], [DirectoryNode] and [SymlinkNode]
/// and [node::Node], so we can ask all of them for the name easily.
pub trait NamedNode {
fn get_name(&self) -> &str;
}
impl NamedNode for &FileNode {
fn get_name(&self) -> &str {
self.name.as_str()
}
}
impl NamedNode for &DirectoryNode {
fn get_name(&self) -> &str {
self.name.as_str()
}
}
impl NamedNode for &SymlinkNode {
fn get_name(&self) -> &str {
self.name.as_str()
}
}
impl NamedNode for node::Node {
fn get_name(&self) -> &str {
match self {
node::Node::File(node_file) => &node_file.name,
node::Node::Directory(node_directory) => &node_directory.name,
node::Node::Symlink(node_symlink) => &node_symlink.name,
}
}
}
/// Accepts a name, and a mutable reference to the previous name.
/// If the passed name is larger than the previous one, the reference is updated.
/// If it's not, an error is returned.
fn update_if_lt_prev<'set, 'n>(
prev_name: &'set mut &'n str,
name: &'n str,
) -> Result<(), ValidateDirectoryError> {
if *name < **prev_name {
return Err(ValidateDirectoryError::WrongSorting(name.to_string()));
}
*prev_name = name;
Ok(())
}
/// Inserts the given name into a HashSet if it's not already in there.
/// If it is, an error is returned.
fn insert_once<'n>(
seen_names: &mut HashSet<&'n str>,
name: &'n str,
) -> Result<(), ValidateDirectoryError> {
if seen_names.get(name).is_some() {
return Err(ValidateDirectoryError::DuplicateName(name.to_string()));
}
seen_names.insert(name);
Ok(())
}
impl Directory {
/// The size of a directory is the number of all regular and symlink elements,
/// the number of directory elements, and their size fields.
pub fn size(&self) -> u32 {
self.files.len() as u32
+ self.symlinks.len() as u32
+ self
.directories
.iter()
.fold(0, |acc: u32, e| (acc + 1 + e.size) as u32)
}
/// Calculates the digest of a Directory, which is the blake3 hash of a
/// Directory protobuf message, serialized in protobuf canonical form.
pub fn digest(&self) -> Vec<u8> {
let mut hasher = blake3::Hasher::new();
hasher.update(&self.encode_to_vec()).finalize().as_bytes()[..].to_vec()
}
/// validate checks the directory for invalid data, such as:
/// - violations of name restrictions
/// - invalid digest lengths
/// - not properly sorted lists
/// - duplicate names in the three lists
pub fn validate(&self) -> Result<(), ValidateDirectoryError> {
let mut seen_names: HashSet<&str> = HashSet::new();
let mut last_directory_name: &str = "";
let mut last_file_name: &str = "";
let mut last_symlink_name: &str = "";
// check directories
for directory_node in &self.directories {
validate_node_name(&directory_node.name, ValidateDirectoryError::InvalidName)?;
validate_digest(
&directory_node.digest,
ValidateDirectoryError::InvalidDigestLen,
)?;
update_if_lt_prev(&mut last_directory_name, directory_node.name.as_str())?;
insert_once(&mut seen_names, directory_node.name.as_str())?;
}
// check files
for file_node in &self.files {
validate_node_name(&file_node.name, ValidateDirectoryError::InvalidName)?;
validate_digest(&file_node.digest, ValidateDirectoryError::InvalidDigestLen)?;
update_if_lt_prev(&mut last_file_name, file_node.name.as_str())?;
insert_once(&mut seen_names, file_node.name.as_str())?;
}
// check symlinks
for symlink_node in &self.symlinks {
validate_node_name(&symlink_node.name, ValidateDirectoryError::InvalidName)?;
update_if_lt_prev(&mut last_symlink_name, symlink_node.name.as_str())?;
insert_once(&mut seen_names, symlink_node.name.as_str())?;
}
Ok(())
}
}
|