-
Notifications
You must be signed in to change notification settings - Fork 9
/
memfs.go
311 lines (251 loc) · 6.96 KB
/
memfs.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
305
306
307
308
309
310
311
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package memfs
import (
"bytes"
"context"
"io"
"io/fs"
"path/filepath"
"sort"
"strings"
"namespacelabs.dev/foundation/internal/bytestream"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/fnfs/digestfs"
"namespacelabs.dev/foundation/schema"
)
type FS struct {
files []*fnfs.File
root fsNode
}
type fsNode struct {
file *fnfs.File
children map[string]*fsNode
}
type FSStats struct {
FileCount int
}
var _ fs.ReadDirFS = &FS{}
var _ fnfs.VisitFS = &FS{}
var _ fnfs.TotalSizeFS = &FS{}
func (m *FS) Open(name string) (fs.File, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{Op: "open", Path: name, Err: fnerrors.New("invalid name")}
}
nodeName, node := walk(&m.root, name, false)
if node == nil {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
}
// Is is a dir?
if node.file == nil {
return DirHandle(nodeName, readDir(node)), nil
}
return FileHandle(*node.file), nil
}
func (m *FS) ReadDir(relPath string) ([]fs.DirEntry, error) {
if !fs.ValidPath(relPath) {
return nil, &fs.PathError{Op: "readdir", Path: relPath, Err: fnerrors.New("invalid name")}
}
_, node := walk(&m.root, relPath, false)
if node == nil {
return nil, &fs.PathError{Op: "readdir", Path: relPath, Err: fs.ErrNotExist}
}
if node.file != nil {
return nil, &fs.PathError{Op: "readdir", Path: relPath, Err: fnerrors.New("is a regular file")}
}
return readDir(node), nil
}
func readDir(node *fsNode) []fs.DirEntry {
var entries []fs.DirEntry
for name, child := range node.children {
if f := child.file; f == nil {
entries = append(entries, dirDirent{Basename: name, DirMode: memDirMode})
} else {
entries = append(entries, FileDirent{Path: f.Path, ContentSize: int64(len(f.Contents)), FileMode: memFileMode})
}
}
sort.Slice(entries, func(i, j int) bool {
return strings.Compare(entries[i].Name(), entries[j].Name()) < 0
})
return entries
}
func (m *FS) VisitFiles(ctx context.Context, visitor func(string, bytestream.ByteStream, fs.DirEntry) error) error {
return m.VisitFilesWithoutContext(func(path string, bs bytestream.Static, de FileDirent) error {
return visitor(path, bs, de)
})
}
func (m *FS) VisitFilesWithoutContext(visitor func(string, bytestream.Static, FileDirent) error) error {
// Need to guarantee that VisitFiles calls visitor in a deterministic way.
sorted := make([]*fnfs.File, len(m.files))
copy(sorted, m.files)
sort.Slice(sorted, func(i, j int) bool {
return strings.Compare(sorted[i].Path, sorted[j].Path) < 0
})
for _, f := range sorted {
dirent := FileDirent{Path: f.Path, ContentSize: int64(len(f.Contents)), FileMode: memFileMode}
if err := visitor(f.Path, bytestream.Static{Contents: f.Contents}, dirent); err != nil {
return err
}
}
return nil
}
func (m *FS) OpenWrite(name string, _ fs.FileMode) (fnfs.WriteFileHandle, error) {
// XXX support filemode
if !fs.ValidPath(name) {
return nil, &fs.PathError{Op: "write", Path: name, Err: fnerrors.New("invalid name")}
}
return &writeHandle{parent: m, path: name}, nil
}
func (m *FS) Remove(relPath string) error {
if !fs.ValidPath(relPath) {
return &fs.PathError{Op: "remove", Path: relPath, Err: fnerrors.New("invalid name")}
}
dirname := filepath.Dir(relPath)
filename := filepath.Base(relPath)
_, dir := walk(&m.root, dirname, false)
if dir == nil {
return &fs.PathError{Op: "remove", Path: relPath, Err: fs.ErrNotExist}
}
dirent, ok := dir.children[filename]
if !ok {
return &fs.PathError{Op: "remove", Path: relPath, Err: fs.ErrNotExist}
}
if dirent.file == nil && len(dirent.children) > 0 {
return &fs.PathError{Op: "remove", Path: relPath, Err: fnerrors.New("directory is not empty")}
}
delete(dir.children, filename)
return nil
}
func (m *FS) Clone() fnfs.ReadWriteFS {
c := &FS{}
c.files = make([]*fnfs.File, len(m.files))
copy(c.files, m.files)
c.root = *m.root.clone()
return c
}
func (m *FS) ComputeDigest(ctx context.Context) (schema.Digest, error) {
return digestfs.Digest(ctx, m)
}
func (m *FS) TotalSize(ctx context.Context) (uint64, error) {
var count uint64
for _, f := range m.files {
count += uint64(len(f.Contents))
}
return count, nil
}
func (n *fsNode) clone() *fsNode {
c := &fsNode{file: n.file, children: map[string]*fsNode{}}
for k, v := range n.children {
c.children[k] = v.clone()
}
return c
}
func walk(root *fsNode, path string, create bool) (string, *fsNode) {
if path == "/" || path == "." {
return "/", root
}
if path == "" {
return "", nil
}
if path[0] == '/' {
return walk(root, path[1:], create)
}
var search, sub string
i := strings.IndexByte(path, '/')
if i < 0 {
search = path
sub = ""
} else {
search = path[0:i]
sub = path[(i + 1):]
}
child, ok := root.children[search]
if create && !ok {
if root.children == nil {
root.children = map[string]*fsNode{}
}
child = &fsNode{}
root.children[search] = child
} else if !ok {
return "", nil
}
if sub != "" {
return walk(child, sub, create)
}
return search, child
}
func (m *FS) Add(path string, contents []byte) {
file := &fnfs.File{Path: path, Contents: contents}
_, node := walk(&m.root, path, true)
node.file = file
m.files = append(m.files, file)
}
func (m *FS) Stats() FSStats {
return FSStats{FileCount: len(m.files)}
}
func FileHandle(f fnfs.File) fs.File {
return memFile{entry: f, reader: bytes.NewReader(f.Contents)}
}
type memFile struct {
entry fnfs.File
reader *bytes.Reader
}
func (mf memFile) Stat() (fs.FileInfo, error) {
return FileDirent{Path: mf.entry.Path, ContentSize: int64(len(mf.entry.Contents)), FileMode: memFileMode}, nil
}
func (mf memFile) Read(p []byte) (int, error) {
return mf.reader.Read(p)
}
func (mf memFile) Close() error { return nil }
func DirHandle(basename string, entries []fs.DirEntry) fs.ReadDirFile {
return &memDir{basename, entries}
}
type memDir struct {
basename string
entries []fs.DirEntry
}
func (md *memDir) ReadDir(n int) ([]fs.DirEntry, error) {
if n < 0 {
return md.entries, nil
}
if len(md.entries) == 0 {
return nil, io.EOF
}
if n > len(md.entries) {
n = len(md.entries)
}
read := md.entries[0:n]
left := md.entries[n:]
md.entries = left
return read, nil
}
func (md *memDir) Stat() (fs.FileInfo, error) {
return dirDirent{Basename: md.basename}, nil
}
func (md *memDir) Read(p []byte) (int, error) {
return 0, fs.ErrInvalid
}
func (md *memDir) Close() error { return nil }
type writeHandle struct {
parent *FS
path string
buffer bytes.Buffer
closed bool
}
func (h *writeHandle) Write(p []byte) (n int, err error) {
if h.closed {
return 0, fs.ErrClosed
}
return h.buffer.Write(p)
}
func (h *writeHandle) Close() error {
// XXX locking.
if h.closed {
return fs.ErrClosed
}
h.parent.Add(h.path, h.buffer.Bytes())
h.closed = true
return nil
}