/
push.go
387 lines (317 loc) · 9.04 KB
/
push.go
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
package stoclient
import (
"context"
"encoding/hex"
"errors"
"fmt"
"io"
"io/fs"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/djherbis/times"
"github.com/function61/gokit/ezhttp"
"github.com/function61/gokit/osutil"
"github.com/function61/varasto/pkg/fssnapshot"
"github.com/function61/varasto/pkg/stateresolver"
"github.com/function61/varasto/pkg/stotypes"
"github.com/function61/varasto/pkg/stoutils"
"github.com/minio/sha256-simd"
)
const (
BackgroundUploaderConcurrency = 3
)
func pushCurrentWorkdir(ctx context.Context) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
// take filesystem snapshot, so our reads within the file tree are atomic
snapshotter := fssnapshot.NullSnapshotter()
// snapshotter := fssnapshot.PlatformSpecificSnapshotter()
snapshot, err := snapshotter.Snapshot(cwd)
if err != nil {
return err
}
defer func() { // always release snapshot
osutil.ExitIfError(snapshotter.Release(*snapshot))
}()
// now read the workdir from within the snapshot (and not the actual cwd)
wd, err := NewWorkdirLocation(snapshot.OriginInSnapshotPath)
if err != nil {
return err
}
return push(ctx, wd)
}
func computeChangeset(ctx context.Context, wd *workdirLocation, bdl BlobDiscoveredListener) (*stotypes.CollectionChangeset, error) {
parentState, err := stateresolver.ComputeStateAtHead(wd.manifest.Collection)
if err != nil {
return nil, fmt.Errorf("ComputeStateAtHead: %w", err)
}
collectionId := wd.manifest.Collection.ID
// deleted during directory scan. what's left over is what are missing w.r.t. parent state
filesMissing := parentState.Files()
filesAtParent := parentState.Files() // this will not be mutated
created := []stotypes.File{}
updated := []stotypes.File{}
errWalk := filepath.Walk(wd.path, func(path string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err // stop if encountering Walk() errors
}
if fileInfo.Mode()&fs.ModeSymlink != 0 {
return fmt.Errorf("symlinks not yet supported: %s", path)
}
if !fileInfo.IsDir() { // don't handle directories
if fileInfo.Name() == LocalStatefile { // skip .varasto files
return nil
}
// this returns \ on Windows, but we'll need to normalize to slashes for interop
relativePath, errRel := filepath.Rel(wd.path, path)
if errRel != nil {
return errRel
}
relativePath = backslashesToForwardSlashes(relativePath)
// ok if key missing
delete(filesMissing, relativePath)
if before, existedBefore := filesAtParent[relativePath]; existedBefore {
definitelyChanged := before.Size != fileInfo.Size()
maybeChanged := !before.Modified.Equal(fileInfo.ModTime())
if definitelyChanged || maybeChanged {
fil, err := scanFileAndDiscoverBlobs(ctx, wd.Join(relativePath), relativePath, fileInfo, collectionId, bdl)
if err != nil {
return fmt.Errorf("scanFileAndDiscoverBlobs: %w", err)
}
// TODO: allow commit to change metadata like modification time or
// execute bit?
if before.Sha256 != fil.Sha256 {
updated = append(updated, *fil)
}
}
} else {
fil, err := scanFileAndDiscoverBlobs(ctx, wd.Join(relativePath), relativePath, fileInfo, collectionId, bdl)
if err != nil {
return fmt.Errorf("scanFileAndDiscoverBlobs: %w", err)
}
created = append(created, *fil)
}
}
return nil
})
if errWalk != nil {
return nil, fmt.Errorf("walk: %w", errWalk)
}
deleted := []string{}
for missing := range filesMissing {
deleted = append(deleted, missing)
}
sort.Strings(deleted)
ch := stotypes.NewChangeset(
stoutils.NewCollectionChangesetId(),
wd.manifest.ChangesetId, // parent
time.Now(),
created,
updated,
deleted)
return &ch, nil
}
// returns ErrChunkMetadataNotFound if blob is not found
func blobExists(blobRef stotypes.BlobRef, clientConfig ClientConfig) (bool, error) {
ctx, cancel := context.WithTimeout(context.TODO(), ezhttp.DefaultTimeout10s)
defer cancel()
// do a HEAD request to see if the blob exists
resp, err := ezhttp.Get(
ctx,
clientConfig.UrlBuilder().GetBlobMetadata(blobRef.AsHex()),
ezhttp.AuthBearer(clientConfig.AuthToken),
ezhttp.Client(clientConfig.HttpClient()))
if err != nil && resp != nil && resp.StatusCode == http.StatusNotFound {
return false, nil
}
if err != nil {
return false, err // an actual error
}
return true, nil
}
func scanFileAndDiscoverBlobs(
ctx context.Context,
absolutePath string,
relativePath string,
fileInfo os.FileInfo,
collectionId string,
bdl BlobDiscoveredListener,
) (*stotypes.File, error) {
file, err := os.Open(absolutePath)
if err != nil {
return nil, err
}
defer file.Close()
// https://unix.stackexchange.com/questions/2802/what-is-the-difference-between-modify-and-change-in-stat-command-context
allTimes := times.Get(fileInfo)
maybeCreationTime := fileInfo.ModTime()
if allTimes.HasBirthTime() {
maybeCreationTime = allTimes.BirthTime()
}
return ScanAndDiscoverBlobs(
ctx,
relativePath,
file,
fileInfo.Size(),
maybeCreationTime,
fileInfo.ModTime(),
collectionId,
bdl)
}
// - for when you don't have a file, but you have a stream
// - totalsize is used for progress calculation, but if you're not using progress UI you
// can set it to 0
func ScanAndDiscoverBlobs(
ctx context.Context,
relativePath string,
file io.Reader,
totalSize int64,
creationTime time.Time,
modifiedTime time.Time,
collectionId string,
bdl BlobDiscoveredListener,
) (*stotypes.File, error) {
bfile := &stotypes.File{
Path: relativePath,
Created: creationTime,
Modified: modifiedTime,
Size: 0, // computed later
Sha256: "", // computed later
BlobRefs: []string{}, // computed later
}
fullContentHash := sha256.New()
discoveryCancel := bdl.CancelCh()
for {
select {
case <-discoveryCancel:
return nil, errors.New("discovery canceled")
case <-ctx.Done():
return nil, ctx.Err()
default:
}
chunk, errRead := ioutil.ReadAll(io.LimitReader(file, stotypes.BlobSize))
if errRead != nil {
return nil, errRead
}
if len(chunk) == 0 {
// should only happen if file size is exact multiple of blobSize
break
}
bfile.Size += int64(len(chunk))
if _, err := fullContentHash.Write(chunk); err != nil {
return nil, err
}
chunkSha256Bytes := sha256.Sum256(chunk)
blobRef, err := stotypes.BlobRefFromHex(hex.EncodeToString(chunkSha256Bytes[:]))
if err != nil {
return nil, err
}
bfile.BlobRefs = append(bfile.BlobRefs, blobRef.AsHex())
bdl.BlobDiscovered(NewBlobDiscoveredAttrs(
*blobRef,
collectionId,
chunk,
stoutils.IsMaybeCompressible(relativePath),
bfile.Path,
totalSize))
}
bfile.Sha256 = fmt.Sprintf("%x", fullContentHash.Sum(nil))
return bfile, nil
}
func (c *Client) Commit(
ctx context.Context,
collectionId string,
changeset stotypes.CollectionChangeset,
) (*stotypes.Collection, error) {
ctx, cancel := context.WithTimeout(ctx, 60*3*time.Second)
defer cancel()
updatedCollection := &stotypes.Collection{}
if _, err := ezhttp.Post(
ctx,
c.conf.UrlBuilder().CommitChangeset(collectionId),
ezhttp.AuthBearer(c.conf.AuthToken),
ezhttp.SendJson(&changeset),
ezhttp.RespondsJson(&updatedCollection, false),
ezhttp.Client(c.conf.HttpClient()),
); err != nil {
return nil, fmt.Errorf("CommitChangeset: %v", err)
}
return updatedCollection, nil
}
func pushOne(ctx context.Context, collectionId string, path string) error {
clientConfig, err := ReadConfig()
if err != nil {
return err
}
client := clientConfig.Client()
coll, err := client.FetchCollectionMetadata(ctx, collectionId)
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}
absolutePath := filepath.Join(wd, path)
fileInfo, err := os.Stat(absolutePath)
if err != nil {
return err
}
buploader := NewBackgroundUploader(
ctx,
BackgroundUploaderConcurrency,
*clientConfig,
textUiUploadProgressOutputIfInTerminal())
file, err := scanFileAndDiscoverBlobs(ctx, absolutePath, path, fileInfo, collectionId, buploader)
if err != nil {
return err
}
if err := buploader.WaitFinished(); err != nil {
return err
}
changeset := stotypes.NewChangeset(
stoutils.NewCollectionChangesetId(),
coll.Head,
time.Now(),
[]stotypes.File{*file},
[]stotypes.File{},
[]string{})
_, err = client.Commit(ctx, coll.ID, changeset)
return err
}
func push(ctx context.Context, wd *workdirLocation) error {
buploader := NewBackgroundUploader(
ctx,
BackgroundUploaderConcurrency,
wd.clientConfig,
textUiUploadProgressOutputIfInTerminal())
ch, err := computeChangeset(ctx, wd, buploader)
if err != nil {
return err
}
if !ch.AnyChanges() {
log.Println("No files changed")
return nil
}
if err := buploader.WaitFinished(); err != nil {
return err
}
updatedCollection, err := wd.clientConfig.Client().Commit(ctx, wd.manifest.Collection.ID, *ch)
if err != nil {
return err
}
wd.manifest.ChangesetId = updatedCollection.Head
wd.manifest.Collection = *updatedCollection
return wd.SaveToDisk()
}
func backslashesToForwardSlashes(in string) string {
return strings.ReplaceAll(in, `\`, "/")
}