-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_vault.go
231 lines (188 loc) · 4.88 KB
/
memory_vault.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
package goblin
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
)
const (
filesystemRootPath = "."
)
func makeMemoryVaultName(name string) string {
return fmt.Sprintf("goblinMemoryVaultX%s", name)
}
// MemoryVaultOption is an option used when creating a memory vault.
type MemoryVaultOption func(*memoryVaultOptions)
type memoryVaultOptions struct {
Root *memoryDir
}
func newMemoryVaultOptions() *memoryVaultOptions {
return &memoryVaultOptions{
Root: newMemoryDir(filesystemRootPath),
}
}
// MemoryVault is a vault stored in memory. It can be used as a temporary
// in-memory filesystem or as a way to load a filesystem from binary data,
// such as one embedded in a file.
type MemoryVault struct {
root *memoryDir
}
var _ Vault = &MemoryVault{}
// NewMemoryVault creates a new memory vault.
func NewMemoryVault(opts ...MemoryVaultOption) *MemoryVault {
vaultOpts := newMemoryVaultOptions()
for _, opt := range opts {
opt(vaultOpts)
}
return &MemoryVault{
root: vaultOpts.Root,
}
}
func (v *MemoryVault) String() string {
return "Memory Vault"
}
// WriteFile reads data from the provided io.Reader and then writes it to the memory vault
// at the provided path.
func (v *MemoryVault) WriteFile(path string, r io.Reader, opts ...FileOption) error {
pathParts := strings.Split(path, string(os.PathSeparator))
curRoot := v.root
for idx := 0; idx < len(pathParts)-1; idx++ {
part := pathParts[idx]
newRoot, ok := curRoot.nodes[part]
if !ok {
// TODO set a real modtime
curFullPath := strings.Join(pathParts[:idx+1], pathSeparator)
newRoot = newMemoryDir(curFullPath)
curRoot.nodes[part] = newRoot
}
dirRoot, ok := newRoot.(*memoryDir)
if !ok {
return fmt.Errorf("%s is not a directory", part)
}
curRoot = dirRoot
}
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
f := newMemoryFile(path, make([]byte, len(data)), opts...)
copy(f.data, data)
curRoot.nodes[pathParts[len(pathParts)-1]] = f
return nil
}
// Open will open the file at the provided path from the in-memory vault.
func (v *MemoryVault) Open(name string) (File, error) {
tokens, err := splitPath(name)
if err != nil {
return nil, err
}
node, err := v.root.GetNode(tokens)
if err != nil {
return nil, err
}
return node.Open()
}
// Stat returns file info for the provided path in the in-memory vault.
func (v *MemoryVault) Stat(name string) (os.FileInfo, error) {
tokens, err := splitPath(name)
if err != nil {
return nil, err
}
n, err := v.root.GetNode(tokens)
if err != nil {
return nil, err
}
return n.Stat()
}
// ReadDir returns a slice of file info for the provided directory in the in-memory vault.
func (v *MemoryVault) ReadDir(dirName string) ([]os.FileInfo, error) {
var res []os.FileInfo
var pathTokens []string
var err error
if strings.TrimSpace(dirName) == filesystemRootPath {
pathTokens = []string{}
} else {
pathTokens, err = splitPath(dirName)
if err != nil {
return nil, err
}
}
node, err := v.root.GetNode(pathTokens)
if err != nil {
return nil, err
}
dirNode, ok := node.(*memoryDir)
if !ok {
return nil, fmt.Errorf("not a directory: %s", dirName)
}
for _, node := range dirNode.Nodes() {
fi, err := node.Stat()
if err != nil {
return nil, err
}
res = append(res, fi)
}
// ReadDir returns contents in filename order
sort.Slice(res, func(i, j int) bool {
rI := res[i]
rJ := res[j]
return strings.Compare(rI.Name(), rJ.Name()) == -1
})
return res, nil
}
// Glob returns names of files in the in-memory vault that match the given pattern.
func (v *MemoryVault) Glob(pattern string) ([]string, error) {
// Naive implementation that just navigates the whole FS tree
// and runs filepath.Match on all the paths.
if strings.ContainsRune(pattern, '\\') {
return nil, fmt.Errorf("backslash is not allowed in glob patterns")
}
var glob func(string, *memoryDir) ([]string, error)
glob = func(pattern string, dir *memoryDir) ([]string, error) {
var res []string
for _, node := range dir.nodes {
if dirNode, ok := node.(*memoryDir); ok {
dirNodes, err := glob(pattern, dirNode)
if err != nil {
return nil, err
}
res = append(res, dirNodes...)
}
nodePath := node.FullPath()
match, err := filepath.Match(pattern, nodePath)
if err != nil {
return nil, err
}
if match {
res = append(res, node.FullPath())
}
}
return res, nil
}
if strings.TrimSpace(pattern) == filesystemRootPath {
pattern = "*"
}
res, err := glob(pattern, v.root)
if err != nil {
return nil, err
}
// Sort the glob results by name
sort.Strings(res)
return res, nil
}
// ReadFile returns the contents of the file at the given path from
// the in-memory vault.
func (v *MemoryVault) ReadFile(name string) ([]byte, error) {
f, err := v.Open(name)
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
return data, nil
}