about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-10-12T23·09+0200
committerclbot <clbot@tvl.fyi>2023-10-14T12·09+0000
commit532f414da6afd9f0b9409d1b944f91da143d8b5c (patch)
tree62810c87a966511fe851f2b213b1ac57471b8d9d
parentf3353b5dcf63a8bffcfff9f8ac4e8c1fd95684e0 (diff)
refactor(tvix/castore): simplify node validation checks r/6798
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>
-rw-r--r--tvix/castore/src/proto/mod.rs17
1 files changed, 8 insertions, 9 deletions
diff --git a/tvix/castore/src/proto/mod.rs b/tvix/castore/src/proto/mod.rs
index ae00d4f158e3..0e7a363149b2 100644
--- a/tvix/castore/src/proto/mod.rs
+++ b/tvix/castore/src/proto/mod.rs
@@ -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(),
                     ))?;