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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var bufPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return make([]byte, 32*1<<10)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
type Stream interface {
|
|
|
|
RecvMsg(interface{}) error
|
|
|
|
SendMsg(m interface{}) error
|
|
|
|
Context() context.Context
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:09:51 -04:00
|
|
|
func Send(ctx context.Context, conn Stream, fs FS, progressCb func(int, bool)) error {
|
2017-05-04 00:49:12 -04:00
|
|
|
s := &sender{
|
|
|
|
conn: &syncStream{Stream: conn},
|
2018-08-02 14:09:51 -04:00
|
|
|
fs: fs,
|
2017-05-04 00:49:12 -04:00
|
|
|
files: make(map[uint32]string),
|
|
|
|
progressCb: progressCb,
|
|
|
|
sendpipeline: make(chan *sendHandle, 128),
|
|
|
|
}
|
|
|
|
return s.run(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
type sendHandle struct {
|
|
|
|
id uint32
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
type sender struct {
|
|
|
|
conn Stream
|
2018-08-02 14:09:51 -04:00
|
|
|
fs FS
|
2017-05-04 00:49:12 -04:00
|
|
|
files map[uint32]string
|
|
|
|
mu sync.RWMutex
|
|
|
|
progressCb func(int, bool)
|
|
|
|
progressCurrent int
|
|
|
|
sendpipeline chan *sendHandle
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sender) run(ctx context.Context) error {
|
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
defer s.updateProgress(0, true)
|
|
|
|
|
|
|
|
g.Go(func() error {
|
2018-06-02 12:46:53 -04:00
|
|
|
err := s.walk(ctx)
|
|
|
|
if err != nil {
|
2018-10-05 05:04:52 -04:00
|
|
|
s.conn.SendMsg(&types.Packet{Type: types.PACKET_ERR, Data: []byte(err.Error())})
|
2018-06-02 12:46:53 -04:00
|
|
|
}
|
|
|
|
return err
|
2017-05-04 00:49:12 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
g.Go(func() error {
|
|
|
|
for h := range s.sendpipeline {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if err := s.sendFile(h); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
defer close(s.sendpipeline)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2018-10-05 05:04:52 -04:00
|
|
|
var p types.Packet
|
2017-05-04 00:49:12 -04:00
|
|
|
if err := s.conn.RecvMsg(&p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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 receiver: %s", p.Data)
|
2018-10-05 05:04:52 -04:00
|
|
|
case types.PACKET_REQ:
|
2017-05-04 00:49:12 -04:00
|
|
|
if err := s.queue(p.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-05 05:04:52 -04:00
|
|
|
case types.PACKET_FIN:
|
|
|
|
return s.conn.SendMsg(&types.Packet{Type: types.PACKET_FIN})
|
2017-05-04 00:49:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return g.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sender) updateProgress(size int, last bool) {
|
|
|
|
if s.progressCb != nil {
|
|
|
|
s.progressCurrent += size
|
|
|
|
s.progressCb(s.progressCurrent, last)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sender) queue(id uint32) error {
|
|
|
|
s.mu.Lock()
|
|
|
|
p, ok := s.files[id]
|
|
|
|
if !ok {
|
|
|
|
s.mu.Unlock()
|
|
|
|
return errors.Errorf("invalid file id %d", id)
|
|
|
|
}
|
|
|
|
delete(s.files, id)
|
|
|
|
s.mu.Unlock()
|
|
|
|
s.sendpipeline <- &sendHandle{id, p}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sender) sendFile(h *sendHandle) error {
|
2018-08-02 14:09:51 -04:00
|
|
|
f, err := s.fs.Open(h.path)
|
2017-05-04 00:49:12 -04:00
|
|
|
if err == nil {
|
2018-06-02 12:46:53 -04:00
|
|
|
defer f.Close()
|
2017-05-04 00:49:12 -04:00
|
|
|
buf := bufPool.Get().([]byte)
|
|
|
|
defer bufPool.Put(buf)
|
|
|
|
if _, err := io.CopyBuffer(&fileSender{sender: s, id: h.id}, f, buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 05:04:52 -04:00
|
|
|
return s.conn.SendMsg(&types.Packet{ID: h.id, Type: types.PACKET_DATA})
|
2017-05-04 00:49:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sender) walk(ctx context.Context) error {
|
|
|
|
var i uint32 = 0
|
2018-08-02 14:09:51 -04:00
|
|
|
err := s.fs.Walk(ctx, func(path string, fi os.FileInfo, err error) error {
|
2017-05-04 00:49:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-05 05:04:52 -04:00
|
|
|
stat, ok := fi.Sys().(*types.Stat)
|
2017-05-04 00:49:12 -04:00
|
|
|
if !ok {
|
|
|
|
return errors.Wrapf(err, "invalid fileinfo without stat info: %s", path)
|
|
|
|
}
|
|
|
|
|
2018-10-05 05:04:52 -04:00
|
|
|
p := &types.Packet{
|
|
|
|
Type: types.PACKET_STAT,
|
2017-05-04 00:49:12 -04:00
|
|
|
Stat: stat,
|
|
|
|
}
|
|
|
|
if fileCanRequestData(os.FileMode(stat.Mode)) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.files[i] = stat.Path
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
s.updateProgress(p.Size(), false)
|
|
|
|
return errors.Wrapf(s.conn.SendMsg(p), "failed to send stat %s", path)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-05 05:04:52 -04:00
|
|
|
return errors.Wrapf(s.conn.SendMsg(&types.Packet{Type: types.PACKET_STAT}), "failed to send last stat")
|
2017-05-04 00:49:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func fileCanRequestData(m os.FileMode) bool {
|
|
|
|
// avoid updating this function as it needs to match between sender/receiver.
|
|
|
|
// version if needed
|
|
|
|
return m&os.ModeType == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileSender struct {
|
|
|
|
sender *sender
|
|
|
|
id uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *fileSender) Write(dt []byte) (int, error) {
|
|
|
|
if len(dt) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2018-10-05 05:04:52 -04:00
|
|
|
p := &types.Packet{Type: types.PACKET_DATA, ID: fs.id, Data: dt}
|
2017-05-04 00:49:12 -04:00
|
|
|
if err := fs.sender.conn.SendMsg(p); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
fs.sender.updateProgress(p.Size(), false)
|
|
|
|
return len(dt), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type syncStream struct {
|
|
|
|
Stream
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *syncStream) SendMsg(m interface{}) error {
|
|
|
|
ss.mu.Lock()
|
|
|
|
err := ss.Stream.SendMsg(m)
|
|
|
|
ss.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|