From ad393eb6477bb063b74148665de039a8ea740f27 Mon Sep 17 00:00:00 2001 From: Luke Granger-Brown Date: Sun, 9 Oct 2022 21:27:14 +0100 Subject: [PATCH] go/nix: split Nix encoding stuff into nixwire package --- go/nix/default.nix | 1 + go/nix/nar/default.nix | 3 +++ go/nix/nar/nar.go | 41 ++++---------------------------------- go/nix/nixwire/default.nix | 12 +++++++++++ go/nix/nixwire/nixwire.go | 40 +++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 go/nix/nixwire/default.nix create mode 100644 go/nix/nixwire/nixwire.go diff --git a/go/nix/default.nix b/go/nix/default.nix index ffb07ced31..9f7960af21 100644 --- a/go/nix/default.nix +++ b/go/nix/default.nix @@ -6,6 +6,7 @@ args: { nar = import ./nar args; nixstore = import ./nixstore args; + nixwire = import ./nixwire args; bcachegc = import ./bcachegc args; bcacheup = import ./bcacheup args; } diff --git a/go/nix/nar/default.nix b/go/nix/nar/default.nix index f89c951e1c..b022b5871f 100644 --- a/go/nix/nar/default.nix +++ b/go/nix/nar/default.nix @@ -10,6 +10,9 @@ ./nar.go ./dirfs.go ]; + deps = with depot; [ + go.nix.nixwire + ]; }) // { narinfo = import ./narinfo args; } diff --git a/go/nix/nar/nar.go b/go/nix/nar/nar.go index 3ddee4b903..8a35fdb0ff 100644 --- a/go/nix/nar/nar.go +++ b/go/nix/nar/nar.go @@ -1,48 +1,15 @@ package nar import ( - "encoding/binary" "fmt" "io" "io/fs" "path" "sort" + + "hg.lukegb.com/lukegb/depot/go/nix/nixwire" ) -type serializeWriter struct { - io.Writer -} - -func (w serializeWriter) WritePadding(n int64) (int64, error) { - if n%8 > 0 { - n, err := w.Write(make([]byte, 8-(n%8))) - return int64(n), err - } - return 0, nil -} - -func (w serializeWriter) WriteUint64(n uint64) (int64, error) { - buf := make([]byte, 8) - binary.LittleEndian.PutUint64(buf, n) - wrote, err := w.Write(buf) - return int64(wrote), err -} - -func (w serializeWriter) WriteString(s string) (int64, error) { - nSize, err := w.WriteUint64(uint64(len(s))) - if err != nil { - return int64(nSize), err - } - - nData, err := w.Write([]byte(s)) - if err != nil { - return int64(nSize) + int64(nData), err - } - - nPad, err := w.WritePadding(int64(len(s))) - return int64(nSize) + int64(nData) + int64(nPad), err -} - type FS interface { Open(string) (fs.File, error) Stat(string) (fs.FileInfo, error) @@ -50,7 +17,7 @@ type FS interface { Readlink(string) (string, error) } -func packFile(sw serializeWriter, root FS, fn string, stat fs.FileInfo) (int64, error) { +func packFile(sw nixwire.Serializer, root FS, fn string, stat fs.FileInfo) (int64, error) { var nSoFar int64 write := func(data ...any) (int64, error) { @@ -179,7 +146,7 @@ func packFile(sw serializeWriter, root FS, fn string, stat fs.FileInfo) (int64, } func Pack(w io.Writer, fs FS, fn string) (int64, error) { - sw := serializeWriter{w} + sw := nixwire.Serializer{w} n, err := sw.WriteString("nix-archive-1") if err != nil { diff --git a/go/nix/nixwire/default.nix b/go/nix/nixwire/default.nix new file mode 100644 index 0000000000..c4a76ee39c --- /dev/null +++ b/go/nix/nixwire/default.nix @@ -0,0 +1,12 @@ +# SPDX-FileCopyrightText: 2022 Luke Granger-Brown +# +# SPDX-License-Identifier: Apache-2.0 + +{ depot, ... }@args: +depot.third_party.buildGo.package { + name = "nixwire"; + path = "hg.lukegb.com/lukegb/depot/go/nix/nixwire"; + srcs = [ + ./nixwire.go + ]; +} diff --git a/go/nix/nixwire/nixwire.go b/go/nix/nixwire/nixwire.go new file mode 100644 index 0000000000..df77c52650 --- /dev/null +++ b/go/nix/nixwire/nixwire.go @@ -0,0 +1,40 @@ +package nixwire + +import ( + "encoding/binary" + "io" +) + +type Serializer struct { + io.Writer +} + +func (w Serializer) WritePadding(n int64) (int64, error) { + if n%8 > 0 { + n, err := w.Write(make([]byte, 8-(n%8))) + return int64(n), err + } + return 0, nil +} + +func (w Serializer) WriteUint64(n uint64) (int64, error) { + buf := make([]byte, 8) + binary.LittleEndian.PutUint64(buf, n) + wrote, err := w.Write(buf) + return int64(wrote), err +} + +func (w Serializer) WriteString(s string) (int64, error) { + nSize, err := w.WriteUint64(uint64(len(s))) + if err != nil { + return int64(nSize), err + } + + nData, err := w.Write([]byte(s)) + if err != nil { + return int64(nSize) + int64(nData), err + } + + nPad, err := w.WritePadding(int64(len(s))) + return int64(nSize) + int64(nData) + int64(nPad), err +}