Extract attach and open from *Server; pass groups and log explicitly

- attach() is now a function taking (*olog.Logger, *GroupTable, ...)
- open() is now a function taking (*fs.Tree, *GroupTable, ...)
- Both captured in the handler map via closures over s.sink, s.groups
- 3 methods remain on *Server: Serve, handle, Shutdown
This commit is contained in:
Ollie Agent 2026-08-02 16:13:23 +02:00
parent 3242f108f9
commit 447156ce13
1 changed files with 11 additions and 11 deletions

View File

@ -125,9 +125,9 @@ func New(cfg Config) *Server {
},
plan9.Tauth: func(cs *connState, fc *plan9.Fcall, _ context.Context) *plan9.Fcall { return errFcall(fc, "no auth required") },
plan9.Tflush: func(cs *connState, fc *plan9.Fcall, _ context.Context) *plan9.Fcall { return flush(cs, fc) },
plan9.Tattach: func(cs *connState, fc *plan9.Fcall, _ context.Context) *plan9.Fcall { return s.attach(cs, fc) },
plan9.Tattach: func(cs *connState, fc *plan9.Fcall, _ context.Context) *plan9.Fcall { return attach(s.sink.Logger("9p", olog.LevelDebug), s.groups, cs, fc) },
plan9.Twalk: func(cs *connState, fc *plan9.Fcall, _ context.Context) *plan9.Fcall { return walk(cfg.RootTree, cs, fc) },
plan9.Topen: func(cs *connState, fc *plan9.Fcall, _ context.Context) *plan9.Fcall { return s.open(cs, fc) },
plan9.Topen: func(cs *connState, fc *plan9.Fcall, _ context.Context) *plan9.Fcall { return open(cfg.RootTree, s.groups, cs, fc) },
plan9.Tcreate: func(cs *connState, fc *plan9.Fcall, _ context.Context) *plan9.Fcall { return create(cfg.RootTree, cs, fc) },
plan9.Tread: func(cs *connState, fc *plan9.Fcall, ctx context.Context) *plan9.Fcall { return read(cfg.RootTree, cs, fc, ctx) },
plan9.Twrite: func(cs *connState, fc *plan9.Fcall, _ context.Context) *plan9.Fcall { return write(cs, fc) },
@ -333,7 +333,7 @@ func pathBase(path string) string {
return path[i+1:]
}
func (s *Server) attach(cs *connState, fc *plan9.Fcall) *plan9.Fcall {
func attach(log *olog.Logger, groups *GroupTable, cs *connState, fc *plan9.Fcall) *plan9.Fcall {
cs.mu.Lock()
defer cs.mu.Unlock()
if _, exists := cs.fids[fc.Fid]; exists {
@ -342,9 +342,9 @@ func (s *Server) attach(cs *connState, fc *plan9.Fcall) *plan9.Fcall {
cs.uname = fc.Uname
qid := plan9.Qid{Type: QTDir, Path: 0}
cs.fids[fc.Fid] = &fid{path: "/", qid: qid}
s.log.Debug("Tattach uname=%q", fc.Uname)
log.Debug("Tattach uname=%q", fc.Uname)
if fc.Uname != "" {
s.groups.Add("agent", fc.Uname)
groups.Add("agent", fc.Uname)
}
return &plan9.Fcall{Type: plan9.Rattach, Tag: fc.Tag, Qid: qid}
}
@ -400,22 +400,22 @@ func walk(root *fs.Tree, cs *connState, fc *plan9.Fcall) *plan9.Fcall {
return &plan9.Fcall{Type: plan9.Rwalk, Tag: fc.Tag, Wqid: wqids}
}
func (s *Server) open(cs *connState, fc *plan9.Fcall) *plan9.Fcall {
func open(root *fs.Tree, groups *GroupTable, cs *connState, fc *plan9.Fcall) *plan9.Fcall {
cs.mu.Lock()
defer cs.mu.Unlock()
f, ok := cs.fids[fc.Fid]
if !ok {
return errFcall(fc, "bad fid")
}
if _, err := s.rootTree.Stat(f.path[1:]); err != nil {
if _, err := root.Stat(f.path[1:]); err != nil {
delete(cs.fids, fc.Fid)
return errFcall(fc, "file not found")
}
uid, gid := fileOwnerGroup(s.rootTree, f.path)
perm := uint32(makeStat(s.rootTree, f.path).Mode) & 0777
uid, gid := fileOwnerGroup(root, f.path)
perm := uint32(makeStat(root, f.path).Mode) & 0777
bits := perm & 7 // world bits
if s.groups.IsMember(gid, cs.uname) {
if groups.IsMember(gid, cs.uname) {
bits |= (perm >> 3) & 7 // group bits
}
if cs.uname == uid {
@ -425,7 +425,7 @@ func (s *Server) open(cs *connState, fc *plan9.Fcall) *plan9.Fcall {
return errFcall(fc, "permission denied")
}
f.mode = fc.Mode
if ent, err := s.rootTree.Open(f.path[1:]); err == nil {
if ent, err := root.Open(f.path[1:]); err == nil {
f.entry = ent
}
return &plan9.Fcall{Type: plan9.Ropen, Tag: fc.Tag, Qid: f.qid}