2022-12-29 12:23:54 +01:00
|
|
|
#![allow(clippy::derive_partial_eq_without_eq)]
|
|
|
|
// https://github.com/hyperium/tonic/issues/1056
|
2022-12-27 18:10:46 +01:00
|
|
|
use std::collections::HashSet;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
2022-11-12 00:40:09 +01:00
|
|
|
use prost::Message;
|
|
|
|
|
2023-01-06 16:27:27 +01:00
|
|
|
use crate::store_path::{ParseStorePathError, StorePath};
|
2022-12-29 21:37:52 +01:00
|
|
|
|
2022-11-13 00:23:14 +01:00
|
|
|
tonic::include_proto!("tvix.store.v1");
|
2022-11-12 00:40:09 +01:00
|
|
|
|
2022-11-26 02:14:02 +01:00
|
|
|
#[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");
|
|
|
|
|
2022-12-27 18:10:46 +01:00
|
|
|
/// Errors that can occur during the validation of Directory messages.
|
2022-12-29 12:23:54 +01:00
|
|
|
#[derive(Debug, PartialEq, Eq, Error)]
|
2022-12-27 18:10:46 +01:00
|
|
|
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
|
2022-12-29 21:13:46 +01:00
|
|
|
#[error("Invalid Digest length: {0}")]
|
2022-12-27 18:10:46 +01:00
|
|
|
InvalidDigestLen(usize),
|
|
|
|
}
|
|
|
|
|
2022-12-29 21:37:52 +01:00
|
|
|
/// 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.
|
2023-01-09 15:29:57 +01:00
|
|
|
#[error("Failed to parse {0} as NixPath: {1}")]
|
2023-01-06 16:27:27 +01:00
|
|
|
InvalidNodeName(String, ParseStorePathError),
|
2022-12-29 21:37:52 +01:00
|
|
|
|
|
|
|
/// 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),
|
|
|
|
}
|
|
|
|
|
2022-12-29 21:32:34 +01:00
|
|
|
/// Checks a Node name for validity as an intermediate node, and returns an
|
|
|
|
/// error that's generated from the supplied constructor.
|
|
|
|
///
|
2022-12-27 18:10:46 +01:00
|
|
|
/// We disallow slashes, null bytes, '.', '..' and the empty string.
|
2022-12-29 21:32:34 +01:00
|
|
|
fn validate_node_name<E>(name: &str, err: fn(String) -> E) -> Result<(), E> {
|
2022-12-29 12:23:54 +01:00
|
|
|
if name.is_empty() || name == ".." || name == "." || name.contains('\x00') || name.contains('/')
|
|
|
|
{
|
2022-12-29 21:32:34 +01:00
|
|
|
return Err(err(name.to_string()));
|
2022-12-27 18:10:46 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks a digest for validity.
|
|
|
|
/// Digests are 32 bytes long, as we store blake3 digests.
|
2022-12-30 15:14:41 +01:00
|
|
|
fn validate_digest<E>(digest: &Vec<u8>, err: fn(usize) -> E) -> Result<(), E> {
|
2022-12-27 18:10:46 +01:00
|
|
|
if digest.len() != 32 {
|
2022-12-30 15:14:41 +01:00
|
|
|
return Err(err(digest.len()));
|
2022-12-27 18:10:46 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-12-29 21:37:52 +01:00
|
|
|
/// Parses a root node name.
|
|
|
|
///
|
2023-01-06 16:27:27 +01:00
|
|
|
/// On success, this returns the parsed [StorePath].
|
2022-12-29 21:37:52 +01:00
|
|
|
/// On error, it returns an error generated from the supplied constructor.
|
|
|
|
fn parse_node_name_root<E>(
|
|
|
|
name: &str,
|
2023-01-06 16:27:27 +01:00
|
|
|
err: fn(String, ParseStorePathError) -> E,
|
|
|
|
) -> Result<StorePath, E> {
|
|
|
|
match StorePath::from_string(name) {
|
2022-12-29 21:37:52 +01:00
|
|
|
Ok(np) => Ok(np),
|
|
|
|
Err(e) => Err(err(name.to_string(), e)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PathInfo {
|
|
|
|
/// validate performs some checks on the PathInfo struct,
|
2023-01-06 16:27:27 +01:00
|
|
|
/// Returning either a [StorePath] of the root node, or a
|
2022-12-29 21:37:52 +01:00
|
|
|
/// [ValidatePathInfoError].
|
2023-01-06 16:27:27 +01:00
|
|
|
pub fn validate(&self) -> Result<StorePath, ValidatePathInfoError> {
|
2022-12-29 21:37:52 +01:00
|
|
|
// 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.
|
|
|
|
|
2023-01-06 16:27:27 +01:00
|
|
|
// Ensure there is a (root) node present, and it properly parses to a [StorePath].
|
2022-12-29 21:37:52 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 18:10:46 +01:00
|
|
|
/// 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 {
|
2022-12-29 12:23:54 +01:00
|
|
|
return Err(ValidateDirectoryError::WrongSorting(name.to_string()));
|
2022-12-27 18:10:46 +01:00
|
|
|
}
|
|
|
|
*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() {
|
2022-12-29 12:23:54 +01:00
|
|
|
return Err(ValidateDirectoryError::DuplicateName(name.to_string()));
|
2022-12-27 18:10:46 +01:00
|
|
|
}
|
|
|
|
seen_names.insert(name);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-11-12 00:40:09 +01:00
|
|
|
impl Directory {
|
2023-01-29 17:17:01 +01:00
|
|
|
/// The size of a directory is the number of all regular and symlink elements,
|
|
|
|
/// the number of directory elements, and their size fields.
|
2022-11-12 00:40:09 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn digest(&self) -> Vec<u8> {
|
|
|
|
let mut hasher = blake3::Hasher::new();
|
|
|
|
|
|
|
|
hasher.update(&self.encode_to_vec()).finalize().as_bytes()[..].to_vec()
|
|
|
|
}
|
2022-12-27 18:10:46 +01:00
|
|
|
|
|
|
|
/// 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 {
|
2022-12-29 21:32:34 +01:00
|
|
|
validate_node_name(&directory_node.name, ValidateDirectoryError::InvalidName)?;
|
2022-12-30 15:14:41 +01:00
|
|
|
validate_digest(
|
|
|
|
&directory_node.digest,
|
|
|
|
ValidateDirectoryError::InvalidDigestLen,
|
|
|
|
)?;
|
2022-12-27 18:10:46 +01:00
|
|
|
|
2022-12-29 12:23:54 +01:00
|
|
|
update_if_lt_prev(&mut last_directory_name, directory_node.name.as_str())?;
|
|
|
|
insert_once(&mut seen_names, directory_node.name.as_str())?;
|
2022-12-27 18:10:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// check files
|
|
|
|
for file_node in &self.files {
|
2022-12-29 21:32:34 +01:00
|
|
|
validate_node_name(&file_node.name, ValidateDirectoryError::InvalidName)?;
|
2022-12-30 15:14:41 +01:00
|
|
|
validate_digest(&file_node.digest, ValidateDirectoryError::InvalidDigestLen)?;
|
2022-12-27 18:10:46 +01:00
|
|
|
|
2022-12-29 12:23:54 +01:00
|
|
|
update_if_lt_prev(&mut last_file_name, file_node.name.as_str())?;
|
|
|
|
insert_once(&mut seen_names, file_node.name.as_str())?;
|
2022-12-27 18:10:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// check symlinks
|
|
|
|
for symlink_node in &self.symlinks {
|
2022-12-29 21:32:34 +01:00
|
|
|
validate_node_name(&symlink_node.name, ValidateDirectoryError::InvalidName)?;
|
2022-12-27 18:10:46 +01:00
|
|
|
|
2022-12-29 12:23:54 +01:00
|
|
|
update_if_lt_prev(&mut last_symlink_name, symlink_node.name.as_str())?;
|
|
|
|
insert_once(&mut seen_names, symlink_node.name.as_str())?;
|
2022-12-27 18:10:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-11-12 00:40:09 +01:00
|
|
|
}
|