forked from itchio/butler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
249 lines (206 loc) · 6.81 KB
/
diff.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
package diff
import (
"fmt"
"io"
"os"
"time"
humanize "github.com/dustin/go-humanize"
"github.com/go-errors/errors"
"github.com/itchio/butler/comm"
"github.com/itchio/butler/filtering"
"github.com/itchio/butler/mansion"
"github.com/itchio/savior/seeksource"
"github.com/itchio/wharf/counter"
"github.com/itchio/wharf/eos"
"github.com/itchio/wharf/pools"
"github.com/itchio/wharf/pools/nullpool"
"github.com/itchio/wharf/pwr"
"github.com/itchio/wharf/tlc"
"github.com/itchio/wharf/wire"
"github.com/itchio/wharf/wsync"
)
var args = struct {
old *string
new *string
patch *string
verify *bool
}{}
func Register(ctx *mansion.Context) {
cmd := ctx.App.Command("diff", "(Advanced) Compute the difference between two directories or .zip archives. Stores the patch in `patch.pwr`, and a signature in `patch.pwr.sig` for integrity checks and further diff.")
args.old = cmd.Arg("old", "Directory or .zip archive (slower) with older files, or signature file generated from old directory.").Required().String()
args.new = cmd.Arg("new", "Directory or .zip archive (slower) with newer files").Required().String()
args.patch = cmd.Arg("patch", "Path to write the patch file (recommended extension is `.pwr`) The signature file will be written to the same path, with .sig added to the end.").Default("patch.pwr").String()
args.verify = cmd.Flag("verify", "Make sure generated patch applies cleanly by applying it (slower)").Bool()
ctx.Register(cmd, do)
}
type Params struct {
// Target is the old version of the data
Target string
// Source is the new version of the data
Source string
// Patch is where to write the patch
Patch string
Compression pwr.CompressionSettings
// Verify enables dry-run apply patch validation (slow)
Verify bool
}
func do(ctx *mansion.Context) {
ctx.Must(Do(&Params{
Target: *args.old,
Source: *args.new,
Patch: *args.patch,
Compression: ctx.CompressionSettings(),
Verify: *args.verify,
}))
}
func Do(params *Params) error {
var err error
startTime := time.Now()
targetSignature := &pwr.SignatureInfo{}
if params.Target == "" {
return errors.New("diff: must specify Target")
}
if params.Source == "" {
return errors.New("diff: must specify Source")
}
if params.Patch == "" {
return errors.New("diff: must specify Patch")
}
targetSignature.Container, err = tlc.WalkAny(params.Target, &tlc.WalkOpts{Filter: filtering.FilterPaths})
if err != nil {
// Signature file perhaps?
signatureReader, err := eos.Open(params.Target)
if err != nil {
return errors.Wrap(err, 0)
}
signatureSource := seeksource.FromFile(signatureReader)
_, err = signatureSource.Resume(nil)
if err != nil {
return errors.Wrap(err, 0)
}
targetSignature, err = pwr.ReadSignature(signatureSource)
if err != nil {
if errors.Is(err, wire.ErrFormat) {
return fmt.Errorf("unrecognized target %s (not a container, not a signature file)", params.Target)
}
return errors.Wrap(err, 0)
}
comm.Opf("Read signature from %s", params.Target)
err = signatureReader.Close()
if err != nil {
return errors.Wrap(err, 0)
}
} else {
// Container (dir, archive, etc.)
comm.Opf("Hashing %s", params.Target)
comm.StartProgress()
var targetPool wsync.Pool
targetPool, err = pools.New(targetSignature.Container, params.Target)
if err != nil {
return errors.Wrap(err, 0)
}
targetSignature.Hashes, err = pwr.ComputeSignature(targetSignature.Container, targetPool, comm.NewStateConsumer())
comm.EndProgress()
if err != nil {
return errors.Wrap(err, 0)
}
{
prettySize := humanize.IBytes(uint64(targetSignature.Container.Size))
perSecond := humanize.IBytes(uint64(float64(targetSignature.Container.Size) / time.Since(startTime).Seconds()))
comm.Statf("%s (%s) @ %s/s\n", prettySize, targetSignature.Container.Stats(), perSecond)
}
}
startTime = time.Now()
var sourceContainer *tlc.Container
sourceContainer, err = tlc.WalkAny(params.Source, &tlc.WalkOpts{Filter: filtering.FilterPaths})
if err != nil {
return errors.Wrap(err, 0)
}
var sourcePool wsync.Pool
sourcePool, err = pools.New(sourceContainer, params.Source)
if err != nil {
return errors.Wrap(err, 0)
}
patchWriter, err := os.Create(params.Patch)
if err != nil {
return errors.Wrap(err, 0)
}
defer patchWriter.Close()
signaturePath := params.Patch + ".sig"
signatureWriter, err := os.Create(signaturePath)
if err != nil {
return errors.Wrap(err, 0)
}
defer signatureWriter.Close()
patchCounter := counter.NewWriter(patchWriter)
signatureCounter := counter.NewWriter(signatureWriter)
dctx := &pwr.DiffContext{
SourceContainer: sourceContainer,
Pool: sourcePool,
TargetContainer: targetSignature.Container,
TargetSignature: targetSignature.Hashes,
Consumer: comm.NewStateConsumer(),
Compression: ¶ms.Compression,
}
comm.Opf("Diffing %s", params.Source)
comm.StartProgress()
err = dctx.WritePatch(patchCounter, signatureCounter)
if err != nil {
return errors.Wrap(err, 0)
}
comm.EndProgress()
totalDuration := time.Since(startTime)
{
prettySize := humanize.IBytes(uint64(sourceContainer.Size))
perSecond := humanize.IBytes(uint64(float64(sourceContainer.Size) / totalDuration.Seconds()))
comm.Statf("%s (%s) @ %s/s\n", prettySize, sourceContainer.Stats(), perSecond)
}
{
prettyPatchSize := humanize.IBytes(uint64(patchCounter.Count()))
percReused := 100.0 * float64(dctx.ReusedBytes) / float64(dctx.FreshBytes+dctx.ReusedBytes)
relToNew := 100.0 * float64(patchCounter.Count()) / float64(sourceContainer.Size)
prettyFreshSize := humanize.IBytes(uint64(dctx.FreshBytes))
comm.Statf("Re-used %.2f%% of old, added %s fresh data", percReused, prettyFreshSize)
comm.Statf("%s patch (%.2f%% of the full size) in %s", prettyPatchSize, relToNew, totalDuration)
}
if params.Verify {
comm.Opf("Applying patch to verify it...")
_, err := signatureWriter.Seek(0, io.SeekStart)
if err != nil {
return errors.Wrap(err, 0)
}
signatureSource := seeksource.FromFile(signatureWriter)
_, err = signatureSource.Resume(nil)
if err != nil {
return errors.Wrap(err, 0)
}
signature, err := pwr.ReadSignature(signatureSource)
if err != nil {
return errors.Wrap(err, 0)
}
actx := &pwr.ApplyContext{
OutputPool: &pwr.ValidatingPool{
Pool: nullpool.New(sourceContainer),
Container: sourceContainer,
Signature: signature,
},
TargetPath: params.Target,
TargetContainer: targetSignature.Container,
SourceContainer: sourceContainer,
Consumer: comm.NewStateConsumer(),
}
patchSource := seeksource.FromFile(patchWriter)
_, err = patchSource.Resume(nil)
if err != nil {
return errors.Wrap(err, 0)
}
comm.StartProgress()
err = actx.ApplyPatch(patchSource)
comm.EndProgress()
if err != nil {
return errors.Wrap(err, 0)
}
comm.Statf("Patch applies cleanly!")
}
return nil
}