file_linux.odin 10 KB

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