1
0

stat.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. stat.h
  8. Abstract:
  9. This header contains standard file status definitions.
  10. Author:
  11. Evan Green 17-Jun-2013
  12. --*/
  13. #ifndef _STAT_H
  14. #define _STAT_H
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <sys/types.h>
  19. #include <time.h>
  20. //
  21. // --------------------------------------------------------------------- Macros
  22. //
  23. //
  24. // This macro returns non-zero if the given mode bits are set for a block
  25. // special file.
  26. //
  27. #define S_ISBLK(_Mode) (((_Mode) & S_IFMT) == S_IFBLK)
  28. //
  29. // This macro returns non-zero if the given mode bits are set for a character
  30. // special file.
  31. //
  32. #define S_ISCHR(_Mode) (((_Mode) & S_IFMT) == S_IFCHR)
  33. //
  34. // This macro returns non-zero if the given mode bits are set for a directory
  35. //
  36. #define S_ISDIR(_Mode) (((_Mode) & S_IFMT) == S_IFDIR)
  37. //
  38. // This macro returns non-zero if the given mode bits are set for a FIFO
  39. // special file.
  40. //
  41. #define S_ISFIFO(_Mode) (((_Mode) & S_IFMT) == S_IFIFO)
  42. //
  43. // This macro returns non-zero if the given mode bits are set for a regular
  44. // file.
  45. //
  46. #define S_ISREG(_Mode) (((_Mode) & S_IFMT) == S_IFREG)
  47. //
  48. // This macro returns non-zero if the given mode bits are set for a symbolic
  49. // link.
  50. //
  51. #define S_ISLNK(_Mode) (((_Mode) & S_IFMT) == S_IFLNK)
  52. //
  53. // This macro returns non-zero if the given mode bits are set for a socket.
  54. //
  55. #define S_ISSOCK(_Mode) (((_Mode) & S_IFMT) == S_IFSOCK)
  56. //
  57. // Define the time value used in the nanosecond field to indicate that the
  58. // current time should be used.
  59. //
  60. #define UTIME_NOW ((long)-1)
  61. //
  62. // Define the time value used in the nanosecond field to indicate that the
  63. // time setting should be omitted.
  64. //
  65. #define UTIME_OMIT ((long)-2)
  66. //
  67. // ---------------------------------------------------------------- Definitions
  68. //
  69. #ifdef __cplusplus
  70. extern "C" {
  71. #endif
  72. //
  73. // Define the types of files, starting with the overall mask. Hardcoded values
  74. // are often baked into applications, so these values line up most accurately
  75. // with historical implementations.
  76. //
  77. #define S_IFMT 0x0000F000
  78. //
  79. // FIFO special device
  80. //
  81. #define S_IFIFO 0x00001000
  82. //
  83. // Character special device
  84. //
  85. #define S_IFCHR 0x00002000
  86. //
  87. // Regular directory
  88. //
  89. #define S_IFDIR 0x00004000
  90. //
  91. // Block special device
  92. //
  93. #define S_IFBLK 0x00006000
  94. //
  95. // Regular file
  96. //
  97. #define S_IFREG 0x00008000
  98. //
  99. // Symbolic link
  100. //
  101. #define S_IFLNK 0x0000A000
  102. //
  103. // Socket file type
  104. //
  105. #define S_IFSOCK 0x0000C000
  106. //
  107. // Define the file mode bits. These values are actually standardized, in
  108. // addition to being hardcoded into applications.
  109. //
  110. //
  111. // Other read, write, and execute bits
  112. //
  113. #define S_IXOTH 0x00000001
  114. #define S_IWOTH 0x00000002
  115. #define S_IROTH 0x00000004
  116. #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
  117. //
  118. // Group read, write, and execute bits
  119. //
  120. #define S_IXGRP 0x00000008
  121. #define S_IWGRP 0x00000010
  122. #define S_IRGRP 0x00000020
  123. #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
  124. //
  125. // User read, write, and execute bits
  126. //
  127. #define S_IXUSR 0x00000040
  128. #define S_IWUSR 0x00000080
  129. #define S_IRUSR 0x00000100
  130. #define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
  131. //
  132. // User read, write, and execute synonyms for compatibility.
  133. //
  134. #define S_IEXEC S_IXUSR
  135. #define S_IWRITE S_IWUSR
  136. #define S_IREAD S_IRUSR
  137. //
  138. // Restricted deletion in directory flag
  139. //
  140. #define S_ISVTX 0x00000200
  141. //
  142. // Set user ID and set group ID on execution bits
  143. //
  144. #define S_ISGID 0x00000400
  145. #define S_ISUID 0x00000800
  146. //
  147. // Define common bit masks.
  148. //
  149. #define ACCESSPERMS (S_IRWXU | S_IRWXG | S_IRWXO)
  150. #define ALLPERMS (ACCESSPERMS | S_ISUID | S_ISGID | S_ISVTX)
  151. #define DEFFILEMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
  152. //
  153. // These definitions create the proper POSIX structure members for struct
  154. // stat's access, modification, and status change time.
  155. //
  156. #define st_atime st_atim.tv_sec
  157. #define st_mtime st_mtim.tv_sec
  158. #define st_ctime st_ctim.tv_sec
  159. #define st_birthtime st_birthtim.tv_sec
  160. //
  161. // ------------------------------------------------------ Data Type Definitions
  162. //
  163. /*++
  164. Structure Description:
  165. This structure defines file object information.
  166. Members:
  167. st_dev - Stores the device ID of the containing file.
  168. st_ino - Stores the file serial number.
  169. __st_type - Stores the file type.
  170. st_mode - Stores the file mode bits. See S_I* definitions.
  171. st_nlink - Stores the number of hard links to the file.
  172. st_uid - Stores the user ID of the file.
  173. st_gid - Stores the group ID of the file.
  174. st_rdev - Stores the device ID if the device is a character or block
  175. special device.
  176. st_size - Stores the file size in bytes for regular files. For symbolic
  177. links, stores the length in bytes of the pathname contained in the
  178. symbolic link.
  179. st_atim - Stores the time of the last access.
  180. st_mtim - Stores the time of the last data modification.
  181. st_ctim - Stores the time of the last status change.
  182. st_birthtim - Stores the creation time of the file.
  183. st_blksize - Stores a file system specific preferred I/O block size for
  184. this object. This may vary from file to file.
  185. st_blocks - Stores the number of blocks allocated for this object.
  186. st_flags - Stores user defined file flags.
  187. st_gen - Stores the file generation number.
  188. --*/
  189. struct stat {
  190. dev_t st_dev;
  191. ino_t st_ino;
  192. int __st_type;
  193. mode_t st_mode;
  194. nlink_t st_nlink;
  195. uid_t st_uid;
  196. gid_t st_gid;
  197. dev_t st_rdev;
  198. off_t st_size;
  199. struct timespec st_atim;
  200. struct timespec st_mtim;
  201. struct timespec st_ctim;
  202. struct timespec st_birthtim;
  203. blksize_t st_blksize;
  204. blkcnt_t st_blocks;
  205. unsigned int st_flags;
  206. unsigned int st_gen;
  207. };
  208. //
  209. // -------------------------------------------------------------------- Globals
  210. //
  211. //
  212. // -------------------------------------------------------- Function Prototypes
  213. //
  214. LIBC_API
  215. int
  216. stat (
  217. const char *Path,
  218. struct stat *Stat
  219. );
  220. /*++
  221. Routine Description:
  222. This routine gets file information for the given file.
  223. Arguments:
  224. Path - Supplies the path string of the file to get the status information
  225. for.
  226. Stat - Supplies a pointer where the information will be returned on
  227. success.
  228. Return Value:
  229. 0 on success.
  230. -1 on failure and the errno variable will be set to provide more
  231. information.
  232. --*/
  233. LIBC_API
  234. int
  235. lstat (
  236. const char *Path,
  237. struct stat *Stat
  238. );
  239. /*++
  240. Routine Description:
  241. This routine gets file information for the given file. It is the same as
  242. the stat function, except that when the given path refers to a symbolic
  243. link, this routine returns information for the link itself, where stat
  244. returns information for the link destination.
  245. Arguments:
  246. Path - Supplies the path string of the file to get the status information
  247. for.
  248. Stat - Supplies a pointer where the information will be returned on
  249. success.
  250. Return Value:
  251. 0 on success.
  252. -1 on failure and the errno variable will be set to provide more
  253. information.
  254. --*/
  255. LIBC_API
  256. int
  257. fstatat (
  258. int Directory,
  259. const char *Path,
  260. struct stat *Stat,
  261. int Flags
  262. );
  263. /*++
  264. Routine Description:
  265. This routine gets file information for the given file.
  266. Arguments:
  267. Directory - Supplies an optional file descriptor. If the given path
  268. is a relative path, the directory referenced by this descriptor will
  269. be used as a starting point for path resolution. Supply AT_FDCWD to
  270. use the working directory for relative paths.
  271. Path - Supplies the path string of the file to get the status information
  272. for.
  273. Stat - Supplies a pointer where the information will be returned on
  274. success.
  275. Flags - Supplies AT_SYMLINK_NOFOLLOW if the routine should return
  276. information for the symbolic link itself, or 0 if the call should
  277. follow a symbolic link at the destination.
  278. Return Value:
  279. 0 on success.
  280. -1 on failure and the errno variable will be set to provide more
  281. information.
  282. --*/
  283. LIBC_API
  284. int
  285. creat (
  286. const char *Path,
  287. mode_t Mode
  288. );
  289. /*++
  290. Routine Description:
  291. This routine attempts to create a new file or truncate an existing one.
  292. This routine is equivalent to
  293. open(Path, O_WRONLY | O_CREAT | O_TRUNC, Mode).
  294. Arguments:
  295. Path - Supplies a pointer to the null terminated string containing the file
  296. path to open.
  297. Mode - Supplies the mode to open the file with.
  298. Return Value:
  299. Like open, returns the new file descriptor on success.
  300. -1 on error, and errno will be set to indicate the error.
  301. --*/
  302. LIBC_API
  303. int
  304. fstat (
  305. int FileDescriptor,
  306. struct stat *Stat
  307. );
  308. /*++
  309. Routine Description:
  310. This routine gets file information corresponding to the given file
  311. descriptor.
  312. Arguments:
  313. FileDescriptor - Supplies the open file descriptor to get file information
  314. for.
  315. Stat - Supplies a pointer where the file information will be returned on
  316. success.
  317. Return Value:
  318. 0 on success.
  319. -1 on failure and the errno variable will be set to provide more
  320. information.
  321. --*/
  322. LIBC_API
  323. int
  324. fchmod (
  325. int FileDescriptor,
  326. mode_t Mode
  327. );
  328. /*++
  329. Routine Description:
  330. This routine sets the file permissions of the file opened with the given
  331. file descriptor.
  332. Arguments:
  333. FileDescriptor - Supplies the file descriptor whose permissions should be
  334. modified.
  335. Mode - Supplies the new mode to set.
  336. Return Value:
  337. 0 on success.
  338. -1 on failure. The errno variable will be set to indicate the error.
  339. --*/
  340. LIBC_API
  341. int
  342. mkdir (
  343. const char *Path,
  344. mode_t Permissions
  345. );
  346. /*++
  347. Routine Description:
  348. This routine creates a new directory.
  349. Arguments:
  350. Path - Supplies the path string of the directory to create.
  351. Permissions - Supplies the permission bits to create the file with.
  352. Return Value:
  353. 0 on success.
  354. -1 on failure and the errno variable will be set to provide more
  355. information.
  356. --*/
  357. LIBC_API
  358. int
  359. mkdirat (
  360. int Directory,
  361. const char *Path,
  362. mode_t Permissions
  363. );
  364. /*++
  365. Routine Description:
  366. This routine creates a new directory.
  367. Arguments:
  368. Directory - Supplies an optional file descriptor. If the given path
  369. is a relative path, the directory referenced by this descriptor will
  370. be used as a starting point for path resolution. Supply AT_FDCWD to
  371. use the working directory for relative paths.
  372. Path - Supplies the path string of the directory to create.
  373. Permissions - Supplies the permission bits to create the file with.
  374. Return Value:
  375. 0 on success.
  376. -1 on failure and the errno variable will be set to provide more
  377. information.
  378. --*/
  379. LIBC_API
  380. mode_t
  381. umask (
  382. mode_t CreationMask
  383. );
  384. /*++
  385. Routine Description:
  386. This routine sets the creation mask for file permissions on calls to open,
  387. creat, mkdir, and mkfifo.
  388. Arguments:
  389. CreationMask - Supplies the new mask to set. Bits set in this creation mask
  390. will be cleared from the permissions given to open, creat, mkdir, and
  391. mkfifo.
  392. Return Value:
  393. Returns the original value of the creation mask.
  394. --*/
  395. LIBC_API
  396. int
  397. chmod (
  398. const char *Path,
  399. mode_t Permissions
  400. );
  401. /*++
  402. Routine Description:
  403. This routine sets the file permission bits of the given path.
  404. Arguments:
  405. Path - Supplies a pointer to the path whose permissions should be changed.
  406. Permissions - Supplies the new file permissions to set.
  407. Return Value:
  408. 0 on success.
  409. -1 on failure, and errno will be set to contain more information.
  410. --*/
  411. LIBC_API
  412. int
  413. fchmodat (
  414. int Directory,
  415. const char *Path,
  416. mode_t Permissions,
  417. int Flags
  418. );
  419. /*++
  420. Routine Description:
  421. This routine sets the file permission bits of the given path.
  422. Arguments:
  423. Directory - Supplies an optional file descriptor. If the given path
  424. is a relative path, the directory referenced by this descriptor will
  425. be used as a starting point for path resolution. Supply AT_FDCWD to
  426. use the working directory for relative paths.
  427. Path - Supplies a pointer to the path whose permissions should be changed.
  428. Permissions - Supplies the new file permissions to set.
  429. Flags - Supplies AT_SYMLINK_NOFOLLOW if the routine should affect a
  430. symbolic link itself, or AT_SYMLINK_FOLLOW if the call should follow a
  431. symbolic link at the destination.
  432. Return Value:
  433. 0 on success.
  434. -1 on failure, and errno will be set to contain more information.
  435. --*/
  436. LIBC_API
  437. int
  438. mkfifo (
  439. const char *Path,
  440. mode_t Permissions
  441. );
  442. /*++
  443. Routine Description:
  444. This routine creates a new named pipe.
  445. Arguments:
  446. Path - Supplies a pointer to the path of the new named pipe. This path must
  447. not already exist.
  448. Permissions - Supplies the initial permissions of the pipe.
  449. Return Value:
  450. 0 on success.
  451. -1 on failure, and errno will be set to contain more information.
  452. --*/
  453. LIBC_API
  454. int
  455. mkfifoat (
  456. int Directory,
  457. const char *Path,
  458. mode_t Permissions
  459. );
  460. /*++
  461. Routine Description:
  462. This routine creates a new named pipe.
  463. Arguments:
  464. Directory - Supplies an optional file descriptor. If the given path
  465. is a relative path, the directory referenced by this descriptor will
  466. be used as a starting point for path resolution. Supply AT_FDCWD to
  467. use the working directory for relative paths.
  468. Path - Supplies a pointer to the path of the new named pipe. This path must
  469. not already exist.
  470. Permissions - Supplies the initial permissions of the pipe.
  471. Return Value:
  472. 0 on success.
  473. -1 on failure, and errno will be set to contain more information.
  474. --*/
  475. LIBC_API
  476. int
  477. mknod (
  478. const char *Path,
  479. mode_t Mode,
  480. dev_t Device
  481. );
  482. /*++
  483. Routine Description:
  484. This routine creates a new regular file or special file.
  485. Arguments:
  486. Path - Supplies a pointer to the path to create.
  487. Mode - Supplies the type of file and permissions to create.
  488. Device - Supplies the device number to create.
  489. Return Value:
  490. 0 on success.
  491. -1 on failure, and errno will be set to contain more information.
  492. --*/
  493. LIBC_API
  494. int
  495. mknodat (
  496. int Directory,
  497. const char *Path,
  498. mode_t Mode,
  499. dev_t Device
  500. );
  501. /*++
  502. Routine Description:
  503. This routine creates a new regular file or special file.
  504. Arguments:
  505. Directory - Supplies an optional file descriptor. If the given path
  506. is a relative path, the directory referenced by this descriptor will
  507. be used as a starting point for path resolution. Supply AT_FDCWD to
  508. use the working directory for relative paths.
  509. Path - Supplies a pointer to the path to create.
  510. Mode - Supplies the type of file and permissions to create.
  511. Device - Supplies the device number to create.
  512. Return Value:
  513. 0 on success.
  514. -1 on failure, and errno will be set to contain more information.
  515. --*/
  516. LIBC_API
  517. int
  518. utimensat (
  519. int Directory,
  520. const char *Path,
  521. const struct timespec Times[2],
  522. int Flags
  523. );
  524. /*++
  525. Routine Description:
  526. This routine sets the access and modification times of the given file. The
  527. effective user ID of the process must match the owner of the file, or the
  528. process must have appropriate privileges.
  529. Arguments:
  530. Directory - Supplies an optional file descriptor. If the given path
  531. is a relative path, the directory referenced by this descriptor will
  532. be used as a starting point for path resolution. Supply AT_FDCWD to
  533. use the working directory for relative paths.
  534. Path - Supplies a pointer to the path of the file to change times for.
  535. Times - Supplies an optional array of time spec structures containing the
  536. access (index 0) and modification (index 1) times to set. If NULL is
  537. supplied, then the current time is used for both values. If either
  538. value has UTIME_NOW in the nanoseconds field, then the curren ttime is
  539. used. If either value has UTIME_OMIT in the nanoseconds field, then
  540. that field will not be changed.
  541. Flags - Supplies AT_SYMLINK_NOFOLLOW if the routine should modify
  542. information for the symbolic link itself, or 0 if the call should
  543. follow a symbolic link at the destination.
  544. Return Value:
  545. 0 on success.
  546. -1 on failure, and errno will be set to contain more information.
  547. --*/
  548. LIBC_API
  549. int
  550. futimens (
  551. int File,
  552. const struct timespec Times[2]
  553. );
  554. /*++
  555. Routine Description:
  556. This routine sets the access and modification times of the file referenced
  557. by the given file descriptor.
  558. Arguments:
  559. File - Supplies the file descriptor of the file to modify.
  560. Times - Supplies an optional array of time spec structures containing the
  561. access (index 0) and modification (index 1) times to set. If NULL is
  562. supplied, then the current time is used for both values. If either
  563. value has UTIME_NOW in the nanoseconds field, then the curren ttime is
  564. used. If either value has UTIME_OMIT in the nanoseconds field, then
  565. that field will not be changed.
  566. Return Value:
  567. 0 on success.
  568. -1 on failure, and errno will be set to contain more information.
  569. --*/
  570. #ifdef __cplusplus
  571. }
  572. #endif
  573. #endif