blob: a992f9a5393bcffaad64ec9644ce1405ad0aed82 [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH STAT 3
2.SH NAME
3stat, fstat, wstat, fwstat, dirstat, dirfstat, dirwstat, dirfwstat, nulldir \- get and put file status
4.SH SYNOPSIS
5.B #include <u.h>
6.br
7.B #include <libc.h>
8.PP
9.B
10int stat(char *name, uchar *edir, int nedir)
11.PP
12.B
13int fstat(int fd, uchar *edir, int nedir)
14.PP
15.B
16int wstat(char *name, uchar *edir, int nedir)
17.PP
18.B
19int fwstat(int fd, uchar *edir, int nedir)
20.PP
21.B
22Dir* dirstat(char *name)
23.PP
24.B
25Dir* dirfstat(int fd)
26.PP
27.B
28int dirwstat(char *name, Dir *dir)
29.PP
30.B
31int dirfwstat(int fd, Dir *dir)
32.PP
33.B
34void nulldir(Dir *d)
35.SH DESCRIPTION
36Given a file's
37.IR name ,
38or an open file descriptor
39.IR fd ,
40these routines retrieve or modify file status information.
41.IR Stat ,
42.IR fstat ,
43.IR wstat ,
44and
45.I fwstat
46are the system calls; they deal with machine-independent
47.IR "directory entries" .
48Their format is defined by
rsc058b0112005-01-03 06:40:20 +000049.IR stat (9p).
rsccfa37a72004-04-10 18:53:55 +000050.I Stat
51and
52.I fstat
53retrieve information about
54.I name
55or
56.I fd
57into
58.IR edir ,
59a buffer of length
60.IR nedir ,
61defined in
62.BR <libc.h> .
63.I Wstat
64and
65.I fwstat
66write information back, thus changing file attributes according to the contents of
67.IR edir .
68The data returned from the kernel includes its leading 16-bit length field
69as described in
rsc058b0112005-01-03 06:40:20 +000070.IR intro (9p).
rsccfa37a72004-04-10 18:53:55 +000071For symmetry, this field must also be present when passing data to the kernel in a call to
72.I wstat
73and
74.IR fwstat ,
75but its value is ignored.
76.PP
77.IR Dirstat ,
78.IR dirfstat ,
79.IR dirwstat ,
80and
81.I dirfwstat
82are similar to their counterparts, except that they
83operate on
84.I Dir
85structures:
86.IP
87.EX
88.ta 6n +\w'ulong 'u +\w'mtime; 'u
89typedef
90struct Dir {
91 /* system-modified data */
92 uint type; /* server type */
93 uint dev; /* server subtype */
94 /* file data */
95 Qid qid; /* unique id from server */
96 ulong mode; /* permissions */
97 ulong atime; /* last read time */
98 ulong mtime; /* last write time */
99 vlong length; /* file length: see <u.h> */
100 char *name; /* last element of path */
101 char *uid; /* owner name */
102 char *gid; /* group name */
103 char *muid; /* last modifier name */
104} Dir;
105.EE
106.PP
107The returned structure is allocated by
rscbf8a59f2004-04-11 03:42:27 +0000108.IR malloc (3);
rsccfa37a72004-04-10 18:53:55 +0000109freeing it also frees the associated strings.
110.PP
111This structure and
112the
113.B Qid
114structure
115are defined in
116.BR <libc.h> .
117If the file resides on permanent storage and is not a directory,
118the length returned by
119.I stat
120is the number of bytes in the file.
121For directories, the length returned is zero.
122For files that are streams (e.g., pipes and network connections),
123the length is the number of bytes that can be read without blocking.
124.PP
125Each file is the responsibility of some
126.IR server :
127it could be a file server, a kernel device, or a user process.
128.B Type
129identifies the server type, and
130.B dev
131says which of a group of servers of the same type is the one
132responsible for this file.
133.B Qid
134is a structure containing
135.B path
136and
137.B vers
138fields:
139.B path
140is guaranteed to be
141unique among all path names currently on the file server, and
142.B vers
143changes each time the file is modified.
144The
145.B path
146is a
147.B long
148.B long
149(64 bits,
150.BR vlong )
151and the
152.B vers
153is an
154.B unsigned long
155(32 bits,
156.BR ulong ).
157Thus, if two files have the same
158.BR type ,
159.BR dev ,
160and
161.B qid
162they are the same file.
163.PP
164The bits in
165.B mode
166are defined by
167.PP
168.ta 6n +\w'\fL0x80000000 'u
169.nf
170\fL 0x80000000\fP directory
171\fL 0x40000000\fP append only
172\fL 0x20000000\fP exclusive use (locked)
rsc17ddbe12005-02-08 20:18:34 +0000173\fL 0x00800000\fP Unix device file
174\fL 0x00400000\fP symbolic link
175\fL 0x00200000\fP named pipe
176\fL 0x00100000\fP socket
rsccfa37a72004-04-10 18:53:55 +0000177
178\fL 0400\fP read permission by owner
179\fL 0200\fP write permission by owner
180\fL 0100\fP execute permission (search on directory) by owner
181\fL 0070\fP read, write, execute (search) by group
182\fL 0007\fP read, write, execute (search) by others
183.fi
184.PP
185There are constants defined in
186.B <libc.h>
187for these bits:
188.BR DMDIR ,
189.BR DMAPPEND ,
190and
191.B DMEXCL
192for the first three; and
193.BR DMREAD ,
194.BR DMWRITE ,
195and
196.B DMEXEC
197for the read, write, and execute bits for others.
198.PP
199The two time fields are measured in seconds since the epoch
200(Jan 1 00:00 1970 GMT).
201.B Mtime
202is the time of the last change of content.
203Similarly,
204.B atime
205is set whenever the contents are accessed;
206also, it is set whenever
207.B mtime
208is set.
209.PP
210.B Uid
211and
212.B gid
213are the names of the owner and group of the file;
214.B muid
215is the name of the user that last modified the file (setting
216.BR mtime ).
217Groups are also users, but each server is free to associate
218a list of users with any user name
219.IR g ,
220and that list is the
221set of users in the group
222.IR g .
223When an initial attachment is made to a server,
224the user string in the process group is communicated to the server.
225Thus, the server knows, for any given file access, whether the accessing
226process is the owner of, or in the group of, the file.
227This selects which sets of three bits in
228.B mode
229is used to check permissions.
230.PP
231Only some of the fields may be changed with the
232.I wstat
233calls.
234The
235.B name
236can be changed by anyone with write permission in the parent directory.
237The
238.B mode
239and
240.B mtime
241can be changed by the owner or the group leader of the file's current
242group.
243The
244.I gid
245can be changed: by the owner if also a member of the new group; or
246by the group leader of the file's current group
247if also leader of the new group
248(see
rsc058b0112005-01-03 06:40:20 +0000249.IR intro (9p)
250for more information about permissions, users, and groups).
rsccfa37a72004-04-10 18:53:55 +0000251The
252.B length
253can be changed by anyone with write permission, provided the operation
254is implemented by the server.
255(See
rsc058b0112005-01-03 06:40:20 +0000256.IR intro (9p)
257for permission, user, and group information).
rsccfa37a72004-04-10 18:53:55 +0000258.PP
259Special values in the fields of the
260.B Dir
261passed to
262.I wstat
263indicate that the field is not intended to be changed by the call.
264The values are the maximum unsigned integer of appropriate size
265for integral values (usually
266.BR ~0 ,
267but beware of conversions and size mismatches
268when comparing values) and the empty or nil string for string values.
269The routine
270.I nulldir
271initializes all the elements of
272.I d
273to these ``don't care'' values.
274Thus one may change the mode, for example, by using
275.I nulldir
276to initialize a
277.BR Dir ,
278then setting the mode, and then doing
279.IR wstat ;
280it is not necessary to use
281.I stat
282to retrieve the initial values first.
283.SH SOURCE
rscc3674de2005-01-11 17:37:33 +0000284.B \*9/src/lib9/dirstat.c
rsccfa37a72004-04-10 18:53:55 +0000285.SH SEE ALSO
rscbf8a59f2004-04-11 03:42:27 +0000286.IR intro (3),
287.IR fcall (3),
288.IR dirread (3),
rsc058b0112005-01-03 06:40:20 +0000289.IR stat (9p)
rsccfa37a72004-04-10 18:53:55 +0000290.SH DIAGNOSTICS
291The
292.I dir
293functions return a pointer to the data for a successful call, or
294.B nil
295on error.
296The others
297return the number of bytes copied on success, or \-1 on error.
298All set
299.IR errstr .
300.PP
301If the buffer for
302.I stat
303or
304.I fstat
305is too short for the returned data, the return value will be
306.B BIT16SZ
307(see
rscbf8a59f2004-04-11 03:42:27 +0000308.IR fcall (3))
rsccfa37a72004-04-10 18:53:55 +0000309and the two bytes
310returned will contain the initial count field of the
311returned data;
312retrying with
313.B nedir
314equal to
315that value plus
316.B BIT16SZ
317(for the count itself) should succeed.