sys_stat.odin 14 KB

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