file_linux.odin 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. //+private
  2. package os2
  3. import "core:io"
  4. import "core:time"
  5. import "core:strings"
  6. import "core:runtime"
  7. import "core:sys/unix"
  8. INVALID_HANDLE :: -1
  9. _O_RDONLY :: 0o0
  10. _O_WRONLY :: 0o1
  11. _O_RDWR :: 0o2
  12. _O_CREAT :: 0o100
  13. _O_EXCL :: 0o200
  14. _O_TRUNC :: 0o1000
  15. _O_APPEND :: 0o2000
  16. _O_NONBLOCK :: 0o4000
  17. _O_LARGEFILE :: 0o100000
  18. _O_DIRECTORY :: 0o200000
  19. _O_NOFOLLOW :: 0o400000
  20. _O_SYNC :: 0o4010000
  21. _O_CLOEXEC :: 0o2000000
  22. _O_PATH :: 0o10000000
  23. _AT_FDCWD :: -100
  24. _CSTRING_NAME_HEAP_THRESHOLD :: 512
  25. _File :: struct {
  26. name: string,
  27. fd: int,
  28. allocator: runtime.Allocator,
  29. }
  30. _file_allocator :: proc() -> runtime.Allocator {
  31. return heap_allocator()
  32. }
  33. _open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (^File, Error) {
  34. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  35. name_cstr := _name_to_cstring(name)
  36. flags_i: int
  37. switch flags & O_RDONLY|O_WRONLY|O_RDWR {
  38. case O_RDONLY: flags_i = _O_RDONLY
  39. case O_WRONLY: flags_i = _O_WRONLY
  40. case O_RDWR: flags_i = _O_RDWR
  41. }
  42. flags_i |= (_O_APPEND * int(.Append in flags))
  43. flags_i |= (_O_CREAT * int(.Create in flags))
  44. flags_i |= (_O_EXCL * int(.Excl in flags))
  45. flags_i |= (_O_SYNC * int(.Sync in flags))
  46. flags_i |= (_O_TRUNC * int(.Trunc in flags))
  47. flags_i |= (_O_CLOEXEC * int(.Close_On_Exec in flags))
  48. fd := unix.sys_open(name_cstr, flags_i, int(perm))
  49. if fd < 0 {
  50. return nil, _get_platform_error(fd)
  51. }
  52. return _new_file(uintptr(fd), name), nil
  53. }
  54. _new_file :: proc(fd: uintptr, _: string) -> ^File {
  55. file := new(File, _file_allocator())
  56. file.impl.fd = int(fd)
  57. file.impl.allocator = _file_allocator()
  58. file.impl.name = _get_full_path(file.impl.fd, file.impl.allocator)
  59. return file
  60. }
  61. _destroy :: proc(f: ^File) -> Error {
  62. if f == nil {
  63. return nil
  64. }
  65. delete(f.impl.name, f.impl.allocator)
  66. free(f, f.impl.allocator)
  67. return nil
  68. }
  69. _close :: proc(f: ^File) -> Error {
  70. res := unix.sys_close(f.impl.fd)
  71. return _ok_or_error(res)
  72. }
  73. _fd :: proc(f: ^File) -> uintptr {
  74. if f == nil {
  75. return ~uintptr(0)
  76. }
  77. return uintptr(f.impl.fd)
  78. }
  79. _name :: proc(f: ^File) -> string {
  80. return f.impl.name if f != nil else ""
  81. }
  82. _seek :: proc(f: ^File, offset: i64, whence: Seek_From) -> (ret: i64, err: Error) {
  83. res := unix.sys_lseek(f.impl.fd, offset, int(whence))
  84. if res < 0 {
  85. return -1, _get_platform_error(int(res))
  86. }
  87. return res, nil
  88. }
  89. _read :: proc(f: ^File, p: []byte) -> (n: int, err: Error) {
  90. if len(p) == 0 {
  91. return 0, nil
  92. }
  93. n = unix.sys_read(f.impl.fd, &p[0], len(p))
  94. if n < 0 {
  95. return -1, _get_platform_error(n)
  96. }
  97. return n, nil
  98. }
  99. _read_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: int, err: Error) {
  100. if offset < 0 {
  101. return 0, .Invalid_Offset
  102. }
  103. b, offset := p, offset
  104. for len(b) > 0 {
  105. m := unix.sys_pread(f.impl.fd, &b[0], len(b), offset)
  106. if m < 0 {
  107. return -1, _get_platform_error(m)
  108. }
  109. n += m
  110. b = b[m:]
  111. offset += i64(m)
  112. }
  113. return
  114. }
  115. _read_from :: proc(f: ^File, r: io.Reader) -> (n: i64, err: Error) {
  116. //TODO
  117. return
  118. }
  119. _write :: proc(f: ^File, p: []byte) -> (n: int, err: Error) {
  120. if len(p) == 0 {
  121. return 0, nil
  122. }
  123. n = unix.sys_write(f.impl.fd, &p[0], uint(len(p)))
  124. if n < 0 {
  125. return -1, _get_platform_error(n)
  126. }
  127. return int(n), nil
  128. }
  129. _write_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: int, err: Error) {
  130. if offset < 0 {
  131. return 0, .Invalid_Offset
  132. }
  133. b, offset := p, offset
  134. for len(b) > 0 {
  135. m := unix.sys_pwrite(f.impl.fd, &b[0], len(b), offset)
  136. if m < 0 {
  137. return -1, _get_platform_error(m)
  138. }
  139. n += m
  140. b = b[m:]
  141. offset += i64(m)
  142. }
  143. return
  144. }
  145. _write_to :: proc(f: ^File, w: io.Writer) -> (n: i64, err: Error) {
  146. //TODO
  147. return
  148. }
  149. _file_size :: proc(f: ^File) -> (n: i64, err: Error) {
  150. s: _Stat = ---
  151. res := unix.sys_fstat(f.impl.fd, &s)
  152. if res < 0 {
  153. return -1, _get_platform_error(res)
  154. }
  155. return s.size, nil
  156. }
  157. _sync :: proc(f: ^File) -> Error {
  158. return _ok_or_error(unix.sys_fsync(f.impl.fd))
  159. }
  160. _flush :: proc(f: ^File) -> Error {
  161. return _ok_or_error(unix.sys_fsync(f.impl.fd))
  162. }
  163. _truncate :: proc(f: ^File, size: i64) -> Error {
  164. return _ok_or_error(unix.sys_ftruncate(f.impl.fd, size))
  165. }
  166. _remove :: proc(name: string) -> Error {
  167. name_cstr, allocated := _name_to_cstring(name)
  168. defer if allocated {
  169. delete(name_cstr)
  170. }
  171. fd := unix.sys_open(name_cstr, int(File_Flags.Read))
  172. if fd < 0 {
  173. return _get_platform_error(fd)
  174. }
  175. defer unix.sys_close(fd)
  176. if _is_dir_fd(fd) {
  177. return _ok_or_error(unix.sys_rmdir(name_cstr))
  178. }
  179. return _ok_or_error(unix.sys_unlink(name_cstr))
  180. }
  181. _rename :: proc(old_name, new_name: string) -> Error {
  182. old_name_cstr, old_allocated := _name_to_cstring(old_name)
  183. new_name_cstr, new_allocated := _name_to_cstring(new_name)
  184. defer if old_allocated {
  185. delete(old_name_cstr)
  186. }
  187. defer if new_allocated {
  188. delete(new_name_cstr)
  189. }
  190. return _ok_or_error(unix.sys_rename(old_name_cstr, new_name_cstr))
  191. }
  192. _link :: proc(old_name, new_name: string) -> Error {
  193. old_name_cstr, old_allocated := _name_to_cstring(old_name)
  194. new_name_cstr, new_allocated := _name_to_cstring(new_name)
  195. defer if old_allocated {
  196. delete(old_name_cstr)
  197. }
  198. defer if new_allocated {
  199. delete(new_name_cstr)
  200. }
  201. return _ok_or_error(unix.sys_link(old_name_cstr, new_name_cstr))
  202. }
  203. _symlink :: proc(old_name, new_name: string) -> Error {
  204. old_name_cstr, old_allocated := _name_to_cstring(old_name)
  205. new_name_cstr, new_allocated := _name_to_cstring(new_name)
  206. defer if old_allocated {
  207. delete(old_name_cstr)
  208. }
  209. defer if new_allocated {
  210. delete(new_name_cstr)
  211. }
  212. return _ok_or_error(unix.sys_symlink(old_name_cstr, new_name_cstr))
  213. }
  214. _read_link_cstr :: proc(name_cstr: cstring, allocator: runtime.Allocator) -> (string, Error) {
  215. bufsz : uint = 256
  216. buf := make([]byte, bufsz, allocator)
  217. for {
  218. rc := unix.sys_readlink(name_cstr, &(buf[0]), bufsz)
  219. if rc < 0 {
  220. delete(buf)
  221. return "", _get_platform_error(rc)
  222. } else if rc == int(bufsz) {
  223. bufsz *= 2
  224. delete(buf)
  225. buf = make([]byte, bufsz, allocator)
  226. } else {
  227. return strings.string_from_ptr(&buf[0], rc), nil
  228. }
  229. }
  230. }
  231. _read_link :: proc(name: string, allocator: runtime.Allocator) -> (string, Error) {
  232. name_cstr, allocated := _name_to_cstring(name)
  233. defer if allocated {
  234. delete(name_cstr)
  235. }
  236. return _read_link_cstr(name_cstr, allocator)
  237. }
  238. _unlink :: proc(name: string) -> Error {
  239. name_cstr, allocated := _name_to_cstring(name)
  240. defer if allocated {
  241. delete(name_cstr)
  242. }
  243. return _ok_or_error(unix.sys_unlink(name_cstr))
  244. }
  245. _chdir :: proc(name: string) -> Error {
  246. name_cstr, allocated := _name_to_cstring(name)
  247. defer if allocated {
  248. delete(name_cstr)
  249. }
  250. return _ok_or_error(unix.sys_chdir(name_cstr))
  251. }
  252. _fchdir :: proc(f: ^File) -> Error {
  253. return _ok_or_error(unix.sys_fchdir(f.impl.fd))
  254. }
  255. _chmod :: proc(name: string, mode: File_Mode) -> Error {
  256. name_cstr, allocated := _name_to_cstring(name)
  257. defer if allocated {
  258. delete(name_cstr)
  259. }
  260. return _ok_or_error(unix.sys_chmod(name_cstr, int(mode)))
  261. }
  262. _fchmod :: proc(f: ^File, mode: File_Mode) -> Error {
  263. return _ok_or_error(unix.sys_fchmod(f.impl.fd, int(mode)))
  264. }
  265. // NOTE: will throw error without super user priviledges
  266. _chown :: proc(name: string, uid, gid: int) -> Error {
  267. name_cstr, allocated := _name_to_cstring(name)
  268. defer if allocated {
  269. delete(name_cstr)
  270. }
  271. return _ok_or_error(unix.sys_chown(name_cstr, uid, gid))
  272. }
  273. // NOTE: will throw error without super user priviledges
  274. _lchown :: proc(name: string, uid, gid: int) -> Error {
  275. name_cstr, allocated := _name_to_cstring(name)
  276. defer if allocated {
  277. delete(name_cstr)
  278. }
  279. return _ok_or_error(unix.sys_lchown(name_cstr, uid, gid))
  280. }
  281. // NOTE: will throw error without super user priviledges
  282. _fchown :: proc(f: ^File, uid, gid: int) -> Error {
  283. return _ok_or_error(unix.sys_fchown(f.impl.fd, uid, gid))
  284. }
  285. _chtimes :: proc(name: string, atime, mtime: time.Time) -> Error {
  286. name_cstr, allocated := _name_to_cstring(name)
  287. defer if allocated {
  288. delete(name_cstr)
  289. }
  290. times := [2]Unix_File_Time {
  291. { atime._nsec, 0 },
  292. { mtime._nsec, 0 },
  293. }
  294. return _ok_or_error(unix.sys_utimensat(_AT_FDCWD, name_cstr, &times, 0))
  295. }
  296. _fchtimes :: proc(f: ^File, atime, mtime: time.Time) -> Error {
  297. times := [2]Unix_File_Time {
  298. { atime._nsec, 0 },
  299. { mtime._nsec, 0 },
  300. }
  301. return _ok_or_error(unix.sys_utimensat(f.impl.fd, nil, &times, 0))
  302. }
  303. _exists :: proc(name: string) -> bool {
  304. name_cstr, allocated := _name_to_cstring(name)
  305. defer if allocated {
  306. delete(name_cstr)
  307. }
  308. return unix.sys_access(name_cstr, F_OK) == 0
  309. }
  310. _is_file :: proc(name: string) -> bool {
  311. name_cstr, allocated := _name_to_cstring(name)
  312. defer if allocated {
  313. delete(name_cstr)
  314. }
  315. s: _Stat
  316. res := unix.sys_stat(name_cstr, &s)
  317. if res < 0 {
  318. return false
  319. }
  320. return S_ISREG(s.mode)
  321. }
  322. _is_file_fd :: proc(fd: int) -> bool {
  323. s: _Stat
  324. res := unix.sys_fstat(fd, &s)
  325. if res < 0 { // error
  326. return false
  327. }
  328. return S_ISREG(s.mode)
  329. }
  330. _is_dir :: proc(name: string) -> bool {
  331. name_cstr, allocated := _name_to_cstring(name)
  332. defer if allocated {
  333. delete(name_cstr)
  334. }
  335. s: _Stat
  336. res := unix.sys_stat(name_cstr, &s)
  337. if res < 0 {
  338. return false
  339. }
  340. return S_ISDIR(s.mode)
  341. }
  342. _is_dir_fd :: proc(fd: int) -> bool {
  343. s: _Stat
  344. res := unix.sys_fstat(fd, &s)
  345. if res < 0 { // error
  346. return false
  347. }
  348. return S_ISDIR(s.mode)
  349. }
  350. // Ideally we want to use the temp_allocator. PATH_MAX on Linux is commonly
  351. // defined as 512, however, it is well known that paths can exceed that limit.
  352. // So, in theory you could have a path larger than the entire temp_allocator's
  353. // buffer. Therefor, any large paths will use context.allocator.
  354. @(private="file")
  355. _temp_name_to_cstring :: proc(name: string) -> (cname: cstring) {
  356. return strings.clone_to_cstring(name, context.temp_allocator)
  357. }