-
Notifications
You must be signed in to change notification settings - Fork 1
/
dir_reader.go
147 lines (137 loc) · 2.9 KB
/
dir_reader.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
package binlog
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"time"
)
var fileHeader = []byte{0xfe, 'b', 'i', 'n'}
type dirReader struct {
file *os.File
name *string
nonBlock bool
tmeCache map[uint64]*TableMapEvent
checksum int
}
func newDirReader(dir string, file *string, pos uint32, nonBlock bool) (*dirReader, error) {
f, err := openBinlogFile(path.Join(dir, *file))
if err != nil {
return nil, err
}
checksum := 0
if pos > 4 {
// Decode FormatDescriptionEvent to find checksum.
v, err := findBinlogVersion(f.Name())
if err != nil {
return nil, err
}
r := &reader{rd: f, limit: -1, fde: FormatDescriptionEvent{BinlogVersion: v}}
h := EventHeader{}
if err := h.decode(r); err != nil {
return nil, err
}
fde := FormatDescriptionEvent{}
if err := fde.decode(r, h.EventSize); err != nil {
return nil, err
}
checksum = r.checksum
}
if _, err := f.Seek(int64(pos), io.SeekStart); err != nil {
_ = f.Close()
return nil, err
}
return &dirReader{f, file, nonBlock, make(map[uint64]*TableMapEvent), checksum}, nil
}
func (r *dirReader) Read(p []byte) (int, error) {
delay := time.Second
for {
n, err := r.file.Read(p)
if n > 0 {
return n, nil
}
if err != nil && err != io.EOF {
return 0, err
}
if err == nil {
return n, nil
}
if err != io.EOF {
panic("bug")
}
// Check for next file.
next, err := nextBinlogFile(r.file.Name())
if err != nil {
return 0, err
}
if next == "" {
if r.nonBlock {
return 0, io.EOF
}
time.Sleep(delay)
continue
}
if _, err = os.Stat(next); err != nil {
if os.IsNotExist(err) {
if r.nonBlock {
return 0, io.EOF
}
time.Sleep(delay)
continue
} else {
return 0, err
}
}
// Switch to next file.
f, err := openBinlogFile(next)
if err != nil {
return 0, err
}
_ = r.file.Close()
r.file = f
*r.name = path.Base(next)
for k := range r.tmeCache {
delete(r.tmeCache, k)
}
}
}
// openBinlogFile opens file and seeks location
// to just after the magic header.
func openBinlogFile(file string) (*os.File, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
// read magic header and validate
header := make([]byte, 4)
_, err = io.ReadAtLeast(f, header, len(header))
if err != nil {
return nil, err
}
if !bytes.Equal(header, fileHeader) {
return nil, fmt.Errorf("binlog: %q is not binlog file", file)
}
return f, nil
}
// nextBinlogFile returns next file given current file
// by reading '.next' file. returns nil if next file
// does not exist.
func nextBinlogFile(name string) (string, error) {
dir, file := path.Split(name)
f, err := os.Open(path.Join(dir, file+".next"))
if err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", err
}
defer f.Close()
buff, err := ioutil.ReadAll(f)
if err != nil {
return "", err
}
return path.Join(dir, strings.TrimSpace(string(buff))), nil
}