2017-05-15 17:13:34 -04:00
|
|
|
package filesync
|
|
|
|
|
|
|
|
import (
|
2017-09-29 06:32:26 -04:00
|
|
|
"os"
|
2017-05-15 17:13:34 -04:00
|
|
|
"time"
|
|
|
|
|
2017-08-07 05:52:40 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-15 17:13:34 -04:00
|
|
|
"github.com/tonistiigi/fsutil"
|
2017-09-29 06:32:26 -04:00
|
|
|
"google.golang.org/grpc"
|
2017-05-15 17:13:34 -04:00
|
|
|
)
|
|
|
|
|
2017-09-29 06:32:26 -04:00
|
|
|
func sendDiffCopy(stream grpc.Stream, dir string, includes, excludes []string, progress progressCb, _map func(*fsutil.Stat) bool) error {
|
2017-05-15 17:13:34 -04:00
|
|
|
return fsutil.Send(stream.Context(), stream, dir, &fsutil.WalkOpt{
|
|
|
|
ExcludePatterns: excludes,
|
2017-09-29 06:32:26 -04:00
|
|
|
IncludePatterns: includes,
|
|
|
|
Map: _map,
|
2017-05-15 17:13:34 -04:00
|
|
|
}, progress)
|
|
|
|
}
|
|
|
|
|
2017-09-29 06:32:26 -04:00
|
|
|
func recvDiffCopy(ds grpc.Stream, dest string, cu CacheUpdater, progress progressCb) error {
|
2017-05-15 17:13:34 -04:00
|
|
|
st := time.Now()
|
|
|
|
defer func() {
|
|
|
|
logrus.Debugf("diffcopy took: %v", time.Since(st))
|
|
|
|
}()
|
|
|
|
var cf fsutil.ChangeFunc
|
2017-09-29 06:32:26 -04:00
|
|
|
var ch fsutil.ContentHasher
|
2017-05-15 17:13:34 -04:00
|
|
|
if cu != nil {
|
|
|
|
cu.MarkSupported(true)
|
|
|
|
cf = cu.HandleChange
|
2017-09-29 06:32:26 -04:00
|
|
|
ch = cu.ContentHasher()
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
2017-09-29 06:32:26 -04:00
|
|
|
return fsutil.Receive(ds.Context(), ds, dest, fsutil.ReceiveOpt{
|
|
|
|
NotifyHashed: cf,
|
|
|
|
ContentHasher: ch,
|
|
|
|
ProgressCb: progress,
|
|
|
|
})
|
|
|
|
}
|
2017-05-15 17:13:34 -04:00
|
|
|
|
2017-09-29 06:32:26 -04:00
|
|
|
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-05-15 17:13:34 -04:00
|
|
|
}
|