This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
b64filter.go
276 lines (240 loc) · 6.09 KB
/
b64filter.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
package main
import (
"bufio"
"bytes"
"encoding/base64"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"sync"
"time"
"github.com/golang-collections/go-datastructures/queue"
)
var debug bool
var progress int
type LockBuffer struct {
buf bytes.Buffer
lock sync.Mutex
}
func (lb *LockBuffer) Write(p []byte) (n int, err error) {
lb.lock.Lock()
n, err = lb.buf.Write(p)
lb.lock.Unlock()
return
}
func (lb *LockBuffer) ReadBytes(delim byte) (line []byte, err error) {
lb.lock.Lock()
line, err = lb.buf.ReadBytes(delim)
lb.lock.Unlock()
return
}
func init() {
flag.BoolVar(&debug, "d", false, "Debugging output")
flag.IntVar(&progress, "p", 100, "Report progress every p files")
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s filter [args]\n", os.Args[0])
flag.PrintDefaults()
fmt.Fprintf(flag.CommandLine.Output(),
`
Runs the given program as a filter on the input. Standard input and output are
expected to be base 64 encoded, one document or record per line. The input is
passed in decoded form through the filter program, and then re-encoded.
Example:
$ < test b64filter cat > test.cat
2020/02/16 12:15:29 b64filter.go:188: processed 2 documents
$ diff test test.cat
$
The filter program is executed once and fed input from the entire set of
documents. This means that the filter must produce exactly one line of output
per line of input.
`)
}
}
func readDocs(r io.ReadCloser) (ch chan []byte) {
ch = make(chan []byte)
go func() {
buf := bufio.NewReader(r)
line := make([]byte, 0, 1024)
for {
chunk, pfx, err := buf.ReadLine()
// we got some bytes, accumulate
if len(chunk) > 0 {
line = append(line, chunk...)
}
// we're done
if err != nil {
if debug {
log.Printf("readDocs: finished (%v)", err)
}
if err == io.EOF {
if len(line) > 0 {
b := make([]byte, base64.StdEncoding.DecodedLen(len(line)))
n, err := base64.StdEncoding.Decode(b, line)
if err != nil {
log.Fatalf("readDocs: error decoding line (%v)", err)
}
ch <- b[:n]
}
} else {
log.Fatalf("error reading line: %v", err)
}
close(ch)
return
}
// if we have a complete line, send it
if !pfx {
b := make([]byte, base64.StdEncoding.DecodedLen(len(line)))
n, err := base64.StdEncoding.Decode(b, line)
if err != nil {
log.Fatalf("readDocs: error decoding line (%v)", err)
}
ch <- b[:n]
line = make([]byte, 0, 1024)
}
}
}()
return ch
}
func readNLines(count int, buf *LockBuffer) (lines [][]byte, err error) {
if debug {
log.Printf("readNLines: reading %v lines", count)
}
lines = make([][]byte, 0, count)
line := make([]byte, 0, 1024)
for n := 0; n < count; n++ {
// if debug {
// log.Printf("readNLines: reading line %d", n)
// }
chunk, err := buf.ReadBytes('\n')
line = append(line, chunk...)
// got a complete line
if err == nil {
lines = append(lines, line)
line = make([]byte, 0, 1024)
} else { // not a complete line
n--
}
}
if debug {
log.Printf("readNLines: read %v lines", len(lines))
}
return
}
func writeDocs(counts *queue.Queue, done chan bool, buf *LockBuffer, w io.Writer) {
ndocs := 0
nlines := 0
start := time.Now()
for {
// get a line count off the queue
ns, err := counts.Get(1)
if err != nil {
if counts.Disposed() {
if debug {
log.Printf("writeDocs: write queue finished")
}
break
} else {
log.Fatalf("writeDocs: error reading from queue: %v", err)
}
}
if len(ns) != 1 {
log.Fatalf("writeDocs: asked for 1 item, got %d", len(ns))
}
n := ns[0].(int)
if debug {
log.Printf("writeDocs: processing %d line document (qsize: %d)", n, counts.Len())
}
// read n lines of the document
lines, err := readNLines(n, buf)
if err != nil {
log.Fatalf("writeDocs: error reading %v lines: %v", n, err)
}
if len(lines) != n {
log.Fatalf("writeDocs: expected %v lines got %v", n, len(lines))
}
doc := bytes.Join(lines, []byte(""))
// XXX kludge: remove extraneous trailing newline from document. But it
// shouldn't be here in the first place
if len(doc) > 1 && doc[len(doc)-2] == '\n' && doc[len(doc)-1] == '\n' {
doc = doc[:len(doc)-1]
}
// encode and output
elen := base64.StdEncoding.EncodedLen(len(doc))
b := make([]byte, elen, elen+1)
base64.StdEncoding.Encode(b, doc)
b = append(b, '\n')
_, err = w.Write(b)
if err != nil {
log.Fatalf("writeDocs: error writing %v lines %v", n, err)
}
ndocs += 1
nlines += n
if progress > 0 && ndocs % progress == 0 {
now := time.Now()
log.Printf("writeDocs: written %d docs, %d lines in %s", ndocs, nlines, now.Sub(start).String())
}
}
if debug {
log.Printf("writeDocs: finished")
}
done <- true
}
func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
os.Exit(-1)
}
args := flag.Args()
cmd := exec.Command(args[0], args[1:]...)
cmdin, err := cmd.StdinPipe()
if err != nil {
log.Fatalf("error getting command input: %v", err)
}
var cmdout LockBuffer
cmd.Stdout = &cmdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
log.Fatalf("error starting command: %v", err)
}
counts := queue.New(32)
done := make(chan bool)
go writeDocs(counts, done, &cmdout, os.Stdout)
docs := readDocs(os.Stdin)
i := 0
for doc := range docs {
lines := bytes.Count(doc, []byte("\n"))
if debug {
log.Printf("main: writing %d line document to filter (qsize: %d)", lines+1, counts.Len())
}
if _, err := cmdin.Write(doc); err != nil {
log.Fatalf("error writing to filter: %v", err)
}
if _, err := cmdin.Write([]byte("\n")); err != nil {
log.Fatalf("error writing to filter: %v", err)
}
counts.Put(lines + 1) // extra newline at end
i += 1
}
cmdin.Close()
if err = cmd.Wait(); err != nil {
log.Fatalf("error waiting for command: %v", err)
}
// wait for the queue to drain
for {
if counts.Empty() {
counts.Dispose()
break
}
time.Sleep(100 * time.Millisecond)
}
// it is required that all reading from the command is done before
// calling Wait().
_ = <-done
log.Printf("processed %v documents", i)
}