file_linux.odin 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. //+private
  2. package os2
  3. import "base:runtime"
  4. import "core:io"
  5. import "core:time"
  6. import "core:sys/unix"
  7. INVALID_HANDLE :: -1
  8. _O_RDONLY :: 0o00000000
  9. _O_WRONLY :: 0o00000001
  10. _O_RDWR :: 0o00000002
  11. _O_CREAT :: 0o00000100
  12. _O_EXCL :: 0o00000200
  13. _O_NOCTTY :: 0o00000400
  14. _O_TRUNC :: 0o00001000
  15. _O_APPEND :: 0o00002000
  16. _O_NONBLOCK :: 0o00004000
  17. _O_LARGEFILE :: 0o00100000
  18. _O_DIRECTORY :: 0o00200000
  19. _O_NOFOLLOW :: 0o00400000
  20. _O_SYNC :: 0o04010000
  21. _O_CLOEXEC :: 0o02000000
  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. _open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (f: ^File, err: Error) {
  31. TEMP_ALLOCATOR_GUARD()
  32. name_cstr := temp_cstring(name) or_return
  33. // Just default to using O_NOCTTY because needing to open a controlling
  34. // terminal would be incredibly rare. This has no effect on files while
  35. // allowing us to open serial devices.
  36. flags_i: int = _O_NOCTTY
  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. if .Append in flags { flags_i |= _O_APPEND }
  43. if .Create in flags { flags_i |= _O_CREAT }
  44. if .Excl in flags { flags_i |= _O_EXCL }
  45. if .Sync in flags { flags_i |= _O_SYNC }
  46. if .Trunc in flags { flags_i |= _O_TRUNC }
  47. if .Close_On_Exec in flags { flags_i |= _O_CLOEXEC }
  48. fd := unix.sys_open(name_cstr, flags_i, uint(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. file.stream = {
  60. data = file,
  61. procedure = _file_stream_proc,
  62. }
  63. return file
  64. }
  65. _destroy :: proc(f: ^File) -> Error {
  66. if f == nil {
  67. return nil
  68. }
  69. delete(f.impl.name, f.impl.allocator)
  70. free(f, f.impl.allocator)
  71. return nil
  72. }
  73. _close :: proc(f: ^File) -> Error {
  74. if f != nil {
  75. res := unix.sys_close(f.impl.fd)
  76. _destroy(f)
  77. return _ok_or_error(res)
  78. }
  79. return nil
  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. TEMP_ALLOCATOR_GUARD()
  168. name_cstr := temp_cstring(name) or_return
  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. TEMP_ALLOCATOR_GUARD()
  181. old_name_cstr := temp_cstring(old_name) or_return
  182. new_name_cstr := temp_cstring(new_name) or_return
  183. return _ok_or_error(unix.sys_rename(old_name_cstr, new_name_cstr))
  184. }
  185. _link :: proc(old_name, new_name: string) -> Error {
  186. TEMP_ALLOCATOR_GUARD()
  187. old_name_cstr := temp_cstring(old_name) or_return
  188. new_name_cstr := temp_cstring(new_name) or_return
  189. return _ok_or_error(unix.sys_link(old_name_cstr, new_name_cstr))
  190. }
  191. _symlink :: proc(old_name, new_name: string) -> Error {
  192. TEMP_ALLOCATOR_GUARD()
  193. old_name_cstr := temp_cstring(old_name) or_return
  194. new_name_cstr := temp_cstring(new_name) or_return
  195. return _ok_or_error(unix.sys_symlink(old_name_cstr, new_name_cstr))
  196. }
  197. _read_link_cstr :: proc(name_cstr: cstring, allocator: runtime.Allocator) -> (string, Error) {
  198. bufsz : uint = 256
  199. buf := make([]byte, bufsz, allocator)
  200. for {
  201. rc := unix.sys_readlink(name_cstr, &buf[0], bufsz)
  202. if rc < 0 {
  203. delete(buf)
  204. return "", _get_platform_error(rc)
  205. } else if rc == int(bufsz) {
  206. bufsz *= 2
  207. delete(buf)
  208. buf = make([]byte, bufsz, allocator)
  209. } else {
  210. return string(buf[:rc]), nil
  211. }
  212. }
  213. }
  214. _read_link :: proc(name: string, allocator: runtime.Allocator) -> (path: string, err: Error) {
  215. TEMP_ALLOCATOR_GUARD()
  216. name_cstr := temp_cstring(name) or_return
  217. return _read_link_cstr(name_cstr, allocator)
  218. }
  219. _unlink :: proc(name: string) -> Error {
  220. TEMP_ALLOCATOR_GUARD()
  221. name_cstr := temp_cstring(name) or_return
  222. return _ok_or_error(unix.sys_unlink(name_cstr))
  223. }
  224. _chdir :: proc(name: string) -> Error {
  225. TEMP_ALLOCATOR_GUARD()
  226. name_cstr := temp_cstring(name) or_return
  227. return _ok_or_error(unix.sys_chdir(name_cstr))
  228. }
  229. _fchdir :: proc(f: ^File) -> Error {
  230. return _ok_or_error(unix.sys_fchdir(f.impl.fd))
  231. }
  232. _chmod :: proc(name: string, mode: File_Mode) -> Error {
  233. TEMP_ALLOCATOR_GUARD()
  234. name_cstr := temp_cstring(name) or_return
  235. return _ok_or_error(unix.sys_chmod(name_cstr, uint(mode)))
  236. }
  237. _fchmod :: proc(f: ^File, mode: File_Mode) -> Error {
  238. return _ok_or_error(unix.sys_fchmod(f.impl.fd, uint(mode)))
  239. }
  240. // NOTE: will throw error without super user priviledges
  241. _chown :: proc(name: string, uid, gid: int) -> Error {
  242. TEMP_ALLOCATOR_GUARD()
  243. name_cstr := temp_cstring(name) or_return
  244. return _ok_or_error(unix.sys_chown(name_cstr, uid, gid))
  245. }
  246. // NOTE: will throw error without super user priviledges
  247. _lchown :: proc(name: string, uid, gid: int) -> Error {
  248. TEMP_ALLOCATOR_GUARD()
  249. name_cstr := temp_cstring(name) or_return
  250. return _ok_or_error(unix.sys_lchown(name_cstr, uid, gid))
  251. }
  252. // NOTE: will throw error without super user priviledges
  253. _fchown :: proc(f: ^File, uid, gid: int) -> Error {
  254. return _ok_or_error(unix.sys_fchown(f.impl.fd, uid, gid))
  255. }
  256. _chtimes :: proc(name: string, atime, mtime: time.Time) -> Error {
  257. TEMP_ALLOCATOR_GUARD()
  258. name_cstr := temp_cstring(name) or_return
  259. times := [2]Unix_File_Time {
  260. { atime._nsec, 0 },
  261. { mtime._nsec, 0 },
  262. }
  263. return _ok_or_error(unix.sys_utimensat(_AT_FDCWD, name_cstr, &times, 0))
  264. }
  265. _fchtimes :: proc(f: ^File, atime, mtime: time.Time) -> Error {
  266. times := [2]Unix_File_Time {
  267. { atime._nsec, 0 },
  268. { mtime._nsec, 0 },
  269. }
  270. return _ok_or_error(unix.sys_utimensat(f.impl.fd, nil, &times, 0))
  271. }
  272. _exists :: proc(name: string) -> bool {
  273. TEMP_ALLOCATOR_GUARD()
  274. name_cstr, _ := temp_cstring(name)
  275. return unix.sys_access(name_cstr, F_OK) == 0
  276. }
  277. _is_file :: proc(name: string) -> bool {
  278. TEMP_ALLOCATOR_GUARD()
  279. name_cstr, _ := temp_cstring(name)
  280. s: _Stat
  281. res := unix.sys_stat(name_cstr, &s)
  282. if res < 0 {
  283. return false
  284. }
  285. return S_ISREG(s.mode)
  286. }
  287. _is_file_fd :: proc(fd: int) -> bool {
  288. s: _Stat
  289. res := unix.sys_fstat(fd, &s)
  290. if res < 0 { // error
  291. return false
  292. }
  293. return S_ISREG(s.mode)
  294. }
  295. _is_dir :: proc(name: string) -> bool {
  296. TEMP_ALLOCATOR_GUARD()
  297. name_cstr, _ := temp_cstring(name)
  298. s: _Stat
  299. res := unix.sys_stat(name_cstr, &s)
  300. if res < 0 {
  301. return false
  302. }
  303. return S_ISDIR(s.mode)
  304. }
  305. _is_dir_fd :: proc(fd: int) -> bool {
  306. s: _Stat
  307. res := unix.sys_fstat(fd, &s)
  308. if res < 0 { // error
  309. return false
  310. }
  311. return S_ISDIR(s.mode)
  312. }
  313. @(private="package")
  314. _file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
  315. f := (^File)(stream_data)
  316. ferr: Error
  317. switch mode {
  318. case .Read:
  319. n, ferr = _read(f, p)
  320. err = error_to_io_error(ferr)
  321. return
  322. case .Read_At:
  323. n, ferr = _read_at(f, p, offset)
  324. err = error_to_io_error(ferr)
  325. return
  326. case .Write:
  327. n, ferr = _write(f, p)
  328. err = error_to_io_error(ferr)
  329. return
  330. case .Write_At:
  331. n, ferr = _write_at(f, p, offset)
  332. err = error_to_io_error(ferr)
  333. return
  334. case .Seek:
  335. n, ferr = _seek(f, offset, whence)
  336. err = error_to_io_error(ferr)
  337. return
  338. case .Size:
  339. n, ferr = _file_size(f)
  340. err = error_to_io_error(ferr)
  341. return
  342. case .Flush:
  343. ferr = _flush(f)
  344. err = error_to_io_error(ferr)
  345. return
  346. case .Close, .Destroy:
  347. ferr = _close(f)
  348. err = error_to_io_error(ferr)
  349. return
  350. case .Query:
  351. return io.query_utility({.Read, .Read_At, .Write, .Write_At, .Seek, .Size, .Flush, .Close, .Destroy, .Query})
  352. }
  353. return 0, .Empty
  354. }