lib9p

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

commit 0ea91cffacb137796fdb4a10124134204c6f3693
parent ca96a37982b0074c21557b3c75440f13f2a38123
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri, 29 Dec 2023 08:56:06 +0900

unexport Req.Ifcall

Diffstat:
Mauth_test.go | 2+-
Mexport_test.go | 3+++
Mreq.go | 2+-
Mserver.go | 40++++++++++++++++++++--------------------
Mserver2_test.go | 24++++++++++++------------
Mserver_test.go | 38++++++++++++++++++++++----------------
6 files changed, 59 insertions(+), 50 deletions(-)

diff --git a/auth_test.go b/auth_test.go @@ -19,7 +19,7 @@ func TestAuth(t *testing.T) { acw.Close() }() conn.S.Auth = func(ctx context.Context, r *lib9p.Req) { - ifcall := r.Ifcall.(*lib9p.TAuth) + ifcall := r.Ifcall().(*lib9p.TAuth) aqid := lib9p.Qid{Type: lib9p.QTAUTH, Vers: 0, Path: ^uint64(0)} r.Afid.File = &lib9p.AuthFile{ Qid: aqid, diff --git a/export_test.go b/export_test.go @@ -57,6 +57,9 @@ func (s *Server) SetFPool(fp *FidPool) { s.fPool = fp } func (s *Server) NewMSizeLock() { s.mSizeLock = new(sync.Mutex) } func (s *Server) SetMSize(size uint32) { s.setMSize(size) } +func (r *Req) Ifcall() Msg { return r.ifcall } +func (r *Req) SetIfcall(m Msg) { r.ifcall = m } + func (rp *ReqPool) Add(tag uint16) (*Req, error) { return rp.add(tag) } func (fid *Fid) SetPath(path string) { fid.path = path } diff --git a/req.go b/req.go @@ -8,7 +8,7 @@ import ( type Req struct { tag uint16 // Ifcall is incomming 9P message - Ifcall Msg + ifcall Msg // Ofcall is response message to Ifcall. Ofcall Msg Fid *Fid diff --git a/server.go b/server.go @@ -171,22 +171,22 @@ func getReq(r io.Reader, rp *ReqPool, chatty bool) *Req { if err != nil { // duplicate tag: cons up a fake Req req := new(Req) - req.Ifcall = ifcall + req.ifcall = ifcall req.listenErr = ErrDupTag req.done = make(chan struct{}) if chatty { - fmt.Fprintf(os.Stderr, "<-- %v\n", req.Ifcall) + fmt.Fprintf(os.Stderr, "<-- %v\n", req.ifcall) } return req } req.tag = ifcall.GetTag() - req.Ifcall = ifcall + req.ifcall = ifcall req.done = make(chan struct{}) - if ifcall, ok := req.Ifcall.(*TFlush); ok { + if ifcall, ok := req.ifcall.(*TFlush); ok { req.Oldreq, _ = rp.lookup(ifcall.Oldtag) } if chatty { - fmt.Fprintf(os.Stderr, "<-- %v\n", req.Ifcall) + fmt.Fprintf(os.Stderr, "<-- %v\n", req.ifcall) } return req } @@ -208,7 +208,7 @@ func sVersion(ctx context.Context, s *Server, c <-chan *Req) { if !ok { return } - ifcall := r.Ifcall.(*TVersion) + ifcall := r.ifcall.(*TVersion) version := ifcall.Version if ok := strings.HasPrefix(version, "9P2000"); !ok { version = "unknown" @@ -252,7 +252,7 @@ func sAuth(ctx context.Context, s *Server, c <-chan *Req) { } continue } - ifcall := r.Ifcall.(*TAuth) + ifcall := r.ifcall.(*TAuth) var err error if ifcall.Afid == NOFID { setError(r, fmt.Errorf("NOFID can't be used for afid")) // TODO: really? @@ -292,7 +292,7 @@ func sAttach(ctx context.Context, s *Server, c <-chan *Req) { if !ok { return } - ifcall := r.Ifcall.(*TAttach) + ifcall := r.ifcall.(*TAttach) r.Fid, err = s.fPool.add(ifcall.Fid) if err != nil { r.err = ErrDupFid @@ -388,7 +388,7 @@ func sWalk(ctx context.Context, s *Server, c <-chan *Req) { cwdp string n int ) - ifcall := r.Ifcall.(*TWalk) + ifcall := r.ifcall.(*TWalk) oldFid, ok := s.fPool.lookup(ifcall.Fid) if !ok { r.err = ErrUnknownFid @@ -479,7 +479,7 @@ func sOpen(ctx context.Context, s *Server, c <-chan *Req) { qid Qid st fs.FileInfo ) - ifcall := r.Ifcall.(*TOpen) + ifcall := r.ifcall.(*TOpen) r.Fid, ok = s.fPool.lookup(ifcall.Fid) if !ok { r.err = ErrUnknownFid @@ -563,7 +563,7 @@ func sOpen(ctx context.Context, s *Server, c <-chan *Req) { } continue } - r.Fid.OMode = r.Ifcall.(*TOpen).Mode + r.Fid.OMode = r.ifcall.(*TOpen).Mode if _, ok := r.Fid.File.(*AuthFile); ok { select { case s.respChan <- r: @@ -611,7 +611,7 @@ func sCreate(ctx context.Context, s *Server, c <-chan *Req) { perm FileMode dirperm FileMode ) - ifcall := r.Ifcall.(*TCreate) + ifcall := r.ifcall.(*TCreate) r.Fid, ok = s.fPool.lookup(ifcall.Fid) if !ok { r.err = ErrUnknownFid @@ -691,7 +691,7 @@ func sRead(ctx context.Context, s *Server, c <-chan *Req) { if !ok { return } - ifcall := r.Ifcall.(*TRead) + ifcall := r.ifcall.(*TRead) r.Fid, ok = s.fPool.lookup(ifcall.Fid) if !ok { r.err = ErrUnknownFid @@ -803,7 +803,7 @@ func sWrite(ctx context.Context, s *Server, c <-chan *Req) { errc chan error omode OpenMode ) - ifcall := r.Ifcall.(*TWrite) + ifcall := r.ifcall.(*TWrite) r.Fid, ok = s.fPool.lookup(ifcall.Fid) if !ok { r.err = ErrUnknownFid @@ -875,7 +875,7 @@ func sClunk(ctx context.Context, s *Server, c <-chan *Req) { if !ok { return } - ifcall := r.Ifcall.(*TClunk) + ifcall := r.ifcall.(*TClunk) fid, ok := s.fPool.lookup(ifcall.Fid) if !ok { r.err = ErrUnknownFid @@ -918,7 +918,7 @@ func sRemove(ctx context.Context, s *Server, c <-chan *Req) { err error rfs RemoverFS ) - ifcall := r.Ifcall.(*TRemove) + ifcall := r.ifcall.(*TRemove) r.Fid, ok = s.fPool.lookup(ifcall.Fid) if !ok { r.err = ErrUnknownFid @@ -974,7 +974,7 @@ func sStat(ctx context.Context, s *Server, c <-chan *Req) { fi fs.FileInfo err error ) - ifcall := r.Ifcall.(*TStat) + ifcall := r.ifcall.(*TStat) r.Fid, ok = s.fPool.lookup(ifcall.Fid) if !ok { r.err = ErrUnknownFid @@ -1017,7 +1017,7 @@ func sWStat(ctx context.Context, s *Server, c <-chan *Req) { fi fs.FileInfo err error ) - ifcall := r.Ifcall.(*TWStat) + ifcall := r.ifcall.(*TWStat) r.Fid, ok = s.fPool.lookup(ifcall.Fid) if !ok { r.err = ErrUnknownFid @@ -1209,9 +1209,9 @@ L: } continue L } - switch r.Ifcall.(type) { + switch r.ifcall.(type) { default: - setError(r, fmt.Errorf("unknown message type: %d", r.Ifcall.Type())) + setError(r, fmt.Errorf("unknown message type: %d", r.ifcall.Type())) select { case s.respChan <- r: case <-ctx.Done(): diff --git a/server2_test.go b/server2_test.go @@ -46,7 +46,7 @@ func TestRunListener(t *testing.T) { if r.listenErr != nil { t.Fatalf("listenErr: %v", r.listenErr) } - got := r.Ifcall + got := r.ifcall if !reflect.DeepEqual(want, got) { t.Errorf("listener modified message:\n\twant: %v\n\tgot: %v", want, got) @@ -121,8 +121,8 @@ func TestGetReq(t *testing.T) { if got.tag != wantMsg.GetTag() { t.Errorf("r.tag: want: %v, got: %v", wantMsg.GetTag(), got.tag) } - if !reflect.DeepEqual(got.Ifcall, wantMsg) { - t.Errorf("r.Ifcall:\n\twant: %v,\n\tgot: %v", wantMsg, got.Ifcall) + if !reflect.DeepEqual(got.ifcall, wantMsg) { + t.Errorf("r.ifcall:\n\twant: %v,\n\tgot: %v", wantMsg, got.ifcall) } got2, ok := s.rPool.lookup(wantMsg.GetTag()) if !ok { @@ -140,13 +140,13 @@ func TestSVersion(t *testing.T) { input *Req want *Req }{ - {&Req{Ifcall: &TVersion{Msize: 1024, Version: "9P2000"}}, + {&Req{ifcall: &TVersion{Msize: 1024, Version: "9P2000"}}, &Req{Ofcall: &RVersion{Msize: 1024, Version: "9P2000"}}}, - {&Req{Ifcall: &TVersion{Msize: 564, Version: "9P2000"}}, + {&Req{ifcall: &TVersion{Msize: 564, Version: "9P2000"}}, &Req{Ofcall: &RVersion{Msize: 564, Version: "9P2000"}}}, - {&Req{Ifcall: &TVersion{Msize: 8 * 1024, Version: "9P2000"}}, + {&Req{ifcall: &TVersion{Msize: 8 * 1024, Version: "9P2000"}}, &Req{Ofcall: &RVersion{Msize: 1024, Version: "9P2000"}}}, - {&Req{Ifcall: &TVersion{Msize: 1024, Version: "unko"}}, + {&Req{ifcall: &TVersion{Msize: 1024, Version: "unko"}}, &Req{Ofcall: &RVersion{Msize: 1024, Version: "unknown"}}}, } tc := make(chan *Req) @@ -158,7 +158,7 @@ func TestSVersion(t *testing.T) { for _, test := range tests { oldMsize := s.msize tc <- test.input - ifcall := test.input.Ifcall.(*TVersion) + ifcall := test.input.ifcall.(*TVersion) wantmsg := test.want.Ofcall.(*RVersion) gotmsg := (<-rc).Ofcall.(*RVersion) if !reflect.DeepEqual(wantmsg, gotmsg) { @@ -180,12 +180,12 @@ func TestSAuth(t *testing.T) { want *Req authFunc func(context.Context, *Req) }{ - {&Req{Ifcall: &TAuth{Afid: NOFID, Uname: "kenji", Aname: ""}}, + {&Req{ifcall: &TAuth{Afid: NOFID, Uname: "kenji", Aname: ""}}, &Req{Ofcall: &RError{Ename: errors.New("authentication not required")}}, nil}, - {&Req{Ifcall: &TAuth{Afid: NOFID, Uname: "kenji", Aname: ""}}, + {&Req{ifcall: &TAuth{Afid: NOFID, Uname: "kenji", Aname: ""}}, &Req{Ofcall: &RError{Ename: errors.New("NOFID can't be used for afid")}}, func(ctx context.Context, r *Req) {}}, - {&Req{Ifcall: &TAuth{Afid: 0, Uname: "kenji", Aname: ""}}, + {&Req{ifcall: &TAuth{Afid: 0, Uname: "kenji", Aname: ""}}, &Req{Ofcall: &RAuth{Tag: 0, Aqid: Qid{0, 1, 2}}}, func(ctx context.Context, r *Req) { r.Ofcall = &RAuth{Tag: 0, Aqid: Qid{0, 1, 2}} @@ -232,7 +232,7 @@ func TestSFlush(t *testing.T) { tests := []struct { input *Req }{ - {&Req{Ifcall: &TFlush{}, + {&Req{ifcall: &TFlush{}, Oldreq: &Req{pool: rp, done: make(chan struct{})}}}, } tc := make(chan *Req) diff --git a/server_test.go b/server_test.go @@ -26,27 +26,27 @@ func TestSAttach(t *testing.T) { } fid.File = af tests := []struct { - input *lib9p.Req + input lib9p.Msg auth bool authOK bool want *lib9p.Req }{ - {&lib9p.Req{Ifcall: &lib9p.TAttach{Fid: 0, Afid: lib9p.NOFID, Uname: "kenji", Aname: ""}}, + {&lib9p.TAttach{Fid: 0, Afid: lib9p.NOFID, Uname: "kenji", Aname: ""}, false, false, &lib9p.Req{Ofcall: &lib9p.RError{}}}, // duplicate fid - {&lib9p.Req{Ifcall: &lib9p.TAttach{Fid: 1, Afid: lib9p.NOFID, Uname: "kenji", Aname: ""}}, + {&lib9p.TAttach{Fid: 1, Afid: lib9p.NOFID, Uname: "kenji", Aname: ""}, false, true, &lib9p.Req{Ofcall: &lib9p.RAttach{}}}, // ok - {&lib9p.Req{Ifcall: &lib9p.TAttach{Fid: 2, Afid: lib9p.NOFID, Uname: "kenji", Aname: ""}}, + {&lib9p.TAttach{Fid: 2, Afid: lib9p.NOFID, Uname: "kenji", Aname: ""}, true, false, &lib9p.Req{Ofcall: &lib9p.RError{}}}, // afid is not set - {&lib9p.Req{Ifcall: &lib9p.TAttach{Fid: 2, Afid: 0, Uname: "kenji", Aname: ""}}, + {&lib9p.TAttach{Fid: 2, Afid: 0, Uname: "kenji", Aname: ""}, true, false, &lib9p.Req{Ofcall: &lib9p.RError{}}}, // not authOK - {&lib9p.Req{Ifcall: &lib9p.TAttach{Fid: 2, Afid: 0, Uname: "unko", Aname: ""}}, + {&lib9p.TAttach{Fid: 2, Afid: 0, Uname: "unko", Aname: ""}, true, true, &lib9p.Req{Ofcall: &lib9p.RError{}}}, // wrong user - {&lib9p.Req{Ifcall: &lib9p.TAttach{Fid: 2, Afid: 0, Uname: "kenji", Aname: ""}}, + {&lib9p.TAttach{Fid: 2, Afid: 0, Uname: "kenji", Aname: ""}, true, true, &lib9p.Req{Ofcall: &lib9p.RAttach{}}}, // ok } @@ -68,7 +68,9 @@ func TestSAttach(t *testing.T) { } else { s.Auth = nil } - tc <- test.input + req := new(lib9p.Req) + req.SetIfcall(test.input) + tc <- req ofcall := (<-rc).Ofcall switch wantmsg := test.want.Ofcall.(type) { case *lib9p.RAttach: @@ -91,22 +93,22 @@ func TestSAttach(t *testing.T) { func TestSWalk(t *testing.T) { tests := []struct { - input *lib9p.Req + input lib9p.Msg wantLen int // len(Ofcall.(*lib9p.RWalk).Qids) wantErr error }{ - {&lib9p.Req{Ifcall: &lib9p.TWalk{Fid: 0, Newfid: 1, Wnames: []string{"a"}}}, + {&lib9p.TWalk{Fid: 0, Newfid: 1, Wnames: []string{"a"}}, 1, nil}, - {&lib9p.Req{Ifcall: &lib9p.TWalk{Fid: 0, Newfid: 2, Wnames: []string{"b"}}}, + {&lib9p.TWalk{Fid: 0, Newfid: 2, Wnames: []string{"b"}}, 1, nil}, - {&lib9p.Req{Ifcall: &lib9p.TWalk{Fid: 0, Newfid: 3, Wnames: []string{"dir", "file"}}}, + {&lib9p.TWalk{Fid: 0, Newfid: 3, Wnames: []string{"dir", "file"}}, 2, nil}, - {&lib9p.Req{Ifcall: &lib9p.TWalk{Fid: 0, Newfid: 4, Wnames: []string{"dir", "unko"}}}, + {&lib9p.TWalk{Fid: 0, Newfid: 4, Wnames: []string{"dir", "unko"}}, 1, nil}, // short walk // 9P document says: // If the first element cant be walked for any reason, // RError is returned. - {&lib9p.Req{Ifcall: &lib9p.TWalk{Fid: 0, Newfid: 5, Wnames: []string{"unko", "unko"}}}, + {&lib9p.TWalk{Fid: 0, Newfid: 5, Wnames: []string{"unko", "unko"}}, 0, errors.New("not found")}, } tc := make(chan *lib9p.Req) @@ -128,7 +130,9 @@ func TestSWalk(t *testing.T) { defer cancel() go lib9p.SWalk(ctx, s, tc) for i, test := range tests { - tc <- test.input + req := new(lib9p.Req) + req.SetIfcall(test.input) + tc <- req ofcall := (<-rc).Ofcall if test.wantErr == nil { gotMsg, ok := ofcall.(*lib9p.RWalk) @@ -199,7 +203,9 @@ func TestSOpen(t *testing.T) { t.Error(i, err) continue } - tc <- &lib9p.Req{Ifcall: test.input} + req := new(lib9p.Req) + req.SetIfcall(test.input) + tc <- req ofcall := (<-rc).Ofcall switch test.wantMsg.(type) { case *lib9p.ROpen: