forked from earthly/fsutil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend.go
More file actions
234 lines (209 loc) · 4.99 KB
/
send.go
File metadata and controls
234 lines (209 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package fsutil
import (
"context"
"io"
"os"
"path/filepath"
"sync"
"syscall"
"github.com/pkg/errors"
"github.com/tonistiigi/fsutil/types"
"golang.org/x/sync/errgroup"
)
var bufPool = sync.Pool{
New: func() interface{} {
buf := make([]byte, 32*1<<10)
return &buf
},
}
type Stream interface {
RecvMsg(interface{}) error
SendMsg(m interface{}) error
Context() context.Context
}
func Send(ctx context.Context, conn Stream, fs FS, progressCb func(int, bool), verboseProgressCb VerboseProgressCB) error {
s := &sender{
conn: &syncStream{Stream: conn},
fs: WithHardlinkReset(fs),
files: make(map[uint32]string),
progressCb: progressCb,
verboseProgressCb: verboseProgressCb,
sendpipeline: make(chan *sendHandle, 128),
}
return s.run(ctx)
}
type sendHandle struct {
id uint32
path string
}
type sender struct {
conn Stream
fs FS
files map[uint32]string
mu sync.RWMutex
progressCb func(int, bool)
verboseProgressCb VerboseProgressCB
progressCurrent int
progressCurrentMu sync.Mutex
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 {
err := s.walk(ctx)
if err != nil {
s.conn.SendMsg(&types.Packet{Type: types.PACKET_ERR, Data: []byte(err.Error())})
}
return err
})
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:
}
var p types.Packet
if err := s.conn.RecvMsg(&p); err != nil {
return err
}
switch p.Type {
case types.PACKET_ERR:
return errors.Errorf("error from receiver: %s", p.Data)
case types.PACKET_REQ:
if err := s.queue(p.ID); err != nil {
return err
}
case types.PACKET_FIN:
return s.conn.SendMsg(&types.Packet{Type: types.PACKET_FIN})
}
}
})
return g.Wait()
}
func (s *sender) updateProgress(size int, last bool) {
s.mu.Lock()
defer s.mu.Unlock()
s.progressCurrent += size
if s.progressCb != nil {
s.progressCurrentMu.Lock()
defer s.progressCurrentMu.Unlock()
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 {
f, err := s.fs.Open(h.path)
if err == nil {
defer f.Close()
buf := bufPool.Get().(*[]byte)
defer bufPool.Put(buf)
fs := fileSender{sender: s, id: h.id}
if _, err := io.CopyBuffer(&fs, struct{ io.Reader }{f}, *buf); err != nil {
return err
}
if s.verboseProgressCb != nil {
s.verboseProgressCb(h.path, StatusSent, fs.bytesWritten)
}
}
return s.conn.SendMsg(&types.Packet{ID: h.id, Type: types.PACKET_DATA})
}
func (s *sender) walk(ctx context.Context) error {
var i uint32 = 0
target := string(filepath.Separator)
err := s.fs.Walk(ctx, target, func(path string, entry os.DirEntry, err error) error {
if err != nil {
return err
}
fi, err := entry.Info()
if err != nil {
return err
}
stat, ok := fi.Sys().(*types.Stat)
if !ok {
return errors.WithStack(&os.PathError{Path: path, Err: syscall.EBADMSG, Op: "fileinfo without stat info"})
}
stat.Path = filepath.ToSlash(stat.Path)
stat.Linkname = filepath.ToSlash(stat.Linkname)
p := &types.Packet{
Type: types.PACKET_STAT,
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)
if s.verboseProgressCb != nil {
s.verboseProgressCb(path, StatusStat, p.Size())
}
return errors.Wrapf(s.conn.SendMsg(p), "failed to send stat %s", path)
})
if err != nil {
return err
}
return errors.Wrapf(s.conn.SendMsg(&types.Packet{Type: types.PACKET_STAT}), "failed to send last stat")
}
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
bytesWritten int
}
func (fs *fileSender) Write(dt []byte) (int, error) {
if len(dt) == 0 {
return 0, nil
}
p := &types.Packet{Type: types.PACKET_DATA, ID: fs.id, Data: dt}
if err := fs.sender.conn.SendMsg(p); err != nil {
return 0, err
}
fs.sender.updateProgress(p.Size(), false)
fs.bytesWritten += len(dt)
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
}