about summary refs log tree commit diff
path: root/tvix/store/protos/pathinfo.go
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-10-12T23·16+0200
committerclbot <clbot@tvl.fyi>2023-10-14T14·21+0000
commit786b0324a9df1a63606bae72011978fc415f6f07 (patch)
tree276b9fb3efda71725a1aaade5605796e8832a92d /tvix/store/protos/pathinfo.go
parent2d2c4322d93308ddffe1647466abd91025af6a68 (diff)
feat(tvix/store/protos): use Validate() function on root node r/6806
This updates the code to make use of the new Validate() function defined
on a Node.

Change-Id: I9b6ed694661f41e700f19cc78d53d2224b61852d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9718
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix/store/protos/pathinfo.go')
-rw-r--r--tvix/store/protos/pathinfo.go49
1 files changed, 17 insertions, 32 deletions
diff --git a/tvix/store/protos/pathinfo.go b/tvix/store/protos/pathinfo.go
index 9b51b5266cdc..8c0b94f200c1 100644
--- a/tvix/store/protos/pathinfo.go
+++ b/tvix/store/protos/pathinfo.go
@@ -57,55 +57,40 @@ func (p *PathInfo) Validate() (*storepath.StorePath, error) {
 		return nil, fmt.Errorf("root node must be set")
 	}
 
-	// for all three node types, ensure the name properly parses to a store path,
-	// and in case it refers to a digest, ensure it has the right length.
+	if err := rootNode.Validate(); err != nil {
+		return nil, fmt.Errorf("root node failed validation: %w", err)
+	}
 
-	var storePath *storepath.StorePath
-	var err error
+	// for all three node types, ensure the name properly parses to a store path.
+	// This is a stricter check as the ones already performed in the rootNode.Validate() call.
+	var rootNodeName []byte
 
 	if node := rootNode.GetDirectory(); node != nil {
-		if len(node.Digest) != 32 {
-			return nil, fmt.Errorf("invalid digest size for %s, expected %d, got %d", node.Name, 32, len(node.Digest))
-		}
-
-		storePath, err = storepath.FromString(string(node.GetName()))
-
-		if err != nil {
-			return nil, fmt.Errorf("unable to parse %s as StorePath: %w", node.Name, err)
-		}
-
+		rootNodeName = node.GetName()
 	} else if node := rootNode.GetFile(); node != nil {
-		if len(node.Digest) != 32 {
-			return nil, fmt.Errorf("invalid digest size for %s, expected %d, got %d", node.Name, 32, len(node.Digest))
-		}
-
-		storePath, err = storepath.FromString(string(node.GetName()))
-		if err != nil {
-			return nil, fmt.Errorf("unable to parse %s as StorePath: %w", node.Name, err)
-		}
-
+		rootNodeName = node.GetName()
 	} else if node := rootNode.GetSymlink(); node != nil {
-		storePath, err = storepath.FromString(string(node.GetName()))
-
-		if err != nil {
-			return nil, fmt.Errorf("unable to parse %s as StorePath: %w", node.Name, err)
-		}
-
+		rootNodeName = node.GetName()
 	} else {
-		// this would only happen if we introduced a new type
+		// already caught by rootNode.Validate()
 		panic("unreachable")
 	}
 
+	storePath, err := storepath.FromString(string(rootNodeName))
+	if err != nil {
+		return nil, fmt.Errorf("unable to parse root node name %s as StorePath: %w", rootNodeName, err)
+	}
+
 	// If the Deriver field is populated, ensure it parses to a StorePath.
 	// We can't check for it to *not* end with .drv, as the .drv files produced by
 	// recursive Nix end with multiple .drv suffixes, and only one is popped when
 	// converting to this field.
 	if p.Deriver != nil {
-		storePath := storepath.StorePath{
+		deriverStorePath := storepath.StorePath{
 			Name:   string(p.Deriver.GetName()),
 			Digest: p.Deriver.GetDigest(),
 		}
-		if err := storePath.Validate(); err != nil {
+		if err := deriverStorePath.Validate(); err != nil {
 			return nil, fmt.Errorf("invalid deriver field: %w", err)
 		}
 	}