sys_stat.odin 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. #+build linux, darwin, netbsd, openbsd, freebsd
  2. package posix
  3. import "core:c"
  4. when ODIN_OS == .Darwin {
  5. foreign import lib "system:System.framework"
  6. } else {
  7. foreign import lib "system:c"
  8. }
  9. // sys/stat.h - data returned by the stat() function
  10. foreign lib {
  11. /*
  12. Equivalent to either stat or lstat (based on the SYMLINK_NOFOLLOW bit in flags)
  13. but resolves relative paths based on the given fd.
  14. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstatat.html ]]
  15. */
  16. @(link_name="fstatat" + INODE_SUFFIX)
  17. fstatat :: proc(fd: FD, path: cstring, buf: ^stat_t, flag: AT_Flags) -> result ---
  18. /*
  19. Obtain information about a "file" at the given path.
  20. Follows symbolic links.
  21. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstatat.html ]]
  22. */
  23. @(link_name=LSTAT)
  24. stat :: proc(path: cstring, buf: ^stat_t) -> result ---
  25. /*
  26. Obtain information about an open file.
  27. Follows symbol links.
  28. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstat.html ]]
  29. */
  30. @(link_name=LFSTAT)
  31. fstat :: proc(fildes: FD, buf: ^stat_t) -> result ---
  32. /*
  33. Obtain information about a "file" at the given path.
  34. Does not follow symlinks (will stat the symlink itself).
  35. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstatat.html ]]
  36. */
  37. @(link_name=LLSTAT)
  38. lstat :: proc(path: cstring, buf: ^stat_t) -> result ---
  39. /*
  40. Change the mode of a file.
  41. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html ]]
  42. */
  43. chmod :: proc(path: cstring, mode: mode_t) -> result ---
  44. /*
  45. Equivalent to chmod but takes an open file descriptor.
  46. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchmod.html ]]
  47. */
  48. fchmod :: proc(fd: FD, mode: mode_t) -> result ---
  49. /*
  50. Equivalent to chmod but follows (or doesn't) symlinks based on the flag and resolves
  51. relative paths from the given fd.
  52. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html ]]
  53. */
  54. fchmodat :: proc(fd: FD, path: cstring, mode: mode_t, flag: AT_Flags) -> result ---
  55. /*
  56. Make a directory.
  57. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdir.html ]]
  58. */
  59. mkdir :: proc(path: cstring, mode: mode_t) -> result ---
  60. /*
  61. Equivalent to mkdir but relative paths are relative to fd.
  62. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdir.html ]]
  63. */
  64. mkdirat :: proc(fd: FD, path: cstring, mode: mode_t) -> result ---
  65. /*
  66. Make a FIFO special file.
  67. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkfifo.html ]]
  68. */
  69. mkfifo :: proc(path: cstring, mode: mode_t) -> result ---
  70. /*
  71. Equivalent to mkfifo but relative paths are relative to fd.
  72. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkfifo.html ]]
  73. */
  74. mkfifoat :: proc(fd: FD, path: cstring, mode: mode_t) -> result ---
  75. /*
  76. Make directory, special file, or regular file.
  77. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mknodat.html ]]
  78. */
  79. @(link_name=LMKNOD)
  80. mknod :: proc(path: cstring, mode: mode_t, dev: dev_t) -> result ---
  81. /*
  82. Equivalent to mknod but relative paths are relative to fd.
  83. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mknodat.html ]]
  84. */
  85. mknodat :: proc(fd: FD, path: cstring, mode: mode_t, dev: dev_t) -> result ---
  86. /*
  87. Sets the file access and modification time of the given file descriptor.
  88. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/futimens.html ]]
  89. */
  90. futimens :: proc(fd: FD, times: ^[2]timespec) -> result ---
  91. /*
  92. Equivalent to futimens.
  93. Relative directories are based on fd.
  94. Symlinks may or may not be followed based on the flags.
  95. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/futimens.html ]]
  96. */
  97. utimensat :: proc(fd: FD, path: cstring, times: ^[2]timespec, flag: AT_Flags) -> result ---
  98. /*
  99. Set and get the file mode creation flags.
  100. Makes the file mode permissions bits in cmask the new default for the process.
  101. Returns: the previous value
  102. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/umask.html ]]
  103. */
  104. umask :: proc(cmask: mode_t) -> mode_t ---
  105. }
  106. // Read, write, execute user.
  107. S_IRWXU :: mode_t{ .IRUSR, .IWUSR, .IXUSR }
  108. // Read, write, execute group.
  109. S_IRWXG :: mode_t{ .IRGRP, .IWGRP, .IXGRP }
  110. // Read, write, execute other.
  111. S_IRWXO :: mode_t{ .IROTH, .IWOTH, .IXOTH }
  112. Mode_Bits :: enum c.int {
  113. // File type:
  114. IFCHR = log2(_S_IFCHR), /* Character special */
  115. IFIFO = log2(_S_IFIFO), /* FIFO special */
  116. IFREG = log2(_S_IFREG), /* Regular */
  117. IFDIR = log2(_S_IFDIR), /* Directory */
  118. // Permissions:
  119. IRUSR = log2(_S_IRUSR), /* R for owner */
  120. IWUSR = log2(_S_IWUSR), /* W for owner */
  121. IXUSR = log2(_S_IXUSR), /* X for owner */
  122. IRGRP = log2(_S_IRGRP), /* R for group */
  123. IWGRP = log2(_S_IWGRP), /* W for group */
  124. IXGRP = log2(_S_IXGRP), /* X for group */
  125. IROTH = log2(_S_IROTH), /* R for other */
  126. IWOTH = log2(_S_IWOTH), /* W for other */
  127. IXOTH = log2(_S_IXOTH), /* X for other */
  128. ISUID = log2(_S_ISUID), /* Set user ID on execution */
  129. ISGID = log2(_S_ISGID), /* Set group ID on execution */
  130. ISVXT = log2(_S_ISVTX), /* On directories, restricted deletion flag */
  131. }
  132. mode_t :: bit_set[Mode_Bits; _mode_t]
  133. #assert(size_of(mode_t) == size_of(_mode_t))
  134. S_IFMT :: mode_t{ .IFCHR, .IFREG, .IFDIR, .IFIFO }
  135. S_IFSOCK :: mode_t{ .IFREG, .IFDIR }
  136. S_IFLNK :: mode_t{ .IFREG, .IFCHR }
  137. S_IFBLK :: mode_t{ .IFDIR, .IFCHR }
  138. S_IFIFO :: mode_t{ .IFIFO }
  139. S_IFCHR :: mode_t{ .IFCHR }
  140. S_IFDIR :: mode_t{ .IFDIR }
  141. S_IFREG :: mode_t{ .IFREG }
  142. #assert(_S_IFMT == _S_IFCHR|_S_IFREG|_S_IFDIR|_S_IFIFO)
  143. #assert(_S_IFSOCK == _S_IFREG|_S_IFDIR)
  144. #assert(_S_IFLNK == _S_IFREG|_S_IFCHR)
  145. #assert(_S_IFBLK == _S_IFDIR|_S_IFCHR)
  146. // Test for a block special file.
  147. S_ISBLK :: #force_inline proc "contextless" (m: mode_t) -> bool {
  148. return (m & S_IFMT) == S_IFBLK
  149. }
  150. // Test for a character special file.
  151. S_ISCHR :: #force_inline proc "contextless" (m: mode_t) -> bool {
  152. return (m & S_IFMT) == S_IFCHR
  153. }
  154. // Test for a pipe or FIFO special file.
  155. S_ISFIFO :: #force_inline proc "contextless" (m: mode_t) -> bool {
  156. return (m & S_IFMT) == S_IFIFO
  157. }
  158. // Test for a regular file.
  159. S_ISREG :: #force_inline proc "contextless" (m: mode_t) -> bool {
  160. return (m & S_IFMT) == S_IFREG
  161. }
  162. // Test for a directory.
  163. S_ISDIR :: #force_inline proc "contextless" (m: mode_t) -> bool {
  164. return (m & S_IFMT) == S_IFDIR
  165. }
  166. // Test for a symbolic link.
  167. S_ISLNK :: #force_inline proc "contextless" (m: mode_t) -> bool {
  168. return (m & S_IFMT) == S_IFLNK
  169. }
  170. // Test for a socket.
  171. S_ISSOCK :: #force_inline proc "contextless" (m: mode_t) -> bool {
  172. return (m & S_IFMT) == S_IFSOCK
  173. }
  174. _S_IRWXU :: 0o000700
  175. _S_IRUSR :: 0o000400
  176. _S_IWUSR :: 0o000200
  177. _S_IXUSR :: 0o000100
  178. _S_IRWXG :: 0o000070
  179. _S_IRGRP :: 0o000040
  180. _S_IWGRP :: 0o000020
  181. _S_IXGRP :: 0o000010
  182. _S_IRWXO :: 0o000007
  183. _S_IROTH :: 0o000004
  184. _S_IWOTH :: 0o000002
  185. _S_IXOTH :: 0o000001
  186. _S_ISUID :: 0o004000
  187. _S_ISGID :: 0o002000
  188. _S_ISVTX :: 0o001000
  189. _S_IFBLK :: 0o060000
  190. _S_IFCHR :: 0o020000
  191. _S_IFIFO :: 0o010000
  192. _S_IFREG :: 0o100000
  193. _S_IFDIR :: 0o040000
  194. _S_IFLNK :: 0o120000
  195. _S_IFSOCK :: 0o140000
  196. _S_IFMT :: 0o170000
  197. when ODIN_OS == .NetBSD {
  198. @(private) LSTAT :: "__stat50"
  199. @(private) LFSTAT :: "__fstat50"
  200. @(private) LLSTAT :: "__lstat50"
  201. @(private) LMKNOD :: "__mknod50"
  202. } else {
  203. @(private) LSTAT :: "stat" + INODE_SUFFIX
  204. @(private) LFSTAT :: "fstat" + INODE_SUFFIX
  205. @(private) LLSTAT :: "lstat" + INODE_SUFFIX
  206. @(private) LMKNOD :: "mknod"
  207. }
  208. when ODIN_OS == .Darwin {
  209. dev_t :: distinct c.int32_t
  210. nlink_t :: distinct c.uint16_t
  211. _mode_t :: distinct c.uint16_t
  212. blkcnt_t :: distinct c.int64_t
  213. blksize_t :: distinct c.int32_t
  214. ino_t :: distinct c.uint64_t
  215. stat_t :: struct {
  216. st_dev: dev_t, /* [PSX] ID of device containing file */
  217. st_mode: mode_t, /* [PSX] mode of file */
  218. st_nlink: nlink_t, /* [PSX] number of hard links */
  219. st_ino: ino_t, /* [PSX] file serial number */
  220. st_uid: uid_t, /* [PSX] user ID of the file */
  221. st_gid: gid_t, /* [PSX] group ID of the file */
  222. st_rdev: dev_t, /* [PSX] device ID */
  223. st_atim: timespec, /* [PSX] time of last access */
  224. st_mtim: timespec, /* [PSX] time of last data modification */
  225. st_ctim: timespec, /* [PSX] time of last status change */
  226. st_birthtimespec: timespec, /* time of file creation(birth) */
  227. st_size: off_t, /* [PSX] file size, in bytes */
  228. st_blocks: blkcnt_t, /* [PSX] blocks allocated for file */
  229. st_blksize: blksize_t, /* [PSX] optimal blocksize for I/O */
  230. st_flags: c.uint32_t, /* user defined flags for file */
  231. st_gen: c.uint32_t, /* file generation number */
  232. st_lspare: c.int32_t, /* RESERVED */
  233. st_qspare: [2]c.int64_t, /* RESERVED */
  234. }
  235. UTIME_NOW :: -1
  236. UTIME_OMIT :: -2
  237. } else when ODIN_OS == .FreeBSD {
  238. dev_t :: distinct c.uint64_t
  239. nlink_t :: distinct c.uint64_t
  240. _mode_t :: distinct c.uint16_t
  241. blkcnt_t :: distinct c.int64_t
  242. blksize_t :: distinct c.int32_t
  243. ino_t :: distinct c.uint64_t
  244. when ODIN_ARCH == .i386 {
  245. stat_t :: struct {
  246. st_dev: dev_t, /* [PSX] ID of device containing file */
  247. st_ino: ino_t, /* [PSX] file serial number */
  248. st_nlink: nlink_t, /* [PSX] number of hard links */
  249. st_mode: mode_t, /* [PSX] mode of file */
  250. st_padding0: c.int16_t,
  251. st_uid: uid_t, /* [PSX] user ID of the file */
  252. st_gid: gid_t, /* [PSX] group ID of the file */
  253. st_padding1: c.int32_t,
  254. st_rdev: dev_t, /* [PSX] device ID */
  255. st_atim_ext: c.int32_t,
  256. st_atim: timespec, /* [PSX] time of last access */
  257. st_mtim_ext: c.int32_t,
  258. st_mtim: timespec, /* [PSX] time of last data modification */
  259. st_ctim_ext: c.int32_t,
  260. st_ctim: timespec, /* [PSX] time of last status change */
  261. st_birthtimespec: timespec, /* time of file creation(birth) */
  262. st_size: off_t, /* [PSX] file size, in bytes */
  263. st_blocks: blkcnt_t, /* [PSX] blocks allocated for file */
  264. st_blksize: blksize_t, /* [PSX] optimal blocksize for I/O */
  265. st_flags: c.uint32_t, /* user defined flags for file */
  266. st_gen: c.uint64_t,
  267. st_spare: [10]c.uint64_t,
  268. }
  269. } else {
  270. stat_t :: struct {
  271. st_dev: dev_t, /* [PSX] ID of device containing file */
  272. st_ino: ino_t, /* [PSX] file serial number */
  273. st_nlink: nlink_t, /* [PSX] number of hard links */
  274. st_mode: mode_t, /* [PSX] mode of file */
  275. st_padding0: c.int16_t,
  276. st_uid: uid_t, /* [PSX] user ID of the file */
  277. st_gid: gid_t, /* [PSX] group ID of the file */
  278. st_padding1: c.int32_t,
  279. st_rdev: dev_t, /* [PSX] device ID */
  280. st_atim: timespec, /* [PSX] time of last access */
  281. st_mtim: timespec, /* [PSX] time of last data modification */
  282. st_ctim: timespec, /* [PSX] time of last status change */
  283. st_birthtimespec: timespec, /* time of file creation(birth) */
  284. st_size: off_t, /* [PSX] file size, in bytes */
  285. st_blocks: blkcnt_t, /* [PSX] blocks allocated for file */
  286. st_blksize: blksize_t, /* [PSX] optimal blocksize for I/O */
  287. st_flags: c.uint32_t, /* user defined flags for file */
  288. st_gen: c.uint64_t,
  289. st_spare: [10]c.uint64_t,
  290. }
  291. }
  292. UTIME_NOW :: -1
  293. UTIME_OMIT :: -2
  294. } else when ODIN_OS == .NetBSD {
  295. dev_t :: distinct c.uint64_t
  296. nlink_t :: distinct c.uint32_t
  297. _mode_t :: distinct c.uint32_t
  298. blkcnt_t :: distinct c.int64_t
  299. blksize_t :: distinct c.int32_t
  300. ino_t :: distinct c.uint64_t
  301. stat_t :: struct {
  302. st_dev: dev_t, /* [PSX] ID of device containing file */
  303. st_mode: mode_t, /* [PSX] mode of file */
  304. st_ino: ino_t, /* [PSX] file serial number */
  305. st_nlink: nlink_t, /* [PSX] number of hard links */
  306. st_uid: uid_t, /* [PSX] user ID of the file */
  307. st_gid: gid_t, /* [PSX] group ID of the file */
  308. st_rdev: dev_t, /* [PSX] device ID */
  309. st_atim: timespec, /* [PSX] time of last access */
  310. st_mtim: timespec, /* [PSX] time of last data modification */
  311. st_ctim: timespec, /* [PSX] time of last status change */
  312. st_birthtimespec: timespec, /* time of file creation(birth) */
  313. st_size: off_t, /* [PSX] file size, in bytes */
  314. st_blocks: blkcnt_t, /* [PSX] blocks allocated for file */
  315. st_blksize: blksize_t, /* [PSX] optimal blocksize for I/O */
  316. st_flags: c.uint32_t, /* user defined flags for file */
  317. st_gen: c.uint64_t,
  318. st_spare: [2]c.uint32_t,
  319. }
  320. UTIME_NOW :: (1 << 30) - 1
  321. UTIME_OMIT :: (1 << 30) - 2
  322. } else when ODIN_OS == .OpenBSD {
  323. dev_t :: distinct c.int32_t
  324. nlink_t :: distinct c.uint32_t
  325. _mode_t :: distinct c.uint32_t
  326. blkcnt_t :: distinct c.int64_t
  327. blksize_t :: distinct c.int32_t
  328. ino_t :: distinct c.uint64_t
  329. stat_t :: struct {
  330. st_mode: mode_t, /* [PSX] mode of file */
  331. st_dev: dev_t, /* [PSX] ID of device containing file */
  332. st_ino: ino_t, /* [PSX] file serial number */
  333. st_nlink: nlink_t, /* [PSX] number of hard links */
  334. st_uid: uid_t, /* [PSX] user ID of the file */
  335. st_gid: gid_t, /* [PSX] group ID of the file */
  336. st_rdev: dev_t, /* [PSX] device ID */
  337. st_atim: timespec, /* [PSX] time of last access */
  338. st_mtim: timespec, /* [PSX] time of last data modification */
  339. st_ctim: timespec, /* [PSX] time of last status change */
  340. st_size: off_t, /* [PSX] file size, in bytes */
  341. st_blocks: blkcnt_t, /* [PSX] blocks allocated for file */
  342. st_blksize: blksize_t, /* [PSX] optimal blocksize for I/O */
  343. st_flags: c.uint32_t, /* user defined flags for file */
  344. st_gen: c.int32_t,
  345. st_birthtimespec: timespec,
  346. }
  347. UTIME_NOW :: -2
  348. UTIME_OMIT :: -1
  349. } else when ODIN_OS == .Linux {
  350. dev_t :: distinct u64
  351. _mode_t :: distinct c.uint
  352. blkcnt_t :: distinct i64
  353. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  354. nlink_t :: distinct c.uint
  355. blksize_t :: distinct c.int
  356. } else {
  357. nlink_t :: distinct c.size_t
  358. blksize_t :: distinct c.long
  359. }
  360. ino_t :: distinct u64
  361. when ODIN_ARCH == .amd64 {
  362. stat_t :: struct {
  363. st_dev: dev_t, /* [PSX] ID of device containing file */
  364. st_ino: ino_t, /* [PSX] file serial number */
  365. st_nlink: nlink_t, /* [PSX] number of hard links */
  366. st_mode: mode_t, /* [PSX] mode of file */
  367. st_uid: uid_t, /* [PSX] user ID of the file */
  368. st_gid: gid_t, /* [PSX] group ID of the file */
  369. _pad0: c.uint,
  370. st_rdev: dev_t, /* [PSX] device ID */
  371. st_size: off_t, /* [PSX] file size, in bytes */
  372. st_blksize: blksize_t, /* [PSX] optimal blocksize for I/O */
  373. st_blocks: blkcnt_t, /* [PSX] blocks allocated for file */
  374. st_atim: timespec, /* [PSX] time of last access */
  375. st_mtim: timespec, /* [PSX] time of last data modification */
  376. st_ctim: timespec, /* [PSX] time of last status change */
  377. __unused: [3]c.long,
  378. }
  379. } else {
  380. stat_t :: struct {
  381. st_dev: dev_t, /* [PSX] ID of device containing file */
  382. st_ino: ino_t, /* [PSX] file serial number */
  383. st_mode: mode_t, /* [PSX] mode of file */
  384. st_nlink: nlink_t, /* [PSX] number of hard links */
  385. st_uid: uid_t, /* [PSX] user ID of the file */
  386. st_gid: gid_t, /* [PSX] group ID of the file */
  387. st_rdev: dev_t, /* [PSX] device ID */
  388. __pad: c.ulonglong,
  389. st_size: off_t, /* [PSX] file size, in bytes */
  390. st_blksize: blksize_t, /* [PSX] optimal blocksize for I/O */
  391. __pad2: c.int,
  392. st_blocks: blkcnt_t, /* [PSX] blocks allocated for file */
  393. st_atim: timespec, /* [PSX] time of last access */
  394. st_mtim: timespec, /* [PSX] time of last data modification */
  395. st_ctim: timespec, /* [PSX] time of last status change */
  396. __unused: [2]c.uint,
  397. }
  398. }
  399. }