-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
115 lines (104 loc) · 2.19 KB
/
file.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
package PictureFS
import (
"errors"
"fmt"
"io"
)
type File struct {
name string
fs *FS
i int64
}
func (f *File) Close() error {
f.i = 0
return nil
}
func (f *File) Stat() (FileInfo, error) {
if !f.fs.hasFile(f.name) {
if !f.fs.hasDir(f.name) {
return nil, errors.New(fmt.Sprintf("invalid node: %s", f.name))
}
return &fileStat{
name: f.name,
size: 0,
dir: true,
}, nil
}
return &fileStat{
name: f.name,
size: int64(len(f.fs.data[f.name])), // hasFile makes sure, it exists,
dir: false,
}, nil
}
// Len returns the number of bytes of the unread portion of the
// string.
func (f *File) Len() int64 {
if !f.fs.hasFile(f.name) {
return 0
}
var l int64 = int64(len(f.fs.data[f.name]))
if f.i >= l {
return 0
}
return l - f.i
}
func (f *File) Size() int64 {
if !f.fs.hasFile(f.name) {
return 0
}
return int64(len(f.fs.data[f.name]))
}
func (f *File) Read(buf []byte) (n int, err error) {
if !f.fs.hasFile(f.name) {
return 0, errors.New(fmt.Sprintf("invalid file: %s", f.name))
}
if f.i >= int64(len(f.fs.data[f.name])) {
return 0, io.EOF
}
n = copy(buf, f.fs.data[f.name][f.i:])
f.i += int64(n)
return
}
// ReadAt implements the io.ReaderAt interface.
func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
// cannot modify state - see io.ReaderAt
if off < 0 {
return 0, errors.New("PictureFS.File.ReadAt: negative offset")
}
if off >= int64(len(f.fs.data[f.name])) {
return 0, io.EOF
}
n = copy(b, f.fs.data[f.name][off:])
if n < len(b) {
err = io.EOF
}
return
}
// ReadByte implements the io.ByteReader interface.
func (f *File) ReadByte() (byte, error) {
if f.i >= int64(len(f.fs.data[f.name])) {
return 0, io.EOF
}
b := f.fs.data[f.name][f.i]
f.i++
return b, nil
}
// Seek implements the io.Seeker interface.
func (f *File) Seek(offset int64, whence int) (int64, error) {
var abs int64
switch whence {
case io.SeekStart:
abs = offset
case io.SeekCurrent:
abs = f.i + offset
case io.SeekEnd:
abs = int64(len(f.fs.data[f.name])) + offset
default:
return 0, errors.New("strings.Reader.Seek: invalid whence")
}
if abs < 0 {
return 0, errors.New("PictureFS.File.Seek: negative position")
}
f.i = abs
return abs, nil
}