-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
345 lines (305 loc) · 7.38 KB
/
fs.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package fsx
import (
"errors"
"io"
"io/fs"
"os"
"strings"
"time"
_ "unsafe"
"golang.org/x/exp/slices"
)
var _ fs.File = (*treeFS)(nil)
var _ fs.DirEntry = (*treeFS)(nil)
var _ fs.ReadDirFile = (*treeFS)(nil)
var _ fs.FS = (*treeFS)(nil)
var _ CreateFS = (*treeFS)(nil)
var _ MkdirFS = (*treeFS)(nil)
var _ fs.FS = DirFS("")
var _ CreateFS = DirFS("")
var _ MkdirFS = DirFS("")
type treeFS struct {
entries []fs.File
treeFile
}
func TestFS(files [][2]string) *treeFS {
tfs := newTreeFS("", 0)
for _, file := range files {
cur := tfs
path, body := file[0], file[1]
parts := strings.Split(path, string(os.PathSeparator))
for i, part := range parts {
if i == len(parts)-1 {
cur.entries = append(cur.entries, newTreeFile(part, 0, []byte(body)))
} else {
j := slices.IndexFunc(cur.entries, func(f fs.File) bool {
return nameOf(f) == part
})
if j < 0 {
cur.entries = append(cur.entries, newTreeFS(part, 0))
j = len(cur.entries) - 1
}
cur = cur.entries[j].(*treeFS)
}
}
}
return tfs
}
// ReadDir implements fs.ReadDirFile
func (tfs *treeFS) ReadDir(count int) ([]fs.DirEntry, error) {
n := len(tfs.entries) - tfs.offset
if n == 0 && count > 0 {
return nil, io.EOF
}
if count > 0 && n > count {
n = count
}
list := make([]fs.DirEntry, n)
for i := range list {
list[i] = tfs.entries[tfs.offset+i].(fs.DirEntry)
}
tfs.offset += n
return list, nil
}
var _ fs.FileInfo = (*treeFile)(nil)
var _ fs.File = (*treeFile)(nil)
var _ fs.DirEntry = (*treeFile)(nil)
var _ WriteableFile = (*treeFile)(nil)
type treeFile struct {
name string
mode fs.FileMode
data []byte
offset int
}
// Write implements WriteableFile
func (tf *treeFile) Write(p []byte) (n int, err error) {
tf.data = append(tf.data, p...)
return len(p), nil
}
// Info implements fs.DirEntry
func (tf *treeFile) Info() (fs.FileInfo, error) {
return tf, nil
}
// Type implements fs.DirEntry
func (tf *treeFile) Type() fs.FileMode {
return tf.mode.Type()
}
// Close implements fs.File
func (tf *treeFile) Close() error {
tf.offset = 0
return nil
}
// Read implements fs.File
func (tf *treeFile) Read(p []byte) (int, error) {
if tf.offset >= len(tf.data) {
return 0, io.EOF
}
n := copy(p, tf.data[tf.offset:])
tf.offset += n
return n, nil
}
// Stat implements fs.File
func (tf *treeFile) Stat() (fs.FileInfo, error) {
return tf, nil
}
// IsDir implements fs.FileInfo
func (tf *treeFile) IsDir() bool {
return tf.mode.IsDir()
}
// ModTime implements fs.FileInfo
func (*treeFile) ModTime() time.Time {
return time.Time{}
}
// Mode implements fs.FileInfo
func (tf *treeFile) Mode() fs.FileMode {
return tf.mode
}
// Name implements fs.FileInfo
func (tf *treeFile) Name() string {
return tf.name
}
// Size implements fs.FileInfo
func (tf *treeFile) Size() int64 {
return int64(len(tf.data))
}
// Sys implements fs.FileInfo
func (*treeFile) Sys() any {
return nil
}
func newTreeFile(name string, mode fs.FileMode, data []byte) *treeFile {
return &treeFile{
name: name,
mode: mode,
data: data,
}
}
func (tfs *treeFS) Read([]byte) (int, error) { return 0, errors.New("cannot read directory") }
// Mkdir implements MkdirFS
func (tfs *treeFS) Mkdir(name string, perm fs.FileMode) (fs.FS, error) {
i := slices.IndexFunc(tfs.entries, func(f fs.File) bool {
return nameOf(f) == name
})
if i >= 0 {
return nil, &fs.PathError{Op: "mkdir", Path: name, Err: fs.ErrExist}
}
nfs := newTreeFS(name, perm)
tfs.entries = append(tfs.entries, nfs)
return nfs, nil
}
func newTreeFS(name string, perm fs.FileMode) *treeFS {
return &treeFS{
entries: []fs.File{},
treeFile: treeFile{
name: name,
mode: perm | fs.ModeDir,
},
}
}
// Create implements CreateFS
func (tfs *treeFS) Create(name string) (WriteableFile, error) {
i := slices.IndexFunc(tfs.entries, func(f fs.File) bool {
return nameOf(f) == name
})
if i >= 0 {
return nil, &fs.PathError{Op: "create", Path: name, Err: fs.ErrExist}
}
f := newTreeFile(name, 0, nil)
tfs.entries = append(tfs.entries, f)
return f, nil
}
func nameOf(f fs.File) string {
switch f := f.(type) {
case *treeFS:
return f.name
case *treeFile:
return f.name
}
panic("unreachable")
}
func (tfs *treeFS) open(name string) (fs.File, error) {
elems := strings.Split(name, string(os.PathSeparator))
cur := tfs
for _, elem := range elems {
if elem == "." {
continue
}
i := slices.IndexFunc(cur.entries, func(f fs.File) bool {
return nameOf(f) == elem
})
if i < 0 {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
}
entry := cur.entries[i]
stat, err := entry.Stat()
if err != nil {
return nil, &fs.PathError{Op: "open", Path: name, Err: err}
}
if stat.IsDir() {
cur = entry.(*treeFS)
} else {
return entry, nil
}
}
ntfs := *cur
return &ntfs, nil
}
// Open implements fs.FS
func (tfs *treeFS) Open(name string) (fs.File, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid}
}
return tfs.open(name)
}
type WriteableFile interface {
fs.File
io.Writer
}
type CreateFS interface {
fs.FS
Create(name string) (WriteableFile, error)
}
func Create(fsys fs.FS, name string) (WriteableFile, error) {
if cfs, ok := fsys.(CreateFS); ok {
return cfs.Create(name)
}
return nil, &fs.PathError{Op: "create", Path: name, Err: fs.ErrNotExist}
}
type MkdirFS interface {
fs.FS
Mkdir(name string, perm fs.FileMode) (fs.FS, error)
}
func Mkdir(fsys fs.FS, name string, perm fs.FileMode) (fs.FS, error) {
if mfs, ok := fsys.(MkdirFS); ok {
return mfs.Mkdir(name, perm)
}
return nil, &fs.PathError{Op: "mkdir", Path: name, Err: fs.ErrInvalid}
}
type DirFS string
// Mkdir implements MkdirFS
func (dir DirFS) Mkdir(name string, perm fs.FileMode) (fs.FS, error) {
fullname, err := dir.join(name)
if err != nil {
return nil, &os.PathError{Op: "mkdir", Path: name, Err: err}
}
if err := os.Mkdir(fullname, perm); err != nil {
err.(*os.PathError).Path = name
return nil, err
}
return DirFS(fullname), nil
}
// Create implements CreateFS
func (dir DirFS) Create(name string) (WriteableFile, error) {
fullname, err := dir.join(name)
if err != nil {
return nil, &os.PathError{Op: "create", Path: name, Err: err}
}
f, err := os.Create(fullname)
if err != nil {
err.(*os.PathError).Path = name
return nil, err
}
return f, nil
}
func (dir DirFS) Open(name string) (fs.File, error) {
fullname, err := dir.join(name)
if err != nil {
return nil, &os.PathError{Op: "stat", Path: name, Err: err}
}
f, err := os.Open(fullname)
if err != nil {
err.(*os.PathError).Path = name
return nil, err
}
return f, nil
}
//go:linkname safefilepath_FromFS internal/safefilepath.FromFS
func safefilepath_FromFS(path string) (string, error)
// join returns the path for name in dir.
func (dir DirFS) join(name string) (string, error) {
if dir == "" {
return "", errors.New("os: DirFS with empty root")
}
if !fs.ValidPath(name) {
return "", os.ErrInvalid
}
name, err := safefilepath_FromFS(name)
if err != nil {
return "", os.ErrInvalid
}
if os.IsPathSeparator(dir[len(dir)-1]) {
return string(dir) + name, nil
}
return string(dir) + string(os.PathSeparator) + name, nil
}
func (dir DirFS) Stat(name string) (fs.FileInfo, error) {
fullname, err := dir.join(name)
if err != nil {
return nil, &os.PathError{Op: "stat", Path: name, Err: err}
}
f, err := os.Stat(fullname)
if err != nil {
err.(*os.PathError).Path = name
return nil, err
}
return f, nil
}