-
Notifications
You must be signed in to change notification settings - Fork 232
/
diff.go
286 lines (260 loc) · 7 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
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
package main
import (
"context"
"fmt"
"io"
"os"
"github.com/containers/storage"
graphdriver "github.com/containers/storage/drivers"
"github.com/containers/storage/pkg/archive"
"github.com/containers/storage/pkg/chunked"
"github.com/containers/storage/pkg/chunked/compressor"
"github.com/containers/storage/pkg/ioutils"
"github.com/containers/storage/pkg/mflag"
digest "github.com/opencontainers/go-digest"
)
var (
applyDiffFile = ""
diffFile = ""
diffUncompressed = false
diffGzip = false
diffBzip2 = false
diffXz = false
)
func changes(flags *mflag.FlagSet, action string, m storage.Store, args []string) (int, error) {
if len(args) < 1 {
return 1, nil
}
to := args[0]
from := ""
if len(args) >= 2 {
from = args[1]
}
changes, err := m.Changes(from, to)
if err != nil {
return 1, err
}
if jsonOutput {
return outputJSON(changes)
}
for _, change := range changes {
what := "?"
switch change.Kind {
case archive.ChangeAdd:
what = "Add"
case archive.ChangeModify:
what = "Modify"
case archive.ChangeDelete:
what = "Delete"
}
fmt.Printf("%s %q\n", what, change.Path)
}
return 0, nil
}
func diff(flags *mflag.FlagSet, action string, m storage.Store, args []string) (int, error) {
if len(args) < 1 {
return 1, nil
}
to := args[0]
from := ""
if len(args) >= 2 {
from = args[1]
}
diffStream := io.Writer(os.Stdout)
if diffFile != "" {
f, err := os.Create(diffFile)
if err != nil {
return 1, err
}
diffStream = f
defer f.Close()
}
options := storage.DiffOptions{}
if diffUncompressed || diffGzip || diffBzip2 || diffXz {
c := archive.Uncompressed
if diffGzip {
c = archive.Gzip
}
if diffBzip2 {
c = archive.Bzip2
}
if diffXz {
c = archive.Xz
}
options.Compression = &c
}
reader, err := m.Diff(from, to, &options)
if err != nil {
return 1, err
}
_, err = io.Copy(diffStream, reader)
reader.Close()
if err != nil {
return 1, err
}
return 0, nil
}
type fileFetcher struct {
file *os.File
}
func sendFileParts(f *fileFetcher, chunks []chunked.ImageSourceChunk, streams chan io.ReadCloser, errors chan error) {
defer close(streams)
defer close(errors)
for _, chunk := range chunks {
l := io.NewSectionReader(f.file, int64(chunk.Offset), int64(chunk.Length))
streams <- ioutils.NewReadCloserWrapper(l, func() error {
return nil
})
}
}
func (f fileFetcher) GetBlobAt(chunks []chunked.ImageSourceChunk) (chan io.ReadCloser, chan error, error) {
streams := make(chan io.ReadCloser)
errs := make(chan error)
go sendFileParts(&f, chunks, streams, errs)
return streams, errs, nil
}
func applyDiffUsingStagingDirectory(flags *mflag.FlagSet, action string, m storage.Store, args []string) (int, error) {
if len(args) < 2 {
return 2, nil
}
layer := args[0]
sourceDirectory := args[1]
tOptions := archive.TarOptions{}
tr, err := archive.TarWithOptions(sourceDirectory, &tOptions)
if err != nil {
return 1, err
}
defer tr.Close()
tar, err := os.CreateTemp("", "layer-diff-tar-")
if err != nil {
return 1, err
}
defer os.Remove(tar.Name())
defer tar.Close()
// we go through the zstd:chunked compressor first so that it generates the metadata required to mount
// a composefs image.
metadata := make(map[string]string)
compressor, err := compressor.ZstdCompressor(tar, metadata, nil)
if err != nil {
return 1, err
}
digesterCompressed := digest.Canonical.Digester()
r := io.TeeReader(tr, digesterCompressed.Hash())
if _, err := io.Copy(compressor, r); err != nil {
return 1, err
}
if err := compressor.Close(); err != nil {
return 1, err
}
size, err := tar.Seek(0, io.SeekCurrent)
if err != nil {
return 1, err
}
fetcher := fileFetcher{
file: tar,
}
differ, err := chunked.GetDiffer(context.Background(), m, digesterCompressed.Digest(), size, metadata, &fetcher)
if err != nil {
return 1, err
}
var options graphdriver.ApplyDiffWithDifferOpts
out, err := m.ApplyDiffWithDiffer("", &options, differ)
if err != nil {
return 1, err
}
if err := m.ApplyDiffFromStagingDirectory(layer, out.Target, out, &options); err != nil {
m.CleanupStagingDirectory(out.Target)
return 1, err
}
return 0, nil
}
func applyDiff(flags *mflag.FlagSet, action string, m storage.Store, args []string) (int, error) {
if len(args) < 1 {
return 1, nil
}
diffStream := io.Reader(os.Stdin)
if applyDiffFile != "" {
f, err := os.Open(applyDiffFile)
if err != nil {
return 1, err
}
diffStream = f
defer f.Close()
}
_, err := m.ApplyDiff(args[0], diffStream)
if err != nil {
return 1, err
}
return 0, nil
}
func diffSize(flags *mflag.FlagSet, action string, m storage.Store, args []string) (int, error) {
if len(args) < 1 {
return 1, nil
}
to := args[0]
from := ""
if len(args) >= 2 {
from = args[1]
}
n, err := m.DiffSize(from, to)
if err != nil {
return 1, err
}
fmt.Printf("%d\n", n)
return 0, nil
}
func init() {
commands = append(commands, command{
names: []string{"changes"},
usage: "Compare two layers",
optionsHelp: "[options [...]] layerNameOrID [referenceLayerNameOrID]",
minArgs: 1,
maxArgs: 2,
action: changes,
addFlags: func(flags *mflag.FlagSet, cmd *command) {
flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
},
})
commands = append(commands, command{
names: []string{"diffsize", "diff-size"},
usage: "Compare two layers",
optionsHelp: "[options [...]] layerNameOrID [referenceLayerNameOrID]",
minArgs: 1,
maxArgs: 2,
action: diffSize,
})
commands = append(commands, command{
names: []string{"diff"},
usage: "Compare two layers",
optionsHelp: "[options [...]] layerNameOrID [referenceLayerNameOrID]",
minArgs: 1,
maxArgs: 2,
action: diff,
addFlags: func(flags *mflag.FlagSet, cmd *command) {
flags.StringVar(&diffFile, []string{"-file", "f"}, "", "Write to file instead of stdout")
flags.BoolVar(&diffUncompressed, []string{"-uncompressed", "u"}, diffUncompressed, "Use no compression")
flags.BoolVar(&diffGzip, []string{"-gzip", "c"}, diffGzip, "Compress using gzip")
flags.BoolVar(&diffBzip2, []string{"-bzip2", "-bz2", "b"}, diffBzip2, "Compress using bzip2 (not currently supported)")
flags.BoolVar(&diffXz, []string{"-xz", "x"}, diffXz, "Compress using xz (not currently supported)")
},
})
commands = append(commands, command{
names: []string{"applydiff", "apply-diff"},
optionsHelp: "[options [...]] layerNameOrID [referenceLayerNameOrID]",
usage: "Apply a diff to a layer",
minArgs: 1,
maxArgs: 1,
action: applyDiff,
addFlags: func(flags *mflag.FlagSet, cmd *command) {
flags.StringVar(&applyDiffFile, []string{"-file", "f"}, "", "Read from file instead of stdin")
},
})
commands = append(commands, command{
names: []string{"applydiff-using-staging-dir"},
optionsHelp: "layerNameOrID directory",
usage: "Apply a diff to a layer using a staging directory",
minArgs: 2,
maxArgs: 2,
action: applyDiffUsingStagingDirectory,
})
}