forked from frizinak/gphoto2go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ex_camera_filepath_readseeker.go
60 lines (49 loc) · 1.16 KB
/
ex_camera_filepath_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
package gphoto2
import (
"errors"
"io"
)
var _ io.ReadSeeker = (*fileReadSeeker)(nil)
type fileReadSeeker struct {
f CameraFilePath
offset int
}
func (cf CameraFilePath) ReadSeeker() (io.ReadSeeker, error) {
rc := &fileReadSeeker{f: cf, offset: 0}
return rc, nil
}
func (rs *fileReadSeeker) Read(p []byte) (n int, err error) {
n, err = rs.f.ReadOffset(rs.offset, p)
rs.offset += n
return n, err
}
// FIXME
func (rs *fileReadSeeker) Read2(p []byte) (n int, err error) {
data, err := rs.f.ReadOffsetSize(rs.offset, len(p))
rs.offset += len(data)
copy(p, data)
return n, err
}
func (rs *fileReadSeeker) Seek(offset int64, whence int) (int64, error) {
n := int64(rs.offset)
switch whence {
case io.SeekStart:
n = offset
case io.SeekCurrent:
n += offset
case io.SeekEnd:
if fi, err := rs.f.GetInfo(); err != nil {
return int64(rs.offset), err
} else {
size := fi.File.Size
n = size + offset
}
default:
return int64(rs.offset), errors.New("gphoto.ReadSeeker.Seek: invalid whence")
}
if n < 0 {
return int64(rs.offset), errors.New("gphoto.ReadSeeker.Seek: negative position")
}
rs.offset = int(n)
return int64(rs.offset), nil
}