357 lines · 6.9 KB · Text
RawDownload
.TH FCALL 2.SH NAMEFcall, convS2M, convD2M, convM2S, convM2D, fcallfmt, dirfmt, dirmodefmt, read9pmsg, statcheck, sizeS2M, sizeD2M \- interface to Plan 9 File protocol.SH SYNOPSIS.B #include <u.h>.br.B #include <libc.h>.br.br.B #include <fcall.h>.PP.Buint convS2M(Fcall *f, uchar *ap, uint nap).PP.Buint convD2M(Dir *d, uchar *ap, uint nap).PP.Buint convM2S(uchar *ap, uint nap, Fcall *f).PP.Buint convM2D(uchar *ap, uint nap, Dir *d, char *strs).PP.Bint dirfmt(Fmt*).PP.Bint fcallfmt(Fmt*).PP.Bint dirmodefmt(Fmt*).PP.Bint read9pmsg(int fd, uchar *buf, uint nbuf).PP.Bint statcheck(uchar *buf, uint nbuf).PP.Buint sizeS2M(Fcall *f).PP.Buint sizeD2M(Dir *d).SH DESCRIPTIONTheseroutines convert messages in the machine-independent format ofthe Plan 9 file protocol,9P, to and from a more convenient form,an.B Fcallstructure:.PP.EX.if n .ta 4n +6n +5n +6n +18n +4n.if t .ta \w'xxxx'u +\w'short 'u +\w'xxxx'u +\w'ushort 'u +\w'ticket[TICKETLEN]; 'u +\w'/* 'u#define MAXWELEM 16 typedefstruct Fcall{	uchar	type;	u32int	fid;	ushort	tag;	union {		struct {			u32int	msize;	/* Tversion, Rversion */			char	*version;	/* Tversion, Rversion */		};		struct {			ushort	oldtag;	/* Tflush */		};		struct {			char	*ename;	/* Rerror */		};		struct {			Qid	qid;	/* Rattach, Ropen, Rcreate */			u32int	iounit;	/* Ropen, Rcreate */		};		struct {			Qid	aqid;	/* Rauth */		};		struct {			u32int	afid;	/* Tauth, Tattach */			char	*uname;	/* Tauth, Tattach */			char	*aname;	/* Tauth, Tattach */		};		struct {			u32int	perm;	/* Tcreate */ 			char	*name;	/* Tcreate */			uchar	mode;	/* Tcreate, Topen */		};		struct {			u32int	newfid;	/* Twalk */			ushort	nwname;	/* Twalk */			char	*wname[MAXWELEM];	/* Twalk */		};		struct {			ushort	nwqid;	/* Rwalk */			Qid	wqid[MAXWELEM];	/* Rwalk */		};		struct {			vlong	offset;	/* Tread, Twrite */			u32int	count;	/* Tread, Twrite, Rread */			char	*data;	/* Twrite, Rread */		};		struct {			ushort	nstat;	/* Twstat, Rstat */			uchar	*stat;	/* Twstat, Rstat */		};	};} Fcall;.EE.EX /* these are implemented as macros */ uchar	GBIT8(uchar*)ushort	GBIT16(uchar*)ulong	GBIT32(uchar*)vlong	GBIT64(uchar*) void	PBIT8(uchar*, uchar)void	PBIT16(uchar*, ushort)void	PBIT32(uchar*, ulong)void	PBIT64(uchar*, vlong) #define	BIT8SZ	1#define	BIT16SZ	2#define	BIT32SZ	4#define	BIT64SZ	8.EE.PPThis structure is defined in.BR <fcall.h> .See section 5for a full description of 9P messages and their encoding.For all message types, the.B typefield of an.B Fcallholds one of.BR Tversion ,.BR Rversion ,.BR Tattach ,.BR Rattach ,etc. (defined in an enumerated type in.BR <fcall.h> )..B Fidis used by most messages, and.B tagis used by all messages.The other fields are used selectively by the message typesgiven in comments..PP.I ConvM2Stakes a 9P message at.I apof length.IR nap ,and uses it to fill in.B Fcallstructure.IR f .If the passed messageincluding any data for.B Twriteand.B Rreadmessagesis formatted properly,the return value is the number of bytes the message occupied in the buffer.IR ap ,which will always be less than or equal to.IR nap ;otherwise it is 0.For.B Twriteand.B Treadmessages,.B datais set to a pointer into the argument message,not a copy..PP.I ConvS2Mdoes the reverse conversion, turning.I finto a message starting at.IR ap .The length of the resulting message is returned.For.B Twriteand.B Rreadmessages,.B countbytes starting at.B dataare copied into the message..PPThe constant.B IOHDRSZis a suitable amount of buffer to reserve for storingthe 9P header;the data portion of a.B Twriteor.B Rreadwill be no more than the buffer size negotiated in the.BR Tversion/Rversionexchange, minus.BR IOHDRSZ ..PPThe routine.I sizeS2Mreturns the number of bytes required to store the machine-independent representation of the.B Fcallstructure.IR f ,including its initial 32-bit size field.In other words, it reports the number of bytes producedby a successful call to.IR convS2M ..PPAnother structure is.BR Dir ,used by the routines described in.IR stat (2)..I ConvM2Dconverts the machine-independent form starting at.I apinto.IR dand returns the length of the machine-independent encoding.The strings in the returned.B Dirstructure are stored at successive locations starting at.BR strs .Usually.B strswill point to storage immediately after the.B Diritself.It can also be a.B nilpointer, in which case the string pointers in the returned.B Dirare all.BR nil ;however, the return value still includes their length..PP.I ConvD2Mdoes the reverse translation,also returning the length of the encoding.If the buffer is too short, the return value will be.B BIT16SZand the correct size will be returned in the first.B BIT16SZbytes.(If the buffer is less that.BR BIT16SZ ,the return value is zero; therefore a correct test forcomplete packing of the message is that the return value isgreater than.BR BIT16SZ ).The macro.B GBIT16can be used to extract the correct value.The related macros with different sizes retrieve the corresponding-sized quantities..B PBIT16and its brethren place values in messages.With the exception of handling short buffers in.IR convD2M ,these macros are not usually needed except by internal routines..PPAnalogous to.IR sizeS2M ,.I sizeD2Mreturns the number of bytes required to store the machine-independent representation of the.B Dirstructure.IR d ,including its initial 16-bit size field..PPThe routine.B statcheckchecks whether the.I nbufbytes of.I bufcontain a validly formatted machine-independent.B Direntry suitable as an argument, for example, for the.B wstat(see.IR stat (2))system call.It checks that the sizes of all the elements of the the entry sum to exactly.IR nbuf ,which is a simple but effective test of validity..I Nbufand.I bufshould include the second two-byte (16-bit) length field that precedes the entry whenformatted in a 9P message (see.IR stat (5));in other words,.I nbufis 2 plus the sum of the sizes of the entry itself..I Statcheckalso verifies that the length field has the correct value (that is,.IB nbuf -2\f1).It returns.B 0for a valid entry and.B -1for an incorrectly formatted entry..PP.IR Dirfmt ,.IR fcallfmt ,and.I dirmodefmtare formatting routines, suitable for.IR fmtinstall (2).They convert.BR Dir* ,.BR Fcall* ,and.BR longvalues into string representations of the directory buffer,.B Fcallbuffer,or file mode value..I Fcallfmtassumes that.I dirfmthas been installed with format letter.L Dand.I dirmodefmtwith format letter.LR M ..PP.I Read9pmsgcalls.IR read (2)multiple times, if necessary, to read an entire 9P message into.BR buf .The return value is 0 for end of file, or -1 for error; it does not returnpartial messages..SH SOURCE.B /sys/src/libc/9sys.SH SEE ALSO.IR intro (2),.IR 9p (2),.IR stat (2),.IR intro (5)