feat(tvix/store/protos): use Validate() function on root node

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>
This commit is contained in:
Florian Klink 2023-10-13 01:16:08 +02:00 committed by clbot
parent 2d2c4322d9
commit 786b0324a9
4 changed files with 22 additions and 37 deletions

View file

@ -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)
}
}