This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
223 lines (187 loc) · 4.48 KB
/
main.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
// main.go - parallel du(1)
//
// (c) 2016 Sudhi Herle <sudhi@herle.net>
//
// Licensing Terms: GPLv2
//
// If you need a commercial license for this work, please contact
// the author.
//
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
package main
import (
"fmt"
"os"
"path"
"sort"
"strings"
"sync"
"syscall"
"github.com/opencoff/go-walk"
"github.com/opencoff/go-utils"
flag "github.com/opencoff/pflag"
)
var Z string = path.Base(os.Args[0])
var Verbose bool
type result struct {
name string
size uint64
}
type bySize []result
func (r bySize) Len() int {
return len(r)
}
func (r bySize) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
// we're doing reverse sort.
func (r bySize) Less(i, j int) bool {
return r[i].size > r[j].size
}
func main() {
var version bool
var human bool
var kb bool
var byts bool
var total bool
var symlinks bool
var onefs bool
var all bool
var excludes []string
flag.BoolVarP(&version, "version", "", false, "Show version info and quit")
flag.BoolVarP(&Verbose, "verbose", "v", false, "Show verbose output")
flag.BoolVarP(&symlinks, "follow-symlinks", "L", false, "Follow symlinks")
flag.BoolVarP(&onefs, "single-filesystem", "x", false, "Don't cross mount points")
flag.BoolVarP(&all, "all", "a", false, "Show all files & dirs")
flag.BoolVarP(&human, "human-size", "h", false, "Show size in human readable form")
flag.BoolVarP(&kb, "kilo-byte", "k", false, "Show size in kilo bytes")
flag.BoolVarP(&byts, "byte", "b", false, "Show size in bytes")
flag.BoolVarP(&total, "total", "t", false, "Show total size")
flag.StringSliceVarP(&excludes, "exclude", "", nil, "Exclude names starting with `N`")
flag.Usage = func() {
fmt.Printf(
`%s - disk utilization calculator (parallel edition)
Usage: %s [options] dir [dir...]
Options:
`, Z, Z)
flag.PrintDefaults()
os.Stdout.Sync()
os.Exit(0)
}
flag.Parse()
if version {
fmt.Printf("%s - %s [%s; %s]\n", Z, ProductVersion, RepoVersion, Buildtime)
os.Exit(0)
}
args := flag.Args()
if len(args) == 0 {
die("Insufficient args. Try %s --help", Z)
}
var size func(uint64) string
if human {
size = utils.HumanizeSize
} else if kb {
size = func(z uint64) string {
z /= 1024
return fmt.Sprintf("%d", z)
}
} else {
size = func(z uint64) string {
return fmt.Sprintf("%d", z)
}
}
// sort the args in decreasing length so our prefix matching always
// finds the longest match
sort.Sort(byLen(args))
opt := &walk.Options{
FollowSymlinks: symlinks,
OneFS: onefs,
Type: walk.FILE,
Excludes: excludes,
}
// We know this function will be called from a single threaded
// context; so we can use a regular map and not sync.Map
linkmap := make(map[string]string)
hardlinked := func(fi os.FileInfo, nm string) bool {
st, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return false
}
if st.Nlink == 1 {
return false
}
key := fmt.Sprintf("%d:%d:%d", st.Dev, st.Rdev, st.Ino)
if _, ok := linkmap[key]; ok {
return true
}
linkmap[key] = nm
return false
}
ch, ech := walk.Walk(args, opt)
// harvest errors
errs := make([]string, 0, 8)
var wg sync.WaitGroup
wg.Add(1)
go func() {
for e := range ech {
errs = append(errs, fmt.Sprintf("%s", e))
}
wg.Done()
}()
// now harvest results - we know we will only get files and their info.
res := make([]result, 0, 1024)
sizes := make(map[string]uint64)
for r := range ch {
// don't count hardlinks
if hardlinked(r.Stat, r.Path) {
continue
}
sz := uint64(r.Stat.Size())
for i := range args {
nm := args[i]
if strings.HasPrefix(r.Path, nm) {
sizes[nm] += sz
break
}
}
if all {
res = append(res, result{r.Path, sz})
}
}
wg.Wait()
if len(errs) > 0 {
die("%s", strings.Join(errs, "\n"))
}
if !all {
for k, v := range sizes {
res = append(res, result{k, v})
}
}
var tot uint64
sort.Sort(bySize(res))
for i := range res {
r := res[i]
tot += r.size
fmt.Printf("%12s %s\n", size(r.size), r.name)
}
if total {
fmt.Printf("%12s TOTAL\n", size(tot))
}
}
type byLen []string
func (b byLen) Len() int {
return len(b)
}
// we're doing decreasing order of length
func (b byLen) Less(i, j int) bool {
return len(b[i]) > len(b[j])
}
func (b byLen) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
// This will be filled in by "build"
var RepoVersion string = "UNDEFINED"
var Buildtime string = "UNDEFINED"
var ProductVersion string = "UNDEFINED"