file_linux.odin 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. }
  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. 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. res := unix.sys_close(f.impl.fd)
  75. return _ok_or_error(res)
  76. }
  77. _fd :: proc(f: ^File) -> uintptr {
  78. if f == nil {
  79. return ~uintptr(0)
  80. }
  81. return uintptr(f.impl.fd)
  82. }
  83. _name :: proc(f: ^File) -> string {
  84. return f.impl.name if f != nil else ""
  85. }
  86. _seek :: proc(f: ^File, offset: i64, whence: Seek_From) -> (ret: i64, err: Error) {
  87. res := unix.sys_lseek(f.impl.fd, offset, int(whence))
  88. if res < 0 {
  89. return -1, _get_platform_error(int(res))
  90. }
  91. return res, nil
  92. }
  93. _read :: proc(f: ^File, p: []byte) -> (n: int, err: Error) {
  94. if len(p) == 0 {
  95. return 0, nil
  96. }
  97. n = unix.sys_read(f.impl.fd, &p[0], len(p))
  98. if n < 0 {
  99. return -1, _get_platform_error(n)
  100. }
  101. return n, nil
  102. }
  103. _read_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: int, err: Error) {
  104. if offset < 0 {
  105. return 0, .Invalid_Offset
  106. }
  107. b, offset := p, offset
  108. for len(b) > 0 {
  109. m := unix.sys_pread(f.impl.fd, &b[0], len(b), offset)
  110. if m < 0 {
  111. return -1, _get_platform_error(m)
  112. }
  113. n += m
  114. b = b[m:]
  115. offset += i64(m)
  116. }
  117. return
  118. }
  119. _read_from :: proc(f: ^File, r: io.Reader) -> (n: i64, err: Error) {
  120. //TODO
  121. return
  122. }
  123. _write :: proc(f: ^File, p: []byte) -> (n: int, err: 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 int(n), nil
  132. }
  133. _write_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: int, 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 += m
  144. b = b[m:]
  145. offset += i64(m)
  146. }
  147. return
  148. }
  149. _write_to :: proc(f: ^File, w: io.Writer) -> (n: i64, err: Error) {
  150. //TODO
  151. return
  152. }
  153. _file_size :: proc(f: ^File) -> (n: i64, err: Error) {
  154. s: _Stat = ---
  155. res := unix.sys_fstat(f.impl.fd, &s)
  156. if res < 0 {
  157. return -1, _get_platform_error(res)
  158. }
  159. return s.size, nil
  160. }
  161. _sync :: proc(f: ^File) -> Error {
  162. return _ok_or_error(unix.sys_fsync(f.impl.fd))
  163. }
  164. _flush :: proc(f: ^File) -> Error {
  165. return _ok_or_error(unix.sys_fsync(f.impl.fd))
  166. }
  167. _truncate :: proc(f: ^File, size: i64) -> Error {
  168. return _ok_or_error(unix.sys_ftruncate(f.impl.fd, size))
  169. }
  170. _remove :: proc(name: string) -> Error {
  171. name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
  172. fd := unix.sys_open(name_cstr, int(File_Flags.Read))
  173. if fd < 0 {
  174. return _get_platform_error(fd)
  175. }
  176. defer unix.sys_close(fd)
  177. if _is_dir_fd(fd) {
  178. return _ok_or_error(unix.sys_rmdir(name_cstr))
  179. }
  180. return _ok_or_error(unix.sys_unlink(name_cstr))
  181. }
  182. _rename :: proc(old_name, new_name: string) -> Error {
  183. old_name_cstr := strings.clone_to_cstring(old_name, context.temp_allocator)
  184. new_name_cstr := strings.clone_to_cstring(new_name, context.temp_allocator)
  185. return _ok_or_error(unix.sys_rename(old_name_cstr, new_name_cstr))
  186. }
  187. _link :: proc(old_name, new_name: string) -> Error {
  188. old_name_cstr := strings.clone_to_cstring(old_name, context.temp_allocator)
  189. new_name_cstr := strings.clone_to_cstring(new_name, context.temp_allocator)
  190. return _ok_or_error(unix.sys_link(old_name_cstr, new_name_cstr))
  191. }
  192. _symlink :: proc(old_name, new_name: string) -> Error {
  193. old_name_cstr := strings.clone_to_cstring(old_name, context.temp_allocator)
  194. new_name_cstr := strings.clone_to_cstring(new_name, context.temp_allocator)
  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 strings.string_from_ptr(&buf[0], rc), nil
  211. }
  212. }
  213. }
  214. _read_link :: proc(name: string, allocator: runtime.Allocator) -> (string, Error) {
  215. name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
  216. return _read_link_cstr(name_cstr, allocator)
  217. }
  218. _unlink :: proc(name: string) -> Error {
  219. name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
  220. return _ok_or_error(unix.sys_unlink(name_cstr))
  221. }
  222. _chdir :: proc(name: string) -> Error {
  223. name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
  224. return _ok_or_error(unix.sys_chdir(name_cstr))
  225. }
  226. _fchdir :: proc(f: ^File) -> Error {
  227. return _ok_or_error(unix.sys_fchdir(f.impl.fd))
  228. }
  229. _chmod :: proc(name: string, mode: File_Mode) -> Error {
  230. name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
  231. return _ok_or_error(unix.sys_chmod(name_cstr, uint(mode)))
  232. }
  233. _fchmod :: proc(f: ^File, mode: File_Mode) -> Error {
  234. return _ok_or_error(unix.sys_fchmod(f.impl.fd, uint(mode)))
  235. }
  236. // NOTE: will throw error without super user priviledges
  237. _chown :: proc(name: string, uid, gid: int) -> Error {
  238. name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
  239. return _ok_or_error(unix.sys_chown(name_cstr, uid, gid))
  240. }
  241. // NOTE: will throw error without super user priviledges
  242. _lchown :: proc(name: string, uid, gid: int) -> Error {
  243. name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
  244. return _ok_or_error(unix.sys_lchown(name_cstr, uid, gid))
  245. }
  246. // NOTE: will throw error without super user priviledges
  247. _fchown :: proc(f: ^File, uid, gid: int) -> Error {
  248. return _ok_or_error(unix.sys_fchown(f.impl.fd, uid, gid))
  249. }
  250. _chtimes :: proc(name: string, atime, mtime: time.Time) -> Error {
  251. name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
  252. times := [2]Unix_File_Time {
  253. { atime._nsec, 0 },
  254. { mtime._nsec, 0 },
  255. }
  256. return _ok_or_error(unix.sys_utimensat(_AT_FDCWD, name_cstr, &times, 0))
  257. }
  258. _fchtimes :: proc(f: ^File, atime, mtime: time.Time) -> Error {
  259. times := [2]Unix_File_Time {
  260. { atime._nsec, 0 },
  261. { mtime._nsec, 0 },
  262. }
  263. return _ok_or_error(unix.sys_utimensat(f.impl.fd, nil, &times, 0))
  264. }
  265. _exists :: proc(name: string) -> bool {
  266. name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
  267. return unix.sys_access(name_cstr, F_OK) == 0
  268. }
  269. _is_file :: proc(name: string) -> bool {
  270. name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
  271. s: _Stat
  272. res := unix.sys_stat(name_cstr, &s)
  273. if res < 0 {
  274. return false
  275. }
  276. return S_ISREG(s.mode)
  277. }
  278. _is_file_fd :: proc(fd: int) -> bool {
  279. s: _Stat
  280. res := unix.sys_fstat(fd, &s)
  281. if res < 0 { // error
  282. return false
  283. }
  284. return S_ISREG(s.mode)
  285. }
  286. _is_dir :: proc(name: string) -> bool {
  287. name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
  288. s: _Stat
  289. res := unix.sys_stat(name_cstr, &s)
  290. if res < 0 {
  291. return false
  292. }
  293. return S_ISDIR(s.mode)
  294. }
  295. _is_dir_fd :: proc(fd: int) -> bool {
  296. s: _Stat
  297. res := unix.sys_fstat(fd, &s)
  298. if res < 0 { // error
  299. return false
  300. }
  301. return S_ISDIR(s.mode)
  302. }
  303. // Ideally we want to use the temp_allocator. PATH_MAX on Linux is commonly
  304. // defined as 512, however, it is well known that paths can exceed that limit.
  305. // So, in theory you could have a path larger than the entire temp_allocator's
  306. // buffer. Therefor, any large paths will use context.allocator.
  307. @(private="file")
  308. _temp_name_to_cstring :: proc(name: string) -> (cname: cstring) {
  309. return strings.clone_to_cstring(name, context.temp_allocator)
  310. }