file_linux.odin 10 KB

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