forked from oracle/smith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzip.go
210 lines (187 loc) · 4.25 KB
/
gzip.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
package main
import (
"compress/gzip"
"fmt"
"io"
"os/exec"
"sync"
"syscall"
"github.com/oracle/smith/execute"
"github.com/Sirupsen/logrus"
)
var (
hasPigz *bool
)
func HasPigz() bool {
if hasPigz == nil {
exists := false
if _, err := exec.LookPath("pigz"); err == nil {
exists = true
} else {
logrus.Warn("pigz binary not found, falling back to internal gzip")
}
hasPigz = &exists
}
return *hasPigz
}
type ReadSeekCloser interface {
io.ReadSeeker
io.Closer
}
type nopCloser struct {
io.ReadSeeker
}
func (nopCloser) Close() error { return nil }
func NopCloser(r io.ReadSeeker) ReadSeekCloser {
return nopCloser{r}
}
type disabledSeeker struct {
nopCloser
}
func (disabledSeeker) Seek(offset int64, whence int) (int64, error) {
return 0, fmt.Errorf("Seek not allowed")
}
func DisabledSeeker(r io.ReadSeeker) ReadSeekCloser {
return disabledSeeker{nopCloser{r}}
}
func MaybeGzipReader(in ReadSeekCloser) (io.ReadCloser, error) {
gzipReader, err := gzip.NewReader(in)
if err != nil {
logrus.Debugf("File is not a gzip, assuming tar: %v", err)
in.Seek(0, 0)
// disable seek so we get all data when calculating id
return DisabledSeeker(in), nil
}
if !HasPigz() {
return gzipReader, nil
}
in.Seek(0, 0)
pigzReader, err := NewPigzReader(in)
if err != nil {
logrus.Errorf("Failed to read with pigz")
return nil, err
}
return pigzReader, nil
}
type PigzReader struct {
stdout io.ReadCloser
stdin io.WriteCloser
cmd *exec.Cmd
commandLine string
wg sync.WaitGroup
}
func NewPigzReader(r io.Reader) (*PigzReader, error) {
z := PigzReader{}
z.cmd = exec.Command("pigz", "-d")
z.commandLine = "pigz -d"
var err error
z.stdout, err = z.cmd.StdoutPipe()
if err != nil {
logrus.Errorf("Failed to get stdout pipe for %s: %v", z.commandLine, err)
return nil, err
}
z.stdin, err = z.cmd.StdinPipe()
if err != nil {
logrus.Errorf("Failed to get stdout pipe for %s: %v", z.commandLine, err)
return nil, err
}
if err := z.cmd.Start(); err != nil {
logrus.Errorf("Error starting %s: %v", z.commandLine, err)
return nil, err
}
z.wg.Add(1)
go func() {
defer z.wg.Done()
defer z.stdin.Close()
io.Copy(z.stdin, r)
}()
return &z, nil
}
func (z *PigzReader) Read(p []byte) (int, error) {
return z.stdout.Read(p)
}
func (z *PigzReader) Close() error {
if z.cmd == nil {
return nil
}
z.stdout.Close()
z.wg.Wait()
if err := execute.WaitExit(0, z.cmd, z.commandLine, true); err != nil {
// ignore the sigpipe if we close the process early
sig, ok := err.(execute.SignalExit)
if !ok || sig.Signal != syscall.SIGPIPE {
logrus.Error(err.Error())
return err
}
}
z.cmd = nil
return nil
}
type PigzWriter struct {
stdout io.ReadCloser
stdin io.WriteCloser
cmd *exec.Cmd
commandLine string
wg sync.WaitGroup
}
func MaybeGzipWriter(w io.Writer) (io.WriteCloser, error) {
var gzipOut io.WriteCloser
var err error
if HasPigz() {
gzipOut, err = NewPigzWriter(w)
if err != nil {
logrus.Errorf("Failed to write gzip: %v", err)
return nil, err
}
} else {
gzipOut = gzip.NewWriter(w)
}
return gzipOut, nil
}
func NewPigzWriter(w io.Writer) (*PigzWriter, error) {
z := PigzWriter{}
z.cmd = exec.Command("pigz", "-n", "-T")
z.commandLine = "pigz -n -T"
var err error
z.stdout, err = z.cmd.StdoutPipe()
if err != nil {
logrus.Errorf("Failed to get stdout pipe for %s: %v", z.commandLine, err)
return nil, err
}
z.stdin, err = z.cmd.StdinPipe()
if err != nil {
logrus.Errorf("Failed to get stdin pipe for %s: %v", z.commandLine, err)
return nil, err
}
if err := z.cmd.Start(); err != nil {
logrus.Errorf("Error starting %s: %v", z.commandLine, err)
return nil, err
}
z.wg.Add(1)
go func() {
defer z.wg.Done()
defer z.stdout.Close()
io.Copy(w, z.stdout)
}()
return &z, nil
}
func (z *PigzWriter) Write(p []byte) (int, error) {
return z.stdin.Write(p)
}
func (z *PigzWriter) Close() error {
if z.cmd == nil {
return nil
}
z.stdin.Close()
z.wg.Wait()
if err := execute.WaitExit(0, z.cmd, z.commandLine, true); err != nil {
// ignore the sigpipe if we close the process early
sig, ok := err.(execute.SignalExit)
if !ok || sig.Signal != syscall.SIGPIPE {
logrus.Error(err.Error())
return err
}
}
z.cmd = nil
return nil
}