-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
261 lines (234 loc) · 6.06 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
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
// Copyright (c) 2010, Andrei Vieru. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
//"compress/lzma"
"code.google.com/p/lzma"
"flag"
"fmt"
"io"
"log"
"os"
"path"
"runtime"
"strings"
)
var (
stdout = flag.Bool("c", false, "write on standard output, keep original files unchanged")
decompress = flag.Bool("d", false, "decompress; see also -c and -k")
force = flag.Bool("f", false, "force overwrite of output file")
help = flag.Bool("h", false, "print this help message")
keep = flag.Bool("k", false, "keep original files unchaned")
suffix = flag.String("s", "lzma", "use provided suffix on compressed files")
level = flag.Int("l", 5, "compression level [1 ... 9]")
cores = flag.Int("cores", 1, "number of cores to use for parallelization")
stdin bool
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]... [FILE]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Compress or uncompress FILE (by default, compress FILE in-place).\n\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nWith no FILE, or when FILE is -, read standard input.\n")
}
func exit(msg string) {
usage()
fmt.Fprintln(os.Stderr)
log.Fatalf("%s: check args: %s\n\n", os.Args[0], msg)
}
func setByUser(name string) (isSet bool) {
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
isSet = true
}
})
return
}
func main() {
flag.Parse()
if *help == true {
usage()
log.Fatal(0)
}
//if *stdout == true && *suffix != "lzma" {
if *stdout == true && setByUser("s") == true {
exit("stdout set, suffix not used")
}
if *stdout == true && *force == true {
exit("stdout set, force not used")
}
if *stdout == true && *keep == true {
exit("stdout set, keep is redundant")
}
if flag.NArg() > 1 {
exit("too many file, provide at most one file at a time or check order of flags")
}
//if *decompress == true && *level != 5 {
if *decompress == true && setByUser("l") == true {
exit("compression level is used for compression only")
}
if *level < 1 || *level > 9 {
exit("compression level out of range")
}
if *cores < 1 || *cores > 32 {
exit("invalid number of cores")
}
runtime.GOMAXPROCS(*cores)
var inFilePath string
var outFilePath string
if flag.NArg() == 0 || flag.NArg() == 1 && flag.Args()[0] == "-" { // parse args: read from stdin
if *stdout != true {
exit("reading from stdin, can write only to stdout")
}
//if *suffix != "lzma" {
if setByUser("s") == true {
exit("reading from stdin, suffix not needed")
}
stdin = true
} else if flag.NArg() == 1 { // parse args: read from file
inFilePath = flag.Args()[0]
f, err := os.Lstat(inFilePath)
if err != nil {
log.Fatal(err.Error())
}
if f == nil {
exit(fmt.Sprintf("file %s not found", inFilePath))
}
if !!f.IsDir() {
exit(fmt.Sprintf("%s is not a regular file", inFilePath))
}
if *stdout == false { // parse args: write to file
if *suffix == "" {
exit("suffix can't be an empty string")
}
if *decompress == true {
outFileDir, outFileName := path.Split(inFilePath)
if strings.HasSuffix(outFileName, "."+*suffix) {
if len(outFileName) > len("."+*suffix) {
nstr := strings.SplitN(outFileName, ".", len(outFileName))
estr := strings.Join(nstr[0:len(nstr)-1], ".")
outFilePath = outFileDir + estr
} else {
log.Fatalf("error: can't strip suffix .%s from file %s", *suffix, inFilePath)
}
} else {
exit(fmt.Sprintf("file %s doesn't have suffix .%s", inFilePath, *suffix))
}
} else {
outFilePath = inFilePath + "." + *suffix
}
f, err = os.Lstat(outFilePath)
if err != nil && f != nil { // should be: ||| if err != nil && err != "file not found" ||| but i can't find the error's id
log.Fatal(err.Error())
}
if f != nil && !f.IsDir() {
if *force == true {
err = os.Remove(outFilePath)
if err != nil {
log.Fatal(err.Error())
}
} else {
exit(fmt.Sprintf("outFile %s exists. use force to overwrite", outFilePath))
}
} else if f != nil {
exit(fmt.Sprintf("outFile %s exists and is not a regular file", outFilePath))
}
}
}
pr, pw := io.Pipe()
//defer pr.Close()
//defer pw.Close()
if *decompress {
// read from inFile into pw
go func() {
defer pw.Close()
var inFile *os.File
var err error
if stdin == true {
inFile = os.Stdin
} else {
inFile, err = os.Open(inFilePath)
}
defer inFile.Close()
if err != nil {
log.Fatal(err.Error())
}
_, err = io.Copy(pw, inFile)
if err != nil {
log.Fatal(err.Error())
}
}()
// write into outFile from z
defer pr.Close()
z := lzma.NewReader(pr)
defer z.Close()
var outFile *os.File
var err error
if *stdout == true {
outFile = os.Stdout
} else {
outFile, err = os.Create(outFilePath)
}
defer outFile.Close()
if err != nil {
log.Fatal(err.Error())
}
_, err = io.Copy(outFile, z)
if err != nil {
log.Fatal(err.Error())
}
} else {
// read from inFile into z
go func() {
defer pw.Close()
var z io.WriteCloser
var inFile *os.File
var err error
if stdin == true {
inFile = os.Stdin
defer inFile.Close()
z = lzma.NewWriterLevel(pw, *level)
defer z.Close()
} else {
inFile, err = os.Open(inFilePath)
defer inFile.Close()
if err != nil {
log.Fatal(err.Error())
}
f, err := os.Lstat(inFilePath)
if err != nil {
log.Fatal(err.Error())
}
z = lzma.NewWriterSizeLevel(pw, int64(f.Size()), *level)
defer z.Close()
}
_, err = io.Copy(z, inFile)
if err != nil {
log.Fatal(err.Error())
}
}()
// write into outFile from pr
defer pr.Close()
var outFile *os.File
var err error
if *stdout == true {
outFile = os.Stdout
} else {
outFile, err = os.Create(outFilePath)
}
defer outFile.Close()
if err != nil {
log.Fatal(err.Error())
}
_, err = io.Copy(outFile, pr)
if err != nil {
log.Fatal(err.Error())
}
}
if *stdout == false && *keep == false {
err := os.Remove(inFilePath)
if err != nil {
log.Fatal(err.Error())
}
}
}