-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtdu_export.go
126 lines (117 loc) · 2.75 KB
/
tdu_export.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
/* Top Disk Usage.
* Copyright (C) 2019 Joseph Paul <[email protected]>
* https://github.com/josephpaul0/tdu
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
package main
import (
"fmt"
"os"
"time"
)
const (
ncdu_INIT = iota
ncdu_END
ncdu_OPENDIR
ncdu_CLOSEDIR
ncdu_NEXT
)
func initExport(sc *s_scan) {
mode := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
f, err := os.OpenFile(sc.exportPath, mode, 0666)
if err != nil {
fmt.Printf("\n [ERROR] Cannot open export file: %v\n\n", err)
os.Exit(1)
}
sc.exportFile = f
}
func ncduOpe(operation int, sc *s_scan) {
if !sc.export {
return
}
var s string
switch operation {
case ncdu_INIT:
initExport(sc)
s = "[1,1,{\"progname\":\"tdu\","
s += fmt.Sprintf("\"progver\":\"%s\",", prg_VERSION)
s += fmt.Sprintf("\"timestamp\":%d},\n", time.Now().Unix())
case ncdu_OPENDIR:
s = "["
case ncdu_CLOSEDIR:
s = "]"
case ncdu_NEXT:
s = ",\n"
case ncdu_END:
s = "]\n"
default:
panic("Unknown operation")
}
sc.exportFile.WriteString(s)
if operation == ncdu_END {
sc.exportFile.Close()
}
}
func ncduOpenDir(sc *s_scan) { ncduOpe(ncdu_OPENDIR, sc) }
func ncduCloseDir(sc *s_scan) { ncduOpe(ncdu_CLOSEDIR, sc) }
func ncduNext(sc *s_scan) { ncduOpe(ncdu_NEXT, sc) }
func ncduEnd(sc *s_scan) { ncduOpe(ncdu_END, sc) }
func ncduInit(sc *s_scan) { ncduOpe(ncdu_INIT, sc) }
func ncduDiskUsage(sc *s_scan, f *file) (int64, bool) {
if f.nLinks > 1 && !f.isDir { // Hardlinks exist, recalculate disk usage
return 512 * f.nBlocks512, true
}
return f.diskUsage, false
}
func cleanName(s string) string {
rs := []rune(s)
rd := make([]rune, 0, len(s))
for i := 0; i < len(rs); i++ {
if rs[i] <= 31 || rs[i] == 34 || rs[i] == 127 {
u := []rune(fmt.Sprintf("\\u00%02X", rs[i]))
rd = append(rd, u...)
} else {
rd = append(rd, rs[i])
}
}
return string(rd)
}
func ncduAdd(sc *s_scan, f *file) {
if !sc.export {
return
}
name := cleanName(f.name)
if f.depth == 1 {
name, _ = os.Getwd()
}
s := fmt.Sprintf("{\"name\":\"%s\"", name)
if f.size > 0 && !f.isOtherFs {
s += fmt.Sprintf(",\"asize\":%d", f.size)
}
du, hl := ncduDiskUsage(sc, f)
if du > 0 && !f.isOtherFs {
s += fmt.Sprintf(",\"dsize\":%d", du)
}
if f.depth == 1 || f.isOtherFs {
s += fmt.Sprintf(",\"dev\":%d", f.deviceId)
}
s += fmt.Sprintf(",\"ino\":%d", f.inode)
if hl {
s += ",\"hlnkc\":true"
}
if !f.isDir && !f.isRegular {
s += ",\"notreg\":true"
}
if f.readError {
s += ",\"read_error\":true"
}
if f.isOtherFs {
s += ",\"excluded\":\"othfs\""
}
s += "}"
sc.exportFile.WriteString(s)
}