commit d084bbd3e61eecdf4d9a6ed40742c83847fd22c3
parent 7946abf959f1d81ec0181079ad705971cbdd551c
Author: Matsuda Kenji <info@mtkn.jp>
Date: Tue, 9 Jan 2024 12:21:27 +0900
add NewServerMap
Diffstat:
2 files changed, 29 insertions(+), 10 deletions(-)
diff --git a/server.go b/server.go
@@ -35,7 +35,7 @@ type Server struct {
chatty9P bool
// The file system to export via 9P.
- fs map[string]FS
+ fsmap map[string]FS
// Auth function is passed an auth request when a TAuth message arrives
// If authentication is desired, Auth should
@@ -50,7 +50,14 @@ type Server struct {
// It reads incoming messages from r and writes responses to w.
func NewServer(fsys FS) *Server {
s := &Server{
- fs: map[string]FS{"": fsys},
+ fsmap: map[string]FS{"": fsys},
+ }
+ return s
+}
+
+func NewServerMap(fsmap map[string]FS) *Server {
+ s := &Server{
+ fsmap: fsmap,
}
return s
}
@@ -257,6 +264,10 @@ func sAuth(ctx context.Context, c *conn, rc <-chan *request) {
goto resp
}
ifcall = r.ifcall.(*TAuth)
+ if _, ok := c.s.fsmap[ifcall.Aname]; !ok {
+ r.err = fmt.Errorf("no such file system")
+ goto resp
+ }
if ifcall.Afid == NOFID {
r.err = fmt.Errorf("NOFID can't be used for afid") // TODO: really?
goto resp
@@ -328,13 +339,13 @@ func sAttach(ctx context.Context, c *conn, rc <-chan *request) {
r.fid.omode = -1
r.fid.path = "."
r.fid.uid = ifcall.Uname
- fsys, ok = c.s.fs[ifcall.Aname]
+ fsys, ok = c.s.fsmap[ifcall.Aname]
if !ok {
r.err = fmt.Errorf("no such file system")
goto resp
}
r.fid.fs = fsys
- st, err = fs.Stat(ExportFS{c.s.fs[ifcall.Aname]}, ".")
+ st, err = fs.Stat(ExportFS{c.s.fsmap[ifcall.Aname]}, ".")
if err != nil {
r.err = fmt.Errorf("stat root: %v", err)
goto resp
@@ -1166,11 +1177,6 @@ func sWStat(ctx context.Context, c *conn, rc <-chan *request) {
}
// Serve serves 9P conversation.
-// TODO: If the user of the library make a Server for each
-// connection to the same filesystem, those Servers can access
-// the same file concurrentry.
-// So some additional struct to represent each connection is needed.
-// And Serve method should be attached to that struct.
func (s *Server) Serve(ctx context.Context, r io.Reader, w io.Writer) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
diff --git a/server_test.go b/server_test.go
@@ -191,6 +191,11 @@ func TestSAuth(t *testing.T) {
func(ctx context.Context, r *request) {
r.ofcall = &RAuth{Tag: 0, Aqid: Qid{0, 1, 2}}
}},
+ {&request{ifcall: &TAuth{Afid: 0, Uname: "kenji", Aname: "fs"}},
+ &request{ofcall: &RError{Ename: errors.New("no such file system")}},
+ func(ctx context.Context, r *request) {
+ r.ofcall = &RAuth{Tag: 0, Aqid: Qid{0, 1, 2}}
+ }},
}
for _, test := range tests {
func() {
@@ -252,7 +257,7 @@ func TestSFlush(t *testing.T) {
func TestSAttach(t *testing.T) {
tests := []struct {
- input Msg
+ input *TAttach
auth bool
authOK bool
want Msg
@@ -275,8 +280,15 @@ func TestSAttach(t *testing.T) {
{&TAttach{Fid: 2, Afid: 0, Uname: "kenji", Aname: ""},
true, true,
&RAttach{}}, // ok
+ {&TAttach{Fid: 3, Afid: 0, Uname: "kenji", Aname: "fs"},
+ true, true,
+ &RAttach{}}, // ok
+ {&TAttach{Fid: 4, Afid: 0, Uname: "kenji", Aname: "fss"},
+ true, true,
+ &RError{}}, // no such file system
}
c, rc := setupConn(testfs)
+ c.s.fsmap["fs"] = testfs
tc := make(chan *request)
dammyAuth := func(context.Context, *request) {}
af := &AuthFile{
@@ -294,6 +306,7 @@ func TestSAttach(t *testing.T) {
go sAttach(ctx, c, tc)
for i, test := range tests {
af.AuthOK = test.authOK
+ af.Aname = test.input.Aname
if test.auth {
c.s.Auth = dammyAuth
} else {