lib9p

Go 9P library.
Log | Files | Refs

commit 2b1d32eaafc2315f539685452bd6a8e29b56b3e1
parent 78816749545a396144db5f5e36e214315aa3afc9
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri, 15 Sep 2023 09:33:39 +0900

refactor

Diffstat:
Mserver.go | 49++++++++++++++++++++++---------------------------
1 file changed, 22 insertions(+), 27 deletions(-)

diff --git a/server.go b/server.go @@ -432,13 +432,13 @@ func sWrite(s *Server, r *Req) { ifcall := r.ifcall.(*TWrite) fid, ok := s.fPool.lookup(ifcall.Fid()) if !ok { - respond(r, fmt.Errorf("unknown fid")) + respond(r, ErrUnknownFid) return } file := fid.File if !hasPerm(file, fid.Uid, AWRITE) { - respond(r, fmt.Errorf("permission denied")) + respond(r, ErrPerm) return } @@ -460,7 +460,7 @@ func sWrite(s *Server, r *Req) { } ofcall.count = uint32(n) default: - respond(r, fmt.Errorf("operation not supported")) + respond(r, ErrOperation) return } r.ofcall = ofcall @@ -473,7 +473,7 @@ func sClunk(s *Server, r *Req) { ifcall := r.ifcall.(*TClunk) _, ok := s.fPool.lookup(ifcall.Fid()) if !ok { - respond(r, fmt.Errorf("unknown fid")) + respond(r, ErrUnknownFid) return } s.fPool.delete(ifcall.Fid()) @@ -489,7 +489,7 @@ func sRemove(s *Server, r *Req) { ifcall := r.ifcall.(*TRemove) fid, ok := s.fPool.lookup(ifcall.Fid()) if !ok { - respond(r, fmt.Errorf("unknown fid")) + respond(r, ErrUnknownFid) return } @@ -503,13 +503,13 @@ func sRemove(s *Server, r *Req) { } if !hasPerm(parent, fid.Uid, AWRITE) { - respond(r, fmt.Errorf("permission denied")) + respond(r, ErrPerm) return } rfile, ok := fid.File.(RemoverFile) if !ok { - respond(r, fmt.Errorf("operation not supported")) + respond(r, ErrOperation) return } @@ -525,25 +525,22 @@ func rRemove(r *Req, err error) {} func sStat(s *Server, r *Req) { ifcall := r.ifcall.(*TStat) - fidNum := ifcall.Fid() - fidStruct, ok := s.fPool.lookup(fidNum) + fid, ok := s.fPool.lookup(ifcall.Fid()) if !ok { - respond(r, fmt.Errorf("unknown fid %d", fidNum)) + respond(r, ErrUnknownFid) return } - fileInfo, err := fidStruct.File.Stat() + fileInfo, err := fid.File.Stat() if err != nil { - log.Printf("stat %v: %v", fidStruct.File, err) + log.Printf("stat %v: %v", fid.File, err) respond(r, fmt.Errorf("internal error")) return } - ofcall := &RStat{ + r.ofcall = &RStat{ tag: ifcall.Tag(), stat: fileInfo.Sys().(*Stat), } - - r.ofcall = ofcall respond(r, nil) } @@ -554,13 +551,13 @@ func sWStat(s *Server, r *Req) { fidNum := ifcall.Fid() fidStruct, ok := s.fPool.lookup(fidNum) if !ok { - respond(r, fmt.Errorf("unknown fid %d", fidNum)) + respond(r, ErrUnknownFid) return } wsfile, ok := fidStruct.File.(WriterStatFile) if !ok { - respond(r, fmt.Errorf("operation not supported")) + respond(r, ErrOperation) return } @@ -587,7 +584,7 @@ func sWStat(s *Server, r *Req) { return } if !hasPerm(parent, fidStruct.Uid, AWRITE) { - respond(r, fmt.Errorf("permission denied")) + respond(r, ErrPerm) return } children, err := parent.Child() @@ -610,9 +607,8 @@ func sWStat(s *Server, r *Req) { } if wstat.Length != ^int64(0) { - log.Printf("length: %T, %d", wstat.Length, wstat.Length) if fi.IsDir() || !hasPerm(fidStruct.File, fidStruct.Uid, AWRITE) { - respond(r, fmt.Errorf("permission denied")) + respond(r, ErrPerm) return } newStat.Length = wstat.Length @@ -621,11 +617,11 @@ func sWStat(s *Server, r *Req) { if wstat.Mode != FileMode(^uint32(0)) { // the owner of the file or the group leader of the file's group. if wstat.Uid != newStat.Uid && wstat.Gid != newStat.Uid { - respond(r, fmt.Errorf("permission denied")) + respond(r, ErrPerm) return } if (wstat.Mode^newStat.Mode)&DMDIR != 0 { - respond(r, fmt.Errorf("permission denied")) + respond(r, ErrPerm) return } newStat.Mode = wstat.Mode @@ -634,7 +630,7 @@ func sWStat(s *Server, r *Req) { if wstat.Mtime != ^uint32(0) { // the owner of the file or the group leader of the file's group. if wstat.Uid != newStat.Uid && wstat.Gid != newStat.Uid { - respond(r, fmt.Errorf("permission denied")) + respond(r, ErrPerm) return } newStat.Mtime = wstat.Mtime @@ -651,10 +647,9 @@ func sWStat(s *Server, r *Req) { return } - ofcall := new(RWStat) - ofcall.tag = ifcall.Tag() - r.ofcall = ofcall - + r.ofcall = &RWStat{ + tag: ifcall.Tag(), + } respond(r, nil) }