-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
233 lines (185 loc) · 3.93 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
224
225
226
227
228
229
230
231
232
233
package main
import (
"os"
"path"
"strings"
"sync"
)
var (
// DoQuiet means no output
DoQuiet bool
// Files are the filepaths to be compressed/uncompressed
Files []string
// DstDir is the optional location to place compressed/uncompressed files
DstDir string
// doBring bool
// doSingleArchive bool
// dstArchive string
)
func init() {
if helpRequested() {
printHelp()
os.Exit(0)
}
setGlobalVars()
}
// Check whether the user requested help.
func helpRequested() bool {
if tooFewArgs := (len(os.Args) < 2); tooFewArgs {
return true
}
switch os.Args[1] {
case "-h", "h", "help", "--help", "-H", "H", "HELP", "--HELP", "-help", "--h", "--H":
return true
}
return false
}
// Parse user arguments and modify global variables accordingly.
func setGlobalVars() {
max := len(os.Args)
for i := 1; i < max; i++ {
arg := os.Args[i]
switch arg {
case "-q":
DoQuiet = true
case "--dst-dir":
i, arg = nextArg(i)
DstDir = arg
default:
if strings.HasPrefix(arg, "--dst-dir=") {
DstDir = strings.Replace(arg, "--dst-dir=", "", 1)
} else {
Files = append(Files, arg)
}
}
}
if len(Files) > 1 {
DoQuiet = true
}
if DoQuiet {
print = printNoop
}
if DstDir != "" {
DstDir = path.Clean(DstDir)
}
return
}
func nextArg(i int) (int, string) {
i++
if i >= len(os.Args) {
printHelp()
os.Exit(2)
}
arg := os.Args[i]
return i, arg
}
func main() {
// if doSingleArchive {
// }
editFiles()
}
func editFiles() {
lenFiles := len(Files)
var wg sync.WaitGroup
wg.Add(lenFiles)
chanErr := make(chan error, lenFiles)
for _, path := range Files {
go func(path string) {
defer wg.Done()
//path = filepath.Clean(path)
dstName, err := compressOrDecompress(path)
print(dstName)
chanErr <- err
}(path)
}
wg.Wait()
close(chanErr)
for err := range chanErr {
if err != nil {
print(err)
}
}
}
// Determine whether a file should be compressed, uncompressed, or
// added to a tar archive and then compressed.
func compressOrDecompress(path string) (string, error) {
src, err := os.Open(path)
if err != nil {
return "", err
}
defer src.Close()
var dstName string
switch {
// If `src` is a snappy file, uncompress it.
case isSz(src):
dstName, err = unsnapAndUntar(src)
// If `src` is a directory, tar it before compressing it.
// (Simultaneously compressing and tarring the file
// results in a much lower compression ratio.)
case isDir(src):
dstName, err = tarAndSnap(src)
// If `src` is any other type, compress it.
default:
dstName, err = snap(src)
}
return dstName, err
}
// Uncompress a file.
// Then, if the uncompressed file is a tar archive, extract it as well.
func unsnapAndUntar(src *os.File) (string, error) {
// Uncompress it.
unsnappedName, err := unsnap(src)
if err != nil {
return "", err
}
unsnapped, err := os.Open(unsnappedName)
if err != nil {
return "", err
}
defer unsnapped.Close()
// If `unsnapped` is not a tar archive, we are done.
if done := !isTar(unsnapped); done {
return unsnappedName, nil
}
// Remove the temporary tar archive if nothing dies.
defer func() {
if err != nil {
return
}
os.Remove(unsnappedName)
}()
dstName, err := untar(unsnapped)
if err != nil {
return "", err
}
return dstName, nil
}
// Make a temporary tar archive of a file and then compress it.
// (Simultaneously compressing and tarring the file
// results in a much lower compression ratio.)
// Remove the temporary tar archive if no errors occur.
func tarAndSnap(src *os.File) (string, error) {
// Tar it.
tarredName, err := tarDir(src)
if err != nil {
return "", err
}
tarred, err := os.Open(tarredName)
if err != nil {
return "", err
}
defer tarred.Close()
// Remove the temporary tar archive if nothing dies.
defer func() {
if err != nil {
return
}
os.Remove(tarredName)
}()
// Compress it.
dstName, err := snap(tarred)
if err != nil {
return "", err
}
return dstName, nil
}