2017-05-04 00:49:12 -04:00
|
|
|
package fsutil
|
|
|
|
|
|
|
|
import (
|
2018-06-08 20:39:07 -04:00
|
|
|
"context"
|
2017-05-04 00:49:12 -04:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-10-05 05:04:52 -04:00
|
|
|
"github.com/tonistiigi/fsutil/types"
|
2017-05-04 00:49:12 -04:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
2017-09-21 20:54:27 -04:00
|
|
|
type ReceiveOpt struct {
|
|
|
|
NotifyHashed ChangeFunc
|
|
|
|
ContentHasher ContentHasher
|
|
|
|
ProgressCb func(int, bool)
|
|
|
|
Merge bool
|
|
|
|
Filter FilterFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func Receive(ctx context.Context, conn Stream, dest string, opt ReceiveOpt) error {
|
2017-05-04 00:49:12 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
r := &receiver{
|
2017-09-21 20:54:27 -04:00
|
|
|
conn: &syncStream{Stream: conn},
|
|
|
|
dest: dest,
|
|
|
|
files: make(map[string]uint32),
|
|
|
|
pipes: make(map[uint32]io.WriteCloser),
|
|
|
|
notifyHashed: opt.NotifyHashed,
|
|
|
|
contentHasher: opt.ContentHasher,
|
|
|
|
progressCb: opt.ProgressCb,
|
|
|
|
merge: opt.Merge,
|
|
|
|
filter: opt.Filter,
|
2017-05-04 00:49:12 -04:00
|
|
|
}
|
|
|
|
return r.run(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
type receiver struct {
|
2017-09-21 20:54:27 -04:00
|
|
|
dest string
|
|
|
|
conn Stream
|
|
|
|
files map[string]uint32
|
|
|
|
pipes map[uint32]io.WriteCloser
|
|
|
|
mu sync.RWMutex
|
|
|
|
muPipes sync.RWMutex
|
|
|
|
progressCb func(int, bool)
|
|
|
|
merge bool
|
|
|
|
filter FilterFunc
|
2017-05-04 00:49:12 -04:00
|
|
|
|
|
|
|
notifyHashed ChangeFunc
|
2017-09-21 20:54:27 -04:00
|
|
|
contentHasher ContentHasher
|
2017-05-04 00:49:12 -04:00
|
|
|
orderValidator Validator
|
|
|
|
hlValidator Hardlinks
|
|
|
|
}
|
|
|
|
|
|
|
|
type dynamicWalker struct {
|
|
|
|
walkChan chan *currentPath
|
2018-06-02 12:46:53 -04:00
|
|
|
err error
|
|
|
|
closeCh chan struct{}
|
2017-05-04 00:49:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newDynamicWalker() *dynamicWalker {
|
|
|
|
return &dynamicWalker{
|
|
|
|
walkChan: make(chan *currentPath, 128),
|
2018-06-02 12:46:53 -04:00
|
|
|
closeCh: make(chan struct{}),
|
2017-05-04 00:49:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *dynamicWalker) update(p *currentPath) error {
|
2018-06-02 12:46:53 -04:00
|
|
|
select {
|
|
|
|
case <-w.closeCh:
|
|
|
|
return errors.Wrap(w.err, "walker is closed")
|
|
|
|
default:
|
2017-05-04 00:49:12 -04:00
|
|
|
}
|
|
|
|
if p == nil {
|
|
|
|
close(w.walkChan)
|
|
|
|
return nil
|
|
|
|
}
|
2018-06-02 12:46:53 -04:00
|
|
|
select {
|
|
|
|
case w.walkChan <- p:
|
|
|
|
return nil
|
|
|
|
case <-w.closeCh:
|
|
|
|
return errors.Wrap(w.err, "walker is closed")
|
|
|
|
}
|
2017-05-04 00:49:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *dynamicWalker) fill(ctx context.Context, pathC chan<- *currentPath) error {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case p, ok := <-w.walkChan:
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pathC <- p
|
|
|
|
case <-ctx.Done():
|
2018-06-02 12:46:53 -04:00
|
|
|
w.err = ctx.Err()
|
|
|
|
close(w.closeCh)
|
2017-05-04 00:49:12 -04:00
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *receiver) run(ctx context.Context) error {
|
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
dw, err := NewDiskWriter(ctx, r.dest, DiskWriterOpt{
|
2017-09-21 20:54:27 -04:00
|
|
|
AsyncDataCb: r.asyncDataFunc,
|
|
|
|
NotifyCb: r.notifyHashed,
|
|
|
|
ContentHasher: r.contentHasher,
|
|
|
|
Filter: r.filter,
|
2017-05-04 00:49:12 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w := newDynamicWalker()
|
|
|
|
|
2018-06-02 12:46:53 -04:00
|
|
|
g.Go(func() (retErr error) {
|
|
|
|
defer func() {
|
|
|
|
if retErr != nil {
|
2018-10-05 05:04:52 -04:00
|
|
|
r.conn.SendMsg(&types.Packet{Type: types.PACKET_ERR, Data: []byte(retErr.Error())})
|
2018-06-02 12:46:53 -04:00
|
|
|
}
|
|
|
|
}()
|
2017-09-21 20:54:27 -04:00
|
|
|
destWalker := emptyWalker
|
|
|
|
if !r.merge {
|
|
|
|
destWalker = GetWalkerFn(r.dest)
|
|
|
|
}
|
|
|
|
err := doubleWalkDiff(ctx, dw.HandleChange, destWalker, w.fill)
|
2017-05-04 00:49:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := dw.Wait(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-05 05:04:52 -04:00
|
|
|
r.conn.SendMsg(&types.Packet{Type: types.PACKET_FIN})
|
2017-05-04 00:49:12 -04:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
var i uint32 = 0
|
|
|
|
|
2017-09-21 20:54:27 -04:00
|
|
|
size := 0
|
|
|
|
if r.progressCb != nil {
|
|
|
|
defer func() {
|
|
|
|
r.progressCb(size, true)
|
|
|
|
}()
|
|
|
|
}
|
2018-10-05 05:04:52 -04:00
|
|
|
var p types.Packet
|
2017-05-04 00:49:12 -04:00
|
|
|
for {
|
2018-10-05 05:04:52 -04:00
|
|
|
p = types.Packet{Data: p.Data[:0]}
|
2017-05-04 00:49:12 -04:00
|
|
|
if err := r.conn.RecvMsg(&p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-21 20:54:27 -04:00
|
|
|
if r.progressCb != nil {
|
|
|
|
size += p.Size()
|
|
|
|
r.progressCb(size, false)
|
|
|
|
}
|
|
|
|
|
2017-05-04 00:49:12 -04:00
|
|
|
switch p.Type {
|
2018-10-05 05:04:52 -04:00
|
|
|
case types.PACKET_ERR:
|
2018-06-02 12:46:53 -04:00
|
|
|
return errors.Errorf("error from sender: %s", p.Data)
|
2018-10-05 05:04:52 -04:00
|
|
|
case types.PACKET_STAT:
|
2017-05-04 00:49:12 -04:00
|
|
|
if p.Stat == nil {
|
|
|
|
if err := w.update(nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if fileCanRequestData(os.FileMode(p.Stat.Mode)) {
|
|
|
|
r.mu.Lock()
|
|
|
|
r.files[p.Stat.Path] = i
|
|
|
|
r.mu.Unlock()
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
cp := ¤tPath{path: p.Stat.Path, f: &StatInfo{p.Stat}}
|
|
|
|
if err := r.orderValidator.HandleChange(ChangeKindAdd, cp.path, cp.f, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := r.hlValidator.HandleChange(ChangeKindAdd, cp.path, cp.f, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := w.update(cp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-05 05:04:52 -04:00
|
|
|
case types.PACKET_DATA:
|
2017-05-04 00:49:12 -04:00
|
|
|
r.muPipes.Lock()
|
|
|
|
pw, ok := r.pipes[p.ID]
|
|
|
|
r.muPipes.Unlock()
|
|
|
|
if !ok {
|
2018-10-05 05:04:52 -04:00
|
|
|
return errors.Errorf("invalid file request %d", p.ID)
|
2017-05-04 00:49:12 -04:00
|
|
|
}
|
|
|
|
if len(p.Data) == 0 {
|
|
|
|
if err := pw.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := pw.Write(p.Data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 05:04:52 -04:00
|
|
|
case types.PACKET_FIN:
|
2018-06-02 12:46:53 -04:00
|
|
|
for {
|
2018-10-05 05:04:52 -04:00
|
|
|
var p types.Packet
|
2018-06-02 12:46:53 -04:00
|
|
|
if err := r.conn.RecvMsg(&p); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-05-04 00:49:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return g.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *receiver) asyncDataFunc(ctx context.Context, p string, wc io.WriteCloser) error {
|
|
|
|
r.mu.Lock()
|
|
|
|
id, ok := r.files[p]
|
|
|
|
if !ok {
|
|
|
|
r.mu.Unlock()
|
|
|
|
return errors.Errorf("invalid file request %s", p)
|
|
|
|
}
|
|
|
|
delete(r.files, p)
|
|
|
|
r.mu.Unlock()
|
|
|
|
|
|
|
|
wwc := newWrappedWriteCloser(wc)
|
|
|
|
r.muPipes.Lock()
|
|
|
|
r.pipes[id] = wwc
|
|
|
|
r.muPipes.Unlock()
|
2018-10-05 05:04:52 -04:00
|
|
|
if err := r.conn.SendMsg(&types.Packet{Type: types.PACKET_REQ, ID: id}); err != nil {
|
2017-05-04 00:49:12 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err := wwc.Wait(ctx)
|
2018-06-02 12:46:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-04 00:49:12 -04:00
|
|
|
r.muPipes.Lock()
|
|
|
|
delete(r.pipes, id)
|
|
|
|
r.muPipes.Unlock()
|
2018-06-02 12:46:53 -04:00
|
|
|
return nil
|
2017-05-04 00:49:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type wrappedWriteCloser struct {
|
|
|
|
io.WriteCloser
|
|
|
|
err error
|
|
|
|
once sync.Once
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWrappedWriteCloser(wc io.WriteCloser) *wrappedWriteCloser {
|
|
|
|
return &wrappedWriteCloser{WriteCloser: wc, done: make(chan struct{})}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrappedWriteCloser) Close() error {
|
|
|
|
w.err = w.WriteCloser.Close()
|
|
|
|
w.once.Do(func() { close(w.done) })
|
|
|
|
return w.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrappedWriteCloser) Wait(ctx context.Context) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-w.done:
|
|
|
|
return w.err
|
|
|
|
}
|
|
|
|
}
|