path_linux.odin 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #+private
  2. package os2
  3. import "core:strings"
  4. import "core:strconv"
  5. import "base:runtime"
  6. import "core:sys/linux"
  7. _Path_Separator :: '/'
  8. _Path_Separator_String :: "/"
  9. _Path_List_Separator :: ':'
  10. _OPENDIR_FLAGS : linux.Open_Flags : {.NONBLOCK, .DIRECTORY, .LARGEFILE, .CLOEXEC}
  11. _is_path_separator :: proc(c: byte) -> bool {
  12. return c == '/'
  13. }
  14. _mkdir :: proc(path: string, perm: int) -> Error {
  15. TEMP_ALLOCATOR_GUARD()
  16. path_cstr := temp_cstring(path) or_return
  17. return _get_platform_error(linux.mkdir(path_cstr, transmute(linux.Mode)u32(perm)))
  18. }
  19. _mkdir_all :: proc(path: string, perm: int) -> Error {
  20. mkdirat :: proc(dfd: linux.Fd, path: []u8, perm: int, has_created: ^bool) -> Error {
  21. i: int
  22. for ; i < len(path) - 1 && path[i] != '/'; i += 1 {}
  23. if i == 0 {
  24. return _get_platform_error(linux.close(dfd))
  25. }
  26. path[i] = 0
  27. new_dfd, errno := linux.openat(dfd, cstring(&path[0]), _OPENDIR_FLAGS)
  28. #partial switch errno {
  29. case .ENOENT:
  30. if errno = linux.mkdirat(dfd, cstring(&path[0]), transmute(linux.Mode)u32(perm)); errno != .NONE {
  31. return _get_platform_error(errno)
  32. }
  33. has_created^ = true
  34. if new_dfd, errno = linux.openat(dfd, cstring(&path[0]), _OPENDIR_FLAGS); errno != .NONE {
  35. return _get_platform_error(errno)
  36. }
  37. fallthrough
  38. case .NONE:
  39. if errno = linux.close(dfd); errno != .NONE {
  40. return _get_platform_error(errno)
  41. }
  42. // skip consecutive '/'
  43. for i += 1; i < len(path) && path[i] == '/'; i += 1 {}
  44. return mkdirat(new_dfd, path[i:], perm, has_created)
  45. }
  46. return _get_platform_error(errno)
  47. }
  48. TEMP_ALLOCATOR_GUARD()
  49. // need something we can edit, and use to generate cstrings
  50. path_bytes := make([]u8, len(path) + 1, temp_allocator())
  51. // zero terminate the byte slice to make it a valid cstring
  52. copy(path_bytes, path)
  53. path_bytes[len(path)] = 0
  54. dfd: linux.Fd
  55. errno: linux.Errno
  56. if path_bytes[0] == '/' {
  57. dfd, errno = linux.open("/", _OPENDIR_FLAGS)
  58. path_bytes = path_bytes[1:]
  59. } else {
  60. dfd, errno = linux.open(".", _OPENDIR_FLAGS)
  61. }
  62. if errno != .NONE {
  63. return _get_platform_error(errno)
  64. }
  65. has_created: bool
  66. mkdirat(dfd, path_bytes, perm, &has_created) or_return
  67. return nil if has_created else .Exist
  68. }
  69. _remove_all :: proc(path: string) -> Error {
  70. remove_all_dir :: proc(dfd: linux.Fd) -> Error {
  71. n := 64
  72. buf := make([]u8, n)
  73. defer delete(buf)
  74. loop: for {
  75. buflen, errno := linux.getdents(dfd, buf[:])
  76. #partial switch errno {
  77. case .EINVAL:
  78. delete(buf)
  79. n *= 2
  80. buf = make([]u8, n)
  81. continue loop
  82. case .NONE:
  83. if buflen == 0 { break loop }
  84. case:
  85. return _get_platform_error(errno)
  86. }
  87. offset: int
  88. for d in linux.dirent_iterate_buf(buf[:buflen], &offset) {
  89. d_name_str := linux.dirent_name(d)
  90. d_name_cstr := strings.unsafe_string_to_cstring(d_name_str)
  91. /* check for current or parent directory (. or ..) */
  92. if d_name_str == "." || d_name_str == ".." {
  93. continue
  94. }
  95. #partial switch d.type {
  96. case .DIR:
  97. new_dfd: linux.Fd
  98. new_dfd, errno = linux.openat(dfd, d_name_cstr, _OPENDIR_FLAGS)
  99. if errno != .NONE {
  100. return _get_platform_error(errno)
  101. }
  102. defer linux.close(new_dfd)
  103. remove_all_dir(new_dfd) or_return
  104. errno = linux.unlinkat(dfd, d_name_cstr, {.REMOVEDIR})
  105. case:
  106. errno = linux.unlinkat(dfd, d_name_cstr, nil)
  107. }
  108. if errno != .NONE {
  109. return _get_platform_error(errno)
  110. }
  111. }
  112. }
  113. return nil
  114. }
  115. TEMP_ALLOCATOR_GUARD()
  116. path_cstr := temp_cstring(path) or_return
  117. fd, errno := linux.open(path_cstr, _OPENDIR_FLAGS)
  118. #partial switch errno {
  119. case .NONE:
  120. break
  121. case .ENOTDIR:
  122. return _get_platform_error(linux.unlink(path_cstr))
  123. case:
  124. return _get_platform_error(errno)
  125. }
  126. defer linux.close(fd)
  127. remove_all_dir(fd) or_return
  128. return _get_platform_error(linux.rmdir(path_cstr))
  129. }
  130. _get_working_directory :: proc(allocator: runtime.Allocator) -> (string, Error) {
  131. // NOTE(tetra): I would use PATH_MAX here, but I was not able to find
  132. // an authoritative value for it across all systems.
  133. // The largest value I could find was 4096, so might as well use the page size.
  134. // NOTE(jason): Avoiding libc, so just use 4096 directly
  135. PATH_MAX :: 4096
  136. buf := make([dynamic]u8, PATH_MAX, allocator)
  137. for {
  138. #no_bounds_check n, errno := linux.getcwd(buf[:])
  139. if errno == .NONE {
  140. return string(buf[:n-1]), nil
  141. }
  142. if errno != .ERANGE {
  143. return "", _get_platform_error(errno)
  144. }
  145. resize(&buf, len(buf)+PATH_MAX)
  146. }
  147. unreachable()
  148. }
  149. _set_working_directory :: proc(dir: string) -> Error {
  150. dir_cstr := temp_cstring(dir) or_return
  151. return _get_platform_error(linux.chdir(dir_cstr))
  152. }
  153. _get_full_path :: proc(fd: linux.Fd, allocator: runtime.Allocator) -> (fullpath: string, err: Error) {
  154. PROC_FD_PATH :: "/proc/self/fd/"
  155. buf: [32]u8
  156. copy(buf[:], PROC_FD_PATH)
  157. strconv.itoa(buf[len(PROC_FD_PATH):], int(fd))
  158. if fullpath, err = _read_link_cstr(cstring(&buf[0]), allocator); err != nil || fullpath[0] != '/' {
  159. delete(fullpath, allocator)
  160. fullpath = ""
  161. }
  162. return
  163. }