Documentation ¶
Overview ¶
Package lib9p is a 9P2000 library to implement 9P servers. For detailed information on 9P protocol, consult the protocol's documentation.
To use this package, implement the FS interface, and create a Server struct by calling NewServer with the FS, and then call *Server.Serve. The server listens to r specified by the argument of NewServer and writes responses to w. It does not assume network connection, and can be used with any io.Reader/io.Writer. If the server is to serve via a tcp connection, write something like the following code:
l, err := net.Listen("tcp", "localhost:5640") if err != nil { log.Fatal(err) } for { conn, err := l.Accept() if err != nil { log.Print(err) } s := lib9p.NewServer(fsys, 8*1024, conn, conn) go s.Serve(context.Background()) }
OpenFile method of FS interface is similar to that of os.OpenFile, but it takes as argument OpenMode instead of integer flag. And it does not create file even if the specified file does not exists. The returned value is File, not fs.File.
A File is the representation of a file of the exported file system. It is almost identical to fs.File, but the Stat method returns the *FileInfo struct instead of fs.FileInfo interface. The difference of *FileInfo and fs.FileInfo is that the Sys method of *FileInfo returns the underlying *Stat struct as a value of any, while I can't asume anything on Stat of fs.FileInfo. This ensures the type safety.
Index ¶
- Constants
- Variables
- func SendMsg(msg Msg, w io.Writer) error
- type AuthFile
- type CreaterFS
- type DirEntry
- type ExportFS
- type ExportFile
- type FS
- type File
- type FileInfo
- type FileMode
- type GroupFS
- type Msg
- type MsgType
- type OpenMode
- type Qid
- type QidType
- type RAttach
- type RAuth
- type RClunk
- type RCreate
- type RError
- type RFlush
- type ROpen
- type RRead
- type RRemove
- type RStat
- type RVersion
- type RWStat
- type RWalk
- type RWrite
- type ReadDirFile
- type RemoverFS
- type Server
- type Stat
- type TAttach
- type TAuth
- type TClunk
- type TCreate
- type TFlush
- type TOpen
- type TRead
- type TRemove
- type TStat
- type TVersion
- type TWStat
- type TWalk
- type TWrite
- type WriterFile
- type WriterStatFile
Constants ¶
const ( Tversion MsgType = 100 Rversion = 101 Tauth = 102 Rauth = 103 Tattach = 104 Rattach = 105 Terror = 106 /* illegal */ Rerror = 107 Tflush = 108 Rflush = 109 Twalk = 110 Rwalk = 111 Topen = 112 Ropen = 113 Tcreate = 114 Rcreate = 115 Tread = 116 Rread = 117 Twrite = 118 Rwrite = 119 Tclunk = 120 Rclunk = 121 Tremove = 122 Rremove = 123 Tstat = 124 Rstat = 125 Twstat = 126 Rwstat = 127 Tmax = 128 )
const ( OREAD OpenMode = 0 OWRITE = 1 ORDWR = 2 OEXEC = 3 OTRUNC = 16 ORCLOSE = 64 )
const ( QTDIR QidType = 0x80 /* type bit for directories */ QTAPPEND = 0x40 /* type bit for append only files */ QTEXCL = 0x20 /* type bit for exclusive use files */ QTMOUNT = 0x10 /* type bit for mounted channel */ QTAUTH = 0x08 /* type bit for authentication file */ QTTMP = 0x04 /* type bit for non-backed-up file */ QTSYMLINK = 0x02 /* type bit for symbolic link */ QTFILE = 0x00 /* type bits for plain file */ )
These consts are used to record the access mode to a file. This is derived from plan9's lib9p, but I think this is not needed.
const IOHDRSZ = 23
IOHDRSZ is the ample room for Twrite/Rread header.
- Twrite: size[4] type[1] Tag[2] fid[4] offset[8] count[4] = 23
- Rread: size[4] type[1] Tag[2] count[4] = 11
In Plan9, this const is 24.
const NOFID = ^uint32(0)
NOFID is used in afid field of Tattach message when no authentication is required.
const NOTAG = ^uint16(0)
The Tag used by version messages. The client can use it, when establishing a connection, to override Tag matching in version messages.
Variables ¶
var ( ErrBotch = fmt.Errorf("bocchi") ErrPerm = fmt.Errorf("permission denied") ErrOperation = fmt.Errorf("operation not supported") ErrDupTag = fmt.Errorf("duplicate tag") ErrUnknownFid = fmt.Errorf("unknown fid") ErrDupFid = fmt.Errorf("duplicate fid") ErrNotFound = fmt.Errorf("not found") )
Functions ¶
Types ¶
type AuthFile ¶
An AuthFile is a file allocated by Auth function of Server struct. If authentication is desired, Auth member of Server struct must be set and it should set an AuthFile ready to Read/Write via R/W member.
type CreaterFS ¶ added in v0.3.0
type ExportFS ¶ added in v0.2.1
type ExportFS struct {
FS
}
An ExportFS is a wrapper of FS to export it as an fs.FS.
type ExportFile ¶ added in v0.5.0
type ExportFile struct {
// contains filtered or unexported fields
}
An ExportFile is a wrapper of File to export it as an fs.File.
func (ExportFile) Close ¶ added in v0.5.0
func (f ExportFile) Close() error
func (ExportFile) Read ¶ added in v0.5.0
func (f ExportFile) Read(p []byte) (n int, err error)
func (ExportFile) ReadDir ¶ added in v0.5.0
func (f ExportFile) ReadDir(n int) ([]fs.DirEntry, error)
func (ExportFile) Stat ¶ added in v0.5.0
func (f ExportFile) Stat() (fs.FileInfo, error)
type FS ¶
type FS interface { // OpenFile opens file named name with omode. OpenFile(name string, omode OpenMode) (File, error) }
An FS is an file system to be exported by 9P server.
type File ¶
A File is an open file. The interface is similar to that of fs.File, but Stat returns *FileInfo of this package instead of fs.FileInfo.
type FileInfo ¶
type FileInfo struct {
Stat Stat
}
A FileInfo is a struct returned by File.Stat(). This struct is needed because of the name collision: Stat.Name and FileInfo.Name.
type GroupFS ¶ added in v0.6.0
type GroupFS interface { FS // IsGroupLeader reports whether uid is the leader of group. IsGroupLeader(group, uid string) bool // IsGroupMember reports whether uid is a member of group. IsGroupMember(group, uid string) bool }
A GroupFS is an file system with the notion of group.
type Msg ¶
type Msg interface { // Size returns the size field of message. // Size field holds the size of the message in bytes // including the 4-byte size field itself. Size() uint32 // Type returns the type field of message. Type() MsgType // GetTag returns the Tag of message. // Tag is the identifier of each message. // The Get prefix is to avoid name confliction with the each // message's Tag field. GetTag() uint16 // SetTag sets the Tag field of the message. SetTag(uint16) String() string // contains filtered or unexported methods }
A Msg represents any kind of message of 9P. It defines methods for common fields. For each message type <T>, new<T>([]byte) function parses the byte array of 9P message into the corresponding message struct. For detailed information on each message, consult the documentation of 9P protocol.
type OpenMode ¶
type OpenMode int8
An OpenMode is the mode of an open file. This is used by fid structure. If the file is not open, the mode must be -1.
type Qid ¶
type Qid struct { Type QidType // type of the file. Vers uint32 // version of the file. Path uint64 // uniq number of each file. }
Qid is the identifier of each file in 9P server.
type RAttach ¶
type RAuth ¶
type RCreate ¶
type RError ¶
type ROpen ¶
type RRead ¶
type RStat ¶
type RVersion ¶
type RWalk ¶
type RWrite ¶
type ReadDirFile ¶
A ReadDirFile is a directory. Every directory file shoul implement this interface. Non-directory files can implement this. In this case, ReadDir should return an error.
type Server ¶
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. // 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) // contains filtered or unexported fields }
A Server is a 9P server.
func NewServer ¶
NewServer creates a Server and runs listener and responder goroutines. It reads incoming messages from r and writes responses to w.
func (*Server) Chatty ¶
func (s *Server) Chatty()
Chatty enables the server's log messages of 9P messages.
func (*Server) Serve ¶
Serve serves 9P conversation. TODO: If the user of the library make a Server for each connection to the same filesystem, those Servers can access the same file concurrentry. So some additional struct to represent each connection is needed. And Serve method should be attached to that struct.
type Stat ¶
type Stat struct { Type uint16 Dev uint32 Qid Qid Mode FileMode Atime uint32 Mtime uint32 //TODO: In 9P protocol Length is unsigned integer. Length int64 Name string Uid string Gid string Muid string }
Stat represents the stat defined by 9P.
func NewStat ¶ added in v0.3.0
NewStat converts a byte array of stat from a 9P message into Stat struct.
func (*Stat) Size ¶ added in v0.3.0
Size returns the size of marshaled stat in bytes excluding the size field itself.
type TAttach ¶
type TAuth ¶
type TClunk ¶
type TCreate ¶
type TFlush ¶
type TOpen ¶
type TRead ¶
type TRemove ¶
type TStat ¶
type TVersion ¶
type TWStat ¶
type TWalk ¶
type TWrite ¶
type WriterFile ¶
A WriterFile is a file with Write method. If the server is to accept Twrite messages, files in this server must implement this interface.
type WriterStatFile ¶
type WriterStatFile interface { File // WStat sets file Stat to stat. // After successful call, the file's Stat() method should return // the same Stat as stat. // If there is an error, file's status must remain the same as before. WStat(stat *Stat) error }
A WriterStatFile is a file which can modify its attributes such as the permission and the group (Note that changing ownership of a file is prohibited by the protocol).
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
diskfs
Diskfs exports the file system on the disk.
|
Diskfs exports the file system on the disk. |
iofs
Iofs exports the file system on the disk.
|
Iofs exports the file system on the disk. |
Package diskfs provides a glue layer between OS's file system and 9P file system.
|
Package diskfs provides a glue layer between OS's file system and 9P file system. |
Package iofs provides a glue layer between fs.FS and lib9p.FS The resulting file system is readonly.
|
Package iofs provides a glue layer between fs.FS and lib9p.FS The resulting file system is readonly. |