Repository navigation
Expand file tree
/
Copy pathdiff.go
More file actions
460 lines (445 loc) · 12.6 KB
/
Copy pathdiff.go
File metadata and controls
460 lines (445 loc) · 12.6 KB
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
package main
import (
"encoding/json"
"fmt"
"path"
"sort"
"strings"
)
type DiffReport struct {
Before DiffSnapshot `json:"before"`
After DiffSnapshot `json:"after"`
Root *DiffNode `json:"root"`
Complete bool `json:"complete"`
Increased int64 `json:"increased"`
Decreased int64 `json:"decreased"`
StatusCounts map[string]int `json:"statusCounts"`
Rankings DiffRankings `json:"rankings"`
}
type DiffSnapshot struct {
Source string `json:"source"`
Root string `json:"root"`
Size int64 `json:"size"`
Files int `json:"files"`
Directories int `json:"directories"`
CapturedAt string `json:"capturedAt,omitempty"`
}
type DiffNode struct {
Name string `json:"name"`
Path string `json:"path"`
Type string `json:"type"`
BeforeType string `json:"beforeType,omitempty"`
AfterType string `json:"afterType,omitempty"`
Before int64 `json:"before"`
After int64 `json:"after"`
Delta int64 `json:"delta"`
BeforeFiles int `json:"beforeFiles"`
AfterFiles int `json:"afterFiles"`
BeforeItems int `json:"beforeItems"`
AfterItems int `json:"afterItems"`
BeforeMTime string `json:"beforeMtime,omitempty"`
AfterMTime string `json:"afterMtime,omitempty"`
Status string `json:"status"`
Children []*DiffNode `json:"children,omitempty"`
}
type diffMetadata struct {
root, capturedAt string
}
type diffOptions struct {
Complete bool
}
func buildDiffReport(before, after *Node, beforeSource, afterSource string) (*DiffReport, error) {
return buildDiffReportWithOptions(before, after, beforeSource, afterSource, diffOptions{})
}
func buildDiffReportWithOptions(before, after *Node, beforeSource, afterSource string, opts diffOptions) (*DiffReport, error) {
if before == nil || after == nil {
return nil, fmt.Errorf("both snapshots must have a root")
}
beforeRoot, afterRoot := cleanDiffRoot(before.Path), cleanDiffRoot(after.Path)
if beforeRoot == "" || afterRoot == "" {
return nil, fmt.Errorf("snapshot root is unavailable")
}
if beforeRoot != afterRoot {
return nil, fmt.Errorf("snapshot roots differ: baseline %q, current %q", beforeRoot, afterRoot)
}
bIdx, err := indexDiffTree(before, beforeRoot, "baseline")
if err != nil {
return nil, err
}
aIdx, err := indexDiffTree(after, afterRoot, "current")
if err != nil {
return nil, err
}
if len(bIdx)+len(aIdx) > maxReportNodes {
return nil, fmt.Errorf("comparison contains more than %d combined nodes", maxReportNodes)
}
statuses := map[string]int{"added": 0, "removed": 0, "grown": 0, "shrunk": 0, "changed": 0, "unchanged": 0}
increased, decreased := int64(0), int64(0)
rankings := newDiffRankingsBuilder()
var build func(string) *DiffNode
build = func(p string) *DiffNode {
b, bok := bIdx[p]
a, aok := aIdx[p]
var node *DiffNode
name := path.Base(p)
if p == beforeRoot {
name = before.Name
}
if bok && b.Name != "" {
name = b.Name
}
if aok && a.Name != "" {
name = a.Name
}
bt, at := "", ""
if bok {
bt = b.Type
}
if aok {
at = a.Type
}
typ := at
if typ == "" {
typ = bt
}
if bt == "dir" || at == "dir" {
typ = "dir"
}
bv, av, bf, af := int64(0), int64(0), 0, 0
if bok {
bv, bf = b.Size, b.Files
}
if aok {
av, af = a.Size, a.Files
}
status := "unchanged"
switch {
case !bok:
status = "added"
case !aok:
status = "removed"
case bt != at:
status = "changed"
case av > bv:
status = "grown"
case av < bv:
status = "shrunk"
case bf != af || b.MTime != a.MTime || b.Flag != a.Flag || b.MIME != a.MIME:
status = "changed"
}
delta := av - bv
rankings.add(DiffRankingRow{Name: name, Path: p, Type: typ, Before: bv, After: av, Delta: delta, BeforeFiles: bf, AfterFiles: af, Status: status})
if status != "unchanged" {
statuses[status]++
}
// Directory totals include descendants. Count only a directory's own
// allocation plus files, so an activity's net is never counted twice.
countedDelta := delta
if (bok && bt == "dir") || (aok && at == "dir") {
beforeChildren, afterChildren := int64(0), int64(0)
if bok {
for _, child := range b.Children {
beforeChildren += child.Size
}
}
if aok {
for _, child := range a.Children {
afterChildren += child.Size
}
}
countedDelta = (av - afterChildren) - (bv - beforeChildren)
}
if countedDelta > 0 {
increased += countedDelta
}
if countedDelta < 0 {
decreased -= countedDelta
}
node = &DiffNode{Name: name, Path: p, Type: typ, BeforeType: bt, AfterType: at, Before: bv, After: av, Delta: delta, BeforeFiles: bf, AfterFiles: af, Status: status}
if bok {
node.BeforeItems, node.BeforeMTime = b.Items, b.MTime
}
if aok {
node.AfterItems, node.AfterMTime = a.Items, a.MTime
}
children := map[string]bool{}
if bok {
for _, c := range b.Children {
children[cleanDiffRoot(c.Path)] = true
}
}
if aok {
for _, c := range a.Children {
children[cleanDiffRoot(c.Path)] = true
}
}
keys := make([]string, 0, len(children))
for k := range children {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
child := build(k)
if opts.Complete || child.Status != "unchanged" || len(child.Children) > 0 {
node.Children = append(node.Children, child)
}
}
return node
}
root := build(beforeRoot)
return &DiffReport{Before: snapshotInfo(before, beforeSource), After: snapshotInfo(after, afterSource), Root: root, Complete: opts.Complete, Increased: increased, Decreased: decreased, StatusCounts: statuses, Rankings: rankings.report()}, nil
}
func cleanDiffRoot(value string) string {
if strings.TrimSpace(value) == "" {
return ""
}
return path.Clean(value)
}
func indexDiffTree(root *Node, rootPath, label string) (map[string]*Node, error) {
result := make(map[string]*Node)
stack := []*Node{root}
for len(stack) > 0 {
n := stack[len(stack)-1]
stack = stack[:len(stack)-1]
p := cleanDiffRoot(n.Path)
if p == "" {
return nil, fmt.Errorf("%s snapshot contains a node with unavailable path", label)
}
if _, ok := result[p]; ok {
return nil, fmt.Errorf("%s snapshot has duplicate or ambiguous path %q", label, p)
}
result[p] = n
for _, c := range n.Children {
if c == nil {
return nil, fmt.Errorf("%s snapshot contains a null node", label)
}
stack = append(stack, c)
}
}
if result[rootPath] != root {
return nil, fmt.Errorf("%s snapshot root path is inconsistent", label)
}
return result, nil
}
func snapshotInfo(root *Node, source string) DiffSnapshot {
files, dirs := 0, 0
stack := []*Node{root}
for len(stack) > 0 {
n := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if n.Type == "dir" {
dirs++
} else {
files++
}
stack = append(stack, n.Children...)
}
return DiffSnapshot{Source: source, Root: cleanDiffRoot(root.Path), Size: root.Size, Files: files, Directories: dirs}
}
// normalizeGDUForDiff corrects gdu's directory size semantics: dsize is the
// directory's own allocation, so child allocations are added exactly once.
func normalizeGDUForDiff(raw any) (*Node, diffMetadata, error) {
meta := diffMetadata{}
tree := raw
metadata := map[string]any(nil)
if arr, ok := raw.([]any); ok && isExportArray(arr) {
metadata = arr[2].(map[string]any)
if scanner := scannerMetadataIdentity(metadata); scanner != "" && !strings.EqualFold(scanner, "gdu") {
return nil, meta, scannerIdentityError(scanner)
}
tree = arr[3]
} else if mapping, ok := raw.(map[string]any); ok {
metadata = wrappedGDUResourceMetadata(mapping)
if scanner := scannerMetadataIdentity(metadata); scanner != "" && !strings.EqualFold(scanner, "gdu") {
return nil, meta, scannerIdentityError(scanner)
}
// Match normalizeExport's wrapper unwrapping so the size correction
// walks the same tree that produced the normalized Nodes.
tree = unwrapPossibleRoot(mapping)
}
meta.root = metadataValue(metadata, "root", "Root", "path", "Path", "root_path", "rootPath")
meta.capturedAt = metadataValue(metadata, "timestamp", "Timestamp", "capturedAt", "captured_at", "scan_time", "scanTime")
root, err := normalizeExport(raw, "gdu")
if err != nil {
return nil, meta, err
}
if root.Path == "root" && meta.root != "" {
root.Path = meta.root
}
if root.Path == "root" && meta.root == "" {
return nil, meta, fmt.Errorf("gdu snapshot root path is unavailable")
}
own := make(map[string]int64)
collectGDUOwnSizes(tree, "", own)
type frame struct {
node *Node
visited bool
}
stack := []frame{{node: root}}
for len(stack) > 0 {
f := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if !f.visited {
stack = append(stack, frame{node: f.node, visited: true})
for _, c := range f.node.Children {
stack = append(stack, frame{node: c})
}
continue
}
n := f.node
ownSize, isDirectory := own[n.Path]
if isDirectory {
n.Type = "dir"
}
if n.Type == "dir" {
if isDirectory {
n.Size = ownSize
} else if len(n.Children) > 0 {
n.Size = 0
}
for _, c := range n.Children {
n.Size += c.Size
}
}
}
if err := addTotals(root); err != nil {
return nil, meta, err
}
if meta.root == "" {
meta.root = root.Path
}
if cleanDiffRoot(meta.root) != cleanDiffRoot(root.Path) {
return nil, meta, fmt.Errorf("gdu metadata root %q does not match tree root %q", meta.root, root.Path)
}
return root, meta, nil
}
func wrappedGDUResourceMetadata(raw map[string]any) map[string]any {
for _, key := range []string{"metadata", "Metadata", "meta", "Meta"} {
if metadata, ok := raw[key].(map[string]any); ok {
return metadata
}
}
return raw
}
func scannerMetadataIdentity(m map[string]any) string {
for key, value := range m {
lk := strings.ToLower(key)
if lk == "prog" || lk == "progname" || lk == "program" || lk == "programname" {
if name, ok := value.(string); ok {
return strings.TrimSpace(name)
}
return fmt.Sprint(value)
}
}
return ""
}
func scannerIdentityError(scanner string) error {
if strings.EqualFold(scanner, "ncdu") {
return fmt.Errorf("expected gdu JSON; input metadata identifies an ncdu export")
}
return fmt.Errorf("expected gdu JSON; unsupported scanner identity %q", scanner)
}
func metadataValue(m map[string]any, keys ...string) string {
for _, k := range keys {
if v, ok := m[k]; ok {
if s, ok := v.(string); ok {
return s
}
if n, ok := v.(json.Number); ok {
return n.String()
}
}
}
return ""
}
func collectGDUOwnSizes(raw any, parent string, out map[string]int64) {
var info map[string]any
var children []any
var childNames []string
sequenceNode := false
switch v := raw.(type) {
case map[string]any:
info = v
if a, ok := extractChildren(v); ok {
switch items := a.(type) {
case []any:
children = items
case map[string]any:
childNames = make([]string, 0, len(items))
for name := range items {
childNames = append(childNames, name)
}
sort.Strings(childNames)
for _, name := range childNames {
children = append(children, items[name])
}
}
}
case []any:
if isSequenceNode(v) {
sequenceNode = true
info, _ = v[0].(map[string]any)
children = v[1:]
} else {
for _, item := range v {
collectGDUOwnSizes(item, parent, out)
}
return
}
}
if info == nil {
return
}
name := firstString(info, nameKeys)
p := firstString(info, pathKeys)
if name == "" {
name = displayNameFromPath(p)
}
if name == "" {
name = "root"
}
if p == "" {
p = makePath(parent, name)
} else if parent != "" && p == name {
p = makePath(parent, name)
}
if sequenceNode || firstBool(info, dirKeys) || len(children) > 0 || hasEmptyDirectoryChildren(info) {
out[p] = firstNumber(info, sizeKeys)
}
for i, c := range children {
fallback := fmt.Sprintf("item-%d", i)
if i < len(childNames) {
fallback = childNames[i]
}
collectGDUOwnSizesWithFallback(c, p, out, fallback)
}
}
func collectGDUOwnSizesWithFallback(raw any, parent string, out map[string]int64, fallback string) {
if m, ok := raw.(map[string]any); ok && firstString(m, nameKeys) == "" && firstString(m, pathKeys) == "" {
cp := map[string]any{}
for k, v := range m {
cp[k] = v
}
cp["name"] = fallback
collectGDUOwnSizes(cp, parent, out)
return
}
if a, ok := raw.([]any); ok && !isSequenceNode(a) {
collectGDUOwnSizes(raw, parent, out)
return
}
collectGDUOwnSizes(raw, parent, out)
}
func hasEmptyDirectoryChildren(info map[string]any) bool {
for _, k := range childKeys {
if v, ok := info[k]; ok {
if a, ok := v.([]any); ok && len(a) == 0 {
return true
}
if m, ok := v.(map[string]any); ok && len(m) == 0 {
return true
}
}
}
return false
}