commit c52d24ecf6e7b645710d7611cf362cbe26a7fbed
parent 05c1ad643eedac4420ea75434a53eaed099560d4
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sun, 20 Aug 2023 12:39:17 +0900
change QidPool
Diffstat:
4 files changed, 33 insertions(+), 41 deletions(-)
diff --git a/file.go b/file.go
@@ -201,9 +201,9 @@ func openFile(fsys *FS, fpath string) (*File, error) {
}
qtype := fsModeToQidType(fsfi.Mode())
- q, ok := fsys.qidPool.lookup(fpath)
+ q, ok := fsys.qidPool.lookup(fsfi)
if !ok {
- q, err = fsys.qidPool.alloc(fpath, qtype)
+ q, err = fsys.qidPool.alloc(fsfi, qtype)
if err != nil {
return nil, fmt.Errorf("alloc qid for file %v: %v", file, err)
}
diff --git a/fs.go b/fs.go
@@ -28,14 +28,19 @@ func (fsys *FS) walk(root string, wnames []string) ([]*Qid, error) {
if err != nil {
return wqids, err
}
- qid, ok := fsys.qidPool.lookup(f.path) // TODO: use os.SameFile
+ fi, err := f.Stat()
+ if err != nil {
+ return wqids, fmt.Errorf("stat file %v: %v", f, err)
+ }
+
+ qid, ok := fsys.qidPool.lookup(fi.info) // TODO: use os.SameFile
if !ok {
s, err := f.file.Stat()
if err != nil {
return wqids, err
}
qtype := fsModeToQidType(s.Mode())
- qid, err = fsys.qidPool.alloc(f.path, qtype)
+ qid, err = fsys.qidPool.alloc(s, qtype)
if err != nil {
return wqids, err
}
diff --git a/pool.go b/pool.go
@@ -2,28 +2,42 @@ package lib9p
import (
"fmt"
+ "io/fs"
+ "os"
)
type QidPool struct {
- m map[string]*Qid
+ m map[fs.FileInfo]*Qid
nextQid uint64
}
func allocQidPool() *QidPool {
q := new(QidPool)
- q.m = make(map[string]*Qid)
+ q.m = make(map[fs.FileInfo]*Qid)
return q
}
-func (pool *QidPool) lookup(path string) (*Qid, bool) {
- q, ok := pool.m[path]
- return q, ok
+func (pool *QidPool) lookup(fi fs.FileInfo) (*Qid, bool) {
+ for fi1, qid := range pool.m {
+ if os.SameFile(fi, fi1) {
+ return qid, true
+ }
+ }
+ return nil, false
}
-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)
+func (pool *QidPool) delete(fi fs.FileInfo) {
+ for fi1, _ := range pool.m {
+ if os.SameFile(fi, fi1) {
+ delete(pool.m, fi1)
+ }
+ }
+}
+
+
+func (pool *QidPool) alloc(fi fs.FileInfo, t QidType) (*Qid, error) {
+ if _, ok := pool.lookup(fi); ok {
+ return nil, fmt.Errorf("qid already exist for file %s", fi.Name())
}
q := &Qid{t: t, path:pool.nextQid}
@@ -31,7 +45,7 @@ func (pool *QidPool) alloc(path string, t QidType) (*Qid, error) {
if q.path > pool.nextQid {
panic("qid overflow")
}
- pool.m[path] = q
+ pool.m[fi] = q
return q, nil
}
diff --git a/qid.go b/qid.go
@@ -2,7 +2,6 @@ package lib9p
import (
"fmt"
- "os"
)
// QidType represents the type of Qid. the 'QT' prefix may be redundant.
@@ -68,30 +67,4 @@ func (q *Qid) typeStr() string {
return s
}
-type FileQidMap map[*FileInfo]*Qid
-
-func (fqmap FileQidMap) lookup(fi *FileInfo) (*Qid, bool) {
- for fi1, qid := range fqmap {
- if os.SameFile(fi, fi1) {
- return qid, true
- }
- }
- return nil, false
-}
-
-func (fqmap FileQidMap) add(fi *FileInfo, qid *Qid) error {
- if _, ok := fqmap.lookup(fi); ok {
- return fmt.Errorf("qid already exist for file %s", fi.Name())
- }
- fqmap[fi] = qid
- return nil
-}
-
-func (fqmap FileQidMap) delete(fi *FileInfo) {
- for fi1, _ := range fqmap {
- if os.SameFile(fi, fi1) {
- delete(fqmap, fi1)
- }
- }
-}