lib9p

Go 9P library.
Log | Files | Refs

commit c1205be0a090bc1b1a06cee65dad8490a8783174
parent ca17ed8e160f470286f1d2bc1b77dd426f142f65
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Mon, 14 Aug 2023 14:40:23 +0900

change qidPool

Diffstat:
Mfid.go | 78------------------------------------------------------------------------------
Mfile.go | 19++++++++++++++++---
Mfs.go | 14++++++++++----
Apool.go | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 105 insertions(+), 85 deletions(-)

diff --git a/fid.go b/fid.go @@ -118,81 +118,3 @@ func (q *Qid) typeStr() string { } return s } - -type QidPool struct { - m map[*File]*Qid - nextQid uint64 -} - -func allocQidPool() *QidPool { - q := new(QidPool) - q.m = make(map[*File]*Qid) - return q -} - -func (pool *QidPool) lookup(f *File) (*Qid, bool) { - q, ok := pool.m[f] - return q, ok -} - -func (pool *QidPool) alloc(f *File) (*Qid, error) { - _, ok := pool.m[f] - if ok { - return nil, fmt.Errorf("qid already exist for file %v", f) - } - - s, err := f.Stat() - if err != nil { - return nil, fmt.Errorf("stat %v: %v", f, err) - } - t := fsModeToQidType(s.Mode()) - q := &Qid{t: t, path:pool.nextQid} - pool.nextQid++ - if q.path > pool.nextQid { - panic("qid overflow") - } - pool.m[f] = q - return q, nil -} - -type FidPool struct { - m map[uint32]*Fid -} - -func allocFidPool() *FidPool { - f := new(FidPool) - f.m = make(map[uint32]*Fid) - return f -} - -func (pool *FidPool) lookupFid(fid uint32) (*Fid, bool) { - f, ok := pool.m[fid] - return f, ok -} - -func (pool *FidPool) allocFid(fid uint32) (*Fid, error) { - if _, ok := pool.m[fid]; ok { - return nil, fmt.Errorf("fid already in use.") - } - f := newFid(fid) - pool.m[fid] = f - return f, nil -} - -func (pool *FidPool) removeFid(fid uint32) *Fid { - f, ok := pool.lookupFid(fid) - if !ok { - return nil - } - delete(pool.m, fid) - return f -} - -func (pool *FidPool) String() string { - s := "{" - for fnum, fstruct := range pool.m { - s += fmt.Sprintf(" [%d]%v", fnum, fstruct) - } - s += "}" - return s -} diff --git a/file.go b/file.go @@ -154,14 +154,27 @@ type File struct { path string // absolute path from the root of the fs. } -func newFile(fsys *FS, file fs.File) (*File, error) { +func newFile(fsys *FS, path string) (*File, error) { + file, err := fsys.fs.Open(path) + if err != nil { + return nil, fmt.Errorf("open fs: %v", err) + } f := &File{ fs: fsys, file: file, + path: path, } - q, err := fsys.qidPool.alloc(f) + s, err := f.file.Stat() if err != nil { - return nil, fmt.Errorf("alloc qid for file %v: %v", file, err) + return nil, fmt.Errorf("stat %v: %v", f.file, err) + } + qtype := fsModeToQidType(s.Mode()) + q, ok := fsys.qidPool.lookup(path) + if !ok { + q, err = fsys.qidPool.alloc(path, qtype) + if err != nil { + return nil, fmt.Errorf("alloc qid for file %v: %v", file, err) + } } f.qid = q return f, nil diff --git a/fs.go b/fs.go @@ -16,9 +16,10 @@ func (fsys *FS) Open(name string) (*File, error) { if err != nil { return nil, fmt.Errorf("open file %s: %v", name, err) } - file, err := newFile(fsys, f) + + file, err := newFile(fsys, name) if err != nil { - return nil, fmt.Errorf("newFile(%v): %v", file, err) + return nil, fmt.Errorf("newFile(%v, %s): %v", fsys, name, err) } file.path = name fi, err := f.Stat() @@ -38,9 +39,14 @@ func (fsys *FS) walk(root string, wnames []string) ([]*Qid, error) { if err != nil { return wqids, err } - qid, ok := fsys.qidPool.lookup(f) + qid, ok := fsys.qidPool.lookup(f.path) if !ok { - qid, err = fsys.qidPool.alloc(f) + s, err := f.file.Stat() + if err != nil { + return wqids, err + } + qtype := fsModeToQidType(s.Mode()) + qid, err = fsys.qidPool.alloc(f.path, qtype) if err != nil { return wqids, err } diff --git a/pool.go b/pool.go @@ -0,0 +1,79 @@ +package lib9p + +import ( + "fmt" +) + +type QidPool struct { + m map[string]*Qid + nextQid uint64 +} + +func allocQidPool() *QidPool { + q := new(QidPool) + q.m = make(map[string]*Qid) + return q +} + +func (pool *QidPool) lookup(path string) (*Qid, bool) { + q, ok := pool.m[path] + return q, ok +} + +func (pool *QidPool) alloc(path string, t QidType) (*Qid, error) { + _, ok := pool.m[path] + if ok { + return nil, fmt.Errorf("qid already exist for path %s", path) + } + + q := &Qid{t: t, path:pool.nextQid} + pool.nextQid++ + if q.path > pool.nextQid { + panic("qid overflow") + } + pool.m[path] = q + return q, nil +} + + +type FidPool struct { + m map[uint32]*Fid +} + +func allocFidPool() *FidPool { + f := new(FidPool) + f.m = make(map[uint32]*Fid) + return f +} + +func (pool *FidPool) lookupFid(fid uint32) (*Fid, bool) { + f, ok := pool.m[fid] + return f, ok +} + +func (pool *FidPool) allocFid(fid uint32) (*Fid, error) { + if _, ok := pool.m[fid]; ok { + return nil, fmt.Errorf("fid already in use.") + } + f := newFid(fid) + pool.m[fid] = f + return f, nil +} + +func (pool *FidPool) removeFid(fid uint32) *Fid { + f, ok := pool.lookupFid(fid) + if !ok { + return nil + } + delete(pool.m, fid) + return f +} + +func (pool *FidPool) String() string { + s := "{" + for fnum, fstruct := range pool.m { + s += fmt.Sprintf(" [%d]%v", fnum, fstruct) + } + s += "}" + return s +}