2017-07-13 03:45:53 +00:00
|
|
|
package filesync
|
|
|
|
|
|
|
|
import (
|
2017-12-13 00:59:06 +00:00
|
|
|
"bufio"
|
2020-07-18 21:51:19 +00:00
|
|
|
"context"
|
2017-12-13 00:59:06 +00:00
|
|
|
io "io"
|
2017-08-03 22:24:02 +00:00
|
|
|
"os"
|
2017-07-13 03:45:53 +00:00
|
|
|
"time"
|
|
|
|
|
2017-12-13 00:59:06 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-07-13 03:45:53 +00:00
|
|
|
"github.com/tonistiigi/fsutil"
|
2018-10-01 11:16:48 +00:00
|
|
|
fstypes "github.com/tonistiigi/fsutil/types"
|
2017-07-11 17:12:12 +00:00
|
|
|
"google.golang.org/grpc"
|
2017-07-13 03:45:53 +00:00
|
|
|
)
|
|
|
|
|
2020-07-18 21:51:19 +00:00
|
|
|
type Stream interface {
|
|
|
|
Context() context.Context
|
|
|
|
SendMsg(m interface{}) error
|
|
|
|
RecvMsg(m interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendDiffCopy(stream Stream, fs fsutil.FS, progress progressCb) error {
|
2019-06-01 23:52:35 +00:00
|
|
|
return errors.WithStack(fsutil.Send(stream.Context(), stream, fs, progress))
|
2017-07-13 03:45:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 00:59:06 +00:00
|
|
|
func newStreamWriter(stream grpc.ClientStream) io.WriteCloser {
|
|
|
|
wc := &streamWriterCloser{ClientStream: stream}
|
|
|
|
return &bufferedWriteCloser{Writer: bufio.NewWriter(wc), Closer: wc}
|
|
|
|
}
|
|
|
|
|
|
|
|
type bufferedWriteCloser struct {
|
|
|
|
*bufio.Writer
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bwc *bufferedWriteCloser) Close() error {
|
|
|
|
if err := bwc.Writer.Flush(); err != nil {
|
2019-06-01 23:52:35 +00:00
|
|
|
return errors.WithStack(err)
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
|
|
|
return bwc.Closer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
type streamWriterCloser struct {
|
|
|
|
grpc.ClientStream
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *streamWriterCloser) Write(dt []byte) (int, error) {
|
|
|
|
if err := wc.ClientStream.SendMsg(&BytesMessage{Data: dt}); err != nil {
|
2019-07-30 00:50:54 +00:00
|
|
|
// SendMsg return EOF on remote errors
|
2020-04-19 05:17:47 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2019-07-30 00:50:54 +00:00
|
|
|
if err := errors.WithStack(wc.ClientStream.RecvMsg(struct{}{})); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
2019-06-01 23:52:35 +00:00
|
|
|
return 0, errors.WithStack(err)
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
|
|
|
return len(dt), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *streamWriterCloser) Close() error {
|
|
|
|
if err := wc.ClientStream.CloseSend(); err != nil {
|
2019-06-01 23:52:35 +00:00
|
|
|
return errors.WithStack(err)
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
|
|
|
// block until receiver is done
|
|
|
|
var bm BytesMessage
|
|
|
|
if err := wc.ClientStream.RecvMsg(&bm); err != io.EOF {
|
2019-06-01 23:52:35 +00:00
|
|
|
return errors.WithStack(err)
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-18 21:51:19 +00:00
|
|
|
func recvDiffCopy(ds grpc.ClientStream, dest string, cu CacheUpdater, progress progressCb, filter func(string, *fstypes.Stat) bool) error {
|
2017-07-13 03:45:53 +00:00
|
|
|
st := time.Now()
|
|
|
|
defer func() {
|
|
|
|
logrus.Debugf("diffcopy took: %v", time.Since(st))
|
|
|
|
}()
|
|
|
|
var cf fsutil.ChangeFunc
|
2017-07-31 22:06:59 +00:00
|
|
|
var ch fsutil.ContentHasher
|
2017-07-13 03:45:53 +00:00
|
|
|
if cu != nil {
|
|
|
|
cu.MarkSupported(true)
|
|
|
|
cf = cu.HandleChange
|
2017-07-31 22:06:59 +00:00
|
|
|
ch = cu.ContentHasher()
|
2017-07-13 03:45:53 +00:00
|
|
|
}
|
2019-06-01 23:52:35 +00:00
|
|
|
return errors.WithStack(fsutil.Receive(ds.Context(), ds, dest, fsutil.ReceiveOpt{
|
2017-08-03 22:24:02 +00:00
|
|
|
NotifyHashed: cf,
|
|
|
|
ContentHasher: ch,
|
|
|
|
ProgressCb: progress,
|
2019-03-20 21:54:20 +00:00
|
|
|
Filter: fsutil.FilterFunc(filter),
|
2019-06-01 23:52:35 +00:00
|
|
|
}))
|
2017-08-03 22:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 21:51:19 +00:00
|
|
|
func syncTargetDiffCopy(ds grpc.ServerStream, dest string) error {
|
2017-08-03 22:24:02 +00:00
|
|
|
if err := os.MkdirAll(dest, 0700); err != nil {
|
2019-06-01 23:52:35 +00:00
|
|
|
return errors.Wrapf(err, "failed to create synctarget dest dir %s", dest)
|
2017-08-03 22:24:02 +00:00
|
|
|
}
|
2019-06-01 23:52:35 +00:00
|
|
|
return errors.WithStack(fsutil.Receive(ds.Context(), ds, dest, fsutil.ReceiveOpt{
|
2017-08-03 22:24:02 +00:00
|
|
|
Merge: true,
|
2019-03-19 02:02:00 +00:00
|
|
|
Filter: func() func(string, *fstypes.Stat) bool {
|
2017-08-03 22:24:02 +00:00
|
|
|
uid := os.Getuid()
|
|
|
|
gid := os.Getgid()
|
2019-03-19 02:02:00 +00:00
|
|
|
return func(p string, st *fstypes.Stat) bool {
|
2017-08-03 22:24:02 +00:00
|
|
|
st.Uid = uint32(uid)
|
|
|
|
st.Gid = uint32(gid)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}(),
|
2019-06-01 23:52:35 +00:00
|
|
|
}))
|
2017-07-13 03:45:53 +00:00
|
|
|
}
|
2017-12-13 00:59:06 +00:00
|
|
|
|
2020-07-18 21:51:19 +00:00
|
|
|
func writeTargetFile(ds grpc.ServerStream, wc io.WriteCloser) error {
|
2017-12-13 00:59:06 +00:00
|
|
|
for {
|
|
|
|
bm := BytesMessage{}
|
|
|
|
if err := ds.RecvMsg(&bm); err != nil {
|
2020-04-19 05:17:47 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2017-12-13 00:59:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-06-01 23:52:35 +00:00
|
|
|
return errors.WithStack(err)
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
|
|
|
if _, err := wc.Write(bm.Data); err != nil {
|
2019-06-01 23:52:35 +00:00
|
|
|
return errors.WithStack(err)
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|