forked from itchio/butler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply.go
107 lines (88 loc) · 2.59 KB
/
apply.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
package main
import (
"fmt"
"path"
"time"
"github.com/dustin/go-humanize"
"github.com/go-errors/errors"
"github.com/itchio/butler/comm"
"github.com/itchio/wharf/eos"
"github.com/itchio/wharf/pwr"
)
func apply(patch string, target string, output string, inplace bool, signaturePath string, woundsPath string) {
must(doApply(patch, target, output, inplace, signaturePath, woundsPath))
}
func doApply(patch string, target string, output string, inplace bool, signaturePath string, woundsPath string) error {
if output == "" {
output = target
}
target = path.Clean(target)
output = path.Clean(output)
if output == target {
if !inplace {
comm.Dief("Refusing to destructively patch %s without --inplace", output)
}
}
if signaturePath == "" {
comm.Opf("Patching %s", output)
} else {
comm.Opf("Patching %s with validation", output)
}
startTime := time.Now()
patchReader, err := eos.Open(patch)
if err != nil {
return errors.Wrap(err, 1)
}
var signature *pwr.SignatureInfo
if signaturePath != "" {
sigReader, sigErr := eos.Open(signaturePath)
if sigErr != nil {
return errors.Wrap(sigErr, 1)
}
defer sigReader.Close()
signature, sigErr = pwr.ReadSignature(sigReader)
if sigErr != nil {
return errors.Wrap(sigErr, 1)
}
}
actx := &pwr.ApplyContext{
TargetPath: target,
OutputPath: output,
InPlace: inplace,
Signature: signature,
WoundsPath: woundsPath,
Consumer: comm.NewStateConsumer(),
}
comm.StartProgress()
err = actx.ApplyPatch(patchReader)
if err != nil {
return errors.Wrap(err, 1)
}
comm.EndProgress()
container := actx.SourceContainer
prettySize := humanize.IBytes(uint64(container.Size))
perSecond := humanize.IBytes(uint64(float64(container.Size) / time.Since(startTime).Seconds()))
if actx.InPlace {
statStr := ""
if actx.Stats.TouchedFiles > 0 {
statStr += fmt.Sprintf("patched %d, ", actx.Stats.TouchedFiles)
}
if actx.Stats.MovedFiles > 0 {
statStr += fmt.Sprintf("renamed %d, ", actx.Stats.MovedFiles)
}
if actx.Stats.DeletedFiles > 0 {
statStr += fmt.Sprintf("deleted %d, ", actx.Stats.DeletedFiles)
}
comm.Statf("%s (%s stage)", statStr, humanize.IBytes(uint64(actx.Stats.StageSize)))
}
comm.Statf("%s (%s) @ %s/s\n", prettySize, container.Stats(), perSecond)
if actx.WoundsConsumer != nil && actx.WoundsConsumer.HasWounds() {
extra := ""
if actx.WoundsPath != "" {
extra = fmt.Sprintf(" (written to %s)", actx.WoundsPath)
}
totalCorrupted := actx.WoundsConsumer.TotalCorrupted()
comm.Logf("Result has wounds, %s corrupted data%s", humanize.IBytes(uint64(totalCorrupted)), extra)
}
return nil
}