lib9p

Go 9P library.
Log | Files | Refs

commit a01ba18d836802a85e2fd0f2a1043a3d27456237
parent 6683608a29ccd396f5d916c6d34a86d7e7d16cbe
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Wed, 18 Oct 2023 10:07:58 +0900

rename Open to OpenFile

Diffstat:
Mdiskfs/diskfs.go | 10++--------
Mdiskfs/file.go | 71++---------------------------------------------------------------------
Mfile.go | 4++--
Mfs.go | 2+-
Mserver.go | 14+++++++-------
Mtest_fs.go | 2+-
6 files changed, 15 insertions(+), 88 deletions(-)

diff --git a/diskfs/diskfs.go b/diskfs/diskfs.go @@ -14,7 +14,6 @@ DiskFS is a file system opened by OpenDiskFS */ type FS struct { rootPath string -// root *File qidPool *QidPool } @@ -30,19 +29,14 @@ func Open(name string) (*FS, error) { path: ".", } fsys := &FS{ -// 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) { + +func (fsys *FS) OpenFile(name string, omode lib9p.OpenMode, perm fs.FileMode) (lib9p.File, error) { fp := filepath.Join(fsys.rootPath, name) var m int switch omode&3 { diff --git a/diskfs/file.go b/diskfs/file.go @@ -59,44 +59,6 @@ func (f *File) Stat() (*lib9p.FileInfo, error) { return &lib9p.FileInfo{stat}, nil } -func (f *File) Qid() lib9p.Qid { - qid, ok := f.fs.qidPool.lookup(f) - if !ok { - var err error - qid, err = f.fs.qidPool.alloc(f) - if err != nil { - panic(fmt.Errorf("alloc qid: %v", err)) - } - } - return qid -} - -func (f *File) Open(mode lib9p.OpenMode) error { - var flag int - switch mode&3 { - case lib9p.OREAD, lib9p.OEXEC: - flag = os.O_RDONLY - case lib9p.OWRITE: - flag = os.O_WRONLY - case lib9p.ORDWR: - flag = os.O_RDWR - } - if mode&lib9p.OTRUNC != 0 { - flag |= os.O_TRUNC - } - if mode&lib9p.ORCLOSE != 0 { - return fmt.Errorf("not implemented") - } - - ospath := filepath.Join(f.fs.rootPath, f.path) - file, err := os.OpenFile(ospath, flag, 0) - if err != nil { - return fmt.Errorf("open: %v", err) - } - f.file = file - return nil -} - func (f *File) Create(name string, uid string, mode lib9p.OpenMode, perm lib9p.FileMode) (lib9p.File, error) { dirinfo, err := f.Stat() @@ -144,8 +106,8 @@ func (f *File) Create(name string, uid string, return nil, fmt.Errorf("set file mode: %v", err) } - file := &File{path: filepath.Join(f.path, name), fs: f.fs} - if err := file.Open(mode); err != nil { + file, err := f.fs.OpenFile(filepath.Join(f.path,name) , mode, 0) + if err != nil { return nil, fmt.Errorf("open new file: %v", err) } return file, nil @@ -188,35 +150,6 @@ func (f *File) ReadDir(n int) ([]*lib9p.DirEntry, error) { return de, nil } -func (f *File) Parent() (lib9p.File, error) { - parentPath := filepath.Dir(f.path) - parent, err := os.Open(filepath.Join(f.fs.rootPath, parentPath)) - if err != nil { - return nil, fmt.Errorf("open parent: %v", err) - } - parent.Close() - return &File{path: parentPath, fs: f.fs}, nil -} - -func (f *File) Child() ([]lib9p.File, error) { - ospath := filepath.Join(f.fs.rootPath, f.path) - osde, err := os.ReadDir(ospath) - if err != nil { - return nil, fmt.Errorf("readdir: %v", err) - } - - files := make([]lib9p.File, 0, len(osde)) - for _, de := range osde { - cpath := filepath.Join(f.path, de.Name()) - child := &File{ - path: cpath, - fs: f.fs, - } - files = append(files, child) - } - return files, nil -} - func (f *File) ReadAt(p []byte, off int64) (int, error) { if f.file == nil { return 0, fmt.Errorf("not open") diff --git a/file.go b/file.go @@ -48,7 +48,7 @@ type ReadDirFile interface { } func getChildren(fsys FS, dirpath string) ([]File, error) { - dir0, err := fsys.Open(dirpath, OREAD, 0) + dir0, err := fsys.OpenFile(dirpath, OREAD, 0) if err != nil { return nil, fmt.Errorf("open dir: %v", err) } @@ -63,7 +63,7 @@ func getChildren(fsys FS, dirpath string) ([]File, error) { } children := make([]File, len(des)) for i, de := range des { - f, err := fsys.Open(path.Join(dirpath, de.Name()), OREAD, 0) + f, err := fsys.OpenFile(path.Join(dirpath, de.Name()), OREAD, 0) if err != nil { for j := 0; j < i; j++ { children[j].Close() diff --git a/fs.go b/fs.go @@ -5,7 +5,7 @@ import ( ) type FS interface { - Open(name string, omode OpenMode, perm fs.FileMode) (File, error) + OpenFile(name string, omode OpenMode, perm fs.FileMode) (File, error) } func FSModeToQidType(fm fs.FileMode) QidType { diff --git a/server.go b/server.go @@ -233,7 +233,7 @@ func sAttach(s *Server, r *Req) { } } - fid.File, err = s.fs.Open(".", OREAD, 0) + fid.File, err = s.fs.OpenFile(".", OREAD, 0) if err != nil { respond(r, fmt.Errorf("open root: %v", err)) return @@ -315,7 +315,7 @@ func sWalk(s *Server, r *Req) { var n int for i, name := range ifcall.WName() { cwdp = path.Join(cwdp, name) - cwdf, err = s.fs.Open(cwdp, OREAD, 0) + cwdf, err = s.fs.OpenFile(cwdp, OREAD, 0) if err != nil { log.Printf("open: %v", err) break @@ -408,7 +408,7 @@ func sOpen(s *Server, r *Req) { if ifcall.Mode()&ORCLOSE != 0 { parentPath := path.Dir(r.fid.path) - parent, err := s.fs.Open(parentPath, OREAD, 0) + parent, err := s.fs.OpenFile(parentPath, OREAD, 0) defer parent.Close() if err != nil { respond(r, fmt.Errorf("open parent")) @@ -432,7 +432,7 @@ func rOpen(r *Req, err error) { return } r.fid.OMode = r.ifcall.(*TOpen).Mode() - f, err := r.srv.fs.Open(r.fid.path, r.fid.OMode, 0) + f, err := r.srv.fs.OpenFile(r.fid.path, r.fid.OMode, 0) if err != nil { setError(r, err) return @@ -510,7 +510,7 @@ func rCreate(r *Req, err error) { } r.fid.OMode = r.ifcall.(*TCreate).Mode() // TODO: pass OCREATE with non 0 perm. - f, err := r.srv.fs.Open(r.fid.path, r.fid.OMode, 0) + f, err := r.srv.fs.OpenFile(r.fid.path, r.fid.OMode, 0) if err != nil { setError(r, err) return @@ -685,7 +685,7 @@ func sRemove(s *Server, r *Req) { defer s.fPool.delete(ifcall.Fid()) parentPath := path.Dir(r.fid.path) - parent, err := s.fs.Open(parentPath, OREAD, 0) + parent, err := s.fs.OpenFile(parentPath, OREAD, 0) if err != nil { respond(r, fmt.Errorf("open parent: %v", err)) return @@ -772,7 +772,7 @@ func sWStat(s *Server, r *Req) { if wstat.Name != "" { parentPath := path.Dir(r.fid.path) - parent, err := s.fs.Open(parentPath, OREAD, 0) + parent, err := s.fs.OpenFile(parentPath, OREAD, 0) if err != nil { respond(r, fmt.Errorf("get parent: %v", err)) return diff --git a/test_fs.go b/test_fs.go @@ -81,7 +81,7 @@ func (fs *testFS) Root() File { return fs.root } -func (fs *testFS) Open(path string, omode OpenMode, perm fs.FileMode) (File, error) { +func (fs *testFS) OpenFile(path string, omode OpenMode, perm fs.FileMode) (File, error) { path = clean9path(path) wnames := split9path(path) f, err := fs.walk(wnames)