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) // fsys is user defined *FS. go s.Serve(context.Background(), conn, conn) }
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 ModeToFlag(m OpenMode) int
- func SendMsg(msg Msg, w io.Writer) error
- type AuthFile
- type CreatorFS
- 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 RWalk
- type RWrite
- type RWstat
- 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 TWalk
- type TWrite
- type TWstat
- 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 ( // Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified. O_RDONLY int = os.O_RDONLY // open the file read-only. O_WRONLY int = os.O_WRONLY // open the file write-only. O_RDWR int = os.O_RDWR // open the file read-write. // The remaining values may be or'ed in to control behavior. O_APPEND int = os.O_APPEND // append data to the file when writing. O_CREATE int = os.O_CREATE // create a new file if none exists. O_EXCL int = os.O_EXCL // used with O_CREATE, file must not exist. O_SYNC int = os.O_SYNC // open for synchronous I/O. O_TRUNC int = os.O_TRUNC // truncate regular writable file when opened. )
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("botch") 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 ¶
func ModeToFlag ¶ added in v0.10.1
ModeToFlag converts OpenMode (OREAD etc.) to open flag (O_RDONLY etc.). ORCLOSE is not implemented and ignored silently. O_APPEND, O_CREATE, O_EXCL, O_SYNC can't be specified.
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.
func (*AuthFile) Close ¶
Close closes af and its underlying reader/writer if they implement io.Closer. TODO: should I left W/R not closed?
type CreatorFS ¶ added in v0.11.0
type CreatorFS interface { FS // Create creates a file named name. // It sets the owner of newly created file to the specified uid, // and file mode to the specified perm. // // Create should reject attempts to create names that do not satisfy // fs.ValidPath(name), returning a *fs.PathError with Err set to // fs.ErrInvalid. Create(name string, uid string, mode OpenMode, perm FileMode) (File, error) }
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 specified flag (O_RDONLY etc.). // // When OpenFile returns an error, it should be of type *fs.PathError // with the Op field set to "openfile", the Path field set to name, // and the Err field describing the problem. // // Open should reject attempts to open names that do not satisfy // fs.ValidPath(name), returning a *fs.PathError with Err set to // fs.ErrInvalid or fs.ErrNotExist. OpenFile(name string, flag int) (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. If an FS does not implement this interface, then the Server assumes that that every group is consists of only one member with the same name as the group itself and he is the leader of that 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. It serves fsys with the aname "".
func NewServerMap ¶ added in v0.9.1
NewServerMap creates a Server. Fsmap is the map of aname-FS pair.
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 TWalk ¶
type TWrite ¶
type TWstat ¶ added in v0.7.0
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. |
semfs
Semfs is a semaphore file system.
|
Semfs is a semaphore file system. |
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. |