Repository navigation
Expand file tree
/
Copy pathnormalize.go
More file actions
549 lines (518 loc) · 13.3 KB
/
Copy pathnormalize.go
File metadata and controls
549 lines (518 loc) · 13.3 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
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
package main
import (
"encoding/json"
"fmt"
"mime"
"path"
"regexp"
"sort"
"strconv"
"strings"
)
var (
childKeys = []string{"items", "Items", "children", "Children", "entries", "Entries", "files", "Files", "dirs", "Dirs", "nodes", "Nodes"}
nameKeys = []string{"name", "Name", "path", "Path", "fullPath", "FullPath"}
pathKeys = []string{"path", "Path", "fullPath", "FullPath"}
sizeKeys = []string{"usage", "Usage", "size", "Size", "diskUsage", "DiskUsage", "disk_usage", "dsize", "Dsize", "asize", "Asize", "blocks", "Blocks", "apparentSize", "ApparentSize", "apparent_size", "total", "Total"}
dirKeys = []string{"isDir", "IsDir", "dir", "Dir", "directory", "Directory"}
mtimeKeys = []string{"mtime", "Mtime", "modTime", "ModTime", "modified", "Modified"}
flagKeys = []string{"flag", "Flag", "flags", "Flags"}
mimeKeys = []string{"mime", "MIME", "mimeType", "MimeType", "contentType", "ContentType"}
numberPattern = regexp.MustCompile(`^\d+(\.\d+)?$`)
)
const maxReportNodes = 5_000_000
func normalizeExport(raw any, inputType string) (*Node, error) {
if inputType != "gdu" && inputType != "ncdu" {
return nil, fmt.Errorf("unsupported input type: %s", inputType)
}
if isExportArray(raw) {
if inputType == "ncdu" {
if err := validateNcduExport(raw.([]any)); err != nil {
return nil, err
}
}
raw = raw.([]any)[3]
} else if inputType == "ncdu" {
return nil, fmt.Errorf("expected ncdu JSON export array from `ncdu -o-`")
}
var root *Node
switch value := raw.(type) {
case []any:
if isSequenceNode(value) {
root = normalizeNode(value, "root", "", inputType)
} else {
children := make([]*Node, 0, len(value))
for i, item := range value {
children = append(children, normalizeNode(item, fmt.Sprintf("item-%d", i), "", inputType))
}
root = &Node{Name: "root", Path: "root", Type: "dir", Children: children}
for _, child := range children {
root.Size += child.Size
}
}
case map[string]any:
root = normalizeNode(unwrapPossibleRoot(value), "root", "", inputType)
default:
return nil, fmt.Errorf("expected a JSON object or array")
}
if err := addTotals(root); err != nil {
return nil, err
}
return root, nil
}
func isExportArray(raw any) bool {
value, ok := raw.([]any)
if !ok || len(value) < 4 {
return false
}
_, majorOK := value[0].(json.Number)
_, minorOK := value[1].(json.Number)
_, metaOK := value[2].(map[string]any)
switch value[3].(type) {
case map[string]any, []any:
return majorOK && minorOK && metaOK
default:
return false
}
}
func validateNcduExport(raw []any) error {
major, ok := raw[0].(json.Number)
if !ok || major.String() != "1" {
return fmt.Errorf("unsupported ncdu export major version: %s", scalarString(raw[0]))
}
if _, ok := raw[1].(json.Number); !ok {
return fmt.Errorf("invalid ncdu export minor version")
}
tree, ok := raw[3].([]any)
if !ok || !isSequenceNode(tree) {
return fmt.Errorf("invalid ncdu export directory tree")
}
return nil
}
func unwrapPossibleRoot(raw map[string]any) any {
if looksLikeNode(raw) {
return raw
}
for _, key := range []string{"root", "Root", "data", "Data", "scan", "Scan", "tree", "Tree"} {
if value, ok := raw[key]; ok {
switch value.(type) {
case map[string]any, []any:
return value
}
}
}
var only map[string]any
count := 0
for _, value := range raw {
if node, ok := value.(map[string]any); ok {
only = node
count++
}
}
if count == 1 {
return only
}
return raw
}
func looksLikeNode(raw map[string]any) bool {
for _, keys := range [][]string{nameKeys, sizeKeys, childKeys} {
for _, key := range keys {
if _, ok := raw[key]; ok {
return true
}
}
}
return false
}
func isSequenceNode(raw any) bool {
value, ok := raw.([]any)
if !ok || len(value) == 0 {
return false
}
info, ok := value[0].(map[string]any)
return ok && looksLikeNode(info)
}
func normalizeNode(raw any, fallbackName, parentPath, inputType string) *Node {
switch value := raw.(type) {
case map[string]any:
return normalizeMappingNode(value, fallbackName, parentPath, inputType)
case []any:
if isSequenceNode(value) {
return normalizeSequenceNode(value, fallbackName, parentPath, inputType)
}
children := make([]*Node, 0, len(value))
for _, item := range value {
children = append(children, normalizeNode(item, fallbackName, parentPath, inputType))
}
node := &Node{
Name: fallbackName,
Path: makePath(parentPath, fallbackName),
Type: "dir",
Children: children,
}
for _, child := range children {
node.Size += child.Size
}
return node
default:
return &Node{
Name: fallbackName,
Path: makePath(parentPath, fallbackName),
Size: numberish(raw),
Type: "file",
Ext: extensionFor(fallbackName),
Children: []*Node{},
}
}
}
func normalizeSequenceNode(raw []any, fallbackName, parentPath, inputType string) *Node {
info := raw[0].(map[string]any)
nameValue := firstString(info, nameKeys)
pathValue := firstString(info, pathKeys)
name := nameValue
if pathValue != "" && (nameValue == "" || nameValue == pathValue) {
name = displayNameFromPath(pathValue)
}
if name == "" {
name = fallbackName
}
nodePath := pathValue
if nodePath == "" {
nodePath = makePath(parentPath, name)
}
children := make([]*Node, 0, len(raw)-1)
for i, child := range raw[1:] {
children = append(children, normalizeNode(child, fmt.Sprintf("item-%d", i), nodePath, inputType))
}
size := firstNumber(info, sizeKeys)
var childSize int64
for _, child := range children {
childSize += child.Size
}
if inputType == "ncdu" && len(children) > 0 {
size += childSize
} else if size <= 0 {
size = childSize
}
nodeType := "file"
if len(children) > 0 || firstBool(info, dirKeys) {
nodeType = "dir"
}
node := &Node{Name: name, Path: nodePath, Size: size, Type: nodeType, Children: sortedChildren(children)}
if nodeType == "file" {
node.Ext = extensionFor(name)
}
if value := firstScalar(info, mtimeKeys); value != "" {
node.MTime = value
}
if value := firstString(info, flagKeys); value != "" {
node.Flag = value
}
if value := firstString(info, mimeKeys); value != "" && nodeType != "dir" {
node.MIME = strings.Split(value, ";")[0]
} else if mt := mime.TypeByExtension(node.Ext); mt != "" && nodeType != "dir" {
node.MIME = strings.Split(mt, ";")[0]
}
return node
}
func normalizeMappingNode(raw map[string]any, fallbackName, parentPath, inputType string) *Node {
childrenRaw, hasChildren := extractChildren(raw)
pathValue := firstString(raw, pathKeys)
nameValue := firstString(raw, nameKeys)
name := nameValue
if pathValue != "" && (nameValue == "" || nameValue == pathValue) {
name = displayNameFromPath(pathValue)
}
if name == "" {
name = fallbackName
}
nodePath := pathValue
if nodePath == "" {
nodePath = makePath(parentPath, name)
}
if parentPath != "" && nodePath == name {
nodePath = makePath(parentPath, name)
}
var children []*Node
if hasChildren {
switch value := childrenRaw.(type) {
case map[string]any:
keys := make([]string, 0, len(value))
for key := range value {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
children = append(children, normalizeNode(value[key], key, nodePath, inputType))
}
case []any:
for i, child := range value {
children = append(children, normalizeNode(child, fmt.Sprintf("item-%d", i), nodePath, inputType))
}
}
} else {
children = extractMappingChildren(raw, nodePath, inputType)
}
size := firstNumber(raw, sizeKeys)
if size <= 0 && len(children) > 0 {
for _, child := range children {
size += child.Size
}
}
isDir := len(children) > 0 || firstBool(raw, dirKeys)
nodeType := "file"
ext := extensionFor(name)
if isDir {
nodeType = "dir"
ext = ""
}
node := &Node{Name: name, Path: nodePath, Size: size, Type: nodeType, Ext: ext, Children: sortedChildren(children)}
if value := firstScalar(raw, mtimeKeys); value != "" {
node.MTime = value
}
if value := firstString(raw, flagKeys); value != "" {
node.Flag = value
}
if value := firstString(raw, mimeKeys); value != "" && !isDir {
node.MIME = strings.Split(value, ";")[0]
} else if mt := mime.TypeByExtension(ext); mt != "" && !isDir {
node.MIME = strings.Split(mt, ";")[0]
}
return node
}
func extractChildren(raw map[string]any) (any, bool) {
for _, key := range childKeys {
value, ok := raw[key]
if !ok {
continue
}
switch value.(type) {
case []any, map[string]any:
return value, true
}
}
for key, value := range raw {
if contains(nameKeys, key) || contains(sizeKeys, key) || contains(pathKeys, key) || contains(dirKeys, key) || contains(mtimeKeys, key) || contains(flagKeys, key) || contains(mimeKeys, key) {
continue
}
items, ok := value.([]any)
if !ok || len(items) == 0 {
continue
}
allMaps := true
for _, item := range items {
if _, ok := item.(map[string]any); !ok {
allMaps = false
break
}
}
if allMaps {
return value, true
}
}
return nil, false
}
func extractMappingChildren(raw map[string]any, parentPath, inputType string) []*Node {
if looksLikeNode(raw) {
return nil
}
keys := make([]string, 0, len(raw))
for key := range raw {
keys = append(keys, key)
}
sort.Strings(keys)
var children []*Node
for _, key := range keys {
switch raw[key].(type) {
case map[string]any, []any, json.Number, float64, int, int64:
children = append(children, normalizeNode(raw[key], key, parentPath, inputType))
}
}
return children
}
func addTotals(root *Node) error {
if root == nil {
return nil
}
type frame struct {
node *Node
depth int
visited bool
}
nextID := 0
stack := []frame{{node: root}}
for len(stack) > 0 {
index := len(stack) - 1
current := stack[index]
stack = stack[:index]
if !current.visited {
if nextID >= maxReportNodes {
return fmt.Errorf("report contains more than %d nodes", maxReportNodes)
}
current.node.ID = nextID
nextID++
current.node.Depth = current.depth
stack = append(stack, frame{node: current.node, depth: current.depth, visited: true})
for childIndex := len(current.node.Children) - 1; childIndex >= 0; childIndex-- {
stack = append(stack, frame{node: current.node.Children[childIndex], depth: current.depth + 1})
}
continue
}
totalCount := 1
fileCount := 0
if current.node.Type != "dir" {
fileCount = 1
}
for _, child := range current.node.Children {
totalCount += child.Items + 1
fileCount += child.Files
}
current.node.Items = totalCount - 1
current.node.Files = fileCount
}
return nil
}
func firstString(raw map[string]any, keys []string) string {
for _, key := range keys {
if value, ok := raw[key].(string); ok && value != "" {
return value
}
}
return ""
}
func firstScalar(raw map[string]any, keys []string) string {
for _, key := range keys {
switch value := raw[key].(type) {
case string:
if value != "" {
return value
}
case json.Number:
return value.String()
case float64:
return strconv.FormatFloat(value, 'f', -1, 64)
case int:
return strconv.Itoa(value)
case int64:
return strconv.FormatInt(value, 10)
}
}
return ""
}
func firstNumber(raw map[string]any, keys []string) int64 {
for _, key := range keys {
if number := numberish(raw[key]); number > 0 {
return number
}
}
return 0
}
func firstBool(raw map[string]any, keys []string) bool {
for _, key := range keys {
if value, ok := raw[key].(bool); ok {
return value
}
}
return false
}
func numberish(value any) int64 {
switch v := value.(type) {
case bool, nil:
return 0
case json.Number:
if i, err := v.Int64(); err == nil {
if i < 0 {
return 0
}
return i
}
f, err := strconv.ParseFloat(v.String(), 64)
if err != nil || f < 0 {
return 0
}
return int64(f)
case int:
if v < 0 {
return 0
}
return int64(v)
case int64:
if v < 0 {
return 0
}
return v
case float64:
if v < 0 {
return 0
}
return int64(v)
case string:
cleaned := strings.ReplaceAll(strings.TrimSpace(v), ",", "")
if numberPattern.MatchString(cleaned) {
f, _ := strconv.ParseFloat(cleaned, 64)
return int64(f)
}
}
return 0
}
func displayNameFromPath(value string) string {
trimmed := strings.TrimRight(value, "/")
if trimmed == "" {
if value == "" {
return "root"
}
return value
}
base := path.Base(trimmed)
if base == "." || base == "/" {
return trimmed
}
return base
}
func makePath(parentPath, name string) string {
if parentPath == "" {
return name
}
if parentPath == "/" {
return "/" + strings.TrimLeft(name, "/")
}
return strings.TrimRight(parentPath, "/") + "/" + strings.TrimLeft(name, "/")
}
func extensionFor(name string) string {
base := name
if index := strings.LastIndexByte(base, '/'); index >= 0 {
base = base[index+1:]
}
dot := strings.LastIndexByte(base, '.')
if dot <= 0 || dot == len(base)-1 {
return "[no extension]"
}
if base[0] == '.' && dot == 0 {
return "[no extension]"
}
return "." + strings.ToLower(base[dot+1:])
}
func sortedChildren(children []*Node) []*Node {
sort.SliceStable(children, func(i, j int) bool {
return children[i].Size > children[j].Size
})
return children
}
func scalarString(value any) string {
switch v := value.(type) {
case json.Number:
return v.String()
case string:
return v
default:
return fmt.Sprint(v)
}
}
func contains(values []string, needle string) bool {
for _, value := range values {
if value == needle {
return true
}
}
return false
}