fix(tvix/protos): support []byte node names

The underlying protos were changed to return `[]byte` instead of `string`.

Change-Id: I5d3e5d8de0ed7200325f7ab0d62e3c10d8eb1b7d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9285
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
Brian McGee 2023-09-09 12:55:06 +01:00
parent d546b9a15d
commit 1549f020ee
2 changed files with 35 additions and 34 deletions

View file

@ -1,9 +1,9 @@
package storev1 package storev1
import ( import (
"bytes"
"encoding/base64"
"fmt" "fmt"
"strings"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"lukechampine.com/blake3" "lukechampine.com/blake3"
) )
@ -43,8 +43,8 @@ func (d *Directory) Digest() ([]byte, error) {
// We disallow slashes, null bytes, '.', '..' and the empty string. // We disallow slashes, null bytes, '.', '..' and the empty string.
// Depending on the context, a *Node message with an empty string as name is // Depending on the context, a *Node message with an empty string as name is
// allowed, but they don't occur inside a Directory message. // allowed, but they don't occur inside a Directory message.
func isValidName(n string) bool { func isValidName(n []byte) bool {
if n == "" || n == ".." || n == "." || strings.Contains(n, "\x00") || strings.Contains(n, "/") { if len(n) == 0 || bytes.Equal(n, []byte("..")) || bytes.Equal(n, []byte{'.'}) || bytes.Contains(n, []byte{'\x00'}) || bytes.Contains(n, []byte{'/'}) {
return false return false
} }
return true return true
@ -62,14 +62,14 @@ func (d *Directory) Validate() error {
// We also track the last seen name in each of the three lists, // We also track the last seen name in each of the three lists,
// to ensure nodes are sorted by their names. // to ensure nodes are sorted by their names.
var lastDirectoryName, lastFileName, lastSymlinkName string var lastDirectoryName, lastFileName, lastSymlinkName []byte
// helper function to only insert in sorted order. // helper function to only insert in sorted order.
// used with the three lists above. // used with the three lists above.
// Note this consumes a *pointer to* a string, as it mutates it. // Note this consumes a *pointer to* a string, as it mutates it.
insertIfGt := func(lastName *string, name string) error { insertIfGt := func(lastName *[]byte, name []byte) error {
// update if it's greater than the previous name // update if it's greater than the previous name
if name > *lastName { if bytes.Compare(name, *lastName) == 1 {
*lastName = name *lastName = name
return nil return nil
} else { } else {
@ -78,11 +78,12 @@ func (d *Directory) Validate() error {
} }
// insertOnce inserts into seenNames if the key doesn't exist yet. // insertOnce inserts into seenNames if the key doesn't exist yet.
insertOnce := func(name string) error { insertOnce := func(name []byte) error {
if _, found := seenNames[name]; found { encoded := base64.StdEncoding.EncodeToString(name)
return fmt.Errorf("duplicate name: %v", name) if _, found := seenNames[encoded]; found {
return fmt.Errorf("duplicate name: %v", string(name))
} }
seenNames[name] = nil seenNames[encoded] = nil
return nil return nil
} }

View file

@ -29,7 +29,7 @@ func TestDirectorySize(t *testing.T) {
t.Run("containing single empty directory", func(t *testing.T) { t.Run("containing single empty directory", func(t *testing.T) {
d := storev1pb.Directory{ d := storev1pb.Directory{
Directories: []*storev1pb.DirectoryNode{{ Directories: []*storev1pb.DirectoryNode{{
Name: "foo", Name: []byte([]byte("foo")),
Digest: dummyDigest, Digest: dummyDigest,
Size: 0, Size: 0,
}}, }},
@ -43,7 +43,7 @@ func TestDirectorySize(t *testing.T) {
t.Run("containing single non-empty directory", func(t *testing.T) { t.Run("containing single non-empty directory", func(t *testing.T) {
d := storev1pb.Directory{ d := storev1pb.Directory{
Directories: []*storev1pb.DirectoryNode{{ Directories: []*storev1pb.DirectoryNode{{
Name: "foo", Name: []byte("foo"),
Digest: dummyDigest, Digest: dummyDigest,
Size: 4, Size: 4,
}}, }},
@ -58,7 +58,7 @@ func TestDirectorySize(t *testing.T) {
d := storev1pb.Directory{ d := storev1pb.Directory{
Directories: []*storev1pb.DirectoryNode{}, Directories: []*storev1pb.DirectoryNode{},
Files: []*storev1pb.FileNode{{ Files: []*storev1pb.FileNode{{
Name: "foo", Name: []byte("foo"),
Digest: dummyDigest, Digest: dummyDigest,
Size: 42, Size: 42,
Executable: false, Executable: false,
@ -74,8 +74,8 @@ func TestDirectorySize(t *testing.T) {
Directories: []*storev1pb.DirectoryNode{}, Directories: []*storev1pb.DirectoryNode{},
Files: []*storev1pb.FileNode{}, Files: []*storev1pb.FileNode{},
Symlinks: []*storev1pb.SymlinkNode{{ Symlinks: []*storev1pb.SymlinkNode{{
Name: "foo", Name: []byte("foo"),
Target: "bar", Target: []byte("bar"),
}}, }},
} }
@ -114,7 +114,7 @@ func TestDirectoryValidate(t *testing.T) {
{ {
d := storev1pb.Directory{ d := storev1pb.Directory{
Directories: []*storev1pb.DirectoryNode{{ Directories: []*storev1pb.DirectoryNode{{
Name: "", Name: []byte{},
Digest: dummyDigest, Digest: dummyDigest,
Size: 42, Size: 42,
}}, }},
@ -127,7 +127,7 @@ func TestDirectoryValidate(t *testing.T) {
{ {
d := storev1pb.Directory{ d := storev1pb.Directory{
Directories: []*storev1pb.DirectoryNode{{ Directories: []*storev1pb.DirectoryNode{{
Name: ".", Name: []byte("."),
Digest: dummyDigest, Digest: dummyDigest,
Size: 42, Size: 42,
}}, }},
@ -141,7 +141,7 @@ func TestDirectoryValidate(t *testing.T) {
d := storev1pb.Directory{ d := storev1pb.Directory{
Directories: []*storev1pb.DirectoryNode{}, Directories: []*storev1pb.DirectoryNode{},
Files: []*storev1pb.FileNode{{ Files: []*storev1pb.FileNode{{
Name: "..", Name: []byte(".."),
Digest: dummyDigest, Digest: dummyDigest,
Size: 42, Size: 42,
Executable: false, Executable: false,
@ -156,8 +156,8 @@ func TestDirectoryValidate(t *testing.T) {
Directories: []*storev1pb.DirectoryNode{}, Directories: []*storev1pb.DirectoryNode{},
Files: []*storev1pb.FileNode{}, Files: []*storev1pb.FileNode{},
Symlinks: []*storev1pb.SymlinkNode{{ Symlinks: []*storev1pb.SymlinkNode{{
Name: "\x00", Name: []byte("\x00"),
Target: "foo", Target: []byte("foo"),
}}, }},
} }
@ -168,8 +168,8 @@ func TestDirectoryValidate(t *testing.T) {
Directories: []*storev1pb.DirectoryNode{}, Directories: []*storev1pb.DirectoryNode{},
Files: []*storev1pb.FileNode{}, Files: []*storev1pb.FileNode{},
Symlinks: []*storev1pb.SymlinkNode{{ Symlinks: []*storev1pb.SymlinkNode{{
Name: "foo/bar", Name: []byte("foo/bar"),
Target: "foo", Target: []byte("foo"),
}}, }},
} }
@ -180,7 +180,7 @@ func TestDirectoryValidate(t *testing.T) {
t.Run("invalid digest", func(t *testing.T) { t.Run("invalid digest", func(t *testing.T) {
d := storev1pb.Directory{ d := storev1pb.Directory{
Directories: []*storev1pb.DirectoryNode{{ Directories: []*storev1pb.DirectoryNode{{
Name: "foo", Name: []byte("foo"),
Digest: nil, Digest: nil,
Size: 42, Size: 42,
}}, }},
@ -196,11 +196,11 @@ func TestDirectoryValidate(t *testing.T) {
{ {
d := storev1pb.Directory{ d := storev1pb.Directory{
Directories: []*storev1pb.DirectoryNode{{ Directories: []*storev1pb.DirectoryNode{{
Name: "b", Name: []byte("b"),
Digest: dummyDigest, Digest: dummyDigest,
Size: 42, Size: 42,
}, { }, {
Name: "a", Name: []byte("a"),
Digest: dummyDigest, Digest: dummyDigest,
Size: 42, Size: 42,
}}, }},
@ -214,12 +214,12 @@ func TestDirectoryValidate(t *testing.T) {
{ {
d := storev1pb.Directory{ d := storev1pb.Directory{
Directories: []*storev1pb.DirectoryNode{{ Directories: []*storev1pb.DirectoryNode{{
Name: "a", Name: []byte("a"),
Digest: dummyDigest, Digest: dummyDigest,
Size: 42, Size: 42,
}}, }},
Files: []*storev1pb.FileNode{{ Files: []*storev1pb.FileNode{{
Name: "a", Name: []byte("a"),
Digest: dummyDigest, Digest: dummyDigest,
Size: 42, Size: 42,
Executable: false, Executable: false,
@ -233,11 +233,11 @@ func TestDirectoryValidate(t *testing.T) {
{ {
d := storev1pb.Directory{ d := storev1pb.Directory{
Directories: []*storev1pb.DirectoryNode{{ Directories: []*storev1pb.DirectoryNode{{
Name: "a", Name: []byte("a"),
Digest: dummyDigest, Digest: dummyDigest,
Size: 42, Size: 42,
}, { }, {
Name: "b", Name: []byte("b"),
Digest: dummyDigest, Digest: dummyDigest,
Size: 42, Size: 42,
}}, }},
@ -251,18 +251,18 @@ func TestDirectoryValidate(t *testing.T) {
{ {
d := storev1pb.Directory{ d := storev1pb.Directory{
Directories: []*storev1pb.DirectoryNode{{ Directories: []*storev1pb.DirectoryNode{{
Name: "b", Name: []byte("b"),
Digest: dummyDigest, Digest: dummyDigest,
Size: 42, Size: 42,
}, { }, {
Name: "c", Name: []byte("c"),
Digest: dummyDigest, Digest: dummyDigest,
Size: 42, Size: 42,
}}, }},
Files: []*storev1pb.FileNode{}, Files: []*storev1pb.FileNode{},
Symlinks: []*storev1pb.SymlinkNode{{ Symlinks: []*storev1pb.SymlinkNode{{
Name: "a", Name: []byte("a"),
Target: "foo", Target: []byte("foo"),
}}, }},
} }
assert.NoError(t, d.Validate(), "shouldn't error") assert.NoError(t, d.Validate(), "shouldn't error")