stat 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. .TH STAT 2
  2. .SH NAME
  3. stat, 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
  10. int stat(char *name, uchar *edir, int nedir)
  11. .PP
  12. .B
  13. int fstat(int fd, uchar *edir, int nedir)
  14. .PP
  15. .B
  16. int wstat(char *name, uchar *edir, int nedir)
  17. .PP
  18. .B
  19. int fwstat(int fd, uchar *edir, int nedir)
  20. .PP
  21. .B
  22. Dir* dirstat(char *name)
  23. .PP
  24. .B
  25. Dir* dirfstat(int fd)
  26. .PP
  27. .B
  28. int dirwstat(char *name, Dir *dir)
  29. .PP
  30. .B
  31. int dirfwstat(int fd, Dir *dir)
  32. .PP
  33. .B
  34. void nulldir(Dir *d)
  35. .SH DESCRIPTION
  36. Given a file's
  37. .IR name ,
  38. or an open file descriptor
  39. .IR fd ,
  40. these routines retrieve or modify file status information.
  41. .IR Stat ,
  42. .IR fstat ,
  43. .IR wstat ,
  44. and
  45. .I fwstat
  46. are the system calls; they deal with machine-independent
  47. .IR "directory entries" .
  48. Their format is defined by
  49. .IR stat (5).
  50. .I Stat
  51. and
  52. .I fstat
  53. retrieve information about
  54. .I name
  55. or
  56. .I fd
  57. into
  58. .IR edir ,
  59. a buffer of length
  60. .IR nedir ,
  61. defined in
  62. .BR <libc.h> .
  63. .I Wstat
  64. and
  65. .I fwstat
  66. write information back, thus changing file attributes according to the contents of
  67. .IR edir .
  68. The data returned from the kernel includes its leading 16-bit length field
  69. as described in
  70. .IR intro (5).
  71. For symmetry, this field must also be present when passing data to the kernel in a call to
  72. .I wstat
  73. and
  74. .IR fwstat ,
  75. but its value is ignored.
  76. .PP
  77. .IR Dirstat ,
  78. .IR dirfstat ,
  79. .IR dirwstat ,
  80. and
  81. .I dirfwstat
  82. are similar to their counterparts, except that they
  83. operate on
  84. .I Dir
  85. structures:
  86. .IP
  87. .EX
  88. .ta 6n +\w'ulong 'u +\w'mtime; 'u
  89. typedef
  90. struct 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
  107. The returned structure is allocated by
  108. .IR malloc (2);
  109. freeing it also frees the associated strings.
  110. .PP
  111. This structure and
  112. the
  113. .B Qid
  114. structure
  115. are defined in
  116. .BR <libc.h> .
  117. If the file resides on permanent storage and is not a directory,
  118. the length returned by
  119. .I stat
  120. is the number of bytes in the file.
  121. For directories, the length returned is zero.
  122. For files that are streams (e.g., pipes and network connections),
  123. the length is the number of bytes that can be read without blocking.
  124. .PP
  125. Each file is the responsibility of some
  126. .IR server :
  127. it could be a file server, a kernel device, or a user process.
  128. .B Type
  129. identifies the server type, and
  130. .B dev
  131. says which of a group of servers of the same type is the one
  132. responsible for this file.
  133. .B Qid
  134. is a structure containing
  135. .B path
  136. and
  137. .B vers
  138. fields:
  139. .B path
  140. is guaranteed to be
  141. unique among all path names currently on the file server, and
  142. .B vers
  143. changes each time the file is modified.
  144. The
  145. .B path
  146. is a
  147. .B long
  148. .B long
  149. (64 bits,
  150. .BR vlong )
  151. and the
  152. .B vers
  153. is an
  154. .B unsigned long
  155. (32 bits,
  156. .BR ulong ).
  157. Thus, if two files have the same
  158. .BR type ,
  159. .BR dev ,
  160. and
  161. .B qid
  162. they are the same file.
  163. .PP
  164. The bits in
  165. .B mode
  166. are 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)
  173. .\" \fL 0x10000000\fP mounted channel
  174. .\" \fL 0x08000000\fP authentication file
  175. \fL 0x04000000\fP non-backed-up files
  176. .sp 0.3v
  177. \fL 0400\fP read permission by owner
  178. \fL 0200\fP write permission by owner
  179. \fL 0100\fP execute permission (search on directory) by owner
  180. \fL 0070\fP read, write, execute (search) by group
  181. \fL 0007\fP read, write, execute (search) by others
  182. .fi
  183. .PP
  184. There are constants defined in
  185. .B <libc.h>
  186. for these bits:
  187. .BR DMDIR ,
  188. .BR DMAPPEND ,
  189. .BR DMEXCL ,
  190. .\" .BR DMMOUNT ,
  191. .\" .BR DMAUTH ,
  192. and
  193. .BR DMTMP
  194. for the first four; and
  195. .BR DMREAD ,
  196. .BR DMWRITE ,
  197. and
  198. .B DMEXEC
  199. for the read, write, and execute bits for others.
  200. .PP
  201. The two time fields are measured in seconds since the epoch
  202. (Jan 1 00:00 1970 GMT).
  203. .B Mtime
  204. is the time of the last change of content.
  205. Similarly,
  206. .B atime
  207. is set whenever the contents are accessed;
  208. also, it is set whenever
  209. .B mtime
  210. is set.
  211. .PP
  212. .B Uid
  213. and
  214. .B gid
  215. are the names of the owner and group of the file;
  216. .B muid
  217. is the name of the user that last modified the file (setting
  218. .BR mtime ).
  219. Groups are also users, but each server is free to associate
  220. a list of users with any user name
  221. .IR g ,
  222. and that list is the
  223. set of users in the group
  224. .IR g .
  225. When an initial attachment is made to a server,
  226. the user string in the process group is communicated to the server.
  227. Thus, the server knows, for any given file access, whether the accessing
  228. process is the owner of, or in the group of, the file.
  229. This selects which sets of three bits in
  230. .B mode
  231. is used to check permissions.
  232. .PP
  233. Only some of the fields may be changed with the
  234. .I wstat
  235. calls.
  236. The
  237. .B name
  238. can be changed by anyone with write permission in the parent directory.
  239. The
  240. .B mode
  241. and
  242. .B mtime
  243. can be changed by the owner or the group leader of the file's current
  244. group.
  245. The
  246. .I gid
  247. can be changed: by the owner if also a member of the new group; or
  248. by the group leader of the file's current group
  249. if also leader of the new group
  250. (see
  251. .IR intro (5)
  252. for more information about permissions and
  253. .IR users (6)
  254. for users and groups).
  255. The
  256. .B length
  257. can be changed by anyone with write permission, provided the operation
  258. is implemented by the server.
  259. (See
  260. .IR intro (5)
  261. for permission information, and
  262. .IR users (6)
  263. for user and group information).
  264. .PP
  265. Special values in the fields of the
  266. .B Dir
  267. passed to
  268. .I wstat
  269. indicate that the field is not intended to be changed by the call.
  270. The values are the maximum unsigned integer of appropriate size
  271. for integral values (usually
  272. .BR ~0 ,
  273. but beware of conversions and size mismatches
  274. when comparing values) and the empty or nil string for string values.
  275. The routine
  276. .I nulldir
  277. initializes all the elements of
  278. .I d
  279. to these ``don't care'' values.
  280. Thus one may change the mode, for example, by using
  281. .I nulldir
  282. to initialize a
  283. .BR Dir ,
  284. then setting the mode, and then doing
  285. .IR wstat ;
  286. it is not necessary to use
  287. .I stat
  288. to retrieve the initial values first.
  289. .SH SOURCE
  290. .TF /sys/src/libc/9syscall
  291. .TP
  292. .B /sys/src/libc/9syscall
  293. for the
  294. .RB non- dir
  295. routines
  296. .TP
  297. .B /sys/src/libc/9sys
  298. for the routines prefixed
  299. .B dir
  300. .SH SEE ALSO
  301. .IR intro (2),
  302. .IR fcall (2),
  303. .IR dirread (2),
  304. .IR stat (5)
  305. .SH DIAGNOSTICS
  306. The
  307. .I dir
  308. functions return a pointer to the data for a successful call, or
  309. .B nil
  310. on error.
  311. The others
  312. return the number of bytes copied on success, or \-1 on error.
  313. All set
  314. .IR errstr .
  315. .PP
  316. If the buffer for
  317. .I stat
  318. or
  319. .I fstat
  320. is too short for the returned data, the return value will be
  321. .B BIT16SZ
  322. (see
  323. .IR fcall (2))
  324. and the two bytes
  325. returned will contain the initial count field of the
  326. returned data;
  327. retrying with
  328. .B nedir
  329. equal to
  330. that value plus
  331. .B BIT16SZ
  332. (for the count itself) should succeed.