2017-07-13 03:45:53 +00:00
|
|
|
package filesync
|
|
|
|
|
|
|
|
import (
|
2017-12-13 00:59:06 +00:00
|
|
|
"bufio"
|
|
|
|
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"
|
2017-07-11 17:12:12 +00:00
|
|
|
"google.golang.org/grpc"
|
2017-07-13 03:45:53 +00:00
|
|
|
)
|
|
|
|
|
2017-09-22 05:14:11 +00:00
|
|
|
func sendDiffCopy(stream grpc.Stream, dir string, includes, excludes []string, progress progressCb, _map func(*fsutil.Stat) bool) error {
|
2017-07-13 03:45:53 +00:00
|
|
|
return fsutil.Send(stream.Context(), stream, dir, &fsutil.WalkOpt{
|
|
|
|
ExcludePatterns: excludes,
|
2017-09-22 01:22:14 +00:00
|
|
|
IncludePatterns: includes,
|
2017-09-22 05:14:11 +00:00
|
|
|
Map: _map,
|
2017-07-13 03:45:53 +00:00
|
|
|
}, progress)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(dt), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *streamWriterCloser) Close() error {
|
|
|
|
if err := wc.ClientStream.CloseSend(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// block until receiver is done
|
|
|
|
var bm BytesMessage
|
|
|
|
if err := wc.ClientStream.RecvMsg(&bm); err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-21 00:27:59 +00:00
|
|
|
func recvDiffCopy(ds grpc.Stream, dest string, cu CacheUpdater, progress progressCb) 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
|
|
|
}
|
2017-08-03 22:24:02 +00:00
|
|
|
return fsutil.Receive(ds.Context(), ds, dest, fsutil.ReceiveOpt{
|
|
|
|
NotifyHashed: cf,
|
|
|
|
ContentHasher: ch,
|
|
|
|
ProgressCb: progress,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func syncTargetDiffCopy(ds grpc.Stream, dest string) error {
|
|
|
|
if err := os.MkdirAll(dest, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fsutil.Receive(ds.Context(), ds, dest, fsutil.ReceiveOpt{
|
|
|
|
Merge: true,
|
|
|
|
Filter: func() func(*fsutil.Stat) bool {
|
|
|
|
uid := os.Getuid()
|
|
|
|
gid := os.Getgid()
|
|
|
|
return func(st *fsutil.Stat) bool {
|
|
|
|
st.Uid = uint32(uid)
|
|
|
|
st.Gid = uint32(gid)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
})
|
2017-07-13 03:45:53 +00:00
|
|
|
}
|
2017-12-13 00:59:06 +00:00
|
|
|
|
|
|
|
func writeTargetFile(ds grpc.Stream, wc io.WriteCloser) error {
|
|
|
|
for {
|
|
|
|
bm := BytesMessage{}
|
|
|
|
if err := ds.RecvMsg(&bm); err != nil {
|
|
|
|
if errors.Cause(err) == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := wc.Write(bm.Data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|