-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
241 lines (188 loc) · 4.95 KB
/
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
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
package mmap
import (
"io"
"sync"
)
// Constants used for whence in Seek.
const (
SeekStart int = 0 // seek relative to the start of the file
SeekCurrent int = 1 // seek relative to the current offset
SeekEnd int = 2 // seek relative to the end
)
// Reader reads from a map. It implements the following interfaces from the
// io standard package:
//
// - Reader (Read)
// - ReaderAt (ReadAt)
// - ByteReader (ReadByte)
// - Seeker (Seek)
// - Closer (Close)
// - ReadCloser (Read, Close)
// - ReadSeeker (Read, Seek)
type Reader struct {
*Map
access sync.RWMutex
closed bool
id int
offset int
}
// Reader returns a new Reader for the map.
func (m *Map) Reader() (*Reader, error) {
m.Lock()
defer m.Unlock()
if m.data == nil {
return nil, errors.New("mmap closed")
}
if len(m.direct) > 0 {
return nil, errors.New("mmap has open direct access pointers")
}
id := m.id
m.id++
reader := &Reader{
Map: m,
id: id,
}
m.readers[id] = reader
return reader, nil
}
// Peek returns the value of the byte at offset.
func (r *Reader) Peek(offset int) (byte, error) {
r.access.RLock()
defer r.access.RUnlock()
if r.closed {
return 0, errors.New("mmap reader closed").Set("name", r.name)
}
r.RLock()
defer r.RUnlock()
if r.data == nil {
return 0, errors.New("mmap closed").Set("name", r.name)
}
if offset < 0 || len(r.data) <= offset {
return 0, errors.New("offset out of range").Set("name", r.name).
Set("offset", offset).Set("map_size", len(r.data))
}
return r.data[offset], nil
}
// Read reads up to len(b) bytes from the map Reader. It returns the number of bytes read
// and any error encountered. At end of map, Read returns 0, io.EOF.
func (r *Reader) Read(b []byte) (n int, err error) {
r.access.Lock()
defer r.access.Unlock()
if r.closed {
return 0, errors.New("mmap reader closed").Set("name", r.name)
}
r.RLock()
defer r.RUnlock()
if r.data == nil {
return 0, errors.New("mmap closed").Set("name", r.name)
}
if len(r.data) <= r.offset {
return 0, io.EOF
}
if len(b) == 0 {
return 0, nil
}
n = copy(b, r.data[r.offset:])
r.offset += n
return n, nil
}
// ReadAt reads len(b) bytes from the Map starting at offset.
// It returns the number of bytes read and the error, if any.
// ReadAt returns io.EOF when n < len(b).
func (r *Reader) ReadAt(b []byte, offset int64) (n int, err error) {
r.access.RLock()
defer r.access.RUnlock()
if r.closed {
return 0, errors.New("mmap reader closed").Set("name", r.name)
}
r.RLock()
defer r.RUnlock()
if r.data == nil {
return 0, errors.New("mmap closed").Set("name", r.name)
}
if len(b) == 0 {
return 0, nil
}
if offset < 0 || int64(len(r.data)) <= offset {
return 0, errors.New("offset out of range").Set("name", r.name).
Set("offset", offset).Set("map_size", len(r.data))
}
n = copy(b, r.data[offset:])
if n < len(b) {
return n, io.EOF
}
return n, nil
}
// ReadByte reads and returns the next byte from the map.
// It returns io.EOF if the Reader is at the end of the file.
func (r *Reader) ReadByte() (byte, error) {
r.access.Lock()
defer r.access.Unlock()
if r.closed {
return 0, errors.New("mmap reader closed").Set("name", r.name)
}
r.RLock()
defer r.RUnlock()
if r.data == nil {
return 0, errors.New("mmap closed").Set("name", r.name)
}
if len(r.data) <= r.offset {
return 0, io.EOF
}
b := r.data[r.offset]
r.offset++
return b, nil
}
// Seek sets the offset for the next Read or Write on Reader to offset, interpreted
// according to whence: 0 means relative to the origin of the file, 1 means relative
// to the current offset, and 2 means relative to the end. It returns the new offset
// and an error, if any.
func (r *Reader) Seek(offset int64, whence int) (ret int64, err error) {
r.access.Lock()
defer r.access.Unlock()
if r.closed {
return 0, errors.New("mmap reader closed").Set("name", r.name)
}
r.Lock()
defer r.Unlock()
if r.data == nil {
return 0, errors.New("mmap closed").Set("name", r.name)
}
var pos int64
switch whence {
case SeekStart:
pos = offset
case SeekCurrent:
pos = int64(r.offset) + offset
case SeekEnd:
pos = int64(len(r.data)) + offset
default:
return 0, errors.New("invalid whence").Set("name", r.name).Set("whence", whence)
}
if pos < 0 || int64(len(r.data)) <= pos {
return 0, errors.New("invalid position").Set("name", r.name).
Set("offset", offset).Set("whence", whence).
Set("map_size", len(r.data)).Set("position", pos)
}
if pos != int64(int(pos)) {
return 0, errors.New("position is invalid for architecture").Set("name", r.name).
Set("offset", offset).Set("whence", whence).
Set("map_size", len(r.data)).Set("position", pos)
}
r.offset = int(pos)
return pos, nil
}
// Close closes the Reader.
func (r *Reader) Close() error {
r.Lock()
defer r.Unlock()
r.close()
return nil
}
// Lock map before calling
func (r *Reader) close() {
r.access.Lock()
defer r.access.Unlock()
r.closed = true
delete(r.readers, r.id)
}