-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
446 lines (410 loc) · 12 KB
/
main.go
File metadata and controls
446 lines (410 loc) · 12 KB
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"path/filepath"
"strings"
)
const makeProfile = false
var flagBlockSize uint = defaultBlockSize
func main() {
defer startProfile()()
if len(os.Args) < 2 {
showUsage()
fmt.Println("\nError: No mode specified.")
return
}
if strings.HasPrefix(os.Args[1], "-") {
if handleTopLevel(os.Args[1:]) {
return
}
}
cmdLetter, opts := parseCommand(os.Args[1])
if !strings.ContainsRune("cljx", rune(cmdLetter)) {
showUsage()
fmt.Printf("\nError: Unknown mode: %s\n", os.Args[1])
return
}
flagSet, mflags := initFlags()
flagSet.Parse(os.Args[2:])
blockSize = uint32(flagBlockSize)
quietMode = toStdOut || cmdLetter == 'j'
if quietMode {
progress = false
}
configureFEC(mflags)
if mflags.showVer {
fmt.Println("goxa v" + appVersion)
return
}
mflags.format = detectArchiveFormat(cmdLetter, mflags.format)
buildExtractList(mflags.sel)
applyModeOptions(opts)
configureCompression(mflags.format)
configureSpeed(mflags.speedOpt)
configureChecksum(mflags.sumOpt)
ensureArchiveExtension(cmdLetter, mflags.format)
runMode(cmdLetter, flagSet.Args(), mflags.format)
}
func showUsage() {
fmt.Println("goxa - Go eXpress Archive")
fmt.Println()
fmt.Println("Usage:")
fmt.Println(" goxa MODE[flags] [options] -arc FILE [paths...]")
fmt.Println()
fmt.Println("Modes:")
fmt.Println(" c create an archive")
fmt.Println(" l list archive contents")
fmt.Println(" j output JSON listing")
fmt.Println(" x extract files")
fmt.Println()
fmt.Println("Flags (append after the mode letter):")
fmt.Println(" a store absolute paths p preserve permissions")
fmt.Println(" m preserve modification times s disable checksums")
fmt.Println(" b per-block checksums n disable compression")
fmt.Println(" i include hidden files o allow special files")
fmt.Println(" u use flags from archive v verbose output")
fmt.Println(" f force overwrite / ignore read errors")
fmt.Println()
fmt.Println("Options:")
fmt.Println(" -arc FILE archive file name")
fmt.Println(" -stdout write archive to stdout")
fmt.Println(" -files LIST comma separated files to extract")
fmt.Println(" -progress=false disable progress display")
fmt.Println(" -interactive=false disable prompts for archive flags")
fmt.Println(" -comp ALG compression algorithm (gzip, zstd, lz4, s2, snappy, brotli, xz, none)")
fmt.Println(" -speed LEVEL compression speed (fastest, default, better, best)")
fmt.Println(" -sum ALG checksum algorithm (crc32, crc16, xxhash, sha256, blake3)")
fmt.Println(" -block N compression block size in bytes")
fmt.Println(" -threads N number of threads to use")
fmt.Println(" -format FORMAT archive format (goxa or tar)")
fmt.Println(" -retries N retries when file changes during read (0 = never give up)")
fmt.Println(" -retrydelay N delay between retries in seconds")
fmt.Println(" -failonchange treat changed files as fatal errors")
fmt.Println(" -bombcheck=false disable zip bomb detection")
fmt.Println(" -spacecheck=false disable free space check")
fmt.Println(" -noflush skip final disk flush")
fmt.Println(" -version print program version")
fmt.Println(" -pgo run built-in PGO training (10k files ~2GB, s-curve around 150KB)")
fmt.Println(" -fec-data N number of FEC data shards (default 10)")
fmt.Println(" -fec-parity N number of FEC parity shards (default 3)")
fmt.Println(" -fec-level L FEC redundancy preset (low, medium, high)")
fmt.Println()
fmt.Println("Extensions:")
fmt.Println(" .b32/.b64 output Base32/Base64 encoded archive")
fmt.Println(" .goxaf FEC encoded archive")
fmt.Println(" .tar, .tar.gz tar archive (gzipped if .gz)")
fmt.Println(" .tar.xz tar archive compressed with xz")
fmt.Println()
fmt.Println("Examples:")
fmt.Println(" goxa -version # print version")
fmt.Println(" goxa -pgo # generate default.pgo using 10k files")
fmt.Println(" goxa c -arc=backup.goxa dir/ # create archive")
fmt.Println(" goxa x -arc=backup.goxa # extract to folder")
fmt.Println(" goxa c -arc=backup.tar.gz dir/ # create tar.gz")
fmt.Println(" goxa c -arc=backup.goxa.b64 dir/ # Base64 encoded archive")
fmt.Println(" goxa c -arc=backup.goxaf dir/ # FEC encoded archive")
}
type flagSettings struct {
sel string
format string
speedOpt string
sumOpt string
fecData int
fecParity int
fecLevel string
showVer bool
}
func startProfile() func() {
if !makeProfile {
return func() {}
}
f, err := os.Create("perf.pprof")
if err != nil {
log.Fatalf("could not create CPU profile: %v", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatalf("could not start CPU profile: %v", err)
}
return func() {
pprof.StopCPUProfile()
f.Close()
}
}
func handleTopLevel(args []string) bool {
fs := flag.NewFlagSet("goxa", flag.ExitOnError)
var showVer bool
var showHelp bool
var pgo bool
fs.BoolVar(&showVer, "version", false, "print version and exit")
fs.BoolVar(&showHelp, "help", false, "show help")
fs.BoolVar(&showHelp, "h", false, "show help")
fs.BoolVar(&pgo, "pgo", false, "run PGO training and exit")
fs.Parse(args)
if showVer {
fmt.Println("goxa v" + appVersion)
return true
}
if pgo {
runPGOTraining()
return true
}
if showHelp {
showUsage()
return true
}
showUsage()
return true
}
func parseCommand(cmd string) (byte, string) {
cmd = strings.ToLower(cmd)
if cmd == "" {
return 0, ""
}
letter := cmd[0]
opts := ""
if len(cmd) > 1 {
opts = cmd[1:]
}
return letter, opts
}
func initFlags() (*flag.FlagSet, *flagSettings) {
fs := flag.NewFlagSet("goxa", flag.ExitOnError)
f := &flagSettings{}
fs.StringVar(&archivePath, "arc", defaultArchiveName, "archive file name (extension not required)")
fs.BoolVar(&toStdOut, "stdout", false, "output archive data to stdout")
fs.BoolVar(&progress, "progress", true, "show progress bar")
fs.BoolVar(&interactiveMode, "interactive", true, "prompt when archive uses extra flags")
fs.StringVar(&compression, "comp", "zstd", "compression: gzip|zstd|lz4|s2|snappy|brotli|xz|none")
fs.StringVar(&f.speedOpt, "speed", "fastest", "compression speed: fastest|default|better|best")
fs.StringVar(&f.sumOpt, "sum", "blake3", "checksum: crc32|crc16|xxhash|sha256|blake3")
fs.UintVar(&flagBlockSize, "block", defaultBlockSize, "compression block size in bytes")
fs.IntVar(&threads, "threads", runtime.NumCPU(), "number of threads to use")
fs.StringVar(&f.format, "format", "goxa", "archive format: tar|goxa")
fs.StringVar(&f.sel, "files", "", "comma-separated list of files and directories to extract")
fs.IntVar(&f.fecData, "fec-data", fecDataShards, "FEC data shards")
fs.IntVar(&f.fecParity, "fec-parity", fecParityShards, "FEC parity shards")
fs.StringVar(&f.fecLevel, "fec-level", "", "FEC redundancy preset: low|medium|high")
fs.IntVar(&fileRetries, "retries", 3, "retries when file changes during read (0=never give up)")
fs.IntVar(&fileRetryDelay, "retrydelay", 5, "delay between retries in seconds")
fs.BoolVar(&failOnChange, "failonchange", false, "treat file change after retries as fatal")
fs.BoolVar(&bombCheck, "bombcheck", true, "detect extremely compressed files")
fs.BoolVar(&spaceCheck, "spacecheck", true, "verify free disk space before operations")
fs.BoolVar(&noFlush, "noflush", false, "skip final disk flush")
fs.BoolVar(&f.showVer, "version", false, "print version and exit")
return fs, f
}
func configureFEC(f *flagSettings) {
switch f.fecLevel {
case "low":
fecDataShards, fecParityShards = 10, 3
case "medium":
fecDataShards, fecParityShards = 8, 4
case "high":
fecDataShards, fecParityShards = 5, 5
case "":
fecDataShards = f.fecData
fecParityShards = f.fecParity
default:
log.Fatalf("invalid fec-level: %s", f.fecLevel)
}
}
func detectArchiveFormat(cmdLetter byte, format string) string {
detFmt, noComp := detectFormatFromExt(archivePath)
if detFmt != "" {
format = detFmt
if detFmt == "tar" {
if noComp {
features.Set(fNoCompress)
} else {
features.Clear(fNoCompress)
}
}
}
if cmdLetter != 'c' {
if fFmt, fNoComp, ok := detectFormatFromHeader(archivePath); ok {
format = fFmt
if fFmt == "tar" {
if fNoComp {
features.Set(fNoCompress)
} else {
features.Clear(fNoCompress)
}
}
}
}
return format
}
func buildExtractList(sel string) {
if sel == "" {
return
}
parts := strings.Split(sel, ",")
for _, p := range parts {
p = strings.TrimSpace(p)
if p == "" {
continue
}
extractList = append(extractList, filepath.Clean(p))
}
}
func applyModeOptions(opts string) {
for _, letter := range opts {
switch letter {
case 'a':
features.Set(fAbsolutePaths)
case 'p':
features.Set(fPermissions)
case 'm':
features.Set(fModDates)
case 's':
features.Clear(fChecksums)
case 'b':
features.Set(fChecksums)
features.Set(fBlockChecksums)
case 'n':
features.Set(fNoCompress)
case 'i':
features.Set(fIncludeInvis)
case 'o':
features.Set(fSpecialFiles)
case 'u':
useArchiveFlags = true
case 'v':
verboseMode = true
case 'f':
doForce = true
default:
continue
}
}
}
func configureCompression(format string) {
switch strings.ToLower(compression) {
case "gzip":
compType = compGzip
case "zstd":
compType = compZstd
case "lz4":
compType = compLZ4
case "s2":
compType = compS2
case "snappy":
compType = compSnappy
case "brotli":
compType = compBrotli
case "xz":
if strings.ToLower(format) == "tar" {
tarUseXz = true
} else {
compType = compXZ
}
case "none":
features.Set(fNoCompress)
compType = compGzip
default:
log.Fatalf("Unknown compression: %s", compression)
}
}
func configureSpeed(speed string) {
switch strings.ToLower(speed) {
case "fastest":
compSpeed = SpeedFastest
case "default":
compSpeed = SpeedDefault
case "better":
compSpeed = SpeedBetterCompression
case "best":
compSpeed = SpeedBestCompression
default:
log.Fatalf("Unknown speed: %s", speed)
}
}
func configureChecksum(sum string) {
switch strings.ToLower(sum) {
case "crc32":
checksumType = sumCRC32
checksumLength = 4
case "crc16":
checksumType = sumCRC16
checksumLength = 2
case "xxhash":
checksumType = sumXXHash
checksumLength = 8
case "sha256":
checksumType = sumSHA256
checksumLength = 32
case "blake3":
checksumType = sumBlake3
checksumLength = 32
default:
log.Fatalf("Unknown checksum: %s", sum)
}
}
func ensureArchiveExtension(cmdLetter byte, format string) {
if cmdLetter != 'c' || hasKnownArchiveExt(archivePath) {
return
}
if strings.ToLower(format) == "tar" {
if features.IsNotSet(fNoCompress) {
if tarUseXz {
archivePath += ".tar.xz"
} else {
archivePath += ".tar.gz"
}
} else {
archivePath += ".tar"
}
} else {
archivePath += ".goxa"
}
}
func runMode(cmdLetter byte, args []string, format string) {
switch cmdLetter {
case 'c':
if strings.ToLower(format) == "tar" {
if err := createTar(args); err != nil {
log.Fatalf("tar create failed: %v", err)
}
return
}
create(args)
case 'l':
if strings.ToLower(format) == "tar" {
log.Fatalf("list not supported for tar format")
}
extract(args, true, false)
case 'j':
if strings.ToLower(format) == "tar" {
log.Fatalf("list not supported for tar format")
}
extract(args, true, true)
case 'x':
if archivePath == defaultArchiveName {
log.Fatal("You must specify an archive to extract.")
}
if len(args) > 0 {
if features.IsSet(fAbsolutePaths) {
log.Fatal("Destination specified in conjunction with absolute path mode, stopping.")
}
}
if strings.ToLower(format) == "tar" {
dest := ""
if len(args) > 0 {
dest = args[0]
}
if err := extractTar(dest); err != nil {
log.Fatalf("tar extract failed: %v", err)
}
return
}
extract(args, false, false)
default:
showUsage()
doLog(false, "Unknown mode: %c", cmdLetter)
}
}