commit e9ee50f736c593762580dec92f9d4f2e8c2a401a
parent e180a8fa695b80aeefbe72e2d86f808df1c9c1f7
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Wed, 10 Jan 2024 12:54:40 +0900
refactor
Diffstat:
2 files changed, 21 insertions(+), 23 deletions(-)
diff --git a/fid.go b/fid.go
@@ -44,6 +44,8 @@ type fid struct {
 // is required.
 const NOFID = ^uint32(0)
 
+// NewFid returns an fid with fid set to id and omode set to -1.
+// Other fields should be set propperly after aquiring the fid.
 func newFid(id uint32) *fid {
 	return &fid{
 		fid:   id,
diff --git a/server.go b/server.go
@@ -267,7 +267,7 @@ func sAuth(ctx context.Context, c *conn, rc <-chan *request) {
 				goto resp
 			}
 			if ifcall.Afid == NOFID {
-				r.err = fmt.Errorf("NOFID can't be used for afid") // TODO: really?
+				r.err = fmt.Errorf("NOFID can't be used for afid")
 				goto resp
 			}
 			r.afid, err = c.fPool.add(ifcall.Afid)
@@ -296,20 +296,15 @@ func sAttach(ctx context.Context, c *conn, rc <-chan *request) {
 		case <-ctx.Done():
 			return
 		case r, ok := <-rc:
+			if !ok {
+				return
+			}
 			var (
 				fsys FS
 				st   fs.FileInfo
 				err  error
 			)
-			if !ok {
-				return
-			}
 			ifcall := r.ifcall.(*TAttach)
-			r.fid, err = c.fPool.add(ifcall.Fid)
-			if err != nil {
-				r.err = ErrDupFid
-				goto resp
-			}
 			switch {
 			case c.s.Auth == nil && ifcall.Afid == NOFID:
 			case c.s.Auth == nil && ifcall.Afid != NOFID:
@@ -334,7 +329,11 @@ func sAttach(ctx context.Context, c *conn, rc <-chan *request) {
 					goto resp
 				}
 			}
-			r.fid.omode = -1
+			r.fid, err = c.fPool.add(ifcall.Fid)
+			if err != nil {
+				r.err = ErrDupFid
+				goto resp
+			}
 			r.fid.path = "."
 			r.fid.uid = ifcall.Uname
 			fsys, ok = c.s.fsmap[ifcall.Aname]
@@ -429,7 +428,6 @@ func sWalk(ctx context.Context, c *conn, rc <-chan *request) {
 			if ifcall.Fid == ifcall.Newfid {
 				newFid = oldFid
 			} else {
-				var err error
 				newFid, err = c.fPool.add(ifcall.Newfid)
 				if err != nil {
 					r.err = fmt.Errorf("alloc: %v", err)
@@ -450,7 +448,6 @@ func sWalk(ctx context.Context, c *conn, rc <-chan *request) {
 				wqids[i] = stat.Sys().(*Stat).Qid
 				n++
 			}
-			newFid.omode = -1
 			newFid.path = cwdp
 			newFid.uid = oldFid.uid
 			newFid.fs = oldFid.fs
@@ -459,12 +456,13 @@ func sWalk(ctx context.Context, c *conn, rc <-chan *request) {
 			}
 		resp:
 			if r.ofcall == nil {
-				if r.err == nil {
-					panic("err and r.ofcall are both nil")
-				}
 				setError(r, r.err)
-				c.respChan <- r
-				continue
+				select {
+				case c.respChan <- r:
+					continue
+				case <-ctx.Done():
+					return
+				}
 			}
 			ofcall := r.ofcall.(*RWalk)
 			if r.err != nil || len(ofcall.Qids) < len(ifcall.Wnames) {
@@ -521,10 +519,6 @@ func sOpen(ctx context.Context, c *conn, rc <-chan *request) {
 				}
 				qid = afile.Qid
 			} else {
-				// Write attempt to a directory is prohibitted by the protocol.
-				// See open(5).
-				// In plan9 implementation, ifcall.Mode() is ANDed with ^ORCLOSE,
-				// but ORCLOSE is also prohibitted by the protocol...
 				st, err = fs.Stat(ExportFS{r.fid.fs}, r.fid.path)
 				if err != nil {
 					r.err = fmt.Errorf("stat: %v", err)
@@ -532,6 +526,9 @@ func sOpen(ctx context.Context, c *conn, rc <-chan *request) {
 				}
 				qid = st.Sys().(*Stat).Qid
 			}
+			// Write attempt to a directory is prohibitted by the protocol.
+			// In plan9 implementation, ifcall.Mode is ANDed with ^ORCLOSE,
+			// but ORCLOSE is also prohibitted by the protocol...
 			if qid.Type == QTDIR && ifcall.Mode != OREAD {
 				r.err = fmt.Errorf("is a directory")
 				goto resp
@@ -594,7 +591,7 @@ func sOpen(ctx context.Context, c *conn, rc <-chan *request) {
 				}
 				continue
 			}
-			f, err := r.fid.fs.OpenFile(r.fid.path, r.fid.omode)
+			r.fid.file, err = r.fid.fs.OpenFile(r.fid.path, r.fid.omode)
 			if err != nil {
 				setError(r, err)
 				select {
@@ -604,7 +601,6 @@ func sOpen(ctx context.Context, c *conn, rc <-chan *request) {
 				}
 				continue
 			}
-			r.fid.file = f
 			select {
 			case c.respChan <- r:
 			case <-ctx.Done():