-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
io.go
95 lines (77 loc) · 1.54 KB
/
io.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
package tarfs
import (
"errors"
"io"
)
type readReaderAt interface {
io.Reader
io.ReaderAt
}
type readCounterIface interface {
io.Reader
Count() int64
}
type readCounter struct {
io.Reader
off int64
}
func (cr *readCounter) Read(p []byte) (n int, err error) {
n, err = cr.Reader.Read(p)
cr.off += int64(n)
return
}
func (cr *readCounter) Count() int64 {
return cr.off
}
type readSeekCounter struct {
io.ReadSeeker
off int64
}
func (cr *readSeekCounter) Read(p []byte) (n int, err error) {
n, err = cr.ReadSeeker.Read(p)
cr.off += int64(n)
return
}
func (cr *readSeekCounter) Seek(offset int64, whence int) (abs int64, err error) {
abs, err = cr.ReadSeeker.Seek(offset, whence)
cr.off = abs
return
}
func (cr *readSeekCounter) Count() int64 {
return cr.off
}
type readSeeker struct {
*readCounter
e *regEntry
}
var _ io.ReadSeeker = &readSeeker{}
func (rs *readSeeker) Seek(offset int64, whence int) (int64, error) {
const op = "seek"
var abs int64
switch whence {
case io.SeekStart:
abs = offset
case io.SeekCurrent:
abs = rs.off + offset
case io.SeekEnd:
abs = rs.e.size() + offset
default:
return 0, newErr(op, rs.e.name, errors.New("invalid whence"))
}
if abs < 0 {
return 0, newErr(op, rs.e.name, errors.New("negative position"))
}
if abs < rs.off {
r, err := rs.e.reader()
if err != nil {
return 0, err
}
rs.readCounter = &readCounter{r, 0}
}
if abs > rs.off {
if _, err := io.CopyN(io.Discard, rs.readCounter, abs-rs.off); err != nil && err != io.EOF {
return 0, err
}
}
return abs, nil
}