lib9p

Go 9P library.
Log | Files | Refs | LICENSE

commit 93a84303125862c26d5bd350051a1faf918fa1aa
parent 6fbe899e8c228f2bfacf0b6a917a49c398999a8f
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Wed, 20 Dec 2023 14:46:25 +0900

gofmt

Diffstat:
Mauth_test.go | 3+--
Mclient/fs.go | 30+++++++++++++++---------------
Mcmd/diskfs/main.go | 3+--
Mcmd/semfs/fs.go | 5+++--
Mdiskfs/file.go | 8++++----
Mdiskfs/file_test.go | 3+--
Mdiskfs/fs.go | 7+++----
Mdiskfs/qid_plan9.go | 3++-
Mdiskfs/qid_unix.go | 1+
Mdiskfs/qid_windows.go | 1+
Mdiskfs/stat_plan9.go | 5+++--
Mdiskfs/stat_unix.go | 1+
Mdiskfs/stat_unix_test.go | 4++--
Mdiskfs/stat_windows.go | 26+++++++++++++-------------
Mdiskfs/uid_unix.go | 4++--
Mdoc.go | 3+--
Mexport_fs.go | 3+--
Mexport_fs_test.go | 3+--
Mexport_test.go | 48++++++++++++++++++++++++------------------------
Mfcall.go | 10+++++-----
Mfile_test.go | 3+--
Miofs/file.go | 4++--
Miofs/fs.go | 3+--
Miofs/stat.go | 13+++++++------
Mparse.go | 2+-
Mqid.go | 4++--
Mreq.go | 22+++++++++++-----------
Mreq_test.go | 3+--
Mserver.go | 44++++++++++++++++++++++----------------------
Mserver_test.go | 4++--
Mstat.go | 4+++-
31 files changed, 138 insertions(+), 139 deletions(-)

diff --git a/auth_test.go b/auth_test.go @@ -91,4 +91,4 @@ func runAuth(ctx context.Context, t *testing.T, afile *lib9p.AuthFile, r io.Read afile.AuthOK = true t.Log("authenticated") }() -} -\ No newline at end of file +} diff --git a/client/fs.go b/client/fs.go @@ -32,21 +32,21 @@ func (fsys *ClientFS) OpenFile(name string, omode lib9p.OpenMode) (lib9p.File, e if err != nil { return nil, err /* - // File not found. Create. - f, err = fsys.walkFile(path.Dir(name)) - if err != nil { - return nil, fmt.Errorf("walk to %s: %v", name, err) - } - tag, err := fsys.tPool.add() - if err != nil { - return nil, err - } - qid, iounit, err = fsys.c.Create(context.TODO(), tag, f.fid.fid, path.Base(name), perm, omode) - fsys.tPool.delete(tag) - if err != nil { - f.Close() - return nil, fmt.Errorf("create: %v", err) - } + // File not found. Create. + f, err = fsys.walkFile(path.Dir(name)) + if err != nil { + return nil, fmt.Errorf("walk to %s: %v", name, err) + } + tag, err := fsys.tPool.add() + if err != nil { + return nil, err + } + qid, iounit, err = fsys.c.Create(context.TODO(), tag, f.fid.fid, path.Base(name), perm, omode) + fsys.tPool.delete(tag) + if err != nil { + f.Close() + return nil, fmt.Errorf("create: %v", err) + } */ } else { // File exists. Open it. diff --git a/cmd/diskfs/main.go b/cmd/diskfs/main.go @@ -83,4 +83,4 @@ func runSignalHandler() context.Context { cancel() }() return ctx -} -\ No newline at end of file +} diff --git a/cmd/semfs/fs.go b/cmd/semfs/fs.go @@ -61,7 +61,7 @@ func (fsys *semFS) Create(name string, uid string, mode lib9p.OpenMode, perm lib fs: fsys, name: name, sem: 0, - path: uint64(fsys.lastpath+1), + path: uint64(fsys.lastpath + 1), cancel: cancel, } newfile.start(ctx) @@ -72,7 +72,7 @@ func (fsys *semFS) Create(name string, uid string, mode lib9p.OpenMode, perm lib func (fsys *semFS) Remove(name string) error { var ( - f *semFile + f *semFile path int ) for i, ff := range fsys.semfiles { @@ -109,6 +109,7 @@ func (root *semFS) Close() error { return nil } func (root *semFS) Read(p []byte) (n int, err error) { return 0, errors.New("is a directory") } + // TODO: set diroffset. func (root *semFS) ReadDir(n int) ([]fs.DirEntry, error) { de := make([]fs.DirEntry, 0, len(root.semfiles)) diff --git a/diskfs/file.go b/diskfs/file.go @@ -10,10 +10,10 @@ import ( // File represents a file. type File struct { - fs *FS // file system to which this file belongs - path string // relative path from the root of the fs. - file *os.File // underlying file. nil if not open. - dirIndex int // index for ReadDir + fs *FS // file system to which this file belongs + path string // relative path from the root of the fs. + file *os.File // underlying file. nil if not open. + dirIndex int // index for ReadDir } // Stat returns the *lib9p.FileInfo structure describing file. diff --git a/diskfs/file_test.go b/diskfs/file_test.go @@ -44,4 +44,4 @@ func BenchmarkRead(b *testing.B) { b.Fatalf("read: %v", err) } } -} -\ No newline at end of file +} diff --git a/diskfs/fs.go b/diskfs/fs.go @@ -68,7 +68,7 @@ func (fsys *FS) Create(name string, uid string, omode lib9p.OpenMode, perm lib9p paths := append([]string{fsys.rootPath}, strings.Split(name, "/")...) ospath := filepath.Join(paths...) var flag int - switch omode&3 { + switch omode & 3 { case lib9p.OREAD, lib9p.OEXEC: flag = os.O_RDONLY case lib9p.OWRITE: @@ -102,7 +102,7 @@ func (fsys *FS) Create(name string, uid string, omode lib9p.OpenMode, perm lib9p } } return &File{ - fs: fsys, + fs: fsys, path: name, file: osfile, }, nil @@ -120,4 +120,4 @@ func (fsys *FS) IsGroupLeader(group, uid string) bool { func (fsys *FS) IsGroupMember(group, uid string) bool { return isGroupMember(group, uid) -} -\ No newline at end of file +} diff --git a/diskfs/qid_plan9.go b/diskfs/qid_plan9.go @@ -1,4 +1,5 @@ //go:build plan9 + package diskfs import ( @@ -10,7 +11,7 @@ type fileID struct{} func idFromInfo(path string, fi fs.FileInfo) fileID { return fileID{} } // QidPool is the list of Qids in the file system. -type QidPool struct {} +type QidPool struct{} // newQidPool allocates a QidPool. func newQidPool() *QidPool { diff --git a/diskfs/qid_unix.go b/diskfs/qid_unix.go @@ -1,4 +1,5 @@ //go:build unix + package diskfs import ( diff --git a/diskfs/qid_windows.go b/diskfs/qid_windows.go @@ -1,4 +1,5 @@ //go:build windows + package diskfs import ( diff --git a/diskfs/stat_plan9.go b/diskfs/stat_plan9.go @@ -1,4 +1,5 @@ //go:build plan9 + package diskfs import ( @@ -122,5 +123,6 @@ func (f *File) wstat(s *lib9p.Stat) error { return nil } + // TODO: implement -func chown(ospath, uid, gid string) error { return nil } -\ No newline at end of file +func chown(ospath, uid, gid string) error { return nil } diff --git a/diskfs/stat_unix.go b/diskfs/stat_unix.go @@ -1,4 +1,5 @@ //go:build unix + package diskfs import ( diff --git a/diskfs/stat_unix_test.go b/diskfs/stat_unix_test.go @@ -1,4 +1,5 @@ //go:build unix + package diskfs import ( @@ -99,4 +100,4 @@ func TestChgrp(t *testing.T) { if st.Gid != oldGid { t.Fatalf("gid not restored to %v.", oldGid) } -} -\ No newline at end of file +} diff --git a/diskfs/stat_windows.go b/diskfs/stat_windows.go @@ -1,4 +1,5 @@ //go:build windows + package diskfs import ( @@ -16,7 +17,7 @@ import ( ) func fileTimeToUnixTime(t syscall.Filetime) uint32 { - return uint32(uint64(t.HighDateTime << 32 | t.LowDateTime) / 1e7 - 11644473600) + return uint32(uint64(t.HighDateTime<<32|t.LowDateTime)/1e7 - 11644473600) } // fiStat generates lib9p.Stat from the FileInfo. @@ -32,16 +33,16 @@ func fiStat(pool *QidPool, id fileID, info fs.FileInfo) (*lib9p.Stat, error) { } stat := info.Sys().(*syscall.Win32FileAttributeData) /* - // TODO: get actual file uid - usr, err := user.LookupId(strconv.Itoa(syscall.Getuid())) - if err != nil { - return nil, fmt.Errorf("user: %v", err) - } - // TODO: get actual file gid - group, err := user.LookupGroupId(strconv.Itoa(syscall.Getgid())) - if err != nil { - return nil, fmt.Errorf("group: %v", err) - } + // TODO: get actual file uid + usr, err := user.LookupId(strconv.Itoa(syscall.Getuid())) + if err != nil { + return nil, fmt.Errorf("user: %v", err) + } + // TODO: get actual file gid + group, err := user.LookupGroupId(strconv.Itoa(syscall.Getgid())) + if err != nil { + return nil, fmt.Errorf("group: %v", err) + } */ return &lib9p.Stat{ Type: 0, @@ -185,4 +186,4 @@ func chown(ospath string, uid, gid string) error { return fmt.Errorf("set owner and group: %v", err) } return nil -} -\ No newline at end of file +} diff --git a/diskfs/uid_unix.go b/diskfs/uid_unix.go @@ -1,4 +1,5 @@ //go:build unix + package diskfs import ( @@ -49,4 +50,4 @@ func isGroupMember(group, uid string) bool { } } return false -} -\ No newline at end of file +} diff --git a/doc.go b/doc.go @@ -1,2 +1,2 @@ // 9P2000 implementation. -package lib9p -\ No newline at end of file +package lib9p diff --git a/export_fs.go b/export_fs.go @@ -11,4 +11,4 @@ type ExportFS struct { func (fsys ExportFS) Open(name string) (fs.File, error) { return fsys.OpenFile(name, OREAD) -} -\ No newline at end of file +} diff --git a/export_fs_test.go b/export_fs_test.go @@ -7,4 +7,4 @@ import ( func TestInterface(t *testing.T) { var _ fs.FS = ExportFS{} -} -\ No newline at end of file +} diff --git a/export_test.go b/export_test.go @@ -34,30 +34,30 @@ var ( var ( NewTVersion = newTVersion NewRVersion = newRVersion - NewTAuth = newTAuth - NewRAuth = newRAuth - NewTAttach = newTAttach - NewRAttach = newRAttach - NewTFlush = newTFlush - NewRFlush = newRFlush - NewTWalk = newTWalk - NewRWalk = newRWalk - NewTOpen = newTOpen - NewROpen = newROpen - NewTCreate = newTCreate - NewRCreate = newRCreate - NewTRead = newTRead - NewRRead = newRRead - NewTWrite = newTWrite - NewRWrite = newRWrite - NewTClunk = newTClunk - NewRClunk = newRClunk - NewTRemove = newTRemove - NewRRemove = newRRemove - NewTStat = newTStat - NewRStat = newRStat - NewTWStat = newTWStat - NewRWStat = newRWStat + NewTAuth = newTAuth + NewRAuth = newRAuth + NewTAttach = newTAttach + NewRAttach = newRAttach + NewTFlush = newTFlush + NewRFlush = newRFlush + NewTWalk = newTWalk + NewRWalk = newRWalk + NewTOpen = newTOpen + NewROpen = newROpen + NewTCreate = newTCreate + NewRCreate = newRCreate + NewTRead = newTRead + NewRRead = newRRead + NewTWrite = newTWrite + NewRWrite = newRWrite + NewTClunk = newTClunk + NewRClunk = newRClunk + NewTRemove = newTRemove + NewRRemove = newRRemove + NewTStat = newTStat + NewRStat = newRStat + NewTWStat = newTWStat + NewRWStat = newRWStat ) type BufMsg = bufMsg diff --git a/fcall.go b/fcall.go @@ -196,10 +196,10 @@ func newRVersion(buf []byte) *RVersion { msg.Version = string(buf[13 : 13+vs]) return msg } -func (msg *RVersion) Size() uint32 { return uint32(4 + 1 + 2 + 4 + 2 + len(msg.Version)) } -func (msg *RVersion) Type() MsgType { return Rversion } -func (msg *RVersion) GetTag() uint16 { return msg.Tag } -func (msg *RVersion) SetTag(t uint16) { msg.Tag = t } +func (msg *RVersion) Size() uint32 { return uint32(4 + 1 + 2 + 4 + 2 + len(msg.Version)) } +func (msg *RVersion) Type() MsgType { return Rversion } +func (msg *RVersion) GetTag() uint16 { return msg.Tag } +func (msg *RVersion) SetTag(t uint16) { msg.Tag = t } func (msg *RVersion) marshal() []byte { cur := 0 buf := make([]byte, msg.Size()) @@ -540,7 +540,7 @@ func newRFlush(buf []byte) *RFlush { func (msg *RFlush) Size() uint32 { return 7 } func (msg *RFlush) Type() MsgType { return Rflush } -func (msg *RFlush) GetTag() uint16 { return msg.Tag } +func (msg *RFlush) GetTag() uint16 { return msg.Tag } func (msg *RFlush) SetTag(t uint16) { msg.Tag = t } func (msg *RFlush) marshal() []byte { buf := make([]byte, msg.Size()) diff --git a/file_test.go b/file_test.go @@ -41,4 +41,4 @@ func BenchmarkRead(b *testing.B) { b.Fatalf("read: %v", err) } } -} -\ No newline at end of file +} diff --git a/iofs/file.go b/iofs/file.go @@ -2,7 +2,7 @@ package iofs import ( "fmt" -// "io" + // "io" "io/fs" "path" @@ -41,7 +41,7 @@ func (f *File) Stat() (fs.FileInfo, error) { stat.Length = fsfi.Size() // TODO: fix this work around. // should preserve permission bits - stat.Mode = (fsfi.Mode()&^fs.ModePerm)|0555 + stat.Mode = (fsfi.Mode() &^ fs.ModePerm) | 0555 stat.Name = fsfi.Name() stat.Atime = uint32(fsfi.ModTime().Unix()) stat.Mtime = uint32(fsfi.ModTime().Unix()) diff --git a/iofs/fs.go b/iofs/fs.go @@ -52,4 +52,4 @@ func (fsys *FS) IsGroupLeader(group, uid string) bool { // only the member of group has the same name as group itself. func (fsys *FS) IsGroupMember(group, uid string) bool { return group == uid -} -\ No newline at end of file +} diff --git a/iofs/stat.go b/iofs/stat.go @@ -6,12 +6,13 @@ import ( "git.mtkn.jp/lib9p" ) -func fiType(fi fs.FileInfo) uint16 { return 0 } -func fiDev(fi fs.FileInfo) uint32 { return 0 } -func fiLength(fi fs.FileInfo) int64 { return fi.Size() } - // TODO: fix this work around. - // should preserve permission bits -func fiMode(fi fs.FileInfo) lib9p.FileMode { return (fi.Mode()&^fs.ModePerm)|0555 } +func fiType(fi fs.FileInfo) uint16 { return 0 } +func fiDev(fi fs.FileInfo) uint32 { return 0 } +func fiLength(fi fs.FileInfo) int64 { return fi.Size() } + +// TODO: fix this work around. +// should preserve permission bits +func fiMode(fi fs.FileInfo) lib9p.FileMode { return (fi.Mode() &^ fs.ModePerm) | 0555 } func fiName(fi fs.FileInfo) string { return fi.Name() } func fiAtime(fi fs.FileInfo) uint32 { return uint32(fi.ModTime().Unix()) } func fiMtime(fi fs.FileInfo) uint32 { return uint32(fi.ModTime().Unix()) } diff --git a/parse.go b/parse.go @@ -22,7 +22,7 @@ func readMsg(r io.Reader) ([]byte, error) { } size := bufMsg(buf).Size() mbuf := make([]byte, size-4) - for read = 0; read < int(size) - 4; { + for read = 0; read < int(size)-4; { n, err := r.Read(mbuf[read:]) if err != nil { return buf, fmt.Errorf("read body: %v", err) diff --git a/qid.go b/qid.go @@ -21,8 +21,8 @@ const ( // Qid is the identifier of each file in 9P server. type Qid struct { Type QidType // type of the file. - Vers uint32 // version of the file. - Path uint64 // uniq number of each file. + Vers uint32 // version of the file. + Path uint64 // uniq number of each file. } // unmarshalQid converts a byte array of Qid defined by 9P protocol diff --git a/req.go b/req.go @@ -7,17 +7,17 @@ import ( // Req represents each requests. type Req struct { - Tag uint16 - Srv *Server - Ifcall Msg - Ofcall Msg - Fid *Fid - Afid *Fid - Oldreq *Req - pool *ReqPool - Cancel context.CancelFunc + Tag uint16 + Srv *Server + Ifcall Msg + Ofcall Msg + Fid *Fid + Afid *Fid + Oldreq *Req + pool *ReqPool + Cancel context.CancelFunc // listenErr is any error encountered while waiting for new 9P message. - listenErr error + listenErr error // speakErrChan is used to report any error encountered while sending // the response message. speakErrChan chan error @@ -54,7 +54,7 @@ func (rp *ReqPool) add(tag uint16) (*Req, error) { return nil, ErrDupTag } req := &Req{ - pool: rp, + pool: rp, speakErrChan: make(chan error), } rp.m[tag] = req diff --git a/req_test.go b/req_test.go @@ -95,4 +95,4 @@ func TestFlush(t *testing.T) { t.Logf("read data: %v, err: %v", data, err) t.Log(tPool) } -*/ -\ No newline at end of file +*/ diff --git a/server.go b/server.go @@ -32,32 +32,32 @@ func setError(r *Req, err error) { type Server struct { // If true, the server prints the transcript of the 9P session // to os.Stderr - chatty9P bool + chatty9P bool // The file system to export via 9P. - fs FS + fs FS // Maximum length in byte of 9P messages. - msize uint32 + msize uint32 // Mutex to change msize. - mSizeLock *sync.Mutex + mSizeLock *sync.Mutex // FidPool of the Server. - fPool *FidPool + fPool *FidPool // Pending Requests the server is dealing with. - rPool *ReqPool + rPool *ReqPool // The channel from which incoming requests are delivered by the // listener goroutine. - listenChan <-chan *Req + listenChan <-chan *Req // r is the io.Reader which the listener goroutine reads from. // It should be accessed only by the listener goroutine. - r io.Reader + r io.Reader // The channel to which outgoing replies are sent to the speaker // goroutine. - speakChan chan<- *Req + speakChan chan<- *Req // w is the io.Writer which the speaker goroutine writes to. // It should be accessed only by the speaker goroutine. w io.Writer @@ -127,7 +127,7 @@ func (s *Server) runListener(ctx context.Context) { // and marshalls each of them into 9P messages and writes it to w. // TODO: pass context.Context and stop the goroutine with the Context being // canceled. -func (s *Server) runSpeaker(ctx context.Context) { +func (s *Server) runSpeaker(ctx context.Context) { rc := make(chan *Req) s.speakChan = rc go func() { @@ -406,7 +406,7 @@ func sOpen(ctx context.Context, s *Server, r *Req) { var ( err error qid Qid - st fs.FileInfo + st fs.FileInfo ) if afile, ok := r.Fid.File.(*AuthFile); ok { // TODO: r.Fid.File should not be nil. @@ -858,17 +858,17 @@ func sWStat(ctx context.Context, s *Server, r *Req) { // an existing file. // but 9pfs, 9pfuse does the rename when used with `git init`. /* - children, err := fs.Glob(ExportFS{s.fs}, path.Join(parentPath, "*")) - if err != nil { - Respond(ctx, r, fmt.Errorf("glob children: %v", err)) - return - } - for _, f := range children { - if path.Base(f) == wstat.Name { - Respond(ctx, r, fmt.Errorf("file already exists")) + children, err := fs.Glob(ExportFS{s.fs}, path.Join(parentPath, "*")) + if err != nil { + Respond(ctx, r, fmt.Errorf("glob children: %v", err)) return } - } + for _, f := range children { + if path.Base(f) == wstat.Name { + Respond(ctx, r, fmt.Errorf("file already exists")) + return + } + } */ newStat.Name = wstat.Name } @@ -907,7 +907,7 @@ func sWStat(ctx context.Context, s *Server, r *Req) { // 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) { + s.fs.IsGroupLeader(wstat.Gid, r.Fid.Uid) { newStat.Gid = wstat.Gid } else { Respond(ctx, r, ErrPerm) @@ -1038,7 +1038,7 @@ func Respond(ctx context.Context, r *Req, err error) { log.Printf("req flush: %v", r.Ifcall) } select { - case err := <- r.speakErrChan: + case err := <-r.speakErrChan: // TODO: handle errors. if err != nil { log.Printf("speak: %v", err) diff --git a/server_test.go b/server_test.go @@ -105,7 +105,7 @@ func TestServer(t *testing.T) { Tag: 0, Fid: 0, Newfid: 1, - Wnames: []string{}, + Wnames: []string{}, }, &lib9p.TOpen{ Tag: 0, @@ -125,7 +125,7 @@ func TestServer(t *testing.T) { Tag: 0, Fid: 0, Newfid: 1, - Wnames: []string{"dir", "file"}, + Wnames: []string{"dir", "file"}, }, &lib9p.TOpen{ Tag: 0, diff --git a/stat.go b/stat.go @@ -182,12 +182,14 @@ func (fi *FileInfo) Size() int64 { return fi.Stat.Length } func (fi *FileInfo) Mode() fs.FileMode { return fi.Stat.Mode } func (fi *FileInfo) ModTime() time.Time { return time.Unix(int64(fi.Stat.Mtime), 0) } func (fi *FileInfo) IsDir() bool { return fi.Stat.Mode&fs.ModeDir != 0 } + // Sys returns *Stat func (fi *FileInfo) Sys() any { return &fi.Stat } + //func (fi *FileInfo) Qid() Qid { return fi.Stat.Qid } // DirEntry is returned by File.ReadDir. type DirEntry = FileInfo -func (de *DirEntry) Type() fs.FileMode { return de.Mode().Type() } +func (de *DirEntry) Type() fs.FileMode { return de.Mode().Type() } func (de *DirEntry) Info() (fs.FileInfo, error) { return de, nil }