about summary refs log tree commit diff
path: root/tvix/store/protos/castore.go
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2022-11-19T23·21+0000
committerflokli <flokli@flokli.de>2022-12-27T21·31+0000
commitc3fb6d22187fc759563b265cfab65a48a6e4fa08 (patch)
tree2c7eb28fcdaadadd7186058a447cb8d5d1df4bdb /tvix/store/protos/castore.go
parent81fd9caf3ebce4fa68e9d1281334958578295ed3 (diff)
feat(tvix/store/protos): implement Size() and Digest() for Directory r/5508
This adds Size() and Digest() functions for the golang version.

Change-Id: If71445a9bb26100bb4076ac4f5c96945b33919f9
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7325
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix/store/protos/castore.go')
-rw-r--r--tvix/store/protos/castore.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/tvix/store/protos/castore.go b/tvix/store/protos/castore.go
new file mode 100644
index 000000000000..38419b40da99
--- /dev/null
+++ b/tvix/store/protos/castore.go
@@ -0,0 +1,39 @@
+package storev1
+
+import (
+	"fmt"
+
+	"google.golang.org/protobuf/proto"
+	"lukechampine.com/blake3"
+)
+
+// The size of a directory is calculated by summing up the numbers of
+// `directories`, `files` and `symlinks`, and for each directory, its size
+// field.
+func (d *Directory) Size() uint32 {
+	var size uint32
+	size = uint32(len(d.Files) + len(d.Symlinks))
+	for _, d := range d.Directories {
+		size += 1 + d.Size
+	}
+	return size
+}
+
+func (d *Directory) Digest() ([]byte, error) {
+	b, err := proto.MarshalOptions{
+		Deterministic: true,
+	}.Marshal(d)
+
+	if err != nil {
+		return nil, fmt.Errorf("error while marshalling directory: %w", err)
+	}
+
+	h := blake3.New(32, nil)
+
+	_, err = h.Write(b)
+	if err != nil {
+		return nil, fmt.Errorf("error writing to hasher: %w", err)
+	}
+
+	return h.Sum(nil), nil
+}