Skip to content

Commit

Permalink
Merge pull request #171 from kzys/bye-gogo
Browse files Browse the repository at this point in the history
Migrate off of gogo/protobuf
  • Loading branch information
tonistiigi authored Nov 21, 2023
2 parents f098008 + bea810c commit d22c3fa
Show file tree
Hide file tree
Showing 18 changed files with 428 additions and 1,441 deletions.
8 changes: 4 additions & 4 deletions diff_containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ func doubleWalkDiff(ctx context.Context, changeFn ChangeFunc, a, b walkerFn, fil
var f *types.Stat
var f2copy *currentPath
if f2 != nil {
statCopy := *f2.stat
statCopy := cloneStat(f2.stat)
if filter != nil {
filter(f2.path, &statCopy)
filter(f2.path, statCopy)
}
f2copy = &currentPath{path: f2.path, stat: &statCopy}
f2copy = &currentPath{path: f2.path, stat: statCopy}
}
k, p := pathChange(f1, f2copy)
switch k {
Expand Down Expand Up @@ -188,7 +188,7 @@ func sameFile(f1, f2 *currentPath, differ DiffType) (same bool, retErr error) {
}
// If not a directory also check size, modtime, and content
if !f1.stat.IsDir() {
if f1.stat.Size_ != f2.stat.Size_ {
if f1.stat.Size != f2.stat.Size {
return false, nil
}

Expand Down
12 changes: 6 additions & 6 deletions diskwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ func (dw *DiskWriter) HandleChange(kind ChangeKind, p string, fi os.FileInfo, er
return errors.WithStack(&os.PathError{Path: p, Err: syscall.EBADMSG, Op: "change without stat info"})
}

statCopy := *stat
statCopy := cloneStat(stat)

if dw.filter != nil {
if ok := dw.filter(p, &statCopy); !ok {
if ok := dw.filter(p, statCopy); !ok {
return nil
}
}
Expand All @@ -146,7 +146,7 @@ func (dw *DiskWriter) HandleChange(kind ChangeKind, p string, fi os.FileInfo, er
}

if oldFi != nil && fi.IsDir() && oldFi.IsDir() {
if err := rewriteMetadata(destPath, &statCopy); err != nil {
if err := rewriteMetadata(destPath, statCopy); err != nil {
return errors.Wrapf(err, "error setting dir metadata for %s", destPath)
}
return nil
Expand All @@ -170,7 +170,7 @@ func (dw *DiskWriter) HandleChange(kind ChangeKind, p string, fi os.FileInfo, er
}
dw.dirModTimes[destPath] = statCopy.ModTime
case fi.Mode()&os.ModeDevice != 0 || fi.Mode()&os.ModeNamedPipe != 0:
if err := handleTarTypeBlockCharFifo(newPath, &statCopy); err != nil {
if err := handleTarTypeBlockCharFifo(newPath, statCopy); err != nil {
return errors.Wrapf(err, "failed to create device %s", newPath)
}
case fi.Mode()&os.ModeSymlink != 0:
Expand Down Expand Up @@ -198,7 +198,7 @@ func (dw *DiskWriter) HandleChange(kind ChangeKind, p string, fi os.FileInfo, er
}
}

if err := rewriteMetadata(newPath, &statCopy); err != nil {
if err := rewriteMetadata(newPath, statCopy); err != nil {
return errors.Wrapf(err, "error setting metadata for %s", newPath)
}

Expand All @@ -216,7 +216,7 @@ func (dw *DiskWriter) HandleChange(kind ChangeKind, p string, fi os.FileInfo, er

if isRegularFile {
if dw.opt.AsyncDataCb != nil {
dw.requestAsyncFileData(p, destPath, fi, &statCopy)
dw.requestAsyncFileData(p, destPath, fi, statCopy)
}
} else {
return dw.processChange(kind, p, fi, nil)
Expand Down
11 changes: 5 additions & 6 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (fs *fs) Open(p string) (io.ReadCloser, error) {
}

type Dir struct {
Stat types.Stat
Stat *types.Stat
FS FS
}

Expand Down Expand Up @@ -125,12 +125,12 @@ func (fs *subDirFS) Walk(ctx context.Context, target string, fn gofs.WalkDirFunc
continue
}

fi := &StatInfo{&d.Stat}
fi := &StatInfo{d.Stat}
if !fi.IsDir() {
return errors.WithStack(&os.PathError{Path: d.Stat.Path, Err: syscall.ENOTDIR, Op: "walk subdir"})
}
dStat := d.Stat
if err := fn(d.Stat.Path, &DirEntryInfo{Stat: &dStat}, nil); err != nil {
if err := fn(d.Stat.Path, &DirEntryInfo{Stat: dStat}, nil); err != nil {
return err
}
if err := d.FS.Walk(ctx, rest, func(p string, entry gofs.DirEntry, err error) error {
Expand Down Expand Up @@ -191,7 +191,7 @@ func (s *StatInfo) Name() string {
return filepath.Base(s.Stat.Path)
}
func (s *StatInfo) Size() int64 {
return s.Stat.Size_
return s.Stat.Size
}
func (s *StatInfo) Mode() os.FileMode {
return os.FileMode(s.Stat.Mode)
Expand Down Expand Up @@ -246,6 +246,5 @@ func (s *DirEntryInfo) Info() (gofs.FileInfo, error) {
s.Stat = stat
}

st := *s.Stat
return &StatInfo{&st}, nil
return &StatInfo{s.Stat}, nil
}
4 changes: 2 additions & 2 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ func TestWalkDir(t *testing.T) {

f, err := SubDirFS([]Dir{
{
Stat: types.Stat{
Stat: &types.Stat{
Mode: uint32(os.ModeDir | 0755),
Path: "1",
},
FS: tmpfs,
},
{
Stat: types.Stat{
Stat: &types.Stat{
Mode: uint32(os.ModeDir | 0755),
Path: "2",
},
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ go 1.19
require (
github.com/Microsoft/go-winio v0.5.2
github.com/containerd/continuity v0.4.1
github.com/gogo/protobuf v1.3.2
github.com/moby/patternmatcher v0.5.0
github.com/opencontainers/go-digest v1.0.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.1.0
golang.org/x/sys v0.1.0
google.golang.org/protobuf v1.26.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
google.golang.org/protobuf v1.26.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
29 changes: 0 additions & 29 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ github.com/containerd/continuity v0.4.1 h1:wQnVrjIyQ8vhU2sgOiL5T07jo+ouqc2bnKsv5
github.com/containerd/continuity v0.4.1/go.mod h1:F6PTNCKepoxEaXLQp3wDAjygEnImnZ/7o4JzpodfroQ=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand All @@ -30,37 +26,12 @@ github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
3 changes: 2 additions & 1 deletion receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/tonistiigi/fsutil/types"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
)

type DiffType int
Expand Down Expand Up @@ -170,7 +171,7 @@ func (r *receiver) run(ctx context.Context) error {
return err
}
if r.progressCb != nil {
size += p.Size()
size += proto.Size(&p)
r.progressCb(size, false)
}

Expand Down
33 changes: 23 additions & 10 deletions receive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/tonistiigi/fsutil/types"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
)

func TestSendError(t *testing.T) {
Expand Down Expand Up @@ -79,7 +80,7 @@ func TestCopyWithSubDir(t *testing.T) {

eg.Go(func() error {
defer s1.(*fakeConnProto).closeSend()
subdir, err := SubDirFS([]Dir{{FS: fs, Stat: types.Stat{Path: "sub", Mode: uint32(os.ModeDir | 0755)}}})
subdir, err := SubDirFS([]Dir{{FS: fs, Stat: &types.Stat{Path: "sub", Mode: uint32(os.ModeDir | 0755)}}})
if err != nil {
return err
}
Expand Down Expand Up @@ -385,6 +386,19 @@ func sockPairProto(ctx context.Context) (Stream, Stream) {
return &fakeConnProto{ctx, c1, c2}, &fakeConnProto{ctx, c2, c1}
}

//nolint:unused
func clonePacket(from *types.Packet) *types.Packet {
var data []byte
copy(data, from.Data)

return &types.Packet{
Type: from.Type,
Stat: cloneStat(from.Stat),
ID: from.ID,
Data: data,
}
}

//nolint:unused
type fakeConn struct {
ctx context.Context
Expand All @@ -399,15 +413,14 @@ func (fc *fakeConn) Context() context.Context {

//nolint:unused
func (fc *fakeConn) RecvMsg(m interface{}) error {
p, ok := m.(*types.Packet)
_, ok := m.(*types.Packet)
if !ok {
return errors.Errorf("invalid msg: %#v", m)
}
select {
case <-fc.ctx.Done():
return fc.ctx.Err()
case p2 := <-fc.recvChan:
*p = *p2
case _ = <-fc.recvChan:
return nil
}
}
Expand All @@ -418,12 +431,12 @@ func (fc *fakeConn) SendMsg(m interface{}) error {
if !ok {
return errors.Errorf("invalid msg: %#v", m)
}
p2 := *p
p2 := clonePacket(p)
p2.Data = append([]byte{}, p2.Data...)
select {
case <-fc.ctx.Done():
return fc.ctx.Err()
case fc.sendChan <- &p2:
case fc.sendChan <- p2:
return nil
}
}
Expand All @@ -450,7 +463,7 @@ func (fc *fakeConnProto) RecvMsg(m interface{}) error {
if !ok {
return io.EOF
}
return p.Unmarshal(dt)
return proto.Unmarshal(dt, p)
}
}

Expand All @@ -459,7 +472,7 @@ func (fc *fakeConnProto) SendMsg(m interface{}) error {
if !ok {
return errors.Errorf("invalid msg: %#v", m)
}
dt, err := p.Marshal()
dt, err := proto.Marshal(p)
if err != nil {
return err
}
Expand Down Expand Up @@ -493,7 +506,7 @@ func (c *changes) HandleChange(kind ChangeKind, p string, fi os.FileInfo, err er

func simpleSHA256Hasher(s *types.Stat) (hash.Hash, error) {
h := sha256.New()
ss := *s
ss := cloneStat(s)
ss.ModTime = 0
// Unlike Linux, on FreeBSD's stat() call returns -1 in st_rdev for regular files
ss.Devminor = 0
Expand All @@ -503,7 +516,7 @@ func simpleSHA256Hasher(s *types.Stat) (hash.Hash, error) {
ss.Mode = ss.Mode | 0777
}

dt, err := ss.Marshal()
dt, err := proto.Marshal(ss)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"
"github.com/tonistiigi/fsutil/types"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
)

var bufPool = sync.Pool{
Expand Down Expand Up @@ -167,7 +168,7 @@ func (s *sender) walk(ctx context.Context) error {
s.mu.Unlock()
}
i++
s.updateProgress(p.Size(), false)
s.updateProgress(proto.Size(p), false)
return errors.Wrapf(s.conn.SendMsg(p), "failed to send stat %s", path)
})
if err != nil {
Expand Down Expand Up @@ -195,7 +196,7 @@ func (fs *fileSender) Write(dt []byte) (int, error) {
if err := fs.sender.conn.SendMsg(p); err != nil {
return 0, err
}
fs.sender.updateProgress(p.Size(), false)
fs.sender.updateProgress(proto.Size(p), false)
return len(dt), nil
}

Expand Down
21 changes: 20 additions & 1 deletion stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func mkstat(path, relpath string, fi os.FileInfo, inodemap map[uint64]string) (*
setUnixOpt(fi, stat, relpath, inodemap)

if !fi.IsDir() {
stat.Size_ = fi.Size()
stat.Size = fi.Size()
if fi.Mode()&os.ModeSymlink != 0 {
link, err := os.Readlink(path)
if err != nil {
Expand Down Expand Up @@ -62,3 +62,22 @@ func Stat(path string) (*types.Stat, error) {
}
return mkstat(path, filepath.Base(path), fi, nil)
}

func cloneStat(from *types.Stat) *types.Stat {
clone := &types.Stat{
Path: from.Path,
Mode: from.Mode,
Uid: from.Uid,
Gid: from.Gid,
Size: from.Size,
ModTime: from.ModTime,
Linkname: from.Linkname,
Devmajor: from.Devmajor,
Devminor: from.Devminor,
Xattrs: make(map[string][]byte, len(from.Xattrs)),
}
for k, v := range from.Xattrs {
clone.Xattrs[k] = v
}
return clone
}
Loading

0 comments on commit d22c3fa

Please sign in to comment.