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
|
package fs
import (
"bytes"
"errors"
"io"
"os"
"time"
)
type memfsBase struct {
parent *memfsDir
name string
exec bool
mtime time.Time
}
type memfsChild interface {
File
setName(string)
}
func (b memfsBase) Name() string { return b.name }
func (b *memfsBase) setName(n string) { b.name = n }
func (b memfsBase) Executable() bool { return b.exec }
func (b memfsBase) ModTime() time.Time { return b.mtime }
func (b memfsBase) Delete() error {
if b.parent == nil {
return errors.New("Root entry can not be deleted")
}
b.parent.deleteChild(b.name)
return nil
}
type memfsFile struct {
memfsBase
content *bytes.Buffer
}
func (memfsFile) Type() FileType { return FFile }
func (f memfsFile) Open() (io.ReadCloser, error) {
return f, nil
}
func (f memfsFile) OpenWritable() (io.WriteCloser, error) {
return f, nil
}
func (f memfsFile) Read(p []byte) (int, error) {
return f.content.Read(p)
}
func (f memfsFile) Write(p []byte) (int, error) {
return f.content.Write(p)
}
func (memfsFile) Close() error {
return nil
}
type memfsDir struct {
memfsBase
children map[string]memfsChild
}
func (memfsDir) Type() FileType { return FDir }
func (d memfsDir) Readdir() ([]File, error) {
l := make([]File, 0, len(d.children))
for _, f := range d.children {
l = append(l, f)
}
return l, nil
}
func (d memfsDir) GetChild(name string) (File, error) {
c, ok := d.children[name]
if !ok {
return nil, os.ErrNotExist
}
return c, nil
}
func (d memfsDir) createChildBase(name string, exec bool) memfsBase {
return memfsBase{
parent: &d,
name: name,
exec: exec,
mtime: time.Now(),
}
}
func (d memfsDir) CreateChildFile(name string, exec bool) (RegularFile, error) {
child := memfsFile{
memfsBase: d.createChildBase(name, exec),
content: new(bytes.Buffer),
}
d.children[name] = &child
return &child, nil
}
func (d memfsDir) CreateChildDir(name string) (Dir, error) {
child := memfsDir{
memfsBase: d.createChildBase(name, true),
children: make(map[string]memfsChild),
}
d.children[name] = &child
return &child, nil
}
func (d memfsDir) CreateChildSymlink(name string, target string) (Symlink, error) {
child := memfsSymlink{
memfsBase: d.createChildBase(name, false),
target: target,
}
d.children[name] = &child
return &child, nil
}
func (d *memfsDir) deleteChild(name string) {
delete(d.children, name)
}
func (d *memfsDir) RenameChild(oldname, newname string) error {
c, ok := d.children[oldname]
if !ok {
return os.ErrNotExist
}
c.setName(newname)
delete(d.children, oldname)
d.children[newname] = c
return nil
}
func NewMemoryFSRoot(name string) Dir {
return &memfsDir{
memfsBase: memfsBase{
parent: nil,
name: name,
exec: true,
mtime: time.Now(),
},
children: make(map[string]memfsChild),
}
}
type memfsSymlink struct {
memfsBase
target string
}
func (memfsSymlink) Type() FileType { return FSymlink }
func (s memfsSymlink) Readlink() (string, error) {
return s.target, nil
}
|