forked from emersion/go-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_readseeker.go
348 lines (301 loc) · 7.59 KB
/
multi_readseeker.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//file from https://github.com/moby/moby/blob/2445e6b99d4beecb25d556d9a099bdf47703e174/daemon/logger/jsonfilelog/multireader/multireader.go
//add startOffset NewCombinedBuf
package imap
import (
"bytes"
"fmt"
"io"
"os"
"io/ioutil"
)
type pos struct {
idx int
offset int64
}
type cleanupFunc func() error
type MultiReadSeeker interface {
io.Seeker
io.Reader
io.Closer
Len() int
Size() int64
}
type multiReadSeeker struct {
readers []io.ReadSeeker
pos *pos
posIdx map[io.ReadSeeker]int
startOffset []int64
cleanup cleanupFunc
size int64
}
func (r *multiReadSeeker) Close() error {
if r.cleanup != nil {
return r.cleanup()
}
return nil
}
func (r *multiReadSeeker) Seek(offset int64, whence int) (int64, error) {
var tmpOffset int64
switch whence {
case os.SEEK_SET:
for i, rdr := range r.readers {
// get size of the current reader
s, err := rdr.Seek(0-r.startOffset[i], os.SEEK_END)
if err != nil {
return -1, err
}
if offset > tmpOffset+s {
if i == len(r.readers)-1 {
rdrOffset := s + (offset - tmpOffset)
if _, err := rdr.Seek(rdrOffset+r.startOffset[i], os.SEEK_SET); err != nil {
return -1, err
}
r.pos = &pos{i, rdrOffset}
return offset, nil
}
tmpOffset += s
continue
}
rdrOffset := offset - tmpOffset
idx := i
rdr.Seek(rdrOffset+r.startOffset[i], os.SEEK_SET)
// make sure all following readers are at 0
for j, rdr := range r.readers[i+1:] {
rdr.Seek(r.startOffset[j+i+1], os.SEEK_SET)
}
if rdrOffset == s && i != len(r.readers)-1 {
idx++
rdrOffset = 0
}
r.pos = &pos{idx, rdrOffset}
return offset, nil
}
case os.SEEK_END:
for i, rdr := range r.readers {
s, err := rdr.Seek(0-r.startOffset[i], os.SEEK_END)
if err != nil {
return -1, err
}
tmpOffset += s
}
r.Seek(tmpOffset+offset, os.SEEK_SET)
return tmpOffset + offset, nil
case os.SEEK_CUR:
if r.pos == nil {
return r.Seek(offset, os.SEEK_SET)
}
// Just return the current offset
if offset == 0 {
return r.getCurOffset()
}
curOffset, err := r.getCurOffset()
if err != nil {
return -1, err
}
rdr, rdrOffset, err := r.getReaderForOffset(curOffset + offset)
if err != nil {
return -1, err
}
r.pos = &pos{r.posIdx[rdr], rdrOffset}
return curOffset + offset, nil
default:
return -1, fmt.Errorf("Invalid whence: %d", whence)
}
return -1, fmt.Errorf("Error seeking for whence: %d, offset: %d", whence, offset)
}
func (r *multiReadSeeker) getReaderForOffset(offset int64) (io.ReadSeeker, int64, error) {
var offsetTo int64
for i, rdr := range r.readers {
size, err := getReadSeekerSize(rdr, r.startOffset[i])
if err != nil {
return nil, -1, err
}
if offsetTo+size > offset {
return rdr, offset - offsetTo, nil
}
if rdr == r.readers[len(r.readers)-1] {
return rdr, offsetTo + offset, nil
}
offsetTo += size
}
return nil, 0, nil
}
func (r *multiReadSeeker) getCurOffset() (int64, error) {
var totalSize int64
for i, rdr := range r.readers[:r.pos.idx+1] {
if r.posIdx[rdr] == r.pos.idx {
totalSize += r.pos.offset
break
}
size, err := getReadSeekerSize(rdr, r.startOffset[i])
if err != nil {
return -1, fmt.Errorf("error getting seeker size: %v", err)
}
totalSize += size
}
return totalSize, nil
}
func (r *multiReadSeeker) getOffsetToReader(rdr io.ReadSeeker) (int64, error) {
var offset int64
for i, r2 := range r.readers {
if r2 == rdr {
break
}
size, err := getReadSeekerSize(rdr, r.startOffset[i])
if err != nil {
return -1, err
}
offset += size
}
return offset, nil
}
func (r *multiReadSeeker) Read(b []byte) (int, error) {
if r.pos == nil {
// make sure all readers are at 0
r.Seek(0, os.SEEK_SET)
}
bLen := int64(len(b))
buf := bytes.NewBuffer(nil)
var rdr io.ReadSeeker
for _, rdr = range r.readers[r.pos.idx:] {
readBytes, err := io.CopyN(buf, rdr, bLen)
if err != nil && err != io.EOF {
return -1, err
}
bLen -= readBytes
if bLen == 0 {
break
}
}
rdrPos, err := rdr.Seek(0, os.SEEK_CUR)
if err != nil {
return -1, err
}
r.pos = &pos{r.posIdx[rdr], rdrPos}
return buf.Read(b)
}
func (r *multiReadSeeker) Size() int64 {
if r.size > 0 {
return r.size
}
curPos, _ := r.Seek(0, os.SEEK_CUR)
l, _ := r.Seek(0, os.SEEK_END)
r.Seek(curPos, os.SEEK_SET)
return l
}
func (r *multiReadSeeker) Len() int {
return int(r.Size())
}
func getReadSeekerSize(rdr io.ReadSeeker, startOffset int64) (int64, error) {
// save the current position
pos, err := rdr.Seek(0, os.SEEK_CUR)
if err != nil {
return -1, err
}
// get the size
size, err := rdr.Seek(0-startOffset, os.SEEK_END)
if err != nil {
return -1, err
}
// reset the position
if _, err := rdr.Seek(pos+startOffset, os.SEEK_SET); err != nil {
return -1, err
}
return size, nil
}
// MultiReadSeeker returns a ReadSeeker that's the logical concatenation of the provided
// input readseekers. After calling this method the initial position is set to the
// beginning of the first ReadSeeker. At the end of a ReadSeeker, Read always advances
// to the beginning of the next ReadSeeker and returns EOF at the end of the last ReadSeeker.
// Seek can be used over the sum of lengths of all readseekers.
//
// When a MultiReadSeeker is used, no Read and Seek operations should be made on
// its ReadSeeker components. Also, users should make no assumption on the state
// of individual readseekers while the MultiReadSeeker is used.
func NewMultiReadSeeker(startOffset []int64, readers ...io.ReadSeeker) MultiReadSeeker {
if startOffset == nil || len(startOffset) == 0 {
startOffset = make([]int64, len(readers))
}
idx := make(map[io.ReadSeeker]int)
for i, rdr := range readers {
idx[rdr] = i
}
return &multiReadSeeker{
readers: readers,
posIdx: idx,
startOffset: startOffset,
}
}
func NewMultiReadSeekCloser(startOffset []int64, size int64, cleanup cleanupFunc, readers ...io.ReadSeeker) MultiReadSeeker {
if startOffset == nil || len(startOffset) == 0 {
startOffset = make([]int64, len(readers))
}
idx := make(map[io.ReadSeeker]int)
for i, rdr := range readers {
idx[rdr] = i
}
return &multiReadSeeker{
readers: readers,
posIdx: idx,
startOffset: startOffset,
size: size,
cleanup: cleanup,
}
}
var (
CombinedBufMemBytes int64 = 1048576
CombinedBufMaxBytes int64 = 1048576000
CombinedBufTempFilePrefix string = "combined-buf-"
)
func NewCombinedBuf(input io.Reader, inputsize int64) (MultiReadSeeker, error) {
if inputsize > CombinedBufMaxBytes {
return nil, fmt.Errorf("message size too big.")
}
memBytes := CombinedBufMemBytes
if memBytes > inputsize {
memBytes = inputsize
}
memReader := &io.LimitedReader{
R: input,
N: memBytes,
}
readers := make([]io.ReadSeeker, 0, 2)
buffer, err := ioutil.ReadAll(memReader)
if err != nil {
return nil, err
}
readers = append(readers, bytes.NewReader(buffer))
var file *os.File
totalBytes := int64(len(buffer))
if inputsize > totalBytes {
tempFilePrefix := CombinedBufTempFilePrefix
file, err = ioutil.TempFile("", tempFilePrefix)
if err != nil {
return nil, err
}
readSrc := &io.LimitedReader{R: input, N: inputsize-totalBytes}
writtenBytes, err := io.Copy(file, readSrc)
if err != nil {
return nil, err
}
totalBytes += writtenBytes
file.Seek(0, 0)
readers = append(readers, file)
}
if totalBytes != inputsize {
if file != nil {
file.Close()
os.Remove(file.Name())
}
return nil, io.ErrUnexpectedEOF
}
var cleanup cleanupFunc
if file != nil {
cleanup = func() error {
file.Close()
os.Remove(file.Name())
return nil
}
}
return NewMultiReadSeekCloser(nil, totalBytes, cleanup, readers...), nil
}