-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_cp_chunked.go
More file actions
437 lines (408 loc) · 12.1 KB
/
Copy pathclient_cp_chunked.go
File metadata and controls
437 lines (408 loc) · 12.1 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package slicer
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
pathpkg "path"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
var ErrChunkedCopyUnsupported = errors.New("chunked copy is not supported by the guest agent")
const slicerAgentPath = "/usr/local/bin/slicer-agent"
type copyChunkUpload struct {
index int
offset int64
}
type preparedCopySource struct {
file *os.File
size int64
unpackedSize int64
cleanup func() error
}
type byteCounter struct {
n int64
}
func (c *byteCounter) Write(p []byte) (int, error) {
c.n += int64(len(p))
return len(p), nil
}
// SupportsChunkedCopy reports whether the guest agent provides the upload
// finaliser required by CpToVMChunked.
func (c *SlicerClient) SupportsChunkedCopy(ctx context.Context, vmName string) (bool, error) {
version, supported, err := c.chunkedCopyManifestVersion(ctx, vmName)
return supported && version >= ChunkedCopyManifestVersion, err
}
func (c *SlicerClient) chunkedCopyManifestVersion(ctx context.Context, vmName string) (int, bool, error) {
result, err := c.ExecBuffered(ctx, vmName, SlicerExecRequest{
Command: slicerAgentPath,
Args: []string{"upload", "check"},
UID: 0,
GID: 0,
Stdout: true,
Stderr: true,
})
if err != nil {
return 0, false, err
}
if result.ExitCode != 0 {
return 0, false, nil
}
for _, field := range strings.Fields(result.Stdout) {
if field == "chunked-copy-v2" {
return ChunkedCopyManifestV2, true, nil
}
}
return ChunkedCopyManifestVersion, true, nil
}
// CpToVMChunked copies a file or staged tar stream in bounded, checksummed
// requests, then asks slicer-agent to validate and assemble
// the manifest inside the guest.
func (c *SlicerClient) CpToVMChunked(ctx context.Context, vmName, localPath, vmPath string, opts ChunkedCopyOptions) error {
if opts.ChunkSize == 0 {
opts.ChunkSize = DefaultCopyChunkSize
}
if opts.Concurrency == 0 {
opts.Concurrency = DefaultCopyConcurrency
}
if opts.ChunkSize < 1 || opts.ChunkSize > 64<<20 {
return fmt.Errorf("chunk size must be between 1 byte and 64 MiB")
}
if opts.Concurrency < 1 || opts.Concurrency > 16 {
return fmt.Errorf("copy concurrency must be between 1 and 16")
}
if opts.Mode != "binary" && opts.Mode != "tar" {
return fmt.Errorf("invalid mode: %s", opts.Mode)
}
manifestVersion, supported, err := c.chunkedCopyManifestVersion(ctx, vmName)
if err != nil {
return fmt.Errorf("check guest chunked-copy support: %w", err)
}
if !supported {
return ErrChunkedCopyUnsupported
}
absSrc, err := filepath.Abs(localPath)
if err != nil {
return fmt.Errorf("get absolute source path: %w", err)
}
metadata, err := localCopySourceMetadata(localPath, absSrc, opts.Mode)
if err != nil {
return err
}
destination := vmPath
if manifestVersion < ChunkedCopyManifestV2 {
destination, err = resolveLegacyUploadDestination(ctx, c, vmName, vmPath, metadata, opts.UID)
if err != nil {
return err
}
}
sessionID, err := newCopySessionID()
if err != nil {
return err
}
sessionPath, err := copySessionPath(destination, sessionID)
if err != nil {
return err
}
manifestPath := pathpkg.Join(sessionPath, "manifest.json")
source, err := prepareChunkedCopySource(ctx, absSrc, opts)
if err != nil {
return err
}
defer source.cleanup()
complete := false
defer func() {
if complete {
return
}
cleanupCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = c.abortChunkedCopy(cleanupCtx, vmName, manifestPath, opts.UID, opts.GID)
}()
manifest := CopyManifest{
Version: manifestVersion,
Mode: opts.Mode,
Destination: destination,
UID: opts.UID,
GID: opts.GID,
Permissions: opts.Permissions,
UnpackedSize: source.unpackedSize,
Chunks: []CopyChunk{},
}
if manifestVersion >= ChunkedCopyManifestV2 {
manifest.CopySemantics = cpCopySemanticsV1
manifest.SourceName = metadata.name
manifest.SourceType = metadata.typeName
manifest.CopyContents = metadata.copyContents
}
for offset := int64(0); offset < source.size; offset += int64(opts.ChunkSize) {
size := int64(opts.ChunkSize)
if remaining := source.size - offset; remaining < size {
size = remaining
}
manifest.Chunks = append(manifest.Chunks, CopyChunk{
Index: len(manifest.Chunks),
Size: size,
})
}
manifest.Size = source.size
if err := c.uploadCopyChunks(ctx, vmName, sessionPath, opts, source.file, &manifest); err != nil {
return err
}
if err := manifest.Validate(); err != nil {
return fmt.Errorf("validate generated copy manifest: %w", err)
}
manifestData, err := json.Marshal(manifest)
if err != nil {
return fmt.Errorf("encode copy manifest: %w", err)
}
if err := c.uploadCopyBytes(ctx, vmName, manifestPath, opts.UID, opts.GID, manifestData); err != nil {
return fmt.Errorf("upload copy manifest: %w", err)
}
result, err := c.ExecBuffered(ctx, vmName, SlicerExecRequest{
Command: slicerAgentPath,
Args: []string{
"upload", "finalise",
"--manifest", manifestPath,
"--uid", strconv.FormatUint(uint64(opts.UID), 10),
"--gid", strconv.FormatUint(uint64(opts.GID), 10),
},
UID: 0,
GID: 0,
Stdout: true,
Stderr: true,
})
if err != nil {
return fmt.Errorf("finalise chunked copy: %w", err)
}
if result.ExitCode != 0 {
message := strings.TrimSpace(result.Stderr)
if message == "" {
message = strings.TrimSpace(result.Error)
}
return fmt.Errorf("finalise chunked copy: exit code %d: %s", result.ExitCode, message)
}
complete = true
return nil
}
func (c *SlicerClient) uploadCopyChunks(ctx context.Context, vmName, sessionPath string, opts ChunkedCopyOptions, source *os.File, manifest *CopyManifest) error {
uploadCtx, cancel := context.WithCancel(ctx)
defer cancel()
jobs := make(chan copyChunkUpload)
var wg sync.WaitGroup
var firstErr error
var errOnce sync.Once
workerCount := opts.Concurrency
if len(manifest.Chunks) < workerCount {
workerCount = len(manifest.Chunks)
}
for i := 0; i < workerCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for job := range jobs {
chunk := &manifest.Chunks[job.index]
section := io.NewSectionReader(source, job.offset, chunk.Size)
hasher := sha256.New()
counter := &byteCounter{}
reader := io.TeeReader(section, io.MultiWriter(hasher, counter))
chunkPath := pathpkg.Join(sessionPath, "chunks", CopyChunkFileName(*chunk))
err := c.uploadCopyReader(uploadCtx, vmName, chunkPath, opts.UID, opts.GID, reader, chunk.Size)
if err == nil && counter.n != chunk.Size {
err = fmt.Errorf("streamed %d bytes, expected %d", counter.n, chunk.Size)
}
if err != nil {
errOnce.Do(func() {
firstErr = fmt.Errorf("upload chunk %d: %w", chunk.Index, err)
cancel()
})
continue
}
chunk.SHA256 = hex.EncodeToString(hasher.Sum(nil))
}
}()
}
offset := int64(0)
for i := range manifest.Chunks {
select {
case jobs <- copyChunkUpload{index: i, offset: offset}:
offset += manifest.Chunks[i].Size
case <-uploadCtx.Done():
close(jobs)
wg.Wait()
if firstErr != nil {
return firstErr
}
return uploadCtx.Err()
}
}
close(jobs)
wg.Wait()
if firstErr == nil && ctx.Err() != nil {
return ctx.Err()
}
return firstErr
}
func (c *SlicerClient) uploadCopyBytes(ctx context.Context, vmName, vmPath string, uid, gid uint32, data []byte) error {
return c.uploadCopyReader(ctx, vmName, vmPath, uid, gid, bytes.NewReader(data), int64(len(data)))
}
func (c *SlicerClient) uploadCopyReader(ctx context.Context, vmName, vmPath string, uid, gid uint32, reader io.Reader, size int64) error {
u, err := url.Parse(c.baseURL)
if err != nil {
return fmt.Errorf("parse Slicer URL: %w", err)
}
u.Path = fmt.Sprintf("/vm/%s/cp", vmName)
q := url.Values{}
q.Set("path", vmPath)
q.Set("mode", "binary")
q.Set("permissions", "0600")
if uid != NonRootUser {
q.Set("uid", strconv.FormatUint(uint64(uid), 10))
}
if gid != NonRootUser {
q.Set("gid", strconv.FormatUint(uint64(gid), 10))
}
u.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), reader)
if err != nil {
return fmt.Errorf("create chunk request: %w", err)
}
req.Header.Set("Content-Type", "application/octet-stream")
req.ContentLength = size
c.setAuthHeaders(req)
res, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("perform chunk request: %w", err)
}
if res.Body != nil {
defer func() {
_, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close()
}()
}
if res.StatusCode != http.StatusOK {
var body []byte
if res.Body != nil {
body, _ = io.ReadAll(io.LimitReader(res.Body, 64<<10))
}
return fmt.Errorf("copy chunk to VM: %s: %s", res.Status, strings.TrimSpace(string(body)))
}
return nil
}
func (c *SlicerClient) abortChunkedCopy(ctx context.Context, vmName, manifestPath string, uid, gid uint32) error {
result, err := c.ExecBuffered(ctx, vmName, SlicerExecRequest{
Command: slicerAgentPath,
Args: []string{
"upload", "abort",
"--manifest", manifestPath,
"--uid", strconv.FormatUint(uint64(uid), 10),
"--gid", strconv.FormatUint(uint64(gid), 10),
},
UID: 0,
GID: 0,
Stdout: true,
Stderr: true,
})
if err != nil {
return err
}
if result.ExitCode != 0 {
return fmt.Errorf("abort chunked copy: exit code %d: %s", result.ExitCode, strings.TrimSpace(result.Stderr))
}
return nil
}
func prepareChunkedCopySource(ctx context.Context, absSrc string, opts ChunkedCopyOptions) (preparedCopySource, error) {
if opts.Mode == "binary" {
f, err := os.Open(absSrc)
if err != nil {
return preparedCopySource{}, fmt.Errorf("open binary source: %w", err)
}
info, err := f.Stat()
if err != nil {
_ = f.Close()
return preparedCopySource{}, fmt.Errorf("stat binary source: %w", err)
}
if !info.Mode().IsRegular() {
_ = f.Close()
return preparedCopySource{}, fmt.Errorf("binary source must be a regular file: %s", absSrc)
}
return preparedCopySource{
file: f,
size: info.Size(),
cleanup: func() error {
return f.Close()
},
}, nil
}
parentDir := filepath.Dir(absSrc)
baseName := filepath.Base(absSrc)
unpackedSize, err := EstimateTarUnpackedSize(ctx, parentDir, baseName, opts.ExcludePatterns...)
if err != nil {
return preparedCopySource{}, fmt.Errorf("estimate tar size: %w", err)
}
// Keep the staged archive outside the source tree. Staging beside the
// source makes a root/contents copy capable of archiving its own growing
// tar file, and unnecessarily requires the source parent to be writable.
staged, err := os.CreateTemp("", ".slicer-upload-*.tar")
if err != nil {
return preparedCopySource{}, fmt.Errorf("create staged tar: %w", err)
}
removeStaged := func() {
_ = staged.Close()
_ = os.Remove(staged.Name())
}
if err := StreamTarArchive(ctx, staged, parentDir, baseName, opts.ExcludePatterns...); err != nil {
removeStaged()
return preparedCopySource{}, fmt.Errorf("create staged tar: %w", err)
}
info, err := staged.Stat()
if err != nil {
removeStaged()
return preparedCopySource{}, fmt.Errorf("stat staged tar: %w", err)
}
return preparedCopySource{
file: staged,
size: info.Size(),
unpackedSize: unpackedSize,
cleanup: func() error {
closeErr := staged.Close()
removeErr := os.Remove(staged.Name())
if closeErr != nil {
return closeErr
}
return removeErr
},
}, nil
}
func newCopySessionID() (string, error) {
data := make([]byte, 16)
if _, err := rand.Read(data); err != nil {
return "", fmt.Errorf("generate copy session ID: %w", err)
}
return hex.EncodeToString(data), nil
}
func copySessionPath(vmPath, sessionID string) (string, error) {
clean := pathpkg.Clean(strings.TrimSpace(vmPath))
if clean == "." || clean == "/" || clean == "" {
return "", fmt.Errorf("chunked copy requires a named destination path")
}
parent := pathpkg.Dir(clean)
if clean == "~" {
parent = "~"
}
if parent == "." && !strings.HasPrefix(clean, "/") && !strings.HasPrefix(clean, "~") {
return "", fmt.Errorf("chunked copy destination must be absolute or home-relative: %s", vmPath)
}
return pathpkg.Join(parent, ".slicer-upload-"+sessionID), nil
}