-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
100 lines (83 loc) · 1.93 KB
/
util.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
package gobgpdump
import (
"bufio"
"compress/bzip2"
"fmt"
"github.com/CSUNetSec/protoparse/protocol/mrt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sync"
)
// The dump, stat, and log files are all accessed by multiple
// goroutines. This is a simple file wrapper to lock on a write,
// and unlock once the write is complete
type MultiWriteFile struct {
base io.WriteCloser
mx *sync.Mutex
}
func NewMultiWriteFile(w io.WriteCloser) *MultiWriteFile {
return &MultiWriteFile{w, &sync.Mutex{}}
}
func (mwf *MultiWriteFile) WriteString(s string) (n int, err error) {
mwf.mx.Lock()
// This is to trash output if it's directed to a file that doesn't exist
if mwf.base == nil {
return 0, nil
}
n, err = mwf.base.Write([]byte(s))
mwf.mx.Unlock()
return
}
func (mwf *MultiWriteFile) Write(data []byte) (n int, err error) {
mwf.mx.Lock()
if mwf.base == nil {
return 0, nil
}
n, err = mwf.base.Write(data)
mwf.mx.Unlock()
return
}
func (mwf *MultiWriteFile) Close() error {
if mwf.base == nil {
return nil
}
return mwf.base.Close()
}
func debugPrintf(format string, a ...interface{}) {
if DEBUG {
fmt.Printf(format, a)
}
}
func debugSprintf(format string, a ...interface{}) string {
if DEBUG {
return fmt.Sprintf(format, a...)
}
return ""
}
func getScanner(fd *os.File) (scanner *bufio.Scanner) {
if isBz2(fd.Name()) {
bzreader := bzip2.NewReader(fd)
scanner = bufio.NewScanner(bzreader)
} else {
scanner = bufio.NewScanner(fd)
}
scanner.Split(mrt.SplitMrt)
scanbuffer := make([]byte, 2<<24)
scanner.Buffer(scanbuffer, cap(scanbuffer))
return
}
func GetMRTScanner(fd *os.File) *bufio.Scanner {
return getScanner(fd)
}
func isBz2(fname string) bool {
fext := filepath.Ext(fname)
if fext == ".bz2" {
return true
}
return false
}
type DiscardCloser struct{}
func (d DiscardCloser) Write(data []byte) (n int, err error) { return ioutil.Discard.Write(data) }
func (d DiscardCloser) Close() error { return nil }