commit 2782b7242823bca25d920bf451a1f33b16ad8773
parent a32f1c02bbb6f3c7b80d812569e8e165a9a43f88
Author: Matsuda Kenji <info@mtkn.jp>
Date: Fri, 29 Dec 2023 09:32:23 +0900
unexport members of Fid
Diffstat:
5 files changed, 73 insertions(+), 68 deletions(-)
diff --git a/auth_test.go b/auth_test.go
@@ -21,19 +21,19 @@ func TestAuth(t *testing.T) {
conn.S.Auth = func(ctx context.Context, r *lib9p.Req) {
ifcall := r.Ifcall().(*lib9p.TAuth)
aqid := lib9p.Qid{Type: lib9p.QTAUTH, Vers: 0, Path: ^uint64(0)}
- r.Afid().File = &lib9p.AuthFile{
+ r.Afid().SetFile(&lib9p.AuthFile{
Qid: aqid,
Uname: ifcall.Uname,
Aname: ifcall.Aname,
W: acw,
R: acr,
- }
+ })
go func() {
<-ctx.Done()
asr.Close()
asw.Close()
}()
- runAuth(ctx, t, r.Afid().File.(*lib9p.AuthFile), asr, asw)
+ runAuth(ctx, t, r.Afid().File().(*lib9p.AuthFile), asr, asw)
r.SetOfcall(&lib9p.RAuth{Tag: ifcall.Tag, Aqid: aqid})
}
ctx := context.Background()
diff --git a/export_test.go b/export_test.go
@@ -68,6 +68,10 @@ type ReqPool = reqPool
func (rp *ReqPool) Add(tag uint16) (*Req, error) { return rp.add(tag) }
func (fid *Fid) SetPath(path string) { fid.path = path }
+func (fid *Fid) SetUid(uid string) { fid.uid = uid }
+func (fid *Fid) File() File { return fid.file }
+func (fid *Fid) SetFile(f File) { fid.file = f }
+func (fid *Fid) SetOmode(m OpenMode) { fid.omode = m }
func (fp *FidPool) Lookup(fid uint32) (*Fid, bool) { return fp.lookup(fid) }
func (fp *FidPool) Add(fid uint32) (*Fid, error) { return fp.add(fid) }
diff --git a/fid.go b/fid.go
@@ -20,11 +20,11 @@ const (
// Fid represents the fid defined by 9P
type Fid struct {
- Fid uint32
- OMode OpenMode /* -1 = not open */
+ fid uint32
+ omode OpenMode /* -1 = not open */
path string // The path from the root of the FS.
- File File // The associated File.
- Uid string // The user id derived from the attach message.
+ file File // The associated File.
+ uid string // The user id derived from the attach message.
dirOffset uint64 // Used when reading directory.
dirIndex int // Used when reading directory.
}
@@ -33,13 +33,13 @@ const NOFID = ^uint32(0)
func newFid(fid uint32) *Fid {
return &Fid{
- Fid: fid,
- OMode: -1,
+ fid: fid,
+ omode: -1,
}
}
func (f *Fid) String() string {
- fid := int64(f.Fid)
+ fid := int64(f.fid)
if uint32(fid) == NOFID {
fid = -1
}
@@ -89,11 +89,11 @@ func (pool *FidPool) delete(fid uint32) {
func (pool *FidPool) String() string {
s := "&FidPool{\n"
for fnum, fstruct := range pool.m {
- if fstruct.File == nil {
+ if fstruct.file == nil {
s += fmt.Sprintf(" %d: <nil>\n", fnum)
continue
}
- st, err := fstruct.File.Stat()
+ st, err := fstruct.file.Stat()
if err != nil {
panic(err)
}
diff --git a/server.go b/server.go
@@ -65,8 +65,8 @@ type Server struct {
// Auth function is passed an auth request when a TAuth message arrives
// If authentication is desired, Auth should
- // set request.Afid.File to an *AuthFile and request.ofcall.Qid, and prepare to
- // authenticate via the Read()/Write() calls to request.Afid.File.
+ // set request.Afid.file to an *AuthFile and request.ofcall.Qid, and prepare to
+ // authenticate via the Read()/Write() calls to request.Afid.file.
// Auth should clean up everything it creates when ctx is canceled.
// If this is nil, no authentication is performed.
Auth func(ctx context.Context, r *request)
@@ -312,7 +312,7 @@ func sAttach(ctx context.Context, s *Server, c <-chan *request) {
r.err = ErrUnknownFid
goto resp
}
- af, ok := afid.File.(*AuthFile)
+ af, ok := afid.file.(*AuthFile)
if !ok {
r.err = fmt.Errorf("not auth file")
goto resp
@@ -322,9 +322,9 @@ func sAttach(ctx context.Context, s *Server, c <-chan *request) {
goto resp
}
}
- r.fid.OMode = -1
+ r.fid.omode = -1
r.fid.path = "."
- r.fid.Uid = ifcall.Uname
+ r.fid.uid = ifcall.Uname
st, err = fs.Stat(ExportFS{s.fs}, ".")
if err != nil {
r.err = fmt.Errorf("stat root: %v", err)
@@ -336,7 +336,7 @@ func sAttach(ctx context.Context, s *Server, c <-chan *request) {
resp:
if r.err != nil {
if r.fid != nil {
- s.fPool.delete(r.fid.Fid)
+ s.fPool.delete(r.fid.fid)
}
setError(r, r.err)
}
@@ -394,7 +394,7 @@ func sWalk(ctx context.Context, s *Server, c <-chan *request) {
r.err = ErrUnknownFid
goto resp
}
- if oldFid.OMode != -1 {
+ if oldFid.omode != -1 {
r.err = fmt.Errorf("cannot clone open fid")
goto resp
}
@@ -429,9 +429,9 @@ func sWalk(ctx context.Context, s *Server, c <-chan *request) {
wqids[i] = stat.Sys().(*Stat).Qid
n++
}
- newFid.OMode = -1
+ newFid.omode = -1
newFid.path = cwdp
- newFid.Uid = oldFid.Uid
+ newFid.uid = oldFid.uid
r.ofcall = &RWalk{
Qids: wqids[:n],
}
@@ -485,14 +485,14 @@ func sOpen(ctx context.Context, s *Server, c <-chan *request) {
r.err = ErrUnknownFid
goto resp
}
- if r.fid.OMode != -1 {
+ if r.fid.omode != -1 {
r.err = ErrBotch
goto resp
}
- if afile, ok := r.fid.File.(*AuthFile); ok {
- // s.Auth should set r.fid.File to a valid *AuthFile,
- // so r.fid.File should not be nil.
- st, err = r.fid.File.Stat()
+ if afile, ok := r.fid.file.(*AuthFile); ok {
+ // s.Auth should set r.fid.file to a valid *AuthFile,
+ // so r.fid.file should not be nil.
+ st, err = r.fid.file.Stat()
if err != nil {
r.err = fmt.Errorf("stat: %v", err)
goto resp
@@ -533,7 +533,7 @@ func sOpen(ctx context.Context, s *Server, c <-chan *request) {
r.err = ErrPerm
goto resp
}
- if !hasPerm(s.fs, st, r.fid.Uid, p) {
+ if !hasPerm(s.fs, st, r.fid.uid, p) {
r.err = ErrPerm
goto resp
}
@@ -544,7 +544,7 @@ func sOpen(ctx context.Context, s *Server, c <-chan *request) {
r.err = fmt.Errorf("stat parent: %v", err)
goto resp
}
- if !hasPerm(s.fs, st, r.fid.Uid, AWRITE) {
+ if !hasPerm(s.fs, st, r.fid.uid, AWRITE) {
r.err = ErrPerm
goto resp
}
@@ -563,8 +563,8 @@ func sOpen(ctx context.Context, s *Server, c <-chan *request) {
}
continue
}
- r.fid.OMode = r.ifcall.(*TOpen).Mode
- if _, ok := r.fid.File.(*AuthFile); ok {
+ r.fid.omode = r.ifcall.(*TOpen).Mode
+ if _, ok := r.fid.file.(*AuthFile); ok {
select {
case s.respChan <- r:
case <-ctx.Done():
@@ -572,7 +572,7 @@ func sOpen(ctx context.Context, s *Server, c <-chan *request) {
}
continue
}
- f, err := s.fs.OpenFile(r.fid.path, r.fid.OMode)
+ f, err := s.fs.OpenFile(r.fid.path, r.fid.omode)
if err != nil {
setError(r, err)
select {
@@ -582,7 +582,7 @@ func sOpen(ctx context.Context, s *Server, c <-chan *request) {
}
continue
}
- r.fid.File = f
+ r.fid.file = f
select {
case s.respChan <- r:
case <-ctx.Done():
@@ -626,7 +626,7 @@ func sCreate(ctx context.Context, s *Server, c <-chan *request) {
r.err = fmt.Errorf("create in non-dir")
goto resp
}
- if !hasPerm(s.fs, dirstat, r.fid.Uid, AWRITE) {
+ if !hasPerm(s.fs, dirstat, r.fid.uid, AWRITE) {
r.err = ErrPerm
goto resp
}
@@ -643,15 +643,15 @@ func sCreate(ctx context.Context, s *Server, c <-chan *request) {
perm &= ^FileMode(0777) | (dirperm & FileMode(0777))
}
cpath = path.Join(r.fid.path, ifcall.Name)
- file, err = cfs.Create(cpath, r.fid.Uid, ifcall.Mode, perm)
+ file, err = cfs.Create(cpath, r.fid.uid, ifcall.Mode, perm)
if err != nil {
r.err = fmt.Errorf("create: %v", err)
goto resp
}
- r.fid.File = file
+ r.fid.file = file
r.fid.path = cpath
- r.fid.OMode = ifcall.Mode
- st, err = r.fid.File.Stat()
+ r.fid.omode = ifcall.Mode
+ st, err = r.fid.file.Stat()
if err != nil {
r.err = fmt.Errorf("stat: %v", err)
goto resp
@@ -697,15 +697,15 @@ func sRead(ctx context.Context, s *Server, c <-chan *request) {
r.err = ErrUnknownFid
goto resp
}
- if r.fid.OMode == -1 {
+ if r.fid.omode == -1 {
r.err = fmt.Errorf("not open")
goto resp
}
- if r.fid.OMode != OREAD && r.fid.OMode != ORDWR && r.fid.OMode != OEXEC {
+ if r.fid.omode != OREAD && r.fid.omode != ORDWR && r.fid.omode != OEXEC {
r.err = ErrPerm
goto resp
}
- fi, err = r.fid.File.Stat()
+ fi, err = r.fid.file.Stat()
if err != nil {
r.err = fmt.Errorf("stat: %v", err)
goto resp
@@ -749,10 +749,10 @@ func sRead(ctx context.Context, s *Server, c <-chan *request) {
r.fid.dirIndex = k
} else {
var err error
- if reader, ok := r.fid.File.(io.ReaderAt); ok {
+ if reader, ok := r.fid.file.(io.ReaderAt); ok {
n, err = reader.ReadAt(data, int64(ifcall.Offset))
} else {
- n, err = r.fid.File.Read(data)
+ n, err = r.fid.file.Read(data)
}
if err != io.EOF && err != nil {
errc <- err
@@ -812,16 +812,16 @@ func sWrite(ctx context.Context, s *Server, c <-chan *request) {
if ifcall.Count > s.mSize()-IOHDRSZ {
ifcall.Count = s.mSize() - IOHDRSZ
}
- omode = r.fid.OMode & 3
+ omode = r.fid.omode & 3
if omode != OWRITE && omode != ORDWR {
- r.err = fmt.Errorf("write on fid with open mode 0x%x", r.fid.OMode)
+ r.err = fmt.Errorf("write on fid with open mode 0x%x", r.fid.omode)
goto resp
}
ofcall = new(RWrite)
errc = make(chan error)
go func() {
defer close(errc)
- switch file := r.fid.File.(type) {
+ switch file := r.fid.file.(type) {
case io.WriterAt:
n, err := file.WriteAt(ifcall.Data, int64(ifcall.Offset))
if err != nil {
@@ -882,8 +882,8 @@ func sClunk(ctx context.Context, s *Server, c <-chan *request) {
goto resp
}
s.fPool.delete(ifcall.Fid)
- if fid.OMode != -1 {
- if err := fid.File.Close(); err != nil {
+ if fid.omode != -1 {
+ if err := fid.file.Close(); err != nil {
r.err = fmt.Errorf("close: %v", err)
goto resp
}
@@ -925,8 +925,8 @@ func sRemove(ctx context.Context, s *Server, c <-chan *request) {
goto resp
}
s.fPool.delete(ifcall.Fid)
- if r.fid.OMode != -1 {
- r.fid.File.Close()
+ if r.fid.omode != -1 {
+ r.fid.file.Close()
}
parentPath = path.Dir(r.fid.path)
pstat, err = fs.Stat(ExportFS{s.fs}, parentPath)
@@ -934,7 +934,7 @@ func sRemove(ctx context.Context, s *Server, c <-chan *request) {
r.err = fmt.Errorf("stat parent: %v", err)
goto resp
}
- if !hasPerm(s.fs, pstat, r.fid.Uid, AWRITE) {
+ if !hasPerm(s.fs, pstat, r.fid.uid, AWRITE) {
r.err = ErrPerm
goto resp
}
@@ -1023,22 +1023,22 @@ func sWStat(ctx context.Context, s *Server, c <-chan *request) {
r.err = ErrUnknownFid
goto resp
}
- if r.fid.OMode == -1 {
+ if r.fid.omode == -1 {
var err error
- r.fid.File, err = s.fs.OpenFile(r.fid.path, OREAD)
+ r.fid.file, err = s.fs.OpenFile(r.fid.path, OREAD)
if err != nil {
r.err = fmt.Errorf("open: %v", err)
goto resp
}
- defer r.fid.File.Close()
+ defer r.fid.file.Close()
}
- wsfile, ok = r.fid.File.(WriterStatFile)
+ wsfile, ok = r.fid.file.(WriterStatFile)
if !ok {
r.err = ErrOperation
goto resp
}
wstat = ifcall.Stat
- fi, err = r.fid.File.Stat()
+ fi, err = r.fid.file.Stat()
if err != nil {
r.err = fmt.Errorf("stat: %v", err)
goto resp
@@ -1062,7 +1062,7 @@ func sWStat(ctx context.Context, s *Server, c <-chan *request) {
r.err = fmt.Errorf("stat parent: %v", err)
goto resp
}
- if !hasPerm(s.fs, pstat, r.fid.Uid, AWRITE) {
+ if !hasPerm(s.fs, pstat, r.fid.uid, AWRITE) {
r.err = ErrPerm
goto resp
}
@@ -1087,7 +1087,7 @@ func sWStat(ctx context.Context, s *Server, c <-chan *request) {
newStat.Name = wstat.Name
}
if wstat.Length != ^int64(0) && wstat.Length != newStat.Length {
- if fi.IsDir() || !hasPerm(s.fs, fi, r.fid.Uid, AWRITE) {
+ if fi.IsDir() || !hasPerm(s.fs, fi, r.fid.uid, AWRITE) {
r.err = ErrPerm
goto resp
}
@@ -1095,7 +1095,7 @@ func sWStat(ctx context.Context, s *Server, c <-chan *request) {
}
if wstat.Mode != FileMode(^uint32(0)) && wstat.Mode != newStat.Mode {
// the owner of the file or the group leader of the file's group.
- if r.fid.Uid != newStat.Uid && r.fid.Uid != newStat.Gid {
+ if r.fid.uid != newStat.Uid && r.fid.uid != newStat.Gid {
r.err = ErrPerm
goto resp
}
@@ -1107,7 +1107,7 @@ func sWStat(ctx context.Context, s *Server, c <-chan *request) {
}
if wstat.Mtime != ^uint32(0) && wstat.Mtime != newStat.Mtime {
// the owner of the file or the group leader of the file's group.
- if r.fid.Uid != newStat.Uid && r.fid.Uid != newStat.Gid {
+ if r.fid.uid != newStat.Uid && r.fid.uid != newStat.Gid {
r.err = ErrPerm
goto resp
}
@@ -1117,9 +1117,9 @@ func sWStat(ctx context.Context, s *Server, c <-chan *request) {
// by the owner if also a member of the new group;
// or by the group leader of the file's current group if
// also the leader of the new group.
- if r.fid.Uid == newStat.Uid && s.fs.IsGroupMember(wstat.Gid, r.fid.Uid) ||
- s.fs.IsGroupLeader(newStat.Gid, r.fid.Uid) &&
- s.fs.IsGroupLeader(wstat.Gid, r.fid.Uid) {
+ if r.fid.uid == newStat.Uid && s.fs.IsGroupMember(wstat.Gid, r.fid.uid) ||
+ s.fs.IsGroupLeader(newStat.Gid, r.fid.uid) &&
+ s.fs.IsGroupLeader(wstat.Gid, r.fid.uid) {
newStat.Gid = wstat.Gid
} else {
r.err = ErrPerm
diff --git a/server_test.go b/server_test.go
@@ -24,7 +24,7 @@ func TestSAttach(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- fid.File = af
+ fid.SetFile(af)
tests := []struct {
input lib9p.Msg
auth bool
@@ -120,7 +120,7 @@ func TestSWalk(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- fid.OMode = -1
+ fid.SetOmode(-1)
fid.SetPath(".")
s := &lib9p.Server{}
s.SetFS(testfs.Fsys)
@@ -195,14 +195,15 @@ func TestSOpen(t *testing.T) {
t.Error(i, err)
continue
}
- fid.OMode = -1
+ fid.SetOmode(-1)
fid.SetPath(test.filePath)
- fid.Uid = test.uid
- fid.File, err = testfs.Fsys.WalkPath(test.filePath)
+ fid.SetUid(test.uid)
+ f, err := testfs.Fsys.WalkPath(test.filePath)
if err != nil {
t.Error(i, err)
continue
}
+ fid.SetFile(f)
req := new(lib9p.Req)
req.SetIfcall(test.input)
tc <- req
@@ -343,7 +344,7 @@ func TestServer(t *testing.T) {
if !ok {
t.Fatalf("lookup fid %d", m.(*lib9p.TRead).Fid)
}
- st, err := fid.File.Stat()
+ st, err := fid.File().Stat()
if err != nil {
t.Errorf("stat: %v", err)
continue