lib9p

Go 9P library.
Log | Files | Refs

commit 5d264ba3da16227eda7e838c979ec5f5c7b6698f
parent d1423eba1a6eaf2a21adbeb790bac705da77133c
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Wed, 18 Oct 2023 11:40:10 +0900

update iofs

Diffstat:
Miofs/file.go | 106+++++++++++++++++++------------------------------------------------------------
Miofs/fs.go | 28+++++++++++++++++-----------
2 files changed, 42 insertions(+), 92 deletions(-)

diff --git a/iofs/file.go b/iofs/file.go @@ -14,49 +14,7 @@ type File struct { file fs.File // underlying file path string // absolute path from the root of the fs. } -/* -func openFile(fsys *FS, fpath string) (*File, error) { - file, err := fsys.fs.Open(fpath) - if err != nil { - return nil, fmt.Errorf("open fs: %v", err) - } - f := &File{ - fs: fsys, - file: file, - path: fpath, - } - return f, nil -} -*/ - -func (f *File) Fsys() lib9p.FS { return f.fs } -func (f *File) Parent() (lib9p.File, error) { - parentPath := path.Dir(f.path) - match, err := fs.Glob(f.fs.fs, parentPath) - if err != nil || len(match) != 1 { - return nil, fmt.Errorf("parent not found. err: %v", err) - } - return &File{ - fs: f.fs, - path: match[0], - }, nil -} - -func (f *File) Child() ([]lib9p.File, error) { - childnames, err := fs.Glob(f.fs.fs, f.path + "/*") - if err != nil { - return nil, err - } - children := make([]lib9p.File, len(childnames)) - for i, name := range childnames { - children[i] = &File{ - fs: f.fs, - path: name, - } - } - return children, nil -} func (f *File) Stat() (*lib9p.FileInfo, error) { fsfile, err := f.fs.fs.Open(f.path) @@ -92,54 +50,39 @@ 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 { - fsfile, err := f.fs.fs.Open(f.path) - if err != nil { - return err - } - f.file = fsfile - return nil -} - func (f *File) Close() error { - if f.file == nil { - return fmt.Errorf("not open") - } - if err := f.file.Close(); err != nil { - return err - } - f.file = nil - return nil + return f.file.Close() } func (f *File) Read(b []byte) (int, error) { - if f.file == nil { - return 0, fmt.Errorf("not open") - } return f.file.Read(b) } func (f *File) ReadAt(p []byte, off int64) (int, error) { - return f.file.Read(p) // TODO: seek?? - if reader, ok := f.file.(io.ReaderAt); ok { return reader.ReadAt(p, off) } - buf := make([]byte, off) - if _, err := io.ReadFull(f.file, buf); err != nil { - return 0, err - } - return f.file.Read(p) + return f.file.Read(p) // TODO: seek?? } + +func (f *File) ReadDir(n int) ([]*lib9p.DirEntry, error) { + dir, ok := f.file.(fs.ReadDirFile) + if !ok { + return nil, fmt.Errorf("not a directory") + } + fsde, err := dir.ReadDir(n) + if err != nil { + return nil, err + } + de := make([]*lib9p.DirEntry, len(fsde)) + for i, e := range fsde { + id := fileID(path.Join(f.path, e.Name())) + info, err := e.Info() + if err != nil { + return nil, fmt.Errorf("info: %v") + } + st := fiStat(f.fs.qidPool, id, info) + de[i] = &lib9p.FileInfo{Stat: *st} + } + return de, nil +} +\ No newline at end of file diff --git a/iofs/fs.go b/iofs/fs.go @@ -2,32 +2,38 @@ package iofs import ( "io/fs" + "errors" "lib9p" ) -/* -DiskFS is a file system opened by OpenDiskFS -*/ type FS struct { fs fs.FS - root *File qidPool *QidPool } func NewFS(fsys fs.FS) *FS { - root := &File{ - path: ".", - } fs := &FS{ fs: fsys, - root: root, qidPool: allocQidPool(), } - root.fs = fs return fs } -func (fs *FS) Root() lib9p.File { - return fs.root +func (fsys *FS) OpenFile(name string, omode lib9p.OpenMode, perm fs.FileMode) (lib9p.File, error) { + if omode != lib9p.OREAD { + return nil, errors.New("read only file system") + } + if name == "" { + name = "." + } + f, err := fsys.fs.Open(name) + if err != nil { + return nil, err + } + return &File{ + fs: fsys, + file: f, + path: name, + }, nil } \ No newline at end of file