-
Notifications
You must be signed in to change notification settings - Fork 9
/
fs.go
251 lines (223 loc) · 4.86 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
package wasi
import (
"context"
"io"
"io/fs"
"path"
"time"
)
// FS constructs a fs.FS file system backed by a wasi system.
func FS(ctx context.Context, sys System, root FD) fs.FS {
return &fileSystem{ctx, sys, root}
}
type fileSystem struct {
ctx context.Context
System
root FD
}
func (fsys *fileSystem) Open(name string) (fs.File, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{"open", name, fs.ErrInvalid}
}
const rights = PathOpenRight |
PathFileStatGetRight |
FDReadRight |
FDReadDirRight |
FDSeekRight |
FDTellRight |
FDFileStatGetRight
f, errno := fsys.PathOpen(fsys.ctx, fsys.root, SymlinkFollow, name, 0, rights, rights, 0)
if errno != ESUCCESS {
return nil, &fs.PathError{"open", name, errno}
}
return &file{fsys: fsys, name: name, fd: f}, nil
}
type file struct {
fsys *fileSystem
name string
fd FD
*dir
}
type dir struct {
dirent []DirEntry
cookie DirCookie
}
func (f *file) Close() error {
if f.fd >= 0 {
f.fsys.FDClose(f.fsys.ctx, f.fd)
f.fd = -1
}
return nil
}
func (f *file) Read(b []byte) (int, error) {
if f.fd < 0 {
return 0, io.EOF
}
if len(b) == 0 {
return 0, nil
}
n, errno := f.fsys.FDRead(f.fsys.ctx, f.fd, []IOVec{b})
if errno != ESUCCESS {
return int(n), &fs.PathError{"read", f.name, errno}
}
if n == 0 {
return 0, io.EOF
}
return int(n), nil
}
func (f *file) Stat() (fs.FileInfo, error) {
if f.fd < 0 {
return nil, &fs.PathError{"stat", f.name, fs.ErrClosed}
}
s, errno := f.fsys.FDFileStatGet(f.fsys.ctx, f.fd)
if errno != ESUCCESS {
return nil, &fs.PathError{"stat", f.name, errno}
}
return &fileInfo{stat: s, name: path.Base(f.name)}, nil
}
func (f *file) Seek(offset int64, whence int) (int64, error) {
if f.fd < 0 {
return 0, &fs.PathError{"seek", f.name, fs.ErrClosed}
}
seek, errno := f.fsys.FDSeek(f.fsys.ctx, f.fd, FileDelta(offset), Whence(whence))
if errno != ESUCCESS {
return int64(seek), &fs.PathError{"seek", f.name, errno}
}
if f.dir != nil {
f.dir.cookie = DirCookie(seek)
}
return int64(seek), nil
}
func (f *file) ReadAt(b []byte, off int64) (int, error) {
if f.fd < 0 {
return 0, io.EOF
}
n, errno := f.fsys.FDPread(f.fsys.ctx, f.fd, []IOVec{b}, FileSize(off))
if errno != ESUCCESS {
return int(n), &fs.PathError{"pread", f.name, errno}
}
if int(n) < len(b) {
return int(n), io.EOF
}
return int(n), nil
}
func (f *file) ReadDir(n int) ([]fs.DirEntry, error) {
if f.fd < 0 {
return nil, io.EOF
}
if f.dir == nil {
f.dir = new(dir)
}
capacity := n
if n <= 0 {
if capacity = cap(f.dirent); capacity == 0 {
capacity = 20
}
}
if cap(f.dirent) < capacity {
f.dirent = make([]DirEntry, capacity)
} else {
f.dirent = f.dirent[:capacity]
}
var dirent []fs.DirEntry
for {
limit := len(f.dirent)
if n > 0 {
limit = n - len(dirent)
}
rn, errno := f.fsys.FDReadDir(f.fsys.ctx, f.fd, f.dirent[:limit], f.cookie, 4096)
if rn > 0 {
for _, e := range f.dirent[:rn] {
switch string(e.Name) {
case ".", "..":
continue
}
dirent = append(dirent, &dirEntry{
typ: e.Type,
name: string(e.Name),
dir: f,
})
}
f.cookie = f.dirent[rn-1].Next
}
if errno != ESUCCESS {
return dirent, &fs.PathError{"readdir", f.name, errno}
}
if n > 0 && n == len(dirent) {
return dirent, nil
}
if rn == 0 {
if n <= 0 {
return dirent, nil
} else {
return dirent, io.EOF
}
}
}
}
var (
_ fs.ReadDirFile = (*file)(nil)
_ io.ReaderAt = (*file)(nil)
_ io.Seeker = (*file)(nil)
)
type fileInfo struct {
stat FileStat
name string
}
func (info *fileInfo) Name() string {
return info.name
}
func (info *fileInfo) Size() int64 {
return int64(info.stat.Size)
}
func (info *fileInfo) Mode() fs.FileMode {
return makeFileMode(info.stat.FileType)
}
func (info *fileInfo) ModTime() time.Time {
return time.Unix(0, int64(info.stat.ModifyTime))
}
func (info *fileInfo) IsDir() bool {
return info.stat.FileType == DirectoryType
}
func (info *fileInfo) Sys() any {
return &info.stat
}
type dirEntry struct {
typ FileType
dir *file
name string
}
func (d *dirEntry) Name() string {
return d.name
}
func (d *dirEntry) IsDir() bool {
return d.typ == DirectoryType
}
func (d *dirEntry) Type() fs.FileMode {
return makeFileMode(d.typ)
}
func (d *dirEntry) Info() (fs.FileInfo, error) {
s, errno := d.dir.fsys.PathFileStatGet(d.dir.fsys.ctx, d.dir.fd, 0, d.name)
if errno != ESUCCESS {
return nil, &fs.PathError{"stat", d.name, errno}
}
return &fileInfo{stat: s, name: d.name}, nil
}
func makeFileMode(fileType FileType) fs.FileMode {
switch fileType {
case BlockDeviceType:
return fs.ModeDevice
case CharacterDeviceType:
return fs.ModeDevice | fs.ModeCharDevice
case DirectoryType:
return fs.ModeDir
case RegularFileType:
return 0
case SocketDGramType, SocketStreamType:
return fs.ModeSocket
case SymbolicLinkType:
return fs.ModeSymlink
default:
return fs.ModeIrregular
}
}