-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_nodes.go
304 lines (242 loc) · 5.62 KB
/
memory_nodes.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
package goblin
import (
"fmt"
"io"
"os"
"path"
"strings"
"time"
)
type memoryFileInfo struct {
filename string
modTime time.Time
isDir bool
mode os.FileMode
size int64
node fsNode
}
var _ os.FileInfo = &memoryFileInfo{}
func (mfi *memoryFileInfo) Name() string {
return mfi.filename
}
func (mfi *memoryFileInfo) IsDir() bool {
return mfi.isDir
}
func (mfi *memoryFileInfo) ModTime() time.Time {
return mfi.modTime
}
func (mfi *memoryFileInfo) Mode() os.FileMode {
return mfi.mode
}
func (mfi *memoryFileInfo) Size() int64 {
return mfi.size
}
func (mfi *memoryFileInfo) Sys() interface{} {
return mfi.node
}
type fsNode interface {
Name() string
FullPath() string
Stat() (os.FileInfo, error)
Open() (File, error)
GetNode(path []string) (fsNode, error)
}
type memoryDir struct {
fullPath string
name string
modTime time.Time
nodes map[string]fsNode
}
var _ File = &memoryDir{}
var _ fsNode = &memoryDir{}
func newMemoryDir(fullPath string, opts ...FileOption) *memoryDir {
fOpts := newFileOptions(opts...)
return &memoryDir{
fullPath: fullPath,
name: path.Base(fullPath),
modTime: fOpts.ModTime,
nodes: map[string]fsNode{},
}
}
func (d *memoryDir) CreateNode(path []string, node fsNode) error {
err := validatePath(path)
if err != nil {
return err
} else if node == nil {
return fmt.Errorf("node cannot be nil")
}
var createNode func(*memoryDir, []string, string, []string, fsNode) error
createNode = func(
parentNode *memoryDir,
parentPath []string,
curPath string,
nextPath []string,
node fsNode,
) error {
curNode, ok := parentNode.nodes[curPath]
if len(nextPath) == 0 {
// Make sure the node doesn't exist before we try to write to it
if ok {
return os.ErrExist
}
parentNode.nodes[curPath] = node
return nil
}
fullCurPath := append(parentPath, curPath)
if !ok {
// If we don't have a current node, we need to create the next
// dir to fill the rest of the tree.
curNode = newMemoryDir(strings.Join(fullCurPath, pathSeparator))
parentNode.nodes[curPath] = curNode
}
// Make sure the node we're trying to add to is a directory. We can't add
// additional nodes to a file...
if dirNode, ok := curNode.(*memoryDir); ok {
curPath, nextPath := nextPath[0], nextPath[1:]
return createNode(dirNode, fullCurPath, curPath, nextPath, node)
}
return fmt.Errorf("attempted to add additional nodes to a file")
}
curPath, nextPath := path[0], path[1:]
return createNode(d, []string{}, curPath, nextPath, node)
}
func (d *memoryDir) GetNode(path []string) (fsNode, error) {
if len(path) == 0 ||
(len(path) == 1 && path[0] == filesystemRootPath) {
return d, nil
}
err := validatePath(path)
if err != nil {
return nil, err
}
curPath, nextPaths := path[0], path[1:]
if curNode, ok := d.nodes[curPath]; ok {
// If this is the end of the path, return what we have
if len(nextPaths) == 0 {
return curNode, nil
}
// If this still has additional path segments, traverse down deeper
return curNode.GetNode(nextPaths)
}
return nil, os.ErrNotExist
}
func (d *memoryDir) Nodes() []fsNode {
var res []fsNode
for _, node := range d.nodes {
res = append(res, node)
}
return res
}
func (d *memoryDir) Name() string {
return d.name
}
func (d *memoryDir) FullPath() string {
return d.fullPath
}
func (d *memoryDir) Close() error {
return nil
}
func (d *memoryDir) Read(buf []byte) (int, error) {
return 0, fmt.Errorf("not a file")
}
func (d *memoryDir) Stat() (os.FileInfo, error) {
return &memoryFileInfo{
filename: d.name,
modTime: d.modTime,
isDir: true,
mode: 0,
size: 0,
node: d,
}, nil
}
func (d *memoryDir) Open() (File, error) {
return newOpenMemoryDir(d), nil
}
type openMemoryDir struct {
memoryDir
}
var _ File = &openMemoryDir{}
func newOpenMemoryDir(dir *memoryDir) *openMemoryDir {
return &openMemoryDir{
memoryDir: *dir,
}
}
func (omd *openMemoryDir) Close() error {
return nil
}
func (omd *openMemoryDir) Read(data []byte) (int, error) {
return 0, fmt.Errorf("dir read... what it do?")
}
type memoryFile struct {
fullPath string
name string
modTime time.Time
data []byte
}
var _ fsNode = &memoryFile{}
func newMemoryFile(fullPath string, data []byte, opts ...FileOption) *memoryFile {
fOpts := newFileOptions(opts...)
fileName := path.Base(fullPath)
return &memoryFile{
fullPath: fullPath,
name: fileName,
modTime: fOpts.ModTime,
data: data,
}
}
func (f *memoryFile) GetNode(path []string) (fsNode, error) {
if len(path) == 0 {
return f, nil
}
return nil, fmt.Errorf("cannot get deeper nodes from file")
}
func (f *memoryFile) Name() string {
return f.name
}
func (f *memoryFile) FullPath() string {
return f.fullPath
}
func (f *memoryFile) Stat() (os.FileInfo, error) {
return &memoryFileInfo{
filename: f.name,
modTime: f.modTime,
isDir: false,
mode: 0,
size: int64(len(f.data)),
node: f,
}, nil
}
func (f *memoryFile) Open() (File, error) {
openFile := &openMemoryFile{
memoryFile: *f,
curRead: 0,
closed: false,
}
return openFile, nil
}
type openMemoryFile struct {
memoryFile
curRead int
closed bool
}
var _ File = &openMemoryFile{}
func (omf *openMemoryFile) Read(buf []byte) (int, error) {
if omf.closed {
return 0, os.ErrClosed
}
bufLen := len(buf)
readLeft := len(omf.data) - omf.curRead
readLen := bufLen
var readErr error
if readLeft < readLen {
readLen = readLeft
readErr = io.EOF
}
copy(buf, omf.data[omf.curRead:omf.curRead+readLen])
omf.curRead += readLen
return readLen, readErr
}
func (omf *openMemoryFile) Close() error {
omf.closed = true
return nil
}