From 1392913e981ae4edbec6ef39a4d3de44749ad81c Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Tue, 14 May 2024 12:35:55 +0200 Subject: chore(tvix/nar-bridge): move to nar-bridge-go Make some space for the rust implementation. Change-Id: I924dc1657be10abe5a11951c3b9de50bae06db19 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11662 Tested-by: BuildkiteCI Autosubmit: flokli Reviewed-by: yuka --- tvix/nar-bridge-go/pkg/importer/gen_pathinfo.go | 62 +++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tvix/nar-bridge-go/pkg/importer/gen_pathinfo.go (limited to 'tvix/nar-bridge-go/pkg/importer/gen_pathinfo.go') diff --git a/tvix/nar-bridge-go/pkg/importer/gen_pathinfo.go b/tvix/nar-bridge-go/pkg/importer/gen_pathinfo.go new file mode 100644 index 000000000000..bdc298a9a399 --- /dev/null +++ b/tvix/nar-bridge-go/pkg/importer/gen_pathinfo.go @@ -0,0 +1,62 @@ +package importer + +import ( + castorev1pb "code.tvl.fyi/tvix/castore-go" + storev1pb "code.tvl.fyi/tvix/store-go" + "fmt" + "github.com/nix-community/go-nix/pkg/narinfo" + "github.com/nix-community/go-nix/pkg/storepath" +) + +// GenPathInfo takes a rootNode and narInfo and assembles a PathInfo. +// The rootNode is renamed to match the StorePath in the narInfo. +func GenPathInfo(rootNode *castorev1pb.Node, narInfo *narinfo.NarInfo) (*storev1pb.PathInfo, error) { + // parse the storePath from the .narinfo + storePath, err := storepath.FromAbsolutePath(narInfo.StorePath) + if err != nil { + return nil, fmt.Errorf("unable to parse StorePath: %w", err) + } + + // construct the references, by parsing ReferenceNames and extracting the digest + references := make([][]byte, len(narInfo.References)) + for i, referenceStr := range narInfo.References { + // parse reference as store path + referenceStorePath, err := storepath.FromString(referenceStr) + if err != nil { + return nil, fmt.Errorf("unable to parse reference %s as storepath: %w", referenceStr, err) + } + references[i] = referenceStorePath.Digest + } + + // construct the narInfo.Signatures[*] from pathInfo.Narinfo.Signatures[*] + narinfoSignatures := make([]*storev1pb.NARInfo_Signature, len(narInfo.Signatures)) + for i, narinfoSig := range narInfo.Signatures { + narinfoSignatures[i] = &storev1pb.NARInfo_Signature{ + Name: narinfoSig.Name, + Data: narinfoSig.Data, + } + } + + // assemble the PathInfo. + pathInfo := &storev1pb.PathInfo{ + // embed a new root node with the name set to the store path basename. + Node: castorev1pb.RenamedNode(rootNode, storePath.String()), + References: references, + Narinfo: &storev1pb.NARInfo{ + NarSize: narInfo.NarSize, + NarSha256: narInfo.FileHash.Digest(), + Signatures: narinfoSignatures, + ReferenceNames: narInfo.References, + }, + } + + // run Validate on the PathInfo, more as an additional sanity check our code is sound, + // to make sure we populated everything properly, before returning it. + // Fail hard if we fail validation, this is a code error. + if _, err = pathInfo.Validate(); err != nil { + panic(fmt.Sprintf("PathInfo failed validation: %v", err)) + } + + return pathInfo, nil + +} -- cgit 1.4.1