lib9p

Go 9P library.
Log | Files | Refs

commit 6653f26fb9ecafaf1e347b8addf2e7e8992e4b11
parent c634ec5a305ab7c75b97812255c0b44802c4bc45
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Tue, 17 Oct 2023 06:38:27 +0900

update for new API

Diffstat:
Mdiskfs/diskfs.go | 33++++++++++++++++++++++++++++++---
Mfid.go | 1+
Mfile.go | 9+++++++--
Mfs.go | 2+-
Mserver.go | 41++++++++++++++++++++++++++++++-----------
5 files changed, 69 insertions(+), 17 deletions(-)

diff --git a/diskfs/diskfs.go b/diskfs/diskfs.go @@ -1,7 +1,10 @@ package diskfs import ( + "fmt" + "io/fs" "os" + "path/filepath" "lib9p" ) @@ -11,7 +14,7 @@ DiskFS is a file system opened by OpenDiskFS */ type FS struct { rootPath string - root *File +// root *File qidPool *QidPool } @@ -27,14 +30,38 @@ func Open(name string) (*FS, error) { path: ".", } fsys := &FS{ - root: root, +// root: root, rootPath: name, qidPool: allocQidPool(), } root.fs = fsys return fsys, nil } - +/* func (fsys *FS) Root() lib9p.File { return fsys.root } +*/ +func (fsys *FS) Open(name string, omode lib9p.OpenMode, perm fs.FileMode) (lib9p.File, error) { + fp := filepath.Join(fsys.rootPath, name) + var m int + switch omode&3 { + case lib9p.OREAD: + m = os.O_RDONLY + case lib9p.OWRITE: + m = os.O_WRONLY + case lib9p.ORDWR: + m = os.O_RDWR + } + if omode&lib9p.OTRUNC != 0 { + m |= os.O_TRUNC + } + if omode&lib9p.ORCLOSE != 0 { + return nil, fmt.Errorf("orclose not implemented") + } + osf, err := os.OpenFile(fp, m, perm) + if err != nil { + return nil, err + } + return &File{fs: fsys, path: name, file: osf}, nil +} diff --git a/fid.go b/fid.go @@ -21,6 +21,7 @@ const ( type Fid struct { Fid uint32 OMode OpenMode /* -1 = not open */ + path string File File Uid string dirOffset uint64 diff --git a/file.go b/file.go @@ -14,7 +14,7 @@ type File interface { Stat() (*FileInfo, error) Qid() Qid - Open(mode OpenMode) error +// Open(mode OpenMode) error Close() error io.Reader } @@ -48,6 +48,11 @@ type RemoverFile interface { Remove() error } +type ReadDirFile interface { + File + ReadDir(n int) ([]DirEntry, error) +} +/* // Walkfile1 walks file tree one step down from f to the child specified by name. // It returns the destination File. // name must not "..". @@ -85,7 +90,7 @@ func walkfile(f File, pathname string) (File, error) { } } return cwd, nil -} +}*/ type ClientFile struct { name string diff --git a/fs.go b/fs.go @@ -5,7 +5,7 @@ import ( ) type FS interface { - Root() File + Open(name string, omode OpenMode, perm fs.FileMode) (File, error) } func FSModeToQidType(fm fs.FileMode) QidType { diff --git a/server.go b/server.go @@ -6,6 +6,7 @@ import ( "io/fs" "log" "os" + "path" "strings" "sync" ) @@ -232,7 +233,11 @@ func sAttach(s *Server, r *Req) { } } - fid.File = s.fs.Root() + fid.File, err = s.fs.Open(".", OREAD, 0) + if err != nil { + respond(r, fmt.Errorf("open root: %v", err)) + return + } fid.Uid = ifcall.UName() fid.OMode = -1 // TODO: right? @@ -295,25 +300,28 @@ func sWalk(s *Server, r *Req) { } wqids := make([]Qid, ifcall.NWName()) - cwd := oldFid.File + cwdp := oldFid.path + cwdf := oldFid.File + var err error var n int for i, name := range ifcall.WName() { - child, err := walkfile1(cwd, name) + cwdp = path.Join(cwdp, name) + cwdf, err = s.fs.Open(cwdp, OREAD, 0) if err != nil { - log.Printf("walkfile: %v", err) + log.Printf("open: %v", err) break } - stat, err := child.Stat() + stat, err := cwdf.Stat() if err != nil { log.Printf("stat: %v", err) break } wqids[i] = stat.Qid() - cwd = child n++ } - newFid.File = cwd + newFid.File = cwdf newFid.Uid = oldFid.Uid + newFid.path = cwdp r.ofcall = &RWalk{ qid: wqids[:n], @@ -408,9 +416,12 @@ func rOpen(r *Req, err error) { return } r.fid.OMode = r.ifcall.(*TOpen).Mode() - if err := r.fid.File.Open(r.fid.OMode); err != nil { + f, err := r.srv.fs.Open(r.fid.path, r.fid.OMode, 0) + if err != nil { setError(r, err) + return } + r.fid.File = f } func sCreate(s *Server, r *Req) { @@ -471,17 +482,21 @@ func sCreate(s *Server, r *Req) { } func rCreate(r *Req, err error) { if err != nil { + setError(r, err) return } r.fid.OMode = r.ifcall.(*TCreate).Mode() - if err := r.fid.File.Open(r.fid.OMode); err != nil { + // TODO: pass OCREATE with non 0 perm. + f, err := r.srv.fs.Open(r.fid.path, r.fid.OMode, 0) + if err != nil { setError(r, err) + return } + r.fid.File = f } func sRead(s *Server, r *Req) { ifcall := r.ifcall.(*TRead) - var ok bool r.fid, ok = s.fPool.lookup(ifcall.Fid()) if !ok { @@ -667,7 +682,11 @@ func sRemove(s *Server, r *Req) { r.ofcall = &RRemove{} respond(r, nil) } -func rRemove(r *Req, err error) {} +func rRemove(r *Req, err error) { + if err != nil { + setError(r, err) + } +} func sStat(s *Server, r *Req) { ifcall := r.ifcall.(*TStat)