-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathztgrep.go
More file actions
269 lines (238 loc) · 5.83 KB
/
ztgrep.go
File metadata and controls
269 lines (238 loc) · 5.83 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
package ztgrep
import (
"archive/tar"
"archive/zip"
"bufio"
"bytes"
"compress/bzip2"
"compress/gzip"
"context"
"errors"
"io"
"io/ioutil"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"sync"
"golang.org/x/sync/semaphore"
)
const defaultMaxZipSize = 10 << (10 * 2) // 10 MB
var cpuLock = semaphore.NewWeighted(int64(runtime.NumCPU()))
func acquireCPU() { cpuLock.Acquire(context.Background(), 1) }
func releaseCPU() { cpuLock.Release(1) }
// New returns a *ZTgrep given a regular expression following https://golang.org/s/re2syntax
func New(expr string) (*ZTgrep, error) {
exp, err := regexp.Compile(expr)
if err != nil {
return nil, err
}
return &ZTgrep{
MaxZipSize: defaultMaxZipSize,
exp: exp,
}, nil
}
// ZTgrep searchs for file names and contents within nested compressed archives.
type ZTgrep struct {
MaxZipSize int64 // maximum size of zip file to search (held in memory)
SkipName bool // skip file names
SkipBody bool // skip file contents
exp *regexp.Regexp
}
// Result contains each matching path in Path.
// Each entry in Path[1:] represents a file nested in the previous archive.
type Result struct {
Path []string
Err error
}
// Start searches paths in parallel, returning results via a channel
func (zt *ZTgrep) Start(paths []string) <-chan Result {
// TODO: restrict number of open files
// TODO: buffer output to guarantee order
out := make(chan Result)
wg := sync.WaitGroup{}
wg.Add(len(paths))
go func() {
wg.Wait()
close(out)
}()
go func() {
for _, p := range paths {
p := p
acquireCPU() // encourge ordered output
go func() {
zt.findPath(out, p)
releaseCPU()
wg.Done()
}()
}
}()
return out
}
func (zt *ZTgrep) findPath(out chan<- Result, path string) {
if path == "-" {
zt.find(out, os.Stdin, []string{"-"})
return
}
f, err := os.Open(path)
if err != nil {
out <- Result{Path: []string{path}, Err: err}
return
}
defer f.Close()
zt.find(out, f, []string{path})
}
// TODO: implement version that uses file headers to identify type
func (zt *ZTgrep) find(out chan<- Result, zr io.Reader, path []string) {
zf, xf := zt.newDecompressor(path[len(path)-1])
if xf == nil && zt.SkipBody {
return
}
r, err := zf(zr)
if err != nil {
out <- Result{Path: path, Err: err}
return
}
defer r.Close()
if xf == nil {
if zt.exp.MatchReader(bufio.NewReader(r)) {
out <- Result{Path: path}
}
return
}
if err := xf(r, func(name string, fr io.Reader) error {
p := append(path[:len(path):len(path)], name)
if !zt.SkipName {
if zt.exp.MatchString(name) {
out <- Result{Path: p}
}
}
zt.find(out, fr, p)
return nil
}); err != nil {
out <- Result{Path: path, Err: err}
return
}
}
type decompressor func(io.Reader) (io.ReadCloser, error)
type extractor func(io.Reader, func(string, io.Reader) error) error
func (zt *ZTgrep) newDecompressor(path string) (zf decompressor, xf extractor) {
p := strings.ToLower(path)
switch {
case hasSuffixes(p, ".tar.gz", ".tgz", ".taz"):
return gzReader, tarReader
case hasSuffixes(p, ".tar.bz2", ".tar.bz", ".tbz", ".tbz2", ".tz2", ".tb2"):
return bz2Reader, tarReader
case hasSuffixes(p, ".tar.xz", ".txz"):
return xzReader, tarReader
case hasSuffixes(p, ".tar.zst", ".tzst", ".tar.zstd"):
return zstReader, tarReader
case hasSuffixes(p, ".tar"):
return nopReader, tarReader
case hasSuffixes(p, ".zip"):
return nopReader, zt.zipReader
case hasSuffixes(p, ".gz"):
return gzReader, nil
case hasSuffixes(p, ".bz2", ".bz"):
return bz2Reader, nil
case hasSuffixes(p, ".xz"):
return xzReader, nil
case hasSuffixes(p, ".zst", ".zstd"):
return zstReader, nil
default:
return nopReader, nil
}
}
func hasSuffixes(s string, suffixes ...string) bool {
for _, suffix := range suffixes {
if strings.HasSuffix(s, suffix) {
return true
}
}
return false
}
func tarReader(r io.Reader, fn func(string, io.Reader) error) error {
tr := tar.NewReader(r)
for h, err := tr.Next(); err != io.EOF; h, err = tr.Next() {
if err != nil {
return err
}
if err := fn(h.Name, tr); err != nil {
return err
}
}
return nil
}
func (zt *ZTgrep) zipReader(r io.Reader, fn func(string, io.Reader) error) error {
tr, err := zt.readZip(r)
if err != nil {
return err
}
for _, file := range tr.File {
fr, err := file.Open()
if err != nil {
return err // TODO: process next file if alg error?
}
if err := fn(file.Name, fr); err != nil {
return err
}
}
return nil
}
func nopReader(r io.Reader) (io.ReadCloser, error) {
return io.NopCloser(r), nil
}
func gzReader(r io.Reader) (io.ReadCloser, error) {
r, err := gzip.NewReader(r)
return io.NopCloser(r), err
}
func bz2Reader(r io.Reader) (io.ReadCloser, error) {
return io.NopCloser(bzip2.NewReader(r)), nil
}
func xzReader(r io.Reader) (io.ReadCloser, error) {
return zCmdReader(exec.Command("xz", "-d", "-T0"), r)
}
func zstReader(r io.Reader) (io.ReadCloser, error) {
return zCmdReader(exec.Command("zstd", "-d"), r)
}
func zCmdReader(cmd *exec.Cmd, r io.Reader) (io.ReadCloser, error) {
cmd.Stdin = r
out, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}
return splitCloser{out, closerFunc(func() error {
return cmd.Wait()
})}, nil
}
type closerFunc func() error
func (f closerFunc) Close() error {
return f()
}
type splitCloser struct {
io.Reader
io.Closer
}
func (zt *ZTgrep) readZip(r io.Reader) (*zip.Reader, error) {
if f, ok := r.(*os.File); ok && f != os.Stdin {
if fi, err := f.Stat(); err == nil {
if n := fi.Size(); n > 0 {
return zip.NewReader(f, n)
}
}
}
limitedReader := &io.LimitedReader{R: r, N: zt.MaxZipSize}
data, err := ioutil.ReadAll(limitedReader)
if err != nil {
return nil, err
}
if limitedReader.N <= 0 {
return nil, errors.New("nested zip file larger than limit")
}
br := bytes.NewReader(data)
return zip.NewReader(br, br.Size())
}