-
Notifications
You must be signed in to change notification settings - Fork 0
/
memfilesystem.go
625 lines (530 loc) · 14 KB
/
memfilesystem.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
package fs
import (
"context"
"errors"
"fmt"
iofs "io/fs"
"net/url"
"path"
"strings"
"sync"
"time"
"unsafe"
"github.com/ungerik/go-fs/fsimpl"
)
var (
_ FileSystem = new(MemFileSystem)
// memFileNode implements io/fs.FileInfo
_ iofs.FileInfo = new(memFileInfo)
)
var memFileSystemDefaultPermissions = UserAndGroupReadWrite
// memFileNode implements io/fs.FileInfo
type memFileNode struct {
MemFile
Modified time.Time
Permissions Permissions
Dir map[string]*memFileNode
}
func (n *memFileNode) IsDir() bool {
return n != nil && n.Dir != nil
}
func (n *memFileNode) Mode() iofs.FileMode {
return n.Permissions.FileMode(n.Dir != nil)
}
func (n *memFileNode) ModTime() time.Time {
return n.Modified
}
func (n *memFileNode) Sys() any { return nil }
// MemFileSystem is a fully featured thread-safe
// file system living in random access memory.
//
// Usefull as mock file system for tests
// or caching of slow file systems.
type MemFileSystem struct {
id string
sep string
volume string
prefix string
readOnly bool
root memFileNode
mtx sync.RWMutex
}
func NewMemFileSystem(separator string, initialFiles ...MemFile) (*MemFileSystem, error) {
// Validate arguments
if separator != `/` && separator != `\` {
return nil, fmt.Errorf("invalid separator %q", separator)
}
for i, f := range initialFiles {
if f.FileName == "" {
return nil, fmt.Errorf("empty initialFiles[%d].FileName", i)
}
}
// Create MemFileSystem
now := time.Now()
memFS := &MemFileSystem{
sep: separator,
root: memFileNode{
MemFile: MemFile{FileName: separator},
Modified: now,
Dir: make(map[string]*memFileNode, len(initialFiles)),
},
}
memFS.id = fmt.Sprintf("%x", unsafe.Pointer(memFS))
memFS.updatePrefix()
// Add initial files
for _, f := range initialFiles {
_, err := memFS.AddMemFile(f, now)
if err != nil {
return nil, err
}
}
// Register with global file system registry
Register(memFS)
return memFS, nil
}
// NewSingleMemFileSystem creates and registers a new MemFileSystem
// containing a single MemFile that is returned as a File
// that can be used to access the file without knowing the file system.
// Closing the file system will make the File invalid.
func NewSingleMemFileSystem(file MemFile) (*MemFileSystem, File, error) {
fs, err := NewMemFileSystem("/", file)
if err != nil {
return nil, "", err
}
return fs, fs.JoinCleanFile("/", file.FileName), nil
}
func newMemDirNode(name string, modified time.Time, perm ...Permissions) *memFileNode {
if name == "" {
panic("empty dir name")
}
return &memFileNode{
MemFile: MemFile{FileName: name},
Modified: modified,
Permissions: JoinPermissions(perm, memFileSystemDefaultPermissions),
Dir: make(map[string]*memFileNode),
}
}
func newMemFileNode(f MemFile, modified time.Time, perm ...Permissions) *memFileNode {
if f.FileName == "" {
panic("empty filename")
}
return &memFileNode{
MemFile: f,
Modified: modified,
Permissions: JoinPermissions(perm, memFileSystemDefaultPermissions),
Dir: nil,
}
}
func (fs *MemFileSystem) SetReadOnly(readOnly bool) {
fs.mtx.Lock()
fs.readOnly = readOnly
fs.mtx.Unlock()
}
func (fs *MemFileSystem) WithID(id string) *MemFileSystem {
if id == "" {
panic("empty id")
}
if id == fs.id {
return fs
}
Unregister(fs)
fs.id = id
fs.updatePrefix()
Register(fs)
return fs
}
func (fs *MemFileSystem) WithVolume(volume string) *MemFileSystem {
if volume == fs.volume {
return fs
}
Unregister(fs)
fs.volume = volume
fs.updatePrefix()
Register(fs)
return fs
}
// AddMemFile adds a MemFile to the file system with the given modified time.
// The MemFile.FileName can be a path with the path separator of the MemFileSystem,
// in which case all directories of the path are created.
func (fs *MemFileSystem) AddMemFile(f MemFile, modified time.Time) (File, error) {
if f.FileName == "" {
return "", errors.New("empty filename")
}
fs.mtx.Lock()
defer fs.mtx.Unlock()
if fs.readOnly {
return "", ErrReadOnlyFileSystem
}
pathParts := fs.SplitPath(f.FileName)
parentDir := &fs.root
// Make all dirs first
for i := 0; i < len(pathParts)-2; i++ {
// TODO makeAllDirs
err := fs.makeDir(fs.JoinCleanPath(pathParts[0:i+1]...), nil)
if err != nil {
return "", err
}
panic(" todo set parentDir ")
}
parentDir.Dir[pathParts[len(pathParts)-1]] = newMemFileNode(f, modified)
return fs.JoinCleanFile(pathParts...), nil
}
func (fs *MemFileSystem) pathNodeOrNil(filePath string) (node, parent *memFileNode) {
if filePath == "" {
return nil, nil
}
node = &fs.root
for _, name := range fs.SplitPath(filePath) {
subNode, ok := node.Dir[name]
if !ok {
return nil, parent
}
parent = node
node = subNode
}
return node, parent
}
func (fs *MemFileSystem) MakeDir(dirPath string, perm []Permissions) error {
fs.mtx.Lock()
defer fs.mtx.Unlock()
return fs.makeDir(dirPath, perm)
}
func (fs *MemFileSystem) makeDir(dirPath string, _ []Permissions) error {
if dirPath == "" {
return ErrEmptyPath
}
if fs.readOnly {
return ErrReadOnlyFileSystem
}
node, parent := fs.pathNodeOrNil(dirPath)
if node != nil {
return NewErrAlreadyExists(fs.RootDir().Join(dirPath))
}
parentDir, name := fs.SplitDirAndName(dirPath)
if parent == nil {
return NewErrDoesNotExist(fs.RootDir().Join(parentDir))
}
if !parent.IsDir() {
return NewErrIsNotDirectory(fs.RootDir().Join(parentDir))
}
parent.Dir[name] = newMemDirNode(name, time.Now())
return nil
}
func (fs *MemFileSystem) MakeAllDirs(dirPath string, perm []Permissions) error {
fs.mtx.Lock()
defer fs.mtx.Unlock()
return fs.makeAllDirs(dirPath, perm)
}
func (fs *MemFileSystem) makeAllDirs(dirPath string, _ []Permissions) error {
if dirPath == "" {
return ErrEmptyPath
}
if fs.readOnly {
return ErrReadOnlyFileSystem
}
// pathParts := fs.SplitPath(f.FileName)
// parentDir := &fs.root
// // Make all dirs first
// for i := 0; i < len(pathParts)-2; i++ {
// err := fs.MakeDir(fs.JoinCleanPath(pathParts[0:i+1]...), nil)
// if err != nil {
// return "", err
// }
// panic(" todo set parentDir ")
// }
// parentDir.Dir[pathParts[len(pathParts)-1]] = fs.newMemFileInfo(f, modified)
panic("TODO")
}
func (fs *MemFileSystem) ReadableWritable() (readable, writable bool) {
fs.mtx.Lock()
defer fs.mtx.Unlock()
return true, !fs.readOnly
}
func (fs *MemFileSystem) RootDir() File {
return File(fs.prefix + fs.sep)
}
func (fs *MemFileSystem) ID() (string, error) {
return fs.id, nil
}
func (fs *MemFileSystem) updatePrefix() {
if fs.volume != "" {
fs.prefix = "mem://" + fs.id + "/" + fs.volume
} else {
fs.prefix = "mem://" + fs.id
}
}
func (fs *MemFileSystem) Prefix() string {
return fs.prefix
}
func (*MemFileSystem) Name() string {
return "memory file system"
}
func (fs *MemFileSystem) String() string {
return fmt.Sprintf("MemFileSystem(%s)", fs.prefix)
}
func (fs *MemFileSystem) JoinCleanFile(uri ...string) File {
return File(fs.prefix + fs.JoinCleanPath(uri...))
}
func (fs *MemFileSystem) IsAbsPath(filePath string) bool {
if strings.HasPrefix(filePath, fs.sep) {
return true
}
if fs.volume != "" && strings.HasPrefix(filePath, fs.volume) {
return true
}
return false
}
func (fs *MemFileSystem) AbsPath(filePath string) string {
return fs.JoinCleanPath(filePath)
}
func (fs *MemFileSystem) URL(cleanPath string) string {
return fs.prefix + cleanPath
}
func (fs *MemFileSystem) CleanPathFromURI(uri string) string {
return strings.TrimPrefix(uri, fs.prefix)
}
func (fs *MemFileSystem) JoinCleanPath(uriParts ...string) string {
if len(uriParts) > 0 {
uriParts[0] = strings.TrimPrefix(uriParts[0], fs.prefix)
}
cleanPath := strings.Join(uriParts, fs.sep)
unescPath, err := url.PathUnescape(cleanPath)
if err == nil {
cleanPath = unescPath
}
cleanPath = path.Clean(cleanPath) // TODO use sep
return cleanPath
}
func (fs *MemFileSystem) SplitPath(filePath string) []string {
return fsimpl.SplitPath(filePath, fs.prefix, fs.sep)
}
func (fs *MemFileSystem) Separator() string {
return fs.sep
}
func (*MemFileSystem) MatchAnyPattern(name string, patterns []string) (bool, error) {
return false, nil
}
func (fs *MemFileSystem) SplitDirAndName(filePath string) (dir, name string) {
return fsimpl.SplitDirAndName(filePath, 0, "/")
}
func (fs *MemFileSystem) VolumeName(filePath string) string {
if len(filePath) < len(fs.volume) {
return ""
}
return filePath[:len(fs.volume)]
}
func (fs *MemFileSystem) Volume() string {
return fs.volume
}
func (fs *MemFileSystem) Stat(filePath string) (iofs.FileInfo, error) {
if filePath == "" {
return nil, ErrEmptyPath
}
fs.mtx.RLock()
defer fs.mtx.RUnlock()
node, _ := fs.pathNodeOrNil(filePath)
if node == nil {
return nil, NewErrDoesNotExist(fs.RootDir().Join(filePath))
}
return node, nil
}
func (fs *MemFileSystem) Exists(filePath string) bool {
if filePath == "" {
return false
}
fs.mtx.RLock()
defer fs.mtx.RUnlock()
node, _ := fs.pathNodeOrNil(filePath)
return node != nil
}
func (*MemFileSystem) IsHidden(filePath string) bool {
return false
}
func (*MemFileSystem) IsSymbolicLink(filePath string) bool {
return false
}
func (*MemFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error {
panic("TODO")
}
// func (*MemFileSystem) SetPermissions(filePath string, perm Permissions) error {
// return nil
// }
// func (*MemFileSystem) User(filePath string) (string, error) {
// return "", nil
// }
// func (*MemFileSystem) SetUser(filePath string, user string) error {
// return nil
// }
// func (*MemFileSystem) Group(filePath string) (string, error) {
// return "", nil
// }
// func (*MemFileSystem) SetGroup(filePath string, group string) error {
// return nil
// }
func (fs *MemFileSystem) Touch(filePath string, perm []Permissions) error {
if filePath == "" {
return ErrEmptyPath
}
fs.mtx.Lock()
defer fs.mtx.Unlock()
if fs.readOnly {
return ErrReadOnlyFileSystem
}
node, parent := fs.pathNodeOrNil(filePath)
if node != nil {
node.Modified = time.Now()
return nil
}
parentDir, name := fs.SplitDirAndName(filePath)
if parent == nil {
return NewErrDoesNotExist(fs.RootDir().Join(parentDir))
}
parent.Dir[name] = newMemFileNode(
MemFile{FileName: name},
time.Now(),
JoinPermissions(perm, memFileSystemDefaultPermissions),
)
return nil
}
func (fs *MemFileSystem) ReadAll(ctx context.Context, filePath string) ([]byte, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
if filePath == "" {
return nil, ErrEmptyPath
}
fs.mtx.RLock()
defer fs.mtx.RUnlock()
node, _ := fs.pathNodeOrNil(filePath)
if node == nil {
return nil, NewErrDoesNotExist(fs.RootDir().Join(filePath))
}
return node.FileData, nil
}
func (fs *MemFileSystem) WriteAll(ctx context.Context, filePath string, data []byte, perm []Permissions) error {
if ctx.Err() != nil {
return ctx.Err()
}
if filePath == "" {
return ErrEmptyPath
}
fs.mtx.Lock()
defer fs.mtx.Unlock()
if fs.readOnly {
return ErrReadOnlyFileSystem
}
node, parent := fs.pathNodeOrNil(filePath)
if node != nil {
node.FileData = data
node.Modified = time.Now()
return nil
}
parentDir, name := fs.SplitDirAndName(filePath)
if parent == nil {
return NewErrDoesNotExist(fs.RootDir().Join(parentDir))
}
parent.Dir[name] = newMemFileNode(
MemFile{FileName: name, FileData: data},
time.Now(),
JoinPermissions(perm, memFileSystemDefaultPermissions),
)
return nil
}
func (fs *MemFileSystem) Append(ctx context.Context, filePath string, data []byte, perm []Permissions) error {
if ctx.Err() != nil {
return ctx.Err()
}
if filePath == "" {
return ErrEmptyPath
}
fs.mtx.Lock()
defer fs.mtx.Unlock()
if fs.readOnly {
return ErrReadOnlyFileSystem
}
node, parent := fs.pathNodeOrNil(filePath)
if node != nil {
node.FileData = append(node.FileData, data...)
node.Modified = time.Now()
return nil
}
parentDir, name := fs.SplitDirAndName(filePath)
if parent == nil {
return NewErrDoesNotExist(fs.RootDir().Join(parentDir))
}
parent.Dir[name] = newMemFileNode(
MemFile{FileName: name, FileData: data},
time.Now(),
JoinPermissions(perm, memFileSystemDefaultPermissions),
)
return nil
}
func (fs *MemFileSystem) OpenReader(filePath string) (iofs.File, error) {
return nil, nil
}
func (fs *MemFileSystem) OpenWriter(filePath string, perm []Permissions) (WriteCloser, error) {
return nil, nil
}
func (fs *MemFileSystem) OpenAppendWriter(filePath string, perm []Permissions) (WriteCloser, error) {
return nil, nil
}
func (fs *MemFileSystem) OpenReadWriter(filePath string, perm []Permissions) (ReadWriteSeekCloser, error) {
return nil, nil
}
func (fs *MemFileSystem) Watch(filePath string, onEvent func(File, Event)) (cancel func() error, err error) {
return nil, nil
}
func (fs *MemFileSystem) Truncate(filePath string, newSize int64) error {
if filePath == "" {
return ErrEmptyPath
}
fs.mtx.Lock()
defer fs.mtx.Unlock()
if fs.readOnly {
return ErrReadOnlyFileSystem
}
node, _ := fs.pathNodeOrNil(filePath)
if node == nil {
return NewErrDoesNotExist(fs.RootDir().Join(filePath))
}
currentSize := int64(len(node.FileData))
if currentSize == newSize {
return nil
}
if currentSize > newSize {
node.FileData = node.FileData[:newSize]
} else {
node.FileData = append(node.FileData, make([]byte, newSize-currentSize)...)
}
node.Modified = time.Now()
return nil
}
func (fs *MemFileSystem) CopyFile(ctx context.Context, srcFile string, destFile string, buf *[]byte) error {
return nil
}
func (fs *MemFileSystem) Rename(filePath string, newName string) (string, error) {
return "", nil
}
func (fs *MemFileSystem) Move(filePath string, destPath string) error {
return nil
}
func (fs *MemFileSystem) Remove(filePath string) error {
return nil
}
func (fs *MemFileSystem) Close() error {
fs.mtx.Lock()
if fs.root.Dir == nil {
fs.mtx.Unlock()
return nil // already closed
}
fs.root.Dir = nil
fs.mtx.Unlock() // Unlock before Unregister to avoid deadlock
Unregister(fs)
return nil
}
func (fs *MemFileSystem) Clear() {
fs.mtx.Lock()
defer fs.mtx.Unlock()
clear(fs.root.Dir)
fs.root.Modified = time.Now()
}