-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockfile.go
138 lines (123 loc) · 2.77 KB
/
mockfile.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
package gofs
import (
"errors"
"io"
"os"
)
// Mock implementation of the gofs.File interface.
type mockFile struct {
name string
info *mockFileInfo
position int
}
func (f *mockFile) Name() string {
return f.name
}
func (f *mockFile) Stat() (os.FileInfo, error) {
return f.info, nil
}
func (f *mockFile) Chmod(mode os.FileMode) error {
f.info.mode = (f.info.mode & os.ModeType) | (mode & os.ModePerm)
return nil
}
func (f *mockFile) Readdir(n int) ([]os.FileInfo, error) {
if !f.info.mode.IsDir() {
return nil, errors.New("not a directory")
}
var ret []os.FileInfo
for _, v := range f.info.children {
if n > 0 && len(ret) >= n {
break
}
ret = append(ret, v)
}
return ret, nil
}
func (f *mockFile) Read(b []byte) (int, error) {
if f.position == -1 {
return 0, errors.New("closed")
}
if !f.info.mode.IsRegular() {
return 0, errors.New("not a regular file")
}
if f.position >= len(f.info.data) {
return 0, io.EOF
}
ret := copy(b, f.info.data[f.position:])
f.position += ret
return ret, nil
}
func (f *mockFile) Write(b []byte) (int, error) {
if f.position == -1 {
return 0, errors.New("closed")
}
if !f.info.mode.IsRegular() {
return 0, errors.New("not a regular file")
}
pos := 0
for pos < len(b) {
l := len(b) - pos
if f.position == len(f.info.data) {
// Append.
f.info.data = append(f.info.data, b[pos:]...)
f.position += l
pos += l
} else {
// In-place write.
copied := copy(f.info.data[f.position:], b[pos:])
f.position += copied
pos += copied
}
}
return pos, nil
}
func (f *mockFile) Seek(offset int64, whence int) (int64, error) {
if f.position == -1 {
return 0, errors.New("closed")
}
if !f.info.mode.IsRegular() {
return 0, errors.New("not a regular file")
}
switch whence {
case os.SEEK_SET:
if offset < 0 || offset > int64(len(f.info.data)) {
return 0, errors.New("offset out of bounds")
}
f.position = int(offset)
case os.SEEK_CUR:
if offset < int64(-f.position) || offset > int64(len(f.info.data)-f.position) {
return 0, errors.New("offset out of bounds")
}
f.position = int(int64(f.position) + offset)
case os.SEEK_END:
if offset < 0 || offset > int64(len(f.info.data)) {
return 0, errors.New("offset out of bounds")
}
f.position = len(f.info.data) - int(offset)
}
return int64(f.position), nil
}
func (f *mockFile) Truncate(size int64) error {
if size < 0 {
return errors.New("size out of bounds")
}
if !f.info.mode.IsRegular() {
return errors.New("not a regular file")
}
if size < int64(len(f.info.data)) {
f.info.data = f.info.data[0:size]
} else {
buf := make([]byte, size)
copy(buf, f.info.data)
f.info.data = buf
}
return nil
}
func (f *mockFile) Sync() error {
// no-op.
return nil
}
func (f *mockFile) Close() error {
f.position = -1
return nil
}