-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
153 lines (144 loc) · 3.41 KB
/
core.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
// package codereview ...
package codereview
// import
import (
"os"
"runtime"
"sync"
)
//
// CORE TYPES, VARS, CONTS
//
type statFile struct {
lineTotal int
lineSavings int
byteSavings int
}
type statFiles struct {
totalFiles int
processedFiles int
}
type (
files struct{ totalFiles, processedFiles int }
savings struct{ linesTotal, sLines, sBytes int }
)
var (
bg, walk, walkStat sync.WaitGroup
worker = runtime.NumCPU()
channel_statFile = make(chan statFile, maxTargets)
channel_statFiles = make(chan statFiles, maxTargets)
channel_dir = make(chan string, chanBuff*25)
channel_c = make(chan string, chanBuff)
channel_go = make(chan string, chanBuff)
channel_sh = make(chan string, chanBuff/4)
channel_asm = make(chan string, chanBuff/4)
channel_make = make(chan string, chanBuff/4)
channelFiles = make(chan files, 1)
channelSavings = make(chan savings, 1)
_goDS = []byte(_goD) // thanks, for not having immutable constant slices in go!
_goDSO = []byte(_goDO)
_lf = []byte(_lineFeed)
)
//
// CORE FUNCTIONS
//
// dirWalk ...
func (c *Config) dirWalk() TotalStats {
// spin up worker
go func() {
if c.LangC {
bg.Add(worker)
go c.workerC()
}
if c.LangGO {
bg.Add(worker)
go c.workerGO()
}
if c.LangSH {
bg.Add(worker)
go c.workerSH()
}
if c.LangASM {
bg.Add(worker)
go c.workerASM()
}
if c.LangMAKE {
bg.Add(worker)
go c.workerMake()
}
}()
// spin up stats collection
walkStat.Add(worker)
go func() {
go func() {
var linesTotal, sLines, sBytes int
for s := range channel_statFile {
linesTotal += s.lineTotal
sLines += s.lineSavings
sBytes += s.byteSavings
}
channelSavings <- savings{linesTotal: linesTotal, sLines: sLines, sBytes: sBytes}
}()
go func() {
var totalFiles, processedFiles int
for s := range channel_statFiles {
totalFiles += s.totalFiles
processedFiles += s.processedFiles
}
channelFiles <- files{totalFiles: totalFiles, processedFiles: processedFiles}
}()
}()
// spin up directory walker
{
walk.Add(1)
go c.fastWalker()
}
walk.Wait()
{
close(channel_dir)
close(channel_c)
close(channel_sh)
close(channel_go)
close(channel_asm)
close(channel_make)
}
bg.Wait()
walkStat.Wait()
{
close(channel_statFile)
close(channel_statFiles)
}
filesResult := <-channelFiles
savingsResult := <-channelSavings
return TotalStats{
TotalFiles: filesResult.totalFiles,
ProcessedFiles: filesResult.processedFiles,
TotalLines: savingsResult.linesTotal,
TotalSavingsLines: savingsResult.sLines,
TotalSavingsBytes: savingsResult.sBytes,
}
}
// finalizeWorker ...
func (c *Config) finalizeWorker(totalLines, lineSavings, byteSavings int, filename string, compact []byte) Result {
r := Result{}
switch {
case c.Inplace:
if byteSavings > 0 {
if err := os.WriteFile(filename, compact, 0o644); err != nil {
errOut("[unable to write] [" + filename + "] [" + err.Error() + "]")
}
}
case c.HashReturn:
r.Result = slice2Hex(hash(compact))
case c.HashOut && c.SingleFile:
out(string(slice2Hex(hash(compact))))
case c.HashOut && c.Verbose:
out("[" + string(slice2Hex(hash(compact))) + "] [" + filename + "]")
case !c.Inplace && c.SingleFile:
out(string(compact))
}
r.TotalLines = totalLines
r.LineSavings = lineSavings
r.ByteSavings = byteSavings
return r
}