file_linux.odin 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. //+private
  2. package os2
  3. import "core:io"
  4. import "core:time"
  5. import "base:runtime"
  6. import "core:sys/linux"
  7. _File :: struct {
  8. name: string,
  9. fd: linux.Fd,
  10. allocator: runtime.Allocator,
  11. }
  12. _stdin : File = {
  13. impl = {
  14. name = "/proc/self/fd/0",
  15. fd = 0,
  16. allocator = _file_allocator(),
  17. },
  18. stream = {
  19. procedure = _file_stream_proc,
  20. },
  21. }
  22. _stdout : File = {
  23. impl = {
  24. name = "/proc/self/fd/1",
  25. fd = 1,
  26. allocator = _file_allocator(),
  27. },
  28. stream = {
  29. procedure = _file_stream_proc,
  30. },
  31. }
  32. _stderr : File = {
  33. impl = {
  34. name = "/proc/self/fd/2",
  35. fd = 2,
  36. allocator = _file_allocator(),
  37. },
  38. stream = {
  39. procedure = _file_stream_proc,
  40. },
  41. }
  42. @init
  43. _standard_stream_init :: proc() {
  44. // cannot define these manually because cyclic reference
  45. _stdin.stream.data = &_stdin
  46. _stdout.stream.data = &_stdout
  47. _stderr.stream.data = &_stderr
  48. stdin = &_stdin
  49. stdout = &_stdout
  50. stderr = &_stderr
  51. }
  52. _file_allocator :: proc() -> runtime.Allocator {
  53. return heap_allocator()
  54. }
  55. _open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (f: ^File, err: Error) {
  56. TEMP_ALLOCATOR_GUARD()
  57. name_cstr := temp_cstring(name) or_return
  58. // Just default to using O_NOCTTY because needing to open a controlling
  59. // terminal would be incredibly rare. This has no effect on files while
  60. // allowing us to open serial devices.
  61. sys_flags: linux.Open_Flags = {.NOCTTY}
  62. switch flags & O_RDONLY|O_WRONLY|O_RDWR {
  63. case O_RDONLY:
  64. case O_WRONLY: sys_flags += {.WRONLY}
  65. case O_RDWR: sys_flags += {.RDWR}
  66. }
  67. if .Append in flags { sys_flags += {.APPEND} }
  68. if .Create in flags { sys_flags += {.CREAT} }
  69. if .Excl in flags { sys_flags += {.EXCL} }
  70. if .Sync in flags { sys_flags += {.DSYNC} }
  71. if .Trunc in flags { sys_flags += {.TRUNC} }
  72. if .Close_On_Exec in flags { sys_flags += {.CLOEXEC} }
  73. fd, errno := linux.open(name_cstr, sys_flags, transmute(linux.Mode)(u32(perm)))
  74. if errno != .NONE {
  75. return nil, _get_platform_error(errno)
  76. }
  77. return _new_file(uintptr(fd), name), nil
  78. }
  79. _new_file :: proc(fd: uintptr, _: string = "") -> ^File {
  80. file := new(File, file_allocator())
  81. _construct_file(file, fd, "")
  82. return file
  83. }
  84. _construct_file :: proc(file: ^File, fd: uintptr, _: string = "") {
  85. file^ = {
  86. impl = {
  87. fd = linux.Fd(fd),
  88. allocator = file_allocator(),
  89. name = _get_full_path(file.impl.fd, file.impl.allocator),
  90. },
  91. stream = {
  92. data = file,
  93. procedure = _file_stream_proc,
  94. },
  95. }
  96. }
  97. _destroy :: proc(f: ^File) -> Error {
  98. if f == nil {
  99. return nil
  100. }
  101. delete(f.impl.name, f.impl.allocator)
  102. free(f, f.impl.allocator)
  103. return nil
  104. }
  105. _close :: proc(f: ^File) -> Error {
  106. if f == nil {
  107. return nil
  108. }
  109. errno := linux.close(f.impl.fd)
  110. if errno == .EBADF { // avoid possible double free
  111. return _get_platform_error(errno)
  112. }
  113. _destroy(f)
  114. return _get_platform_error(errno)
  115. }
  116. _fd :: proc(f: ^File) -> uintptr {
  117. if f == nil {
  118. return ~uintptr(0)
  119. }
  120. return uintptr(f.impl.fd)
  121. }
  122. _name :: proc(f: ^File) -> string {
  123. return f.impl.name if f != nil else ""
  124. }
  125. _seek :: proc(f: ^File, offset: i64, whence: io.Seek_From) -> (ret: i64, err: Error) {
  126. n, errno := linux.lseek(f.impl.fd, offset, linux.Seek_Whence(whence))
  127. if errno != .NONE {
  128. return -1, _get_platform_error(errno)
  129. }
  130. return n, nil
  131. }
  132. _read :: proc(f: ^File, p: []byte) -> (i64, Error) {
  133. if len(p) == 0 {
  134. return 0, nil
  135. }
  136. n, errno := linux.read(f.impl.fd, p[:])
  137. if errno != .NONE {
  138. return -1, _get_platform_error(errno)
  139. }
  140. return i64(n), n == 0 ? io.Error.EOF : nil
  141. }
  142. _read_at :: proc(f: ^File, p: []byte, offset: i64) -> (i64, Error) {
  143. if offset < 0 {
  144. return 0, .Invalid_Offset
  145. }
  146. n, errno := linux.pread(f.impl.fd, p[:], offset)
  147. if errno != .NONE {
  148. return -1, _get_platform_error(errno)
  149. }
  150. if n == 0 {
  151. return 0, .EOF
  152. }
  153. return i64(n), nil
  154. }
  155. _write :: proc(f: ^File, p: []byte) -> (i64, Error) {
  156. if len(p) == 0 {
  157. return 0, nil
  158. }
  159. n, errno := linux.write(f.impl.fd, p[:])
  160. if errno != .NONE {
  161. return -1, _get_platform_error(errno)
  162. }
  163. return i64(n), nil
  164. }
  165. _write_at :: proc(f: ^File, p: []byte, offset: i64) -> (i64, Error) {
  166. if offset < 0 {
  167. return 0, .Invalid_Offset
  168. }
  169. n, errno := linux.pwrite(f.impl.fd, p[:], offset)
  170. if errno != .NONE {
  171. return -1, _get_platform_error(errno)
  172. }
  173. return i64(n), nil
  174. }
  175. _file_size :: proc(f: ^File) -> (n: i64, err: Error) {
  176. s: linux.Stat = ---
  177. errno := linux.fstat(f.impl.fd, &s)
  178. if errno != .NONE {
  179. return -1, _get_platform_error(errno)
  180. }
  181. return i64(s.size), nil
  182. }
  183. _sync :: proc(f: ^File) -> Error {
  184. return _get_platform_error(linux.fsync(f.impl.fd))
  185. }
  186. _flush :: proc(f: ^File) -> Error {
  187. return _get_platform_error(linux.fsync(f.impl.fd))
  188. }
  189. _truncate :: proc(f: ^File, size: i64) -> Error {
  190. return _get_platform_error(linux.ftruncate(f.impl.fd, size))
  191. }
  192. _remove :: proc(name: string) -> Error {
  193. TEMP_ALLOCATOR_GUARD()
  194. name_cstr := temp_cstring(name) or_return
  195. fd, errno := linux.open(name_cstr, {.NOFOLLOW})
  196. #partial switch (errno) {
  197. case .ELOOP: /* symlink */
  198. case .NONE:
  199. defer linux.close(fd)
  200. if _is_dir_fd(fd) {
  201. return _get_platform_error(linux.rmdir(name_cstr))
  202. }
  203. case:
  204. return _get_platform_error(errno)
  205. }
  206. return _get_platform_error(linux.unlink(name_cstr))
  207. }
  208. _rename :: proc(old_name, new_name: string) -> Error {
  209. TEMP_ALLOCATOR_GUARD()
  210. old_name_cstr := temp_cstring(old_name) or_return
  211. new_name_cstr := temp_cstring(new_name) or_return
  212. return _get_platform_error(linux.rename(old_name_cstr, new_name_cstr))
  213. }
  214. _link :: proc(old_name, new_name: string) -> Error {
  215. TEMP_ALLOCATOR_GUARD()
  216. old_name_cstr := temp_cstring(old_name) or_return
  217. new_name_cstr := temp_cstring(new_name) or_return
  218. return _get_platform_error(linux.link(old_name_cstr, new_name_cstr))
  219. }
  220. _symlink :: proc(old_name, new_name: string) -> Error {
  221. TEMP_ALLOCATOR_GUARD()
  222. old_name_cstr := temp_cstring(old_name) or_return
  223. new_name_cstr := temp_cstring(new_name) or_return
  224. return _get_platform_error(linux.symlink(old_name_cstr, new_name_cstr))
  225. }
  226. _read_link_cstr :: proc(name_cstr: cstring, allocator: runtime.Allocator) -> (string, Error) {
  227. bufsz : uint = 256
  228. buf := make([]byte, bufsz, allocator)
  229. for {
  230. sz, errno := linux.readlink(name_cstr, buf[:])
  231. if errno != .NONE {
  232. delete(buf, allocator)
  233. return "", _get_platform_error(errno)
  234. } else if sz == int(bufsz) {
  235. bufsz *= 2
  236. delete(buf, allocator)
  237. buf = make([]byte, bufsz, allocator)
  238. } else {
  239. return string(buf[:sz]), nil
  240. }
  241. }
  242. }
  243. _read_link :: proc(name: string, allocator: runtime.Allocator) -> (s: string, e: Error) {
  244. TEMP_ALLOCATOR_GUARD()
  245. name_cstr := temp_cstring(name) or_return
  246. return _read_link_cstr(name_cstr, allocator)
  247. }
  248. _chdir :: proc(name: string) -> Error {
  249. TEMP_ALLOCATOR_GUARD()
  250. name_cstr := temp_cstring(name) or_return
  251. return _get_platform_error(linux.chdir(name_cstr))
  252. }
  253. _fchdir :: proc(f: ^File) -> Error {
  254. return _get_platform_error(linux.fchdir(f.impl.fd))
  255. }
  256. _chmod :: proc(name: string, mode: File_Mode) -> Error {
  257. TEMP_ALLOCATOR_GUARD()
  258. name_cstr := temp_cstring(name) or_return
  259. return _get_platform_error(linux.chmod(name_cstr, transmute(linux.Mode)(u32(mode))))
  260. }
  261. _fchmod :: proc(f: ^File, mode: File_Mode) -> Error {
  262. return _get_platform_error(linux.fchmod(f.impl.fd, transmute(linux.Mode)(u32(mode))))
  263. }
  264. // NOTE: will throw error without super user priviledges
  265. _chown :: proc(name: string, uid, gid: int) -> Error {
  266. TEMP_ALLOCATOR_GUARD()
  267. name_cstr := temp_cstring(name) or_return
  268. return _get_platform_error(linux.chown(name_cstr, linux.Uid(uid), linux.Gid(gid)))
  269. }
  270. // NOTE: will throw error without super user priviledges
  271. _lchown :: proc(name: string, uid, gid: int) -> Error {
  272. TEMP_ALLOCATOR_GUARD()
  273. name_cstr := temp_cstring(name) or_return
  274. return _get_platform_error(linux.lchown(name_cstr, linux.Uid(uid), linux.Gid(gid)))
  275. }
  276. // NOTE: will throw error without super user priviledges
  277. _fchown :: proc(f: ^File, uid, gid: int) -> Error {
  278. return _get_platform_error(linux.fchown(f.impl.fd, linux.Uid(uid), linux.Gid(gid)))
  279. }
  280. _chtimes :: proc(name: string, atime, mtime: time.Time) -> Error {
  281. TEMP_ALLOCATOR_GUARD()
  282. name_cstr := temp_cstring(name) or_return
  283. times := [2]linux.Time_Spec {
  284. {
  285. uint(atime._nsec) / uint(time.Second),
  286. uint(atime._nsec) % uint(time.Second),
  287. },
  288. {
  289. uint(mtime._nsec) / uint(time.Second),
  290. uint(mtime._nsec) % uint(time.Second),
  291. },
  292. }
  293. return _get_platform_error(linux.utimensat(linux.AT_FDCWD, name_cstr, &times[0], nil))
  294. }
  295. _fchtimes :: proc(f: ^File, atime, mtime: time.Time) -> Error {
  296. times := [2]linux.Time_Spec {
  297. {
  298. uint(atime._nsec) / uint(time.Second),
  299. uint(atime._nsec) % uint(time.Second),
  300. },
  301. {
  302. uint(mtime._nsec) / uint(time.Second),
  303. uint(mtime._nsec) % uint(time.Second),
  304. },
  305. }
  306. return _get_platform_error(linux.utimensat(f.impl.fd, nil, &times[0], nil))
  307. }
  308. _exists :: proc(name: string) -> bool {
  309. TEMP_ALLOCATOR_GUARD()
  310. name_cstr, _ := temp_cstring(name)
  311. res, errno := linux.access(name_cstr, linux.F_OK)
  312. return !res && errno == .NONE
  313. }
  314. _is_file :: proc(name: string) -> bool {
  315. TEMP_ALLOCATOR_GUARD()
  316. name_cstr, _ := temp_cstring(name)
  317. s: linux.Stat
  318. if linux.stat(name_cstr, &s) != .NONE {
  319. return false
  320. }
  321. return linux.S_ISREG(s.mode)
  322. }
  323. _is_file_fd :: proc(fd: linux.Fd) -> bool {
  324. s: linux.Stat
  325. if linux.fstat(fd, &s) != .NONE {
  326. return false
  327. }
  328. return linux.S_ISREG(s.mode)
  329. }
  330. _is_dir :: proc(name: string) -> bool {
  331. TEMP_ALLOCATOR_GUARD()
  332. name_cstr, _ := temp_cstring(name)
  333. s: linux.Stat
  334. if linux.stat(name_cstr, &s) != .NONE {
  335. return false
  336. }
  337. return linux.S_ISDIR(s.mode)
  338. }
  339. _is_dir_fd :: proc(fd: linux.Fd) -> bool {
  340. s: linux.Stat
  341. if linux.fstat(fd, &s) != .NONE {
  342. return false
  343. }
  344. return linux.S_ISDIR(s.mode)
  345. }
  346. /* Certain files in the Linux file system are not actual
  347. * files (e.g. everything in /proc/). Therefore, the
  348. * read_entire_file procs fail to actually read anything
  349. * since these "files" stat to a size of 0. Here, we just
  350. * read until there is nothing left.
  351. */
  352. _read_entire_pseudo_file :: proc { _read_entire_pseudo_file_string, _read_entire_pseudo_file_cstring }
  353. _read_entire_pseudo_file_string :: proc(name: string, allocator: runtime.Allocator) -> (b: []u8, e: Error) {
  354. name_cstr := clone_to_cstring(name, allocator) or_return
  355. defer delete(name, allocator)
  356. return _read_entire_pseudo_file_cstring(name_cstr, allocator)
  357. }
  358. _read_entire_pseudo_file_cstring :: proc(name: cstring, allocator: runtime.Allocator) -> ([]u8, Error) {
  359. fd, errno := linux.open(name, {})
  360. if errno != .NONE {
  361. return nil, _get_platform_error(errno)
  362. }
  363. defer linux.close(fd)
  364. BUF_SIZE_STEP :: 128
  365. contents := make([dynamic]u8, 0, BUF_SIZE_STEP, allocator)
  366. n: int
  367. i: int
  368. for {
  369. resize(&contents, i + BUF_SIZE_STEP)
  370. n, errno = linux.read(fd, contents[i:i+BUF_SIZE_STEP])
  371. if errno != .NONE {
  372. delete(contents)
  373. return nil, _get_platform_error(errno)
  374. }
  375. if n < BUF_SIZE_STEP {
  376. break
  377. }
  378. i += BUF_SIZE_STEP
  379. }
  380. resize(&contents, i + n)
  381. return contents[:], nil
  382. }
  383. @(private="package")
  384. _file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
  385. f := (^File)(stream_data)
  386. ferr: Error
  387. switch mode {
  388. case .Read:
  389. n, ferr = _read(f, p)
  390. err = error_to_io_error(ferr)
  391. return
  392. case .Read_At:
  393. n, ferr = _read_at(f, p, offset)
  394. err = error_to_io_error(ferr)
  395. return
  396. case .Write:
  397. n, ferr = _write(f, p)
  398. err = error_to_io_error(ferr)
  399. return
  400. case .Write_At:
  401. n, ferr = _write_at(f, p, offset)
  402. err = error_to_io_error(ferr)
  403. return
  404. case .Seek:
  405. n, ferr = _seek(f, offset, whence)
  406. err = error_to_io_error(ferr)
  407. return
  408. case .Size:
  409. n, ferr = _file_size(f)
  410. err = error_to_io_error(ferr)
  411. return
  412. case .Flush:
  413. ferr = _flush(f)
  414. err = error_to_io_error(ferr)
  415. return
  416. case .Close, .Destroy:
  417. ferr = _close(f)
  418. err = error_to_io_error(ferr)
  419. return
  420. case .Query:
  421. return io.query_utility({.Read, .Read_At, .Write, .Write_At, .Seek, .Size, .Flush, .Close, .Destroy, .Query})
  422. }
  423. return 0, .Empty
  424. }