go/nix: split Nix encoding stuff into nixwire package

This commit is contained in:
Luke Granger-Brown 2022-10-09 21:27:14 +01:00
parent 9dee33f3dc
commit ad393eb647
5 changed files with 60 additions and 37 deletions

View file

@ -6,6 +6,7 @@ args:
{ {
nar = import ./nar args; nar = import ./nar args;
nixstore = import ./nixstore args; nixstore = import ./nixstore args;
nixwire = import ./nixwire args;
bcachegc = import ./bcachegc args; bcachegc = import ./bcachegc args;
bcacheup = import ./bcacheup args; bcacheup = import ./bcacheup args;
} }

View file

@ -10,6 +10,9 @@
./nar.go ./nar.go
./dirfs.go ./dirfs.go
]; ];
deps = with depot; [
go.nix.nixwire
];
}) // { }) // {
narinfo = import ./narinfo args; narinfo = import ./narinfo args;
} }

View file

@ -1,48 +1,15 @@
package nar package nar
import ( import (
"encoding/binary"
"fmt" "fmt"
"io" "io"
"io/fs" "io/fs"
"path" "path"
"sort" "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 { type FS interface {
Open(string) (fs.File, error) Open(string) (fs.File, error)
Stat(string) (fs.FileInfo, error) Stat(string) (fs.FileInfo, error)
@ -50,7 +17,7 @@ type FS interface {
Readlink(string) (string, error) 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 var nSoFar int64
write := func(data ...any) (int64, error) { 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) { 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") n, err := sw.WriteString("nix-archive-1")
if err != nil { if err != nil {

View file

@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: 2022 Luke Granger-Brown <depot@lukegb.com>
#
# 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
];
}

40
go/nix/nixwire/nixwire.go Normal file
View file

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