-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.go
201 lines (183 loc) · 4.32 KB
/
data.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
package main
import (
"fmt"
"strings"
)
type FSNodeStat struct {
Id uint32
ParentId uint32
Path string
IsDir bool
IsSymlink bool
LinkTarget string
Size uint64
SelfSize uint64
Count uint32
SymlinkCount uint32
SFileCount uint32
SFileSize uint64
MFileCount uint32
MFileSize uint64
LFileCount uint32
LFileSize uint64
XLFileCount uint32
XLFileSize uint64
XXLFileCount uint32
XXLFileSize uint64
}
func (f *FSNodeStat) Update(child *FSNodeStat) {
if child == nil {
return
}
f.Size += child.Size
f.Count += child.Count
f.SymlinkCount += child.SymlinkCount
f.SFileCount += child.SFileCount
f.SFileSize += child.SFileSize
f.MFileCount += child.MFileCount
f.MFileSize += child.MFileSize
f.LFileCount += child.LFileCount
f.LFileSize += child.LFileSize
f.XLFileCount += child.XLFileCount
f.XLFileSize += child.XLFileSize
f.XXLFileCount += child.XXLFileCount
f.XXLFileSize += child.XXLFileSize
}
func (f *FSNodeStat) String() string {
return fmt.Sprintf(
`Id: %d
ParentId: %d
Path: %s
IsDir: %t
Count: %d
Size: %s
Symlinks
--------------------------
Count: %d
S Files - less than 512KiB
--------------------------
Count: %d
Size: %s
M Files - 512KiB ~ 4MiB
-----------------------
Count: %d
Size: %s
L Files - 4MiB ~ 50MiB
----------------------
Count: %d
Size: %s
XL Files - 50MiB ~ 200MiB
-------------------------
Count: %d
Size: %s
XXL Files - larger than 200MiB
------------------------------
Count: %d
Size: %s`,
f.Id,
f.ParentId,
f.Path,
f.IsDir,
f.Count,
toAppropriateUnit(f.Size),
f.SymlinkCount,
f.SFileCount,
toAppropriateUnit(f.SFileSize),
f.MFileCount,
toAppropriateUnit(f.MFileSize),
f.LFileCount,
toAppropriateUnit(f.LFileSize),
f.XLFileCount,
toAppropriateUnit(f.XLFileSize),
f.XXLFileCount,
toAppropriateUnit(f.XXLFileSize),
)
}
func unrootPath(path string, root string) string {
unrootedPath := strings.TrimPrefix(path, root)
return "/" + strings.Trim(unrootedPath, "/")
}
func CreateFSLinkStat(root string, path string, parentId uint32, linkTarget string, idChan chan uint32) *FSNodeStat {
return &FSNodeStat{
Id: <-idChan,
ParentId: parentId,
Path: unrootPath(path, root),
IsDir: false,
IsSymlink: true,
LinkTarget: linkTarget,
SelfSize: 0,
Size: 0,
Count: 0,
SymlinkCount: 1,
SFileCount: 0,
SFileSize: 0,
MFileCount: 0,
MFileSize: 0,
LFileCount: 0,
LFileSize: 0,
XLFileCount: 0,
XLFileSize: 0,
XXLFileCount: 0,
XXLFileSize: 0,
}
}
func CreateFSNodeStat(root string, path string, parentId uint32, size int64, isDir bool, idChan chan uint32) *FSNodeStat {
data := FSNodeStat{
Id: <-idChan,
ParentId: parentId,
Path: unrootPath(path, root),
IsDir: isDir,
IsSymlink: false,
LinkTarget: "",
SelfSize: uint64(size),
Size: uint64(size),
Count: 0,
SymlinkCount: 0,
SFileCount: 0,
SFileSize: 0,
MFileCount: 0,
MFileSize: 0,
LFileCount: 0,
LFileSize: 0,
XLFileCount: 0,
XLFileSize: 0,
XXLFileCount: 0,
XXLFileSize: 0,
}
fileCount := uint32(1)
if isDir {
fileCount = 0
}
data.Count = fileCount
switch {
case data.Size <= 512*1024: // 0 to 512 KiB
data.SFileCount = fileCount
data.SFileSize = data.Size
case data.Size >= 512*1024 && data.Size < 4*1024*1024: // 512 KiB to 4 MiB
data.MFileCount = fileCount
data.MFileSize = data.Size
case data.Size >= 4*1024*1024 && data.Size < 50*1024*1024: // 4 MiB to 50 MiB
data.LFileCount = fileCount
data.LFileSize = data.Size
case data.Size >= 50*1024*1024 && data.Size < 200*1024*1024: // 50 MiB to 200 MiB
data.XLFileCount = fileCount
data.XLFileSize = data.Size
case data.Size > 200*1024*1024: // above 200 MiB
data.XXLFileCount = fileCount
data.XXLFileSize = data.Size
}
return &data
}
func toAppropriateUnit(size uint64) string {
if size < 1024 {
return fmt.Sprintf("%d B", size)
} else if size < 1024*1024 {
return fmt.Sprintf("%.2f KiB", float64(size)/1024)
} else if size < 1024*1024*1024 {
return fmt.Sprintf("%.2f MiB", float64(size)/(1024*1024))
} else if size < 1024*1024*1024*1024 {
return fmt.Sprintf("%.2f GiB", float64(size)/(1024*1024*1024))
} else {
return fmt.Sprintf("%.2f TiB", float64(size)/(1024*1024*1024*1024))
}
}