326 lines · 6.7 KB · Text
RawDownload
.TH STAT 2.SH NAMEstat, fstat, wstat, fwstat, dirstat, dirfstat, dirwstat, dirfwstat, nulldir \- get and put file status.SH SYNOPSIS.B #include <u.h>.br.B #include <libc.h>.PP.Bint stat(char *name, uchar *edir, int nedir).PP.Bint fstat(int fd, uchar *edir, int nedir).PP.Bint wstat(char *name, uchar *edir, int nedir).PP.Bint fwstat(int fd, uchar *edir, int nedir).PP.BDir* dirstat(char *name).PP.BDir* dirfstat(int fd).PP.Bint dirwstat(char *name, Dir *dir).PP.Bint dirfwstat(int fd, Dir *dir).PP.Bvoid nulldir(Dir *d).SH DESCRIPTIONGiven a file's.IR name ,or an open file descriptor.IR fd ,these routines retrieve or modify file status information..IR Stat ,.IR fstat ,.IR wstat ,and.I fwstatare the system calls; they deal with machine-independent.IR "directory entries" .Their format is defined by.IR stat (5)..I Statand.I fstatretrieve information about.I nameor.I fdinto.IR edir ,a buffer of length.IR nedir ,defined in.BR <libc.h> ..I Wstatand.I fwstatwrite information back, thus changing file attributes according to the contents of.IR edir .The data returned from the kernel includes its leading 16-bit length fieldas described in.IR intro (5).For symmetry, this field must also be present when passing data to the kernel in a call to.I wstatand.IR fwstat ,but its value is ignored..PP.IR Dirstat ,.IR dirfstat ,.IR dirwstat ,and.I dirfwstatare similar to their counterparts, except that theyoperate on.I Dirstructures:.IP.EX.ta 6n +\w'ulong 'u +\w'mtime;   'utypedefstruct Dir {	/* system-modified data */	uint	type;	/* server type */	uint	dev;	/* server subtype */	/* file data */	Qid	qid;	/* unique id from server */	ulong	mode;	/* permissions */	ulong	atime;	/* last read time */	ulong	mtime;	/* last write time */	vlong	length;		/* file length: see <u.h> */	char	*name;	/* last element of path */	char	*uid;	/* owner name */	char	*gid;	/* group name */	char	*muid;	/* last modifier name */} Dir;.EE.PPThe returned structure is allocated by.IR malloc (2);freeing it also frees the associated strings..PPThis structure andthe.B Qidstructureare defined in.BR <libc.h> .If the file resides on permanent storage and is not a directory,the length returned by.I statis the number of bytes in the file.For directories, the length returned is zero.For files that are streams (e.g., pipes and network connections),the length is the number of bytes that can be read without blocking..PPEach file is the responsibility of some.IR server :it could be a file server, a kernel device, or a user process..B Typeidentifies the server type, and.B devsays which of a group of servers of the same type is the oneresponsible for this file..B Qidis a structure containing.B pathand.B versfields:.B pathis guaranteed to beunique among all path names currently on the file server, and.B verschanges each time the file is modified.The.B pathis a.B long.B long(64 bits,.BR vlong )and the.B versis an.B unsigned long(32 bits,.BR ulong ).Thus, if two files have the same.BR type ,.BR dev ,and.B qidthey are the same file..PPThe bits in.B modeare defined by.PP.ta 6n +\w'\fL0x80000000   'u.nf\fL	0x80000000\fP	directory\fL	0x40000000\fP	append only\fL	0x20000000\fP	exclusive use (locked) \fL	      0400\fP	read permission by owner\fL	      0200\fP	write permission by owner\fL	      0100\fP	execute permission (search on directory) by owner\fL	      0070\fP	read, write, execute (search) by group\fL	      0007\fP	read, write, execute (search) by others.fi.PPThere are constants defined in.B <libc.h>for these bits:.BR DMDIR ,.BR DMAPPEND ,and.B DMEXCLfor the first three; and.BR DMREAD ,.BR DMWRITE ,and.B DMEXECfor the read, write, and execute bits for others..PPThe two time fields are measured in seconds since the epoch(Jan 1 00:00 1970 GMT)..B Mtimeis the time of the last change of content.Similarly,.B atimeis set whenever the contents are accessed;also, it is set whenever.B mtimeis set..PP.B Uidand.B gidare the names of the owner and group of the file;.B muidis the name of the user that last modified the file (setting.BR mtime ).Groups are also users, but each server is free to associatea list of users with any user name.IR g ,and that list is theset of users in the group.IR g .When an initial attachment is made to a server,the user string in the process group is communicated to the server.Thus, the server knows, for any given file access, whether the accessingprocess is the owner of, or in the group of, the file.This selects which sets of three bits in.B modeis used to check permissions..PPOnly some of the fields may be changed with the.I wstatcalls.The.B namecan be changed by anyone with write permission in the parent directory.The.B modeand.B mtimecan be changed by the owner or the group leader of the file's currentgroup.The.I gidcan be changed: by the owner if also a member of the new group; orby the group leader of the file's current groupif also leader of the new group(see.IR intro (5)for more information about permissions and.IR users (6)for users and groups).The.B lengthcan be changed by anyone with write permission, provided the operationis implemented by the server.(See.IR intro (5)for permission information, and.IR users (6)for user and group information)..PPSpecial values in the fields of the.B Dirpassed to.I wstatindicate that the field is not intended to be changed by the call.The values are the maximum unsigned integer of appropriate sizefor integral values (usually.BR ~0 ,but beware of conversions and size mismatcheswhen comparing values) and the empty or nil string for string values.The routine.I nulldirinitializes all the elements of.I dto these ``don't care'' values.Thus one may change the mode, for example, by using.I nulldirto initialize a.BR Dir ,then setting the mode, and then doing.IR wstat ;it is not necessary to use.I statto retrieve the initial values first..SH SOURCE.TF /sys/src/libc/9syscall.TP.B /sys/src/libc/9syscallfor the.RB non- dirroutines.TP.B /sys/src/libc/9sysfor the routines prefixed.B dir.SH SEE ALSO.IR intro (2),.IR fcall (2),.IR dirread (2),.IR stat (5).SH DIAGNOSTICSThe.I dirfunctions return a pointer to the data for a successful call, or.B nilon error.The othersreturn the number of bytes copied on success, or \-1 on error.All set.IR errstr ..PPIf the buffer for.I stator.I fstatis too short for the returned data, the return value will be.B BIT16SZ(see.IR fcall (2))and the two bytesreturned will contain the initial count field of thereturned data;retrying with.B nedirequal tothat value plus.B BIT16SZ(for the count itself) should succeed.