forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxn_writer.go
572 lines (494 loc) · 15.9 KB
/
txn_writer.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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dep
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/sdboyer/gps"
)
// SafeWriter transactionalizes writes of manifest, lock, and vendor dir, both
// individually and in any combination, into a pseudo-atomic action with
// transactional rollback.
//
// It is not impervious to errors (writing to disk is hard), but it should
// guard against non-arcane failure conditions.
type SafeWriter struct {
Payload *SafeWriterPayload
}
// SafeWriterPayload represents the actions SafeWriter will execute when SafeWriter.Write is called.
type SafeWriterPayload struct {
Manifest *Manifest
Lock *Lock
LockDiff *LockDiff
ForceWriteVendor bool
}
func (payload *SafeWriterPayload) HasLock() bool {
return payload.Lock != nil
}
func (payload *SafeWriterPayload) HasManifest() bool {
return payload.Manifest != nil
}
func (payload *SafeWriterPayload) HasVendor() bool {
// TODO(carolynvs) this can be calculated based on if we are writing the lock
// init -> switch to newlock
// ensure checks existence, why not move that into the prep?
return payload.ForceWriteVendor
}
// LockDiff is the set of differences between an existing lock file and an updated lock file.
// Fields are only populated when there is a difference, otherwise they are empty.
// TODO(carolynvs) this should be moved to gps
type LockDiff struct {
HashDiff *StringDiff
Add []LockedProjectDiff
Remove []LockedProjectDiff
Modify []LockedProjectDiff
}
func (diff *LockDiff) Format() (string, error) {
if diff == nil {
return "", nil
}
var buf bytes.Buffer
if len(diff.Add) > 0 {
buf.WriteString("Add: ")
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)
err := enc.Encode(diff.Add)
if err != nil {
return "", errors.Wrap(err, "Unable to format LockDiff.Add")
}
}
if len(diff.Remove) > 0 {
buf.WriteString("Remove: ")
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)
err := enc.Encode(diff.Remove)
if err != nil {
return "", errors.Wrap(err, "Unable to format LockDiff.Remove")
}
}
if len(diff.Modify) > 0 {
buf.WriteString("Modify: ")
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)
err := enc.Encode(diff.Modify)
if err != nil {
return "", errors.Wrap(err, "Unable to format LockDiff.Modify")
}
}
return buf.String(), nil
}
// LockedProjectDiff contains the before and after snapshot of a project reference.
// Fields are only populated when there is a difference, otherwise they are empty.
// TODO(carolynvs) this should be moved to gps
type LockedProjectDiff struct {
Name gps.ProjectRoot `json:"name"`
Source *StringDiff `json:"source,omitempty"`
Version *StringDiff `json:"version,omitempty"`
Branch *StringDiff `json:"branch,omitempty"`
Revision *StringDiff `json:"revision,omitempty"`
Packages []StringDiff `json:"packages,omitempty"`
}
type StringDiff struct {
Previous string
Current string
}
func (diff StringDiff) MarshalJSON() ([]byte, error) {
var value string
if diff.Previous == "" && diff.Current != "" {
value = fmt.Sprintf("+ %s", diff.Current)
} else if diff.Previous != "" && diff.Current == "" {
value = fmt.Sprintf("- %s", diff.Previous)
} else if diff.Previous != diff.Current {
value = fmt.Sprintf("%s -> %s", diff.Previous, diff.Current)
} else {
value = diff.Current
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
err := enc.Encode(value)
return buf.Bytes(), err
}
// Prepare to write a set of config yaml, lock and vendor tree.
//
// - If manifest is provided, it will be written to the standard manifest file
// name beneath root.
// - If lock is provided it will be written to the standard
// lock file name in the root dir, but vendor will NOT be written
// - If lock and newLock are both provided and are equivalent, then neither lock
// nor vendor will be written
// - If lock and newLock are both provided and are not equivalent,
// the newLock will be written to the same location as above, and a vendor
// tree will be written to the vendor directory
// - If newLock is provided and lock is not, it will write both a lock
// and the vendor directory in the same way
// - If the forceVendor param is true, then vendor/ will be unconditionally
// written out based on newLock if present, else lock, else error.
func (sw *SafeWriter) Prepare(manifest *Manifest, lock *Lock, newLock *Lock, forceVendor bool) {
sw.Payload = &SafeWriterPayload{
Manifest: manifest,
ForceWriteVendor: forceVendor,
}
if newLock != nil {
if lock == nil {
sw.Payload.Lock = newLock
sw.Payload.ForceWriteVendor = true
} else {
diff := diffLocks(lock, newLock)
if diff != nil {
sw.Payload.Lock = newLock
sw.Payload.LockDiff = diff
sw.Payload.ForceWriteVendor = true
}
}
} else if lock != nil {
sw.Payload.Lock = lock
}
}
func (payload SafeWriterPayload) validate(root string, sm gps.SourceManager) error {
if root == "" {
return errors.New("root path must be non-empty")
}
if is, err := IsDir(root); !is {
if err != nil {
return err
}
return fmt.Errorf("root path %q does not exist", root)
}
if payload.HasVendor() && sm == nil {
return errors.New("must provide a SourceManager if writing out a vendor dir")
}
if payload.HasVendor() && payload.Lock == nil {
return errors.New("must provide a lock in order to write out vendor")
}
return nil
}
// Write saves some combination of config yaml, lock, and a vendor tree.
// root is the absolute path of root dir in which to write.
// sm is only required if vendor is being written.
//
// It first writes to a temp dir, then moves them in place if and only if all the write
// operations succeeded. It also does its best to roll back if any moves fail.
// This mostly guarantees that dep cannot exit with a partial write that would
// leave an undefined state on disk.
func (sw *SafeWriter) Write(root string, sm gps.SourceManager) error {
if sw.Payload == nil {
return errors.New("Cannot call SafeWriter.Write before SafeWriter.Prepare")
}
err := sw.Payload.validate(root, sm)
if err != nil {
return err
}
if !sw.Payload.HasManifest() && !sw.Payload.HasLock() && !sw.Payload.HasVendor() {
// nothing to do
return nil
}
mpath := filepath.Join(root, ManifestName)
lpath := filepath.Join(root, LockName)
vpath := filepath.Join(root, "vendor")
td, err := ioutil.TempDir(os.TempDir(), "dep")
if err != nil {
return errors.Wrap(err, "error while creating temp dir for writing manifest/lock/vendor")
}
defer os.RemoveAll(td)
if sw.Payload.HasManifest() {
if err := writeFile(filepath.Join(td, ManifestName), sw.Payload.Manifest); err != nil {
return errors.Wrap(err, "failed to write manifest file to temp dir")
}
}
if sw.Payload.HasLock() {
if err := writeFile(filepath.Join(td, LockName), sw.Payload.Lock); err != nil {
return errors.Wrap(err, "failed to write lock file to temp dir")
}
}
if sw.Payload.HasVendor() {
err = gps.WriteDepTree(filepath.Join(td, "vendor"), sw.Payload.Lock, sm, true)
if err != nil {
return errors.Wrap(err, "error while writing out vendor tree")
}
}
// Move the existing files and dirs to the temp dir while we put the new
// ones in, to provide insurance against errors for as long as possible.
type pathpair struct {
from, to string
}
var restore []pathpair
var failerr error
var vendorbak string
if sw.Payload.HasManifest() {
if _, err := os.Stat(mpath); err == nil {
// Move out the old one.
tmploc := filepath.Join(td, ManifestName+".orig")
failerr = renameWithFallback(mpath, tmploc)
if failerr != nil {
goto fail
}
restore = append(restore, pathpair{from: tmploc, to: mpath})
}
// Move in the new one.
failerr = renameWithFallback(filepath.Join(td, ManifestName), mpath)
if failerr != nil {
goto fail
}
}
if sw.Payload.HasLock() {
if _, err := os.Stat(lpath); err == nil {
// Move out the old one.
tmploc := filepath.Join(td, LockName+".orig")
failerr = renameWithFallback(lpath, tmploc)
if failerr != nil {
goto fail
}
restore = append(restore, pathpair{from: tmploc, to: lpath})
}
// Move in the new one.
failerr = renameWithFallback(filepath.Join(td, LockName), lpath)
if failerr != nil {
goto fail
}
}
if sw.Payload.HasVendor() {
if _, err := os.Stat(vpath); err == nil {
// Move out the old vendor dir. just do it into an adjacent dir, to
// try to mitigate the possibility of a pointless cross-filesystem
// move with a temp directory.
vendorbak = vpath + ".orig"
if _, err := os.Stat(vendorbak); err == nil {
// If the adjacent dir already exists, bite the bullet and move
// to a proper tempdir.
vendorbak = filepath.Join(td, "vendor.orig")
}
failerr = renameWithFallback(vpath, vendorbak)
if failerr != nil {
goto fail
}
restore = append(restore, pathpair{from: vendorbak, to: vpath})
}
// Move in the new one.
failerr = renameWithFallback(filepath.Join(td, "vendor"), vpath)
if failerr != nil {
goto fail
}
}
// Renames all went smoothly. The deferred os.RemoveAll will get the temp
// dir, but if we wrote vendor, we have to clean that up directly
if sw.Payload.HasVendor() {
// Nothing we can really do about an error at this point, so ignore it
os.RemoveAll(vendorbak)
}
return nil
fail:
// If we failed at any point, move all the things back into place, then bail.
for _, pair := range restore {
// Nothing we can do on err here, as we're already in recovery mode.
renameWithFallback(pair.from, pair.to)
}
return failerr
}
func (sw *SafeWriter) PrintPreparedActions() error {
if sw.Payload.HasManifest() {
fmt.Println("Would have written the following manifest.json:")
m, err := sw.Payload.Manifest.MarshalJSON()
if err != nil {
return errors.Wrap(err, "ensure DryRun cannot serialize manifest")
}
fmt.Println(string(m))
}
if sw.Payload.HasLock() {
fmt.Println("Would have written the following changes to lock.json:")
diff, err := sw.Payload.LockDiff.Format()
if err != nil {
return errors.Wrap(err, "ensure DryRun cannot serialize the lock diff")
}
fmt.Println(diff)
}
if sw.Payload.HasVendor() {
fmt.Println("Would have written the following projects to the vendor directory:")
for _, project := range sw.Payload.Lock.Projects() {
prj := project.Ident()
rev, _, _ := getVersionInfo(project.Version())
if prj.Source == "" {
fmt.Printf("%s@%s\n", prj.ProjectRoot, rev)
} else {
fmt.Printf("%s -> %s@%s\n", prj.ProjectRoot, prj.Source, rev)
}
}
}
return nil
}
// diffLocks compares two locks and identifies the differences between them.
// Returns nil if there are no differences.
// TODO(carolynvs) this should be moved to gps
func diffLocks(l1 gps.Lock, l2 gps.Lock) *LockDiff {
// Default nil locks to empty locks, so that we can still generate a diff
if l1 == nil {
l1 = &gps.SimpleLock{}
}
if l2 == nil {
l2 = &gps.SimpleLock{}
}
p1, p2 := l1.Projects(), l2.Projects()
// Check if the slices are sorted already. If they are, we can compare
// without copying. Otherwise, we have to copy to avoid altering the
// original input.
sp1, sp2 := SortedLockedProjects(p1), SortedLockedProjects(p2)
if len(p1) > 1 && !sort.IsSorted(sp1) {
p1 = make([]gps.LockedProject, len(p1))
copy(p1, l1.Projects())
sort.Sort(SortedLockedProjects(p1))
}
if len(p2) > 1 && !sort.IsSorted(sp2) {
p2 = make([]gps.LockedProject, len(p2))
copy(p2, l2.Projects())
sort.Sort(SortedLockedProjects(p2))
}
diff := LockDiff{}
h1 := l1.InputHash()
h2 := l2.InputHash()
if !bytes.Equal(h1, h2) {
diff.HashDiff = &StringDiff{Previous: string(h1), Current: string(h2)}
}
var i2next int
for i1 := 0; i1 < len(p1); i1++ {
lp1 := p1[i1]
pr1 := lp1.Ident().ProjectRoot
var matched bool
for i2 := i2next; i2 < len(p2); i2++ {
lp2 := p2[i2]
pr2 := lp2.Ident().ProjectRoot
switch strings.Compare(string(pr1), string(pr2)) {
case 0: // Found a matching project
matched = true
pdiff := diffProjects(lp1, lp2)
if pdiff != nil {
diff.Modify = append(diff.Modify, *pdiff)
}
i2next = i2 + 1 // Don't evaluate to this again
case -1: // Found a new project
add := buildLockedProjectDiff(lp2)
diff.Add = append(diff.Add, add)
i2next = i2 + 1 // Don't evaluate to this again
continue // Keep looking for a matching project
case +1: // Project has been removed, handled below
break
}
break // Done evaluating this project, move onto the next
}
if !matched {
remove := buildLockedProjectDiff(lp1)
diff.Remove = append(diff.Remove, remove)
}
}
// Anything that still hasn't been evaluated are adds
for i2 := i2next; i2 < len(p2); i2++ {
lp2 := p2[i2]
add := buildLockedProjectDiff(lp2)
diff.Add = append(diff.Add, add)
}
if diff.HashDiff == nil && len(diff.Add) == 0 && len(diff.Remove) == 0 && len(diff.Modify) == 0 {
return nil // The locks are the equivalent
}
return &diff
}
func buildLockedProjectDiff(lp gps.LockedProject) LockedProjectDiff {
r2, b2, v2 := getVersionInfo(lp.Version())
var rev, version, branch *StringDiff
if r2 != "" {
rev = &StringDiff{Previous: r2, Current: r2}
}
if b2 != "" {
branch = &StringDiff{Previous: b2, Current: b2}
}
if v2 != "" {
version = &StringDiff{Previous: v2, Current: v2}
}
add := LockedProjectDiff{
Name: lp.Ident().ProjectRoot,
Revision: rev,
Version: version,
Branch: branch,
Packages: make([]StringDiff, len(lp.Packages())),
}
for i, pkg := range lp.Packages() {
add.Packages[i] = StringDiff{Previous: pkg, Current: pkg}
}
return add
}
// diffProjects compares two projects and identifies the differences between them.
// Returns nil if there are no differences
// TODO(carolynvs) this should be moved to gps and updated once the gps unexported fields are available to use.
func diffProjects(lp1 gps.LockedProject, lp2 gps.LockedProject) *LockedProjectDiff {
diff := LockedProjectDiff{Name: lp1.Ident().ProjectRoot}
s1 := lp1.Ident().Source
s2 := lp2.Ident().Source
if s1 != s2 {
diff.Source = &StringDiff{Previous: s1, Current: s2}
}
r1, b1, v1 := getVersionInfo(lp1.Version())
r2, b2, v2 := getVersionInfo(lp2.Version())
if r1 != r2 {
diff.Revision = &StringDiff{Previous: r1, Current: r2}
}
if b1 != b2 {
diff.Branch = &StringDiff{Previous: b1, Current: b2}
}
if v1 != v2 {
diff.Version = &StringDiff{Previous: v1, Current: v2}
}
p1 := lp1.Packages()
p2 := lp2.Packages()
if !sort.StringsAreSorted(p1) {
p1 = make([]string, len(p1))
copy(p1, lp1.Packages())
sort.Strings(p1)
}
if !sort.StringsAreSorted(p2) {
p2 = make([]string, len(p2))
copy(p2, lp2.Packages())
sort.Strings(p2)
}
var i2next int
for i1 := 0; i1 < len(p1); i1++ {
pkg1 := p1[i1]
var matched bool
for i2 := i2next; i2 < len(p2); i2++ {
pkg2 := p2[i2]
switch strings.Compare(pkg1, pkg2) {
case 0: // Found matching package
matched = true
i2next = i2 + 1 // Don't evaluate to this again
case +1: // Found a new package
add := StringDiff{Current: pkg2}
diff.Packages = append(diff.Packages, add)
i2next = i2 + 1 // Don't evaluate to this again
continue // Keep looking for a match
case -1: // Package has been removed (handled below)
}
break // Done evaluating this package, move onto the next
}
if !matched {
diff.Packages = append(diff.Packages, StringDiff{Previous: pkg1})
}
}
// Anything that still hasn't been evaluated are adds
for i2 := i2next; i2 < len(p2); i2++ {
pkg2 := p2[i2]
add := StringDiff{Current: pkg2}
diff.Packages = append(diff.Packages, add)
}
if diff.Source == nil && diff.Version == nil && diff.Revision == nil && len(diff.Packages) == 0 {
return nil // The projects are equivalent
}
return &diff
}