-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtszero.go
276 lines (244 loc) · 7.19 KB
/
tszero.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
//Package to implement the tszero module
package main
import (
"archive/tar"
"archive/zip"
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"runtime/debug"
"time"
)
const (
tarFmt = "tar"
zipFmt = "zip"
)
var emptyByteArray []byte
type config struct {
// The program name
programName string
// This should be set to "tar" or "zip"
format string
// If true, the help flag was specified.
help bool
// if true, the v (verbose) flag was specified
verbose bool
// The name of the archive file to process or empty string to indicate processing the stdin
fileName string
// The command line arguments that are not flags
args []string
}
// Set by main to the configuration
var conf *config
const bufferSize = 1024 * 8
// parses the command-line arguments provided to the program and initialize flags set from the command line.
//
// os.Args[0] is provided as 'programName' and os.args[1:] as 'args'.
// Returns the Config in case parsing succeeded, or an error.
func initFlags(programName string, args []string) (cnf *config, err error) {
flags := flag.NewFlagSet(programName, flag.ContinueOnError)
flags.SetOutput(flag.CommandLine.Output())
var myConf config
myConf.programName = programName
flags.StringVar(&myConf.format, "format", "", "The value of format must be tar or zip.")
flags.BoolVar(&myConf.help, "help", false, "Specify this to see the help message.")
flags.BoolVar(&myConf.verbose, "v", false, "Verbose")
flag.Usage = func() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:is\n", os.Args[0])
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "%s -format tar [-v] [-help] filename\n", os.Args[0])
_, _ = fmt.Fprint(flag.CommandLine.Output(), "or\n")
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "%s -format zip [-v] [-help] filename\n", os.Args[0])
flags.PrintDefaults()
}
err = flags.Parse(args)
if err != nil {
return nil, err
}
myConf.args = flags.Args()
return &myConf, nil
}
// Set the timestamp fields of a header to zero
func zeroHeaderTimeFields(header *tar.Header) {
header.ChangeTime = time.Time{}
header.AccessTime = time.Time{}
header.ModTime = time.Time{}
}
// Handle a tar archive
func doTar(reader io.Reader, out io.Writer) {
tarReader := tar.NewReader(reader)
tarWriter := tar.NewWriter(out)
for {
header, done := readTarHeader(tarReader)
if done {
return
}
logMaybe("processing ", header.Name)
zeroHeaderTimeFields(header)
writeTarHeader(tarWriter, header)
copyContent(tarReader, tarWriter, header)
}
}
func writeTarHeader(tarWriter *tar.Writer, header *tar.Header) {
var h = *header
h.Format = tar.FormatGNU
writeErr := tarWriter.WriteHeader(&h)
if writeErr != nil {
log.Fatal("Error writing header", h, '\n', writeErr)
}
}
func readTarHeader(tarReader *tar.Reader) (*tar.Header, bool) {
header, readErr := tarReader.Next()
if readErr != nil {
if readErr == io.EOF {
return nil, true
}
log.Fatal("Error reading next header: ", readErr)
}
return header, false
}
func copyContent(tarReader *tar.Reader, tarWriter *tar.Writer, header *tar.Header) {
buffer := make([]byte, bufferSize)
for {
count, done := readTarContent(tarReader, buffer)
if done {
return
}
writeTarContent(tarWriter, buffer[:count], header)
}
}
func writeTarContent(tarWriter *tar.Writer, buffer []byte, header *tar.Header) {
switch header.Typeflag {
case tar.TypeLink, tar.TypeSymlink, tar.TypeChar, tar.TypeBlock, tar.TypeDir, tar.TypeFifo:
return // These types contain no data
default:
_, err := tarWriter.Write(buffer)
if err != nil {
log.Fatalf("Error writing contents of %+v: %s\nLength of write buffer is %d", header, err, len(buffer))
}
}
}
// Read the current tar content into the buffer.
// Returns true if all the bytes have been read or false if there are more bytes to be read.
func readTarContent(tarReader *tar.Reader, buffer []byte) (int, bool) {
count, err := tarReader.Read(buffer)
if err == io.EOF && count == 0 {
return 0, true
}
if err != nil && err != io.EOF {
log.Fatal(err)
}
return count, false
}
// Handle a zip archive
//goland:noinspection GoUnusedParameter
func doZip(fileName string, out io.Writer) {
zipReader, err := zip.OpenReader(fileName)
if err != nil {
log.Fatalf("Failed to open file %s: %s", flag.Arg(0), err)
}
defer func(zipReader *zip.ReadCloser) {
err := zipReader.Close()
if err != nil {
log.Printf("Error closing input %s", fileName)
}
}(zipReader)
zipWriter := zip.NewWriter(out)
defer func(zipWriter *zip.Writer) {
err := zipWriter.Close()
if err != nil {
log.Fatal("Error closing output")
}
}(zipWriter)
for _, thisFile := range zipReader.File {
logMaybe("Copying ", thisFile.Name)
zeroZipHeaderTimestamps(thisFile)
fileWriter := createHeader(zipWriter, thisFile.FileHeader)
fileReader := getReader(thisFile)
byteCount := copyFile(fileWriter, fileReader, thisFile)
logMaybe("Copied ", fmt.Sprint(byteCount), " bytes.")
}
}
func copyFile(fileWriter io.Writer, fileReader io.ReadCloser, thisFile *zip.File) int64 {
byteCount, copyErr := io.Copy(fileWriter, fileReader)
if copyErr != nil {
log.Fatalf("Error (%s) copying file from source: %s", copyErr, thisFile.FileHeader.Name)
}
return byteCount
}
func getReader(thisFile *zip.File) io.ReadCloser {
fileReader, err := thisFile.Open()
if err != nil {
log.Fatalf("Error (%s) opening file in zip for reading: %s", err, thisFile.FileHeader.Name)
}
return fileReader
}
func createHeader(zipWriter *zip.Writer, fh zip.FileHeader) io.Writer {
fileWriter, err := zipWriter.CreateHeader(&fh)
if err != nil {
log.Fatalf("Error (%s) creating header in output: %+v", err, fh)
}
return fileWriter
}
//goland:noinspection GoDeprecation
func zeroZipHeaderTimestamps(thisFile *zip.File) {
thisFile.FileHeader.Modified = time.UnixMicro(0)
thisFile.Extra = emptyByteArray
}
// provide the reader that we will use to read the archive.
func withFileReader(consumer func(io.Reader)) {
file, err := os.Open(conf.args[0]) // For read access.
if err == nil {
consume(consumer, file)
return
}
log.Fatal(err, " File name: ", conf.fileName)
}
// Call the given consumer function, passing it the given file object wrapped in a buffered reader.
// Ensure that the file is closed before returning.
func consume(consumer func(io.Reader), file *os.File) {
defer func(file *os.File) {
_ = file.Close()
}(file)
consumer(bufio.NewReader(file))
}
//main entry point into
func main() {
log.SetOutput(flag.CommandLine.Output())
defer stacktrace()
var err error
conf, err = initFlags(os.Args[0], os.Args[1:])
if err != nil {
log.Fatalf("Error parsing command line: %s", err)
}
logMaybe("tszero starting")
if conf.help || len(conf.args) != 1 {
flag.Usage()
} else {
switch conf.format {
case tarFmt:
withFileReader(func(reader io.Reader) { doTar(reader, os.Stdout) })
case zipFmt:
if len(conf.args) < 1 {
log.Fatal("Processing a zip file requires at least one file name.")
}
doZip(conf.args[0], os.Stdout)
default:
// TODO auto-detect type of archive file and make the format flag optional.
flag.Usage()
}
}
logMaybe("tszero finished")
}
func logMaybe(msg ...string) {
if conf.verbose {
log.Println(msg)
}
}
func stacktrace() {
if r := recover(); r != nil {
fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
}
}