refactor(tvix/castore): simplify node validation checks

We can just check the digest length to be correct, and move the symlink
target checks to a single line.

Change-Id: I41d2e3a50e7990ef6c04f02acd754b1e17b43e77
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9717
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Reviewed-by: Connor Brewster <cbrewster@hey.com>
This commit is contained in:
Florian Klink 2023-10-13 01:09:34 +02:00 committed by clbot
parent f3353b5dcf
commit 532f414da6

View file

@ -11,7 +11,7 @@ mod grpc_directoryservice_wrapper;
pub use grpc_blobservice_wrapper::GRPCBlobServiceWrapper;
pub use grpc_directoryservice_wrapper::GRPCDirectoryServiceWrapper;
use crate::B3Digest;
use crate::{B3Digest, B3_LEN};
tonic::include_proto!("tvix.castore.v1");
@ -111,30 +111,29 @@ impl node::Node {
node::Node::Symlink(n) => node::Node::Symlink(SymlinkNode { name, ..n }),
}
}
/// Ensures the node has a valid name, and checks the type-specific fields too.
pub fn validate(&self) -> Result<(), ValidateNodeError> {
match self {
// for a directory root node, ensure the digest has the appropriate size.
node::Node::Directory(directory_node) => {
if TryInto::<B3Digest>::try_into(directory_node.digest.clone()).is_err() {
if directory_node.digest.len() != B3_LEN {
Err(ValidateNodeError::InvalidDigestLen(
directory_node.digest.len(),
))?;
};
}
validate_node_name(&directory_node.name)?;
}
// for a file root node, ensure the digest has the appropriate size.
node::Node::File(file_node) => {
if TryInto::<B3Digest>::try_into(file_node.digest.clone()).is_err() {
if file_node.digest.len() != B3_LEN {
Err(ValidateNodeError::InvalidDigestLen(file_node.digest.len()))?;
};
}
validate_node_name(&file_node.name)?;
}
// ensure the symlink target is not empty and doesn't contain null bytes.
node::Node::Symlink(symlink_node) => {
if symlink_node.target.len() == 0 {
Err(ValidateNodeError::InvalidSymlinkTarget(vec![]))?;
}
if symlink_node.target.contains(&b'\0') {
if symlink_node.target.len() == 0 || symlink_node.target.contains(&b'\0') {
Err(ValidateNodeError::InvalidSymlinkTarget(
symlink_node.target.to_vec(),
))?;