lib9p

Go 9P library.
Log | Files | Refs | LICENSE

commit a33dc456403c6b2afcfa7383d38071da8c0252b4
parent 93dbba4dab189b6ce365bd5f19a544b6de3e97ad
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat, 23 Dec 2023 12:10:00 +0900

remove client prefix from clientFS, clientFile...

Diffstat:
Mclient/client.go | 4++--
Mclient/client_test.go | 2+-
Mclient/export_test.go | 3+--
Mclient/fid.go | 32++++++++++++++++----------------
Mclient/file.go | 16++++++++--------
Mclient/fs.go | 33++++++++-------------------------
Mserver_test.go | 1-
Mtestfs/conn.go | 2+-
8 files changed, 37 insertions(+), 56 deletions(-)

diff --git a/client/client.go b/client/client.go @@ -14,11 +14,11 @@ type Client struct { msize uint32 mSizeLock *sync.Mutex uname string - fPool *clientFidPool + fPool *fidPool txc chan<- *clientReq errc chan error cancel context.CancelFunc - rootFid *clientFid + rootFid *fid wg *sync.WaitGroup } diff --git a/client/client_test.go b/client/client_test.go @@ -56,7 +56,7 @@ func TestClient2(t *testing.T) { t.Errorf("open: %v", err) } t.Log(a0) - a := a0.(*client.ClientFile) + a := a0.(*client.File) b := make([]byte, 64) n, err := a.Read(b) if err != nil { diff --git a/client/export_test.go b/client/export_test.go @@ -1,3 +1,3 @@ package client -var AllocClientFidPool = allocClientFidPool -\ No newline at end of file +var AllocClientFidPool = allocClientFidPool diff --git a/client/fid.go b/client/fid.go @@ -7,35 +7,35 @@ import ( "git.mtkn.jp/lib9p" ) -// clientFid represents the Fid in the client side. -type clientFid struct { +// fid represents the Fid in the client side. +type fid struct { fid uint32 omode lib9p.OpenMode // -1 for not open offset uint64 - file *ClientFile + file *File } -func newClientFid(fid uint32) *clientFid { - return &clientFid{ - fid: fid, +func newClientFid(fidnum uint32) *fid { + return &fid{ + fid: fidnum, omode: -1, offset: 0, } } -type clientFidPool struct { - m map[uint32]*clientFid +type fidPool struct { + m map[uint32]*fid lock *sync.Mutex } -func allocClientFidPool() *clientFidPool { - return &clientFidPool{ - m: make(map[uint32]*clientFid), +func allocClientFidPool() *fidPool { + return &fidPool{ + m: make(map[uint32]*fid), lock: new(sync.Mutex), } } -func (pool *clientFidPool) lookup(fid uint32) (*clientFid, bool) { +func (pool *fidPool) lookup(fid uint32) (*fid, bool) { pool.lock.Lock() defer pool.lock.Unlock() @@ -43,7 +43,7 @@ func (pool *clientFidPool) lookup(fid uint32) (*clientFid, bool) { return f, ok } -func (pool *clientFidPool) nextFid() (uint32, error) { +func (pool *fidPool) nextFid() (uint32, error) { pool.lock.Lock() defer pool.lock.Unlock() for i := uint32(0); i < i+1; i++ { @@ -54,7 +54,7 @@ func (pool *clientFidPool) nextFid() (uint32, error) { return 0, fmt.Errorf("run out of fid") } -func (pool *clientFidPool) add() (*clientFid, error) { +func (pool *fidPool) add() (*fid, error) { fid, err := pool.nextFid() if err != nil { return nil, err @@ -69,14 +69,14 @@ func (pool *clientFidPool) add() (*clientFid, error) { return f, nil } -func (pool *clientFidPool) delete(fid uint32) { +func (pool *fidPool) delete(fid uint32) { pool.lock.Lock() defer pool.lock.Unlock() delete(pool.m, fid) } -func (pool *clientFidPool) String() string { +func (pool *fidPool) String() string { pool.lock.Lock() // TODO: need? defer pool.lock.Unlock() s := "{" diff --git a/client/file.go b/client/file.go @@ -9,17 +9,17 @@ import ( "git.mtkn.jp/lib9p" ) -// ClientFile is a File for Client. -type ClientFile struct { +// File is a File for Client. +type File struct { name string path string // must not contain trailing slash. - fid *clientFid + fid *fid qid lib9p.Qid iounit uint32 - fs *ClientFS + fs *FS } -func (cf *ClientFile) Stat() (fs.FileInfo, error) { +func (cf *File) Stat() (fs.FileInfo, error) { tag, err := cf.fs.tPool.add() if err != nil { return nil, err @@ -33,7 +33,7 @@ func (cf *ClientFile) Stat() (fs.FileInfo, error) { } // Don't use closed file. -func (cf *ClientFile) Close() error { +func (cf *File) Close() error { tag, err := cf.fs.tPool.add() if err != nil { return err @@ -45,7 +45,7 @@ func (cf *ClientFile) Close() error { return err } -func (cf *ClientFile) Read(b []byte) (int, error) { +func (cf *File) Read(b []byte) (int, error) { if cf.fid.omode == -1 { return 0, errors.New("not open") } @@ -89,7 +89,7 @@ func (cf *ClientFile) Read(b []byte) (int, error) { } // TODO: support n -func (cf *ClientFile) ReadDir(n int) ([]fs.DirEntry, error) { +func (cf *File) ReadDir(n int) ([]fs.DirEntry, error) { if cf.qid.Type&lib9p.QTDIR == 0 { return nil, errors.New("not a directory") } diff --git a/client/fs.go b/client/fs.go @@ -10,20 +10,20 @@ import ( "git.mtkn.jp/lib9p" ) -// ClientFS represents the file system the client imports. -type ClientFS struct { +// FS represents the file system the client imports. +type FS struct { c *Client tPool *tagPool } // Open opens the file named name in fsys. -func (fsys *ClientFS) Open(name string) (lib9p.File, error) { +func (fsys *FS) Open(name string) (lib9p.File, error) { return fsys.OpenFile(name, lib9p.OREAD) } // OpenFile opens the file named name in fsys with omode. // If the file does not exist, it create it with perm. -func (fsys *ClientFS) OpenFile(name string, omode lib9p.OpenMode) (lib9p.File, error) { +func (fsys *FS) OpenFile(name string, omode lib9p.OpenMode) (lib9p.File, error) { var ( qid lib9p.Qid iounit uint32 @@ -31,23 +31,6 @@ func (fsys *ClientFS) OpenFile(name string, omode lib9p.OpenMode) (lib9p.File, e f, err := fsys.walkFile(name) if err != nil { return nil, err - /* - // File not found. Create. - f, err = fsys.walkFile(path.Dir(name)) - if err != nil { - return nil, fmt.Errorf("walk to %s: %v", name, err) - } - tag, err := fsys.tPool.add() - if err != nil { - return nil, err - } - qid, iounit, err = fsys.c.Create(context.TODO(), tag, f.fid.fid, path.Base(name), perm, omode) - fsys.tPool.delete(tag) - if err != nil { - f.Close() - return nil, fmt.Errorf("create: %v", err) - } - */ } else { // File exists. Open it. tag, err := fsys.tPool.add() @@ -70,7 +53,7 @@ func (fsys *ClientFS) OpenFile(name string, omode lib9p.OpenMode) (lib9p.File, e // walkFile walks the file system to the named file name and // returns the corresponding file. // returned file is not open. -func (fsys *ClientFS) walkFile(name string) (*ClientFile, error) { +func (fsys *FS) walkFile(name string) (*File, error) { fid, err := fsys.c.fPool.add() if err != nil { return nil, fmt.Errorf("add fid: %v", err) @@ -100,7 +83,7 @@ func (fsys *ClientFS) walkFile(name string) (*ClientFile, error) { } else { return nil, fmt.Errorf("invalid wqid: %v", wqid) } - f := &ClientFile{ + f := &File{ name: path.Base(name), path: name, fid: fid, @@ -113,13 +96,13 @@ func (fsys *ClientFS) walkFile(name string) (*ClientFile, error) { // Mount initiates a 9P session and returns the resulting file system. // The 9P session is established by writing to w and reading from r. -func Mount(r io.Reader, w io.Writer, uname, aname string) (fs *ClientFS, err error) { +func Mount(r io.Reader, w io.Writer, uname, aname string) (fs *FS, err error) { var ( mSize uint32 = 8192 version = "9P2000" ctx = context.TODO() ) - cfs := &ClientFS{ + cfs := &FS{ c: NewClient(mSize, uname, r, w), tPool: newTagPool(), } diff --git a/server_test.go b/server_test.go @@ -11,7 +11,6 @@ import ( "git.mtkn.jp/lib9p/testfs" ) - // This function does the actual work for TestWalk(). func testWalk(t *testing.T, fs *testfs.FS, pathname string, file *testfs.File) { t.Logf("walk %s", pathname) diff --git a/testfs/conn.go b/testfs/conn.go @@ -13,7 +13,7 @@ type Conn struct { C *client.Client cr, sr *io.PipeReader cw, sw *io.PipeWriter - cancel context.CancelFunc + cancel context.CancelFunc } // SetupTestConn setups a connection between a server and a client and