lib9p

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2023 License: 0BSD Imports: 11 Imported by: 0

Documentation

Overview

9P2000 implementation.

Index

Constants

View Source
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
)
View Source
const (
	OREAD  OpenMode = 0
	OWRITE          = 1
	ORDWR           = 2
	OEXEC           = 3

	OTRUNC  = 16
	ORCLOSE = 64
)

-1 = not open

View Source
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 */
)
View Source
const (
	AEXEC fs.FileMode = 1 << iota
	AWRITE
	AREAD
)
View Source
const IOHDRSZ = 23

Ample room for Twrite/Rread header (iounit). 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.

View Source
const NOFID = ^uint32(0)
View Source
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

View Source
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

func Respond added in v0.1.0

func Respond(ctx context.Context, r *Req, err error)

Respond Responds to the request r with the message r.Ofcall if err is nil, or if err is not nil, with the Rerror with the error message. If r is nil, or both r.Ofcall and err are nil it panics.

func SendMsg added in v0.1.0

func SendMsg(msg Msg, w io.Writer) error

SendMsg send a 9P message to w

Types

type AuthFile

type AuthFile struct {
	Qid    Qid
	Uname  string
	Aname  string
	AuthOK bool
	W      io.Writer
	R      io.Reader
}

AuthFile is a file allocated by Tauth messages.

func (*AuthFile) Close

func (af *AuthFile) Close() error

func (*AuthFile) Read

func (af *AuthFile) Read(p []byte) (int, error)

func (*AuthFile) Stat

func (af *AuthFile) Stat() (fs.FileInfo, error)

func (*AuthFile) Write

func (af *AuthFile) Write(p []byte) (int, error)

type CreaterFile

type CreaterFile interface {
	File
	// Create creates a file named name in this directory.
	// It sets the owner of newly created file to the specified uid,
	// and file mode to the specified perm.
	Create(name string, uid string, mode OpenMode, perm FileMode) (File, error)
}

TODO: Delete this. Create should be implemented by using FS.OpenFile.

type DirEntry

type DirEntry = FileInfo

DirEntry is returned by File.ReadDir.

func (*DirEntry) Info

func (de *DirEntry) Info() (fs.FileInfo, error)

func (*DirEntry) Type added in v0.2.0

func (de *DirEntry) Type() fs.FileMode

type FS

type FS interface {
	// OpenFile opens file named name with omode.
	// If the file does not exists, it creates it with FileMode perm.
	OpenFile(name string, omode OpenMode, perm fs.FileMode) (File, error)
}

FS is an file system to be exported by 9P server.

type Fid

type Fid struct {
	Fid   uint32
	OMode OpenMode /* -1 = not open */

	File File   // The associated File.
	Uid  string // The user id derived from the attach message.
	// contains filtered or unexported fields
}

Fid represents the fid defined by 9P

func (*Fid) String

func (f *Fid) String() string

type FidPool

type FidPool struct {
	// contains filtered or unexported fields
}

FidPool is a pool of Fid. It is used to track the fid in the file system the server is exporting.

func (*FidPool) String

func (pool *FidPool) String() string

type File

type File interface {
	Stat() (fs.FileInfo, error)
	Close() error
	io.Reader
}

File is an open file.

type FileInfo

type FileInfo struct {
	Stat Stat
}

FileInfo is a struct returned by File.Stat(). This struct is needed because of the name collision: Stat.Name and FileInfo.Name.

func (*FileInfo) IsDir

func (fi *FileInfo) IsDir() bool

func (*FileInfo) ModTime

func (fi *FileInfo) ModTime() time.Time

func (*FileInfo) Mode

func (fi *FileInfo) Mode() fs.FileMode

func (*FileInfo) Name

func (fi *FileInfo) Name() string

func (*FileInfo) Size

func (fi *FileInfo) Size() int64

func (*FileInfo) Sys

func (fi *FileInfo) Sys() any

Sys returns *Stat

type FileMode

type FileMode = fs.FileMode

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
}

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.

func RecvMsg added in v0.1.0

func RecvMsg(r io.Reader) (Msg, error)

RecvMsg recievs a 9P message from r.

type MsgType

type MsgType uint8

Message types defined by 9P.

type OpenMode

type OpenMode int8 // defined by 9P

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.

func (Qid) String

func (q Qid) String() string

type QidType

type QidType uint8

QidType represents the type of Qid.

func FSModeToQidType

func FSModeToQidType(fm fs.FileMode) QidType

type RAttach

type RAttach struct {
	Tag uint16
	Qid Qid
}

func (*RAttach) GetTag added in v0.1.0

func (msg *RAttach) GetTag() uint16

func (*RAttach) SetTag

func (msg *RAttach) SetTag(t uint16)

func (*RAttach) Size

func (msg *RAttach) Size() uint32

func (*RAttach) String

func (msg *RAttach) String() string

func (*RAttach) Type

func (msg *RAttach) Type() MsgType

type RAuth

type RAuth struct {
	Tag  uint16
	Aqid Qid
}

func (*RAuth) GetTag added in v0.1.0

func (msg *RAuth) GetTag() uint16

func (*RAuth) SetTag

func (msg *RAuth) SetTag(t uint16)

func (*RAuth) Size

func (msg *RAuth) Size() uint32

func (*RAuth) String

func (msg *RAuth) String() string

func (*RAuth) Type

func (msg *RAuth) Type() MsgType

type RClunk

type RClunk struct {
	Tag uint16
}

func (*RClunk) GetTag added in v0.1.0

func (msg *RClunk) GetTag() uint16

func (*RClunk) SetTag

func (msg *RClunk) SetTag(t uint16)

func (*RClunk) Size

func (msg *RClunk) Size() uint32

func (*RClunk) String

func (msg *RClunk) String() string

func (*RClunk) Type

func (msg *RClunk) Type() MsgType

type RCreate

type RCreate struct {
	Tag    uint16
	Qid    Qid
	Iounit uint32
}

func (*RCreate) GetTag added in v0.1.0

func (msg *RCreate) GetTag() uint16

func (*RCreate) SetTag

func (msg *RCreate) SetTag(t uint16)

func (*RCreate) Size

func (msg *RCreate) Size() uint32

func (*RCreate) String

func (msg *RCreate) String() string

func (*RCreate) Type

func (msg *RCreate) Type() MsgType

type RError

type RError struct {
	Tag   uint16
	Ename error
}

func (*RError) GetTag added in v0.1.0

func (msg *RError) GetTag() uint16

func (*RError) SetTag

func (msg *RError) SetTag(t uint16)

func (*RError) Size

func (msg *RError) Size() uint32

func (*RError) String

func (msg *RError) String() string

func (*RError) Type

func (msg *RError) Type() MsgType

type RFlush

type RFlush struct {
	Tag uint16
}

func (*RFlush) GetTag added in v0.1.0

func (msg *RFlush) GetTag() uint16

func (*RFlush) SetTag

func (msg *RFlush) SetTag(t uint16)

func (*RFlush) Size

func (msg *RFlush) Size() uint32

func (*RFlush) String

func (msg *RFlush) String() string

func (*RFlush) Type

func (msg *RFlush) Type() MsgType

type ROpen

type ROpen struct {
	Tag    uint16
	Qid    Qid
	Iounit uint32
}

func (*ROpen) GetTag added in v0.1.0

func (msg *ROpen) GetTag() uint16

func (*ROpen) SetTag

func (msg *ROpen) SetTag(t uint16)

func (*ROpen) Size

func (msg *ROpen) Size() uint32

func (*ROpen) String

func (msg *ROpen) String() string

func (*ROpen) Type

func (msg *ROpen) Type() MsgType

type RRead

type RRead struct {
	Tag   uint16
	Count uint32
	Data  []byte
}

func (*RRead) GetTag added in v0.1.0

func (msg *RRead) GetTag() uint16

func (*RRead) SetTag

func (msg *RRead) SetTag(t uint16)

func (*RRead) Size

func (msg *RRead) Size() uint32

func (*RRead) String

func (msg *RRead) String() string

func (*RRead) Type

func (msg *RRead) Type() MsgType

type RRemove

type RRemove struct {
	Tag uint16
}

func (*RRemove) GetTag added in v0.1.0

func (msg *RRemove) GetTag() uint16

func (*RRemove) SetTag

func (msg *RRemove) SetTag(t uint16)

func (*RRemove) Size

func (msg *RRemove) Size() uint32

func (*RRemove) String

func (msg *RRemove) String() string

func (*RRemove) Type

func (msg *RRemove) Type() MsgType

type RStat

type RStat struct {
	Tag  uint16
	Stat *Stat
}

func (*RStat) GetTag added in v0.1.0

func (msg *RStat) GetTag() uint16

func (*RStat) SetTag

func (msg *RStat) SetTag(t uint16)

func (*RStat) Size

func (msg *RStat) Size() uint32

func (*RStat) String

func (msg *RStat) String() string

func (*RStat) Type

func (msg *RStat) Type() MsgType

type RVersion

type RVersion struct {
	Tag     uint16
	Msize   uint32
	Version string
}

func (*RVersion) GetTag added in v0.1.0

func (msg *RVersion) GetTag() uint16

func (*RVersion) SetTag

func (msg *RVersion) SetTag(t uint16)

func (*RVersion) Size

func (msg *RVersion) Size() uint32

func (*RVersion) String

func (msg *RVersion) String() string

func (*RVersion) Type

func (msg *RVersion) Type() MsgType

type RWStat

type RWStat struct {
	Tag uint16
}

func (*RWStat) GetTag added in v0.1.0

func (msg *RWStat) GetTag() uint16

func (*RWStat) SetTag

func (msg *RWStat) SetTag(t uint16)

func (*RWStat) Size

func (msg *RWStat) Size() uint32

func (*RWStat) String

func (msg *RWStat) String() string

func (*RWStat) Type

func (msg *RWStat) Type() MsgType

type RWalk

type RWalk struct {
	Tag  uint16
	Qids []Qid
}

func (*RWalk) GetTag added in v0.1.0

func (msg *RWalk) GetTag() uint16

func (*RWalk) SetTag

func (msg *RWalk) SetTag(t uint16)

func (*RWalk) Size

func (msg *RWalk) Size() uint32

func (*RWalk) String

func (msg *RWalk) String() string

func (*RWalk) Type

func (msg *RWalk) Type() MsgType

type RWrite

type RWrite struct {
	Tag   uint16
	Count uint32
}

func (*RWrite) GetTag added in v0.1.0

func (msg *RWrite) GetTag() uint16

func (*RWrite) SetTag

func (msg *RWrite) SetTag(t uint16)

func (*RWrite) Size

func (msg *RWrite) Size() uint32

func (*RWrite) String

func (msg *RWrite) String() string

func (*RWrite) Type

func (msg *RWrite) Type() MsgType

type ReadDirFile

type ReadDirFile interface {
	File
	ReadDir(n int) ([]fs.DirEntry, error)
}

ReadDirFile is a directory. Note that non-directory file can implement this. In this case, ReadDir should return an error.

type RemoverFile

type RemoverFile interface {
	File
	Remove() error
}

TODO: I think Remove should be implemented by the FS's method.

type Req

type Req struct {
	Tag    uint16
	Srv    *Server
	Ifcall Msg
	Ofcall Msg
	Fid    *Fid
	Afid   *Fid
	Oldreq *Req

	Cancel context.CancelFunc
	// contains filtered or unexported fields
}

Req represents each requests.

type ReqPool

type ReqPool struct {
	// contains filtered or unexported fields
}

ReqPool is the pool of Reqs the server is dealing with.

type Server

type Server struct {

	// Auth is called when Tauth message arrives.
	// If authentication is desired, the Auth functions should
	// set Req.Afid.Qid and Req.Ofcall.Qid.
	// If this is nil, no authentication is performed.
	Auth func(context.Context, *Req)
	// contains filtered or unexported fields
}

Server is a 9P server

func NewServer

func NewServer(fsys FS, mSize uint32, r io.Reader, w io.Writer) *Server

NewServer creates a Server and runs listener and speaker 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

func (s *Server) Serve(ctx context.Context)

Serve serves 9P conversation.

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 (*Stat) String

func (s *Stat) String() string

type TAttach

type TAttach struct {
	Tag   uint16
	Fid   uint32
	Afid  uint32
	Uname string
	Aname string
}

func (*TAttach) GetTag added in v0.1.0

func (msg *TAttach) GetTag() uint16

func (*TAttach) SetTag

func (msg *TAttach) SetTag(t uint16)

func (*TAttach) Size

func (msg *TAttach) Size() uint32

func (*TAttach) String

func (msg *TAttach) String() string

func (*TAttach) Type

func (msg *TAttach) Type() MsgType

type TAuth

type TAuth struct {
	Tag   uint16
	Afid  uint32
	Uname string
	Aname string
}

func (*TAuth) GetTag added in v0.1.0

func (msg *TAuth) GetTag() uint16

func (*TAuth) SetTag

func (msg *TAuth) SetTag(t uint16)

func (*TAuth) Size

func (msg *TAuth) Size() uint32

func (*TAuth) String

func (msg *TAuth) String() string

func (*TAuth) Type

func (msg *TAuth) Type() MsgType

type TClunk

type TClunk struct {
	Tag uint16
	Fid uint32
}

func (*TClunk) GetTag added in v0.1.0

func (msg *TClunk) GetTag() uint16

func (*TClunk) SetTag

func (msg *TClunk) SetTag(t uint16)

func (*TClunk) Size

func (msg *TClunk) Size() uint32

func (*TClunk) String

func (msg *TClunk) String() string

func (*TClunk) Type

func (msg *TClunk) Type() MsgType

type TCreate

type TCreate struct {
	Tag  uint16
	Fid  uint32
	Name string
	Perm FileMode
	Mode OpenMode
}

func (*TCreate) GetTag added in v0.1.0

func (msg *TCreate) GetTag() uint16

func (*TCreate) SetTag

func (msg *TCreate) SetTag(t uint16)

func (*TCreate) Size

func (msg *TCreate) Size() uint32

func (*TCreate) String

func (msg *TCreate) String() string

func (*TCreate) Type

func (msg *TCreate) Type() MsgType

type TFlush

type TFlush struct {
	Tag    uint16
	Oldtag uint16
}

func (*TFlush) GetTag added in v0.1.0

func (msg *TFlush) GetTag() uint16

func (*TFlush) SetTag

func (msg *TFlush) SetTag(t uint16)

func (*TFlush) Size

func (msg *TFlush) Size() uint32

func (*TFlush) String

func (msg *TFlush) String() string

func (*TFlush) Type

func (msg *TFlush) Type() MsgType

type TOpen

type TOpen struct {
	Tag  uint16
	Fid  uint32
	Mode OpenMode
}

func (*TOpen) GetTag added in v0.1.0

func (msg *TOpen) GetTag() uint16

func (*TOpen) SetTag

func (msg *TOpen) SetTag(t uint16)

func (*TOpen) Size

func (msg *TOpen) Size() uint32

func (*TOpen) String

func (msg *TOpen) String() string

func (*TOpen) Type

func (msg *TOpen) Type() MsgType

type TRead

type TRead struct {
	Tag    uint16
	Fid    uint32
	Offset uint64
	Count  uint32
}

func (*TRead) GetTag added in v0.1.0

func (msg *TRead) GetTag() uint16

func (*TRead) SetTag

func (msg *TRead) SetTag(t uint16)

func (*TRead) Size

func (msg *TRead) Size() uint32

func (*TRead) String

func (msg *TRead) String() string

func (*TRead) Type

func (msg *TRead) Type() MsgType

type TRemove

type TRemove struct {
	Tag uint16
	Fid uint32
}

func (*TRemove) GetTag added in v0.1.0

func (msg *TRemove) GetTag() uint16

func (*TRemove) SetTag

func (msg *TRemove) SetTag(t uint16)

func (*TRemove) Size

func (msg *TRemove) Size() uint32

func (*TRemove) String

func (msg *TRemove) String() string

func (*TRemove) Type

func (msg *TRemove) Type() MsgType

type TStat

type TStat struct {
	Tag uint16
	Fid uint32
}

func (*TStat) GetTag added in v0.1.0

func (msg *TStat) GetTag() uint16

func (*TStat) SetTag

func (msg *TStat) SetTag(t uint16)

func (*TStat) Size

func (msg *TStat) Size() uint32

func (*TStat) String

func (msg *TStat) String() string

func (*TStat) Type

func (msg *TStat) Type() MsgType

type TVersion

type TVersion struct {
	Tag     uint16
	Msize   uint32
	Version string
}

func (*TVersion) GetTag added in v0.1.0

func (msg *TVersion) GetTag() uint16

func (*TVersion) SetTag

func (msg *TVersion) SetTag(t uint16)

func (*TVersion) Size

func (msg *TVersion) Size() uint32

func (*TVersion) String

func (msg *TVersion) String() string

func (*TVersion) Type

func (msg *TVersion) Type() MsgType

type TWStat

type TWStat struct {
	Tag  uint16
	Fid  uint32
	Stat *Stat
}

func (*TWStat) GetTag added in v0.1.0

func (msg *TWStat) GetTag() uint16

func (*TWStat) SetTag

func (msg *TWStat) SetTag(t uint16)

func (*TWStat) Size

func (msg *TWStat) Size() uint32

func (*TWStat) String

func (msg *TWStat) String() string

func (*TWStat) Type

func (msg *TWStat) Type() MsgType

type TWalk

type TWalk struct {
	Tag    uint16
	Fid    uint32
	Newfid uint32
	Wnames []string
}

func (*TWalk) GetTag added in v0.1.0

func (msg *TWalk) GetTag() uint16

func (*TWalk) SetTag

func (msg *TWalk) SetTag(t uint16)

func (*TWalk) Size

func (msg *TWalk) Size() uint32

func (*TWalk) String

func (msg *TWalk) String() string

func (*TWalk) Type

func (msg *TWalk) Type() MsgType

type TWrite

type TWrite struct {
	Tag    uint16
	Fid    uint32
	Offset uint64
	Count  uint32
	Data   []byte
}

func (*TWrite) GetTag added in v0.1.0

func (msg *TWrite) GetTag() uint16

func (*TWrite) SetTag

func (msg *TWrite) SetTag(t uint16)

func (*TWrite) Size

func (msg *TWrite) Size() uint32

func (*TWrite) String

func (msg *TWrite) String() string

func (*TWrite) Type

func (msg *TWrite) Type() MsgType

type WriterFile

type WriterFile interface {
	File
	io.Writer
}

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
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL