lib9p

Go 9P library.
Log | Files | Refs

commit 04eead3819c668c6a7c542fa2808f2c4492936dd
parent 9b7c9946406552f669dd92631ae97ca49ae8097c
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Tue, 10 Oct 2023 17:12:34 +0900

change API

Diffstat:
Mclient.go | 66+++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Mclient_test.go | 37+++++++++++++++++++------------------
2 files changed, 74 insertions(+), 29 deletions(-)

diff --git a/client.go b/client.go @@ -173,12 +173,20 @@ func (c *Client) transact(ctx context.Context, tmsg Msg) (Msg, error) { } } -func (c *Client) Version(ctx context.Context, mSize uint32, version string) (Msg, error) { - tmsg := &TVersion{ - mSize: mSize, - version: version, +func (c *Client) Version(ctx context.Context, mSize uint32, version string) (uint32, string, error) { + tmsg := &TVersion{mSize: mSize, version: version} + rmsg, err := c.transact(ctx, tmsg) + if err != nil { + return 0, "", err + } + switch rmsg := rmsg.(type) { + case *RVersion: + return rmsg.mSize, rmsg.version, nil + case *RError: + return 0, "", rmsg.ename + default: + return 0, "", fmt.Errorf("invalid reply: %v", rmsg) } - return c.transact(ctx, tmsg) } func (c *Client) Auth(ctx context.Context, afid uint32, uname, aname string) (Msg, error) { @@ -187,11 +195,47 @@ func (c *Client) Auth(ctx context.Context, afid uint32, uname, aname string) (Ms } func (c *Client) Attach(ctx context.Context, fid, afid uint32, uname, aname string) (Msg, error) { - tmsg := &TAttach{ - fid: fid, - afid: afid, - uname: uname, - aname: aname, - } + tmsg := &TAttach{fid: fid, afid: afid, uname: uname, aname: aname} + return c.transact(ctx, tmsg) +} + +func (c *Client) Flush(ctx context.Context, oldtag uint16) (Msg, error) { + tmsg := &TFlush{oldtag: oldtag} + return c.transact(ctx, tmsg) +} +func (c *Client) Walk(ctx context.Context, fid, newFid uint32, wname []string) (Msg, error) { + tmsg := &TWalk{fid: fid, newFid: newFid, wname: wname} + return c.transact(ctx, tmsg) +} +func (c *Client) Open(ctx context.Context, fid uint32, mode OpenMode) (Msg, error) { + tmsg := &TOpen{fid: fid, mode: mode} + return c.transact(ctx, tmsg) +} +func (c *Client) Create(ctx context.Context, fid uint32, name string, perm FileMode, mode OpenMode) (Msg, error) { + tmsg := &TCreate{fid: fid, name: name, perm: perm, mode: mode} + return c.transact(ctx, tmsg) +} +func (c *Client) Read(ctx context.Context, fid uint32, offset uint64, count uint32) (Msg, error) { + tmsg := &TRead{fid: fid, offset: offset, count: count} + return c.transact(ctx, tmsg) +} +func (c *Client) Write(ctx context.Context, fid uint32, offset uint64, count uint32, data []byte) (Msg, error) { + tmsg := &TWrite{fid: fid, offset: offset, count: count, data: data} + return c.transact(ctx, tmsg) +} +func (c *Client) Clunk(ctx context.Context, fid uint32) (Msg, error) { + tmsg := &TClunk{fid: fid} + return c.transact(ctx, tmsg) +} +func (c *Client) Remove(ctx context.Context, fid uint32) (Msg, error) { + tmsg := &TRemove{fid: fid} + return c.transact(ctx, tmsg) +} +func (c *Client) Stat(ctx context.Context, fid uint32) (Msg, error) { + tmsg := &TStat{fid: fid} + return c.transact(ctx, tmsg) +} +func (c *Client) Wstat(ctx context.Context, fid uint32, stat *Stat) (Msg, error) { + tmsg := &TWStat{fid: fid, stat: stat} return c.transact(ctx, tmsg) } diff --git a/client_test.go b/client_test.go @@ -31,16 +31,18 @@ func newClientForTest(msize uint32, uname string, tmsgc chan<- Msg, rmsgc <-chan func TestClientVersion(t *testing.T) { tests := []struct { name string - tmsg *TVersion + mSize uint32 + version string rmsg Msg - wantrmsg Msg + wantmsize uint32 + wantversion string }{ - {"0", &TVersion{tag: NOTAG, mSize: mSize, version: "9P2000"}, + {"0", mSize, "9P2000", &RVersion{tag: NOTAG, mSize: mSize, version: "9P2000"}, - &RVersion{tag: NOTAG, mSize: mSize, version: "9P2000"}}, - {"1", &TVersion{tag: NOTAG, mSize: mSize, version: "unko"}, + mSize, "9P2000"}, + {"1", mSize, "unko", &RVersion{tag: NOTAG, mSize: mSize, version: "unknown"}, - &RVersion{tag: NOTAG, mSize: mSize, version: "unknown"}}, + mSize, "unknown"}, } for _, test := range tests { func() { @@ -50,23 +52,19 @@ func TestClientVersion(t *testing.T) { defer c.Stop() bg := context.Background() var ( - gotrmsg Msg + gotmsize uint32 + gotversion string goterr error wg sync.WaitGroup ) wg.Add(1) go func() { - gotrmsg, goterr = c.Version(bg, test.tmsg.mSize, test.tmsg.version) + gotmsize, gotversion, goterr = c.Version(bg, test.mSize, test.version) wg.Done() }() gottmsg := <-tmsgc tag := gottmsg.Tag() - test.tmsg.SetTag(tag) test.rmsg.SetTag(tag) - test.wantrmsg.SetTag(tag) - if !reflect.DeepEqual(gottmsg, test.tmsg) { - t.Errorf("%s: input: %v, sent: %v", test.name, test.tmsg, gottmsg) - } rmsgc <- test.rmsg done := make(chan struct{}) go func() { wg.Wait(); done <- struct{}{} }() @@ -76,11 +74,14 @@ func TestClientVersion(t *testing.T) { return case <-done: } - if !reflect.DeepEqual(gotrmsg, test.wantrmsg) { - t.Errorf("%s: want: %v, got: %v", test.name, test.wantrmsg, gotrmsg) - } if goterr != nil { t.Errorf("%s: goterr: %v", test.name, goterr) + return + } + if test.wantmsize != gotmsize || test.wantversion != gotversion { + t.Errorf("%s: (mSize, verion) want: %d, %s, got: %d, %s", + test.name, test.wantmsize, test.wantversion, + gotmsize, gotversion) } }() } @@ -209,11 +210,11 @@ func TestTransaction(t *testing.T) { bg := context.Background() go server.Serve() - rversion, err := client.Version(bg, mSize, "9P2000") + rmsize, rversion, err := client.Version(bg, mSize, "9P2000") if err != nil { t.Log(err) } else { - t.Log(rversion) + t.Log(&RVersion{mSize: rmsize, version: rversion}) } rauth, err := client.Auth(bg, 0, "kenji", "") if err != nil {