-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
148 lines (116 loc) · 2.74 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
package errgoengine
import (
"io"
"io/fs"
"os"
"reflect"
"time"
)
type MultiReadFileFS struct {
FSs []fs.ReadFileFS
}
func (mfs *MultiReadFileFS) LastAttachedIdx() int {
return len(mfs.FSs) - 1
}
func (mfs *MultiReadFileFS) AttachOrReplace(fs fs.ReadFileFS, idx int) {
if idx < len(mfs.FSs) && idx >= 0 {
if reflect.DeepEqual(mfs.FSs[idx], fs) {
return
}
mfs.FSs[idx] = fs
return
}
mfs.Attach(fs, idx)
}
func (mfs *MultiReadFileFS) Attach(instance fs.ReadFileFS, idx int) {
if idx < len(mfs.FSs) && idx >= 0 {
if reflect.DeepEqual(mfs.FSs[idx], instance) {
return
}
mfs.FSs = append(
append(append([]fs.ReadFileFS{}, mfs.FSs[:idx]...), instance),
mfs.FSs[idx+1:]...)
return
}
// check first if the fs is already attached
for _, f := range mfs.FSs {
if f == instance {
return
}
}
mfs.FSs = append(mfs.FSs, instance)
}
func (mfs *MultiReadFileFS) Open(name string) (fs.File, error) {
for _, fs := range mfs.FSs {
if fs == nil {
continue
}
if file, err := fs.Open(name); err == nil {
return file, nil
}
}
return nil, os.ErrNotExist
}
type virtualFileInfo struct {
name string
}
func (f *virtualFileInfo) Name() string { return f.name }
func (*virtualFileInfo) Size() int64 { return 0 }
func (*virtualFileInfo) Mode() fs.FileMode { return 0400 }
func (*virtualFileInfo) ModTime() time.Time { return time.Now() }
func (*virtualFileInfo) IsDir() bool { return false }
func (*virtualFileInfo) Sys() any { return nil }
type VirtualFile struct {
Name string
}
func (VirtualFile) Read(bt []byte) (int, error) { return 0, io.EOF }
func (vf VirtualFile) Stat() (fs.FileInfo, error) { return &virtualFileInfo{vf.Name}, nil }
func (VirtualFile) Close() error { return nil }
type VirtualFS struct {
Files []VirtualFile
}
func (vfs *VirtualFS) StubFile(name string) VirtualFile {
file := VirtualFile{
Name: name,
}
vfs.Files = append(vfs.Files, file)
return file
}
func (vfs *VirtualFS) Open(name string) (fs.File, error) {
for _, file := range vfs.Files {
if file.Name == name {
return file, nil
}
}
return nil, os.ErrNotExist
}
func (vfs *VirtualFS) ReadFile(name string) ([]byte, error) {
for _, file := range vfs.Files {
if file.Name == name {
return make([]byte, 0), nil
}
}
return nil, os.ErrNotExist
}
func (mfs *MultiReadFileFS) ReadFile(name string) ([]byte, error) {
for _, fs := range mfs.FSs {
if fs == nil {
continue
}
if file, err := fs.Open(name); err == nil {
return io.ReadAll(file)
}
}
return nil, os.ErrNotExist
}
type RawFS struct{}
func (*RawFS) Open(name string) (fs.File, error) {
return os.Open(name)
}
func (rfs *RawFS) ReadFile(name string) ([]byte, error) {
file, err := rfs.Open(name)
if err != nil {
return nil, err
}
return io.ReadAll(file)
}