-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
301 lines (264 loc) · 5.46 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
package ya9p
import (
"errors"
"io"
"io/fs"
"math/rand"
"os"
pathpkg "path"
"strings"
"9fans.net/go/plan9"
"github.com/rmatsuoka/ya9p/extfs"
)
type srvFS struct {
fsys fs.FS
}
type fidFS struct {
omode int
fsys fs.FS
file fs.File
path string
isDir bool
dirEntries []fs.DirEntry
readDirOffset int
readOffset int64
writeOffset int64
}
func FS(fsys fs.FS) Srv {
return &srvFS{fsys}
}
func FSFid(fsys fs.FS, name string) (Fid, Qid, error) {
info, err := fs.Stat(fsys, name)
if err != nil {
return nil, Qid{}, err
}
return newFidFS(fsys, name), FileInfoToQid(info), nil
}
func (s *srvFS) Auth(user, aname string) (Fid, Qid, error) {
return nil, Qid{}, NoAuthRequired
}
func (s *srvFS) Attach(afid Fid, user, aname string) (Fid, Qid, error) {
return FSFid(s.fsys, ".")
}
func newFidFS(fsys fs.FS, path string) *fidFS {
return &fidFS{omode: -1, fsys: fsys, path: path}
}
func cleanPath(p string) string {
s := pathpkg.Clean(p)
if strings.HasPrefix(s, "../") {
return "."
}
return s
}
func (f *fidFS) Walk(names []string) (Fid, []Qid, error) {
if f.omode != -1 {
return nil, nil, ErrBadUseFid
}
if len(names) == 0 {
return newFidFS(f.fsys, f.path), []Qid{}, nil
}
info, err := fs.Stat(f.fsys, f.path)
if err != nil {
return nil, nil, err
}
if !info.IsDir() {
return nil, nil, ErrWalkNoDir
}
var qids []Qid
path := f.path
for _, name := range names {
path = cleanPath(path + "/" + name)
info, err = fs.Stat(f.fsys, path)
if err != nil {
break
}
qids = append(qids, FileInfoToQid(info))
}
if len(qids) == 0 {
if err == nil {
err = errors.New("unknown errors")
}
return nil, nil, err
}
return newFidFS(f.fsys, path), qids, nil
}
func (f *fidFS) Open(mode uint8) (Qid, uint32, error) {
if f.omode != -1 {
return Qid{}, 0, ErrBadUseFid
}
// no support ORCLOSE
if mode&plan9.ORCLOSE != 0 {
return Qid{}, 0, ErrPerm
}
var o int
switch mode & 3 {
case plan9.OREAD:
o = os.O_RDONLY
case plan9.OWRITE:
o = os.O_WRONLY
case plan9.ORDWR:
o = os.O_RDWR
case plan9.OEXEC:
o = os.O_RDONLY
default:
return Qid{}, 0, ErrPerm
}
if mode&plan9.OTRUNC != 0 {
o |= os.O_TRUNC
}
file, err := extfs.OpenFile(f.fsys, f.path, o, 0)
if err != nil {
return Qid{}, 0, err
}
var info fs.FileInfo
info, err = file.Stat()
if err != nil {
return Qid{}, 0, err
}
f.file = file
f.omode = int(mode)
if info.IsDir() {
f.isDir = true
if file, ok := file.(fs.ReadDirFile); ok {
f.dirEntries, _ = file.ReadDir(-1)
} else if fsys, ok := f.fsys.(fs.ReadDirFS); ok {
f.dirEntries, _ = fsys.ReadDir(f.path)
}
}
return FileInfoToQid(info), 0, nil
}
func (f *fidFS) Create(name string, mode uint8, perm Perm) (Qid, uint32, error) {
return Qid{}, 0, ErrNoCreate
}
func (f *fidFS) ReadAt(p []byte, off int64) (int, error) {
if f.omode == -1 {
return 0, ErrBadUseFid
}
if file, ok := f.file.(io.ReaderAt); ok && !f.isDir {
return file.ReadAt(p, off)
}
if off != f.readOffset {
return 0, ErrBadOffset
}
var n int
var err error
if f.isDir {
if f.readDirOffset == len(f.dirEntries) {
return 0, io.EOF
}
l := len(p)
for i := f.readDirOffset; i < len(f.dirEntries); i++ {
info, err := f.dirEntries[i].Info()
if err != nil {
f.readDirOffset++
continue
}
b, _ := FileInfoToDir(info).Bytes()
if n+len(b) > l {
break
}
copy(p[n:], b)
n += len(b)
f.readDirOffset++
}
} else {
n, err = f.file.Read(p)
}
f.readOffset += int64(n)
return n, err
}
func (f *fidFS) WriteAt(p []byte, off int64) (int, error) {
if f.omode == -1 {
return 0, ErrBadUseFid
}
if f.isDir {
return 0, ErrNoWrite
}
if w, ok := f.file.(io.WriterAt); ok {
return w.WriteAt(p, off)
}
w, ok := f.file.(io.Writer)
if !ok {
return 0, ErrNoWrite
}
if off != f.writeOffset {
return 0, ErrBadOffset
}
n, err := w.Write(p)
f.writeOffset += int64(n)
return n, err
}
func (f *fidFS) Clunk() error {
if f.omode == -1 {
return nil
}
f.omode = -1
return f.file.Close()
}
func (f *fidFS) Remove() error {
return ErrNoRemove
}
func (f *fidFS) Stat() (*Dir, error) {
var info fs.FileInfo
var err error
if f.file != nil {
info, err = f.file.Stat()
} else {
info, err = fs.Stat(f.fsys, f.path)
}
if err != nil {
return nil, err
}
return FileInfoToDir(info), nil
}
func (f *fidFS) WStat(*Dir) error {
return nil
}
func FileInfoToQid(info fs.FileInfo) Qid {
return Qid{
Type: uint8(Plan9FileMode(info.Mode()) >> 24),
Vers: uint32(info.ModTime().Unix()) ^ uint32(info.Size()<<8), // from u9fs
Path: rand.Uint64(),
}
}
func FileInfoToDir(info fs.FileInfo) *Dir {
name := info.Name()
if name == "." {
name = "/"
}
return &Dir{
Qid: FileInfoToQid(info),
Mode: Plan9FileMode(info.Mode()),
Atime: uint32(info.ModTime().Unix()),
Mtime: uint32(info.ModTime().Unix()),
Length: uint64(info.Size()),
Name: name,
Uid: "glenda",
Gid: "glenda",
Muid: "glenda",
}
}
var fileModes = []struct {
unix fs.FileMode
plan9 Perm
}{
{fs.ModeDir, plan9.DMDIR},
{fs.ModeAppend, plan9.DMAPPEND},
{fs.ModeExclusive, plan9.DMEXCL},
{fs.ModeTemporary, plan9.DMTMP},
{fs.ModeSymlink, plan9.DMSYMLINK},
{fs.ModeDevice, plan9.DMDEVICE},
{fs.ModeNamedPipe, plan9.DMNAMEDPIPE},
{fs.ModeSocket, plan9.DMSOCKET},
{fs.ModeSetuid, plan9.DMSETUID},
{fs.ModeSetgid, plan9.DMSETGID},
}
func Plan9FileMode(m fs.FileMode) Perm {
var p Perm
for _, fm := range fileModes {
if m&fm.unix != 0 {
p |= fm.plan9
}
}
return p | Perm(m.Perm())
}