lib9p

Go 9P library.
Log | Files | Refs

commit 5864dedab4c71643268b3d27b18d371697a35f21
parent 7f2fed2a291394f89383635ca93345ecd214bde9
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri,  1 Sep 2023 10:31:10 +0900

divide file

Diffstat:
Mfile.go | 116-------------------------------------------------------------------------------
Astat.go | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 121 insertions(+), 116 deletions(-)

diff --git a/file.go b/file.go @@ -1,125 +1,9 @@ package lib9p import ( - "fmt" "io/fs" ) -type FileMode uint32 - -const ( - DMDIR FileMode = 0x80000000 - DMAPPEND = 0x40000000 - DMEXCL = 0x20000000 - DMTMP = 0x04000000 -) - -const ( - AEXEC fs.FileMode = 1 << iota - AWRITE - AREAD -) - -type Stat struct { - Type uint16 - Dev uint32 - Qid *Qid - Mode FileMode - Atime uint32 - Mtime uint32 - Length int64 - Name string - Uid string - Gid string - Muid string -} - -func (s *Stat) size() uint16 { - // type + dev + qid + mode + atime + mtime + length - return uint16(2 + 4 + 13 + 4 + 4 + 4 + 8 + - 2 + len(s.Name) + - 2 + len(s.Uid) + - 2 + len(s.Gid) + - 2 + len(s.Muid)) -} - -func (s *Stat) marshal() []byte { - cur := 0 - size := s.size() - msg := make([]byte, 2+size) - pbit16(msg[cur:cur+2], size) - cur += 2 - pbit16(msg[cur:cur+2], s.Type) - cur += 2 - pbit32(msg[cur:cur+4], s.Dev) - cur += 4 - msg[cur] = uint8(s.Qid.Type) - cur += 1 - pbit32(msg[cur:cur+4], s.Qid.Vers) - cur += 4 - pbit64(msg[cur:cur+8], s.Qid.Path) - cur += 8 - pbit32(msg[cur:cur+4], uint32(s.Mode)) - cur += 4 - pbit32(msg[cur:cur+4], s.Atime) - cur += 4 - pbit32(msg[cur:cur+4], s.Mtime) - cur += 4 - pbit64(msg[cur:cur+8], uint64(s.Length)) - cur += 8 - pbit16(msg[cur:cur+2], uint16(len(s.Name))) - cur += 2 - for i := 0; i < len(s.Name); i++ { - msg[cur+i] = s.Name[i] - } - cur += len(s.Name) - pbit16(msg[cur:cur+2], uint16(len(s.Uid))) - cur += 2 - for i := 0; i < len(s.Uid); i++ { - msg[cur+i] = s.Uid[i] - } - cur += len(s.Uid) - pbit16(msg[cur:cur+2], uint16(len(s.Gid))) - cur += 2 - for i := 0; i < len(s.Gid); i++ { - msg[cur+i] = s.Gid[i] - } - cur += len(s.Gid) - pbit16(msg[cur:cur+2], uint16(len(s.Muid))) - cur += 2 - for i := 0; i < len(s.Muid); i++ { - msg[cur+i] = s.Muid[i] - } - cur += len(s.Muid) - - if len(msg) != cur { - panic(fmt.Errorf("cursor position %d and msg length %d don't match", - cur, len(msg))) - } - return msg -} - -func (s *Stat) String() string { - return fmt.Sprintf("'%s' '%s' '%s' '%s' q %v m %#o at %d mt %d l %d t %d d %d", - s.Name, s.Uid, s.Gid, s.Muid, s.Qid, s.Mode, - s.Atime, s.Mtime, s.Length, s.Type, s.Dev) -} - -// Sys must return Stat struct. -/* -type FileInfo struct { - Stat *Stat -} - -func (fi *FileInfo) Name() string { return fi.Stat.Name } -func (fi *FileInfo) Size() int64 { return fi.Stat.Length } -func (fi *FileInfo) Mode() fs.FileMode { return Mode9ToFSMode(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&DMDIR != 0 } -func (fi *FileInfo) Sys() any { return fi.Stat } -func (fi *FileInfo) Qid() *Qid { return fi.Sys().(*Stat).Qid } -*/ - type File interface { fs.File PathName() string diff --git a/stat.go b/stat.go @@ -0,0 +1,121 @@ +package lib9p + +import ( + "fmt" + "io/fs" +) + +type FileMode uint32 + +const ( + DMDIR FileMode = 0x80000000 + DMAPPEND = 0x40000000 + DMEXCL = 0x20000000 + DMTMP = 0x04000000 +) + +const ( + AEXEC fs.FileMode = 1 << iota + AWRITE + AREAD +) + +type Stat struct { + Type uint16 + Dev uint32 + Qid *Qid + Mode FileMode + Atime uint32 + Mtime uint32 + Length int64 + Name string + Uid string + Gid string + Muid string +} + +func (s *Stat) size() uint16 { + // type + dev + qid + mode + atime + mtime + length + return uint16(2 + 4 + 13 + 4 + 4 + 4 + 8 + + 2 + len(s.Name) + + 2 + len(s.Uid) + + 2 + len(s.Gid) + + 2 + len(s.Muid)) +} + +func (s *Stat) marshal() []byte { + cur := 0 + size := s.size() + msg := make([]byte, 2+size) + pbit16(msg[cur:cur+2], size) + cur += 2 + pbit16(msg[cur:cur+2], s.Type) + cur += 2 + pbit32(msg[cur:cur+4], s.Dev) + cur += 4 + msg[cur] = uint8(s.Qid.Type) + cur += 1 + pbit32(msg[cur:cur+4], s.Qid.Vers) + cur += 4 + pbit64(msg[cur:cur+8], s.Qid.Path) + cur += 8 + pbit32(msg[cur:cur+4], uint32(s.Mode)) + cur += 4 + pbit32(msg[cur:cur+4], s.Atime) + cur += 4 + pbit32(msg[cur:cur+4], s.Mtime) + cur += 4 + pbit64(msg[cur:cur+8], uint64(s.Length)) + cur += 8 + pbit16(msg[cur:cur+2], uint16(len(s.Name))) + cur += 2 + for i := 0; i < len(s.Name); i++ { + msg[cur+i] = s.Name[i] + } + cur += len(s.Name) + pbit16(msg[cur:cur+2], uint16(len(s.Uid))) + cur += 2 + for i := 0; i < len(s.Uid); i++ { + msg[cur+i] = s.Uid[i] + } + cur += len(s.Uid) + pbit16(msg[cur:cur+2], uint16(len(s.Gid))) + cur += 2 + for i := 0; i < len(s.Gid); i++ { + msg[cur+i] = s.Gid[i] + } + cur += len(s.Gid) + pbit16(msg[cur:cur+2], uint16(len(s.Muid))) + cur += 2 + for i := 0; i < len(s.Muid); i++ { + msg[cur+i] = s.Muid[i] + } + cur += len(s.Muid) + + if len(msg) != cur { + panic(fmt.Errorf("cursor position %d and msg length %d don't match", + cur, len(msg))) + } + return msg +} + +func (s *Stat) String() string { + return fmt.Sprintf("'%s' '%s' '%s' '%s' q %v m %#o at %d mt %d l %d t %d d %d", + s.Name, s.Uid, s.Gid, s.Muid, s.Qid, s.Mode, + s.Atime, s.Mtime, s.Length, s.Type, s.Dev) +} + +// Sys must return Stat struct. +/* +type FileInfo struct { + Stat *Stat +} + +func (fi *FileInfo) Name() string { return fi.Stat.Name } +func (fi *FileInfo) Size() int64 { return fi.Stat.Length } +func (fi *FileInfo) Mode() fs.FileMode { return Mode9ToFSMode(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&DMDIR != 0 } +func (fi *FileInfo) Sys() any { return fi.Stat } +func (fi *FileInfo) Qid() *Qid { return fi.Sys().(*Stat).Qid } +*/