commit 05c1ad643eedac4420ea75434a53eaed099560d4
parent da3122002853a361cce4960b1b267ccc2df8509a
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sun, 20 Aug 2023 11:52:43 +0900
add FileQidMap
Diffstat:
| M | fid.go | | | 81 | ++----------------------------------------------------------------------------- |
| M | file.go | | | 2 | +- |
| A | qid.go | | | 97 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 100 insertions(+), 80 deletions(-)
diff --git a/fid.go b/fid.go
@@ -2,21 +2,6 @@ package lib9p
import (
"fmt"
- "io/fs"
-)
-
-// QidType represents the type of Qid. the 'QT' prefix may be redundant.
-type QidType uint8
-
-const (
- QTDIR QidType = 0x80 /* type bit for directories */
- QTAPPEND = 0x40 /* type bit for append only files */
- QTEXCL = 0x20 /* type bit for exclusive use files */
- QTMOUNT = 0x10 /* type bit for mounted channel */
- QTAUTH = 0x08 /* type bit for authentication file */
- QTTMP = 0x04 /* type bit for non-backed-up file */
- QTSYMLINK = 0x02 /* type bit for symbolic link */
- QTFILE = 0x00 /* type bits for plain file */
)
type OpenMode int32 // defined by 9P
@@ -31,20 +16,6 @@ const ( // TODO: is the mode should be implemented using interfaces?
ORCLOSE = 64
)
-type ReadFile interface { fs.File }
-type WriteFile interface {
- fs.File
- Write([]byte) (int, error)
-}
-type ReadWriteFile interface {
- ReadFile
- WriteFile
-}
-type ExecuteFile interface {
- fs.File
- Execute(args... any) (any, error)
-}
-
type Fid struct {
Fid uint32
OMode OpenMode /* -1 = not open */
@@ -68,53 +39,4 @@ func (f *Fid) String() string {
fid = -1
}
return fmt.Sprintf("%d", fid)
-}
-
-type Qid struct {
- t QidType
- vers uint32
- path uint64
-}
-
-func newQid(b []byte) *Qid {
- return &Qid{
- t: QidType(b[0]),
- vers: gbit32(b[1:5]),
- path: gbit64(b[5:13]),
- }
-}
-func (q *Qid) Type() QidType { return q.t }
-func (q *Qid) SetType(t QidType) { q.t = t }
-func (q *Qid) Vers() uint32 { return q.vers }
-func (q *Qid) SetVers(v uint32) { q.vers = v }
-func (q *Qid) Path() uint64 { return q.path }
-func (q *Qid) SetPath(p uint64) { q.path = p }
-func (q *Qid) marshal() []byte {
- buf := make([]byte, 13)
- buf[0] = uint8(q.t)
- pbit32(buf[1:5], q.vers)
- pbit64(buf[5:13], q.path)
- return buf
-}
-func (q *Qid) String() string {
- return fmt.Sprintf("(%016d %d %s)", q.Path(), q.Vers(), q.typeStr())
-}
-
-// TypeStr returns the q.Type() as string for debugging information.
-func (q *Qid) typeStr() string {
- var s string
- t := q.Type()
- if t&QTDIR != 0 {
- s += "d"
- }
- if t&QTAPPEND != 0 {
- s += "a"
- }
- if t&QTEXCL != 0 {
- s += "l"
- }
- if t&QTAUTH != 0 {
- s += "A"
- }
- return s
-}
+}
+\ No newline at end of file
diff --git a/file.go b/file.go
@@ -162,7 +162,7 @@ type File struct {
Qid Qid // unique id from server
// The following data should be derived from fs.Stat()
- // every time.
+ // every time Stat() is called.
/*
Mode FileMode // permissions
Atime uint32 // last read time
diff --git a/qid.go b/qid.go
@@ -0,0 +1,97 @@
+package lib9p
+
+import (
+ "fmt"
+ "os"
+)
+
+// QidType represents the type of Qid. the 'QT' prefix may be redundant.
+type QidType uint8
+
+const (
+ QTDIR QidType = 0x80 /* type bit for directories */
+ QTAPPEND = 0x40 /* type bit for append only files */
+ QTEXCL = 0x20 /* type bit for exclusive use files */
+ QTMOUNT = 0x10 /* type bit for mounted channel */
+ QTAUTH = 0x08 /* type bit for authentication file */
+ QTTMP = 0x04 /* type bit for non-backed-up file */
+ QTSYMLINK = 0x02 /* type bit for symbolic link */
+ QTFILE = 0x00 /* type bits for plain file */
+)
+
+type Qid struct {
+ t QidType
+ vers uint32
+ path uint64
+}
+
+func newQid(b []byte) *Qid {
+ return &Qid{
+ t: QidType(b[0]),
+ vers: gbit32(b[1:5]),
+ path: gbit64(b[5:13]),
+ }
+}
+func (q *Qid) Type() QidType { return q.t }
+func (q *Qid) SetType(t QidType) { q.t = t }
+func (q *Qid) Vers() uint32 { return q.vers }
+func (q *Qid) SetVers(v uint32) { q.vers = v }
+func (q *Qid) Path() uint64 { return q.path }
+func (q *Qid) SetPath(p uint64) { q.path = p }
+func (q *Qid) marshal() []byte {
+ buf := make([]byte, 13)
+ buf[0] = uint8(q.t)
+ pbit32(buf[1:5], q.vers)
+ pbit64(buf[5:13], q.path)
+ return buf
+}
+func (q *Qid) String() string {
+ return fmt.Sprintf("(%016d %d %s)", q.Path(), q.Vers(), q.typeStr())
+}
+
+// TypeStr returns the q.Type() as string for debugging information.
+func (q *Qid) typeStr() string {
+ var s string
+ t := q.Type()
+ if t&QTDIR != 0 {
+ s += "d"
+ }
+ if t&QTAPPEND != 0 {
+ s += "a"
+ }
+ if t&QTEXCL != 0 {
+ s += "l"
+ }
+ if t&QTAUTH != 0 {
+ s += "A"
+ }
+ 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)
+ }
+ }
+}
+