-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.go
349 lines (319 loc) · 6.68 KB
/
io.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
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
package hq
import (
"io"
"io/fs"
"math/bits"
"os"
"runtime"
"sync"
"syscall"
"github.com/klauspost/compress/zstd"
"golang.org/x/term"
)
const (
_modeDir uint32 = 1 << (32 - 1 - 0)
_modeSymlink uint32 = 1 << (32 - 1 - 4)
)
//
// DISPLAY IO SECTION
//
// out ...
func out(msg string) {
os.Stdout.Write([]byte(msg + "\n"))
}
// outPlain ...
func outPlain(msg string) {
os.Stdout.Write([]byte(msg))
}
//
// KEYBOARD IO SECTION
//
// getRune ...
func getRune() byte {
if oldState, err := term.MakeRaw(0); err != nil {
panic(err)
} else {
//nolint:all - there is no alternative/reporting if this fails
defer term.Restore(0, oldState)
}
var buf [1]byte
if n, err := syscall.Read(0, buf[:]); n == 0 || err != nil {
panic(err)
}
return buf[0]
}
// readLine ...
func readLine(name string) string {
outPlain(name)
line, exit := []byte{}, false
for {
v := getRune()
switch v {
case 127, 8:
if l := len(line); l > 0 {
line = line[:l-1]
os.Stdout.Write(append([]byte{}, v))
}
case 13, 10:
exit = true
case 0:
default:
line = append(line, v)
os.Stdout.Write(append([]byte{}, v))
}
if exit {
break
}
}
os.Stdout.Write([]byte("\n"))
return string(line)
}
// readPassword ...
func readPassword(name string, masked bool) string {
outPlain(name)
var pass, bs, mask []byte
if masked {
bs = []byte("\b \b")
mask = []byte("*")
}
exit := false
for {
v := getRune()
switch v {
case 127, 8:
if l := len(pass); l > 0 {
pass = pass[:l-1]
os.Stdout.Write(bs)
}
case 13, 10:
exit = true
case 0:
default:
pass = append(pass, v)
os.Stdout.Write(mask)
}
if exit {
break
}
}
os.Stdout.Write([]byte("\n"))
return string(pass)
}
//
// COMMANDLINE ARGS, PIPE AND ENV SECTION
//
// getArgs ...
func getArgs() [10]string {
var param [10]string
l := len(os.Args)
if l > 2 {
offset := 2
action := os.Args[1]
if action == "r" || action == "run" {
offset++
}
for i := offset; i < l; i++ {
param[i-offset] = os.Args[i]
}
}
return param
}
// isEnv ...
func isEnv(in string) bool {
if _, ok := syscall.Getenv(in); ok {
return true
}
return false
}
// isPipe ...
func isPipe() bool {
out, _ := os.Stdin.Stat()
return out.Mode()&os.ModeCharDevice == 0
}
// getPipe ...
func getPipe() string {
pipe, err := io.ReadAll(os.Stdin)
if err != nil {
errExit("while reading data from pipe")
}
return string(pipe)
}
//
// FILE IO SECTION
//
// isDir ...
func isDir(filename string) bool {
fi, err := os.Stat(filename)
if err != nil {
return false
}
return uint32(fi.Mode())&_modeDir != 0
}
// isReadable ...
func isReadable(filename string) bool {
f, err := os.Open(filename)
if err != nil {
return false
}
f.Close()
return true
}
// readFileErrExit ...
func readFileErrExit(filename string) (data []byte) {
var err error
if data, err = os.ReadFile(filename); err != nil {
errExit("unable to read file [" + filename + "]" + "[" + err.Error() + "]")
}
return data
}
// writeFileErrExit writes a file and flushes via f.Sync cache to phys disk
func writeFileErrExit(filename string, data []byte, filemode fs.FileMode) {
if err := os.WriteFile(filename, data, filemode); err != nil {
errExit("unable to write file [" + filename + "]" + "[" + err.Error() + "]")
}
f, err := os.Open(filename)
if err != nil {
errExit("unable to [sync|verify] file on disk status [" + filename + "]" + "[" + err.Error() + "]")
}
defer f.Close()
err = f.Sync()
if err != nil {
errExit("unable to sync file to disk [" + filename + "]" + "[" + err.Error() + "]")
}
}
// recursiveFileList ...
func recursiveFileList(path string, worker int) []string {
chanNames := make(chan string, 1)
chanReport := make(chan []string, 1)
go func() {
var list []string
for name := range chanNames {
list = append(list, name)
}
chanReport <- list
}()
dirlist := readDir(path)
for _, item := range dirlist {
name := fixPath(path) + item.Name()
switch {
case uint32(item.Type())&_modeDir != 0:
fastWalk(name, chanNames, worker)
case len(name) > 39 && name[:6] == ".hqMAP":
continue
default:
chanNames <- name
}
}
close(chanNames)
return <-chanReport
}
// walk ...
func walk(path string, chanNames chan string) {
list, err := os.ReadDir(path)
if err != nil {
if path != "" {
errOut("unable to read directory [" + path + "] [" + err.Error() + "]")
}
return
}
for _, item := range list {
name := path + "/" + item.Name()
switch {
case uint32(item.Type())&_modeDir != 0:
walk(name, chanNames)
default:
chanNames <- name
}
}
}
// fastWalk ...
func fastWalk(path string, chanNames chan string, threads int) {
bg := sync.WaitGroup{}
chanDir := make(chan string, 10000)
for i := 0; i < threads; i++ {
go func() {
for path := range chanDir {
list, err := os.ReadDir(path)
if err != nil {
errOut("unable to read directory [" + path + "] [" + err.Error() + "]")
bg.Done()
continue
}
for _, item := range list {
name := path + "/" + item.Name()
switch {
case uint32(item.Type())&_modeDir != 0:
bg.Add(1)
chanDir <- name
default:
chanNames <- name
}
}
bg.Done()
}
}()
}
bg.Add(1)
chanDir <- path
bg.Wait()
close(chanDir)
}
// fixPath ...
func fixPath(path string) string {
switch path {
case "/":
return path
case ".":
return ""
default:
return path + "/"
}
}
// readDir ...
func readDir(path string) (list []fs.DirEntry) {
var err error
if list, err = os.ReadDir(path); err != nil {
errExit("unable to list directory [" + path + "]")
}
return list
}
//
// FILE IO COMPRESSION SECTION
//
// compressWriteFile ...
func compressWriteFile(filename string, data []byte, level int, filemode fs.FileMode) {
writeFileErrExit(filename, compressZstd(data, level), filemode)
}
// decompressReadFile ...
func decompressReadFile(filename string) (data []byte) {
return decompressZstd(readFileErrExit(filename))
}
// decompressZstd ...
func decompressZstd(message []byte) []byte {
e, _ := zstd.NewReader(nil)
dst, _ := e.DecodeAll(message, nil)
e.Close()
return dst
}
// compressZstd ...
func compressZstd(message []byte, level int) []byte {
// @UPSTREAM FIX ISSUE
// compress/zstd encoder MaxWindowSize 32bit OS mem alloc fail hack
threads := runtime.NumCPU()
if bits.UintSize == 32 {
runtime.GC()
threads = 1
}
e, _ := zstd.NewWriter(nil,
// zstd.WithWindowSize(zstd.MaxWindowSize),
zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(level)),
zstd.WithEncoderCRC(false),
zstd.WithZeroFrames(false),
zstd.WithLowerEncoderMem(false),
zstd.WithAllLitEntropyCompression(false),
zstd.WithNoEntropyCompression(true),
zstd.WithEncoderConcurrency(threads))
dst := e.EncodeAll(message, nil)
e.Close()
return dst
}