path_posix.odin 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #+private
  2. #+build darwin, netbsd, freebsd, openbsd
  3. package os2
  4. import "base:runtime"
  5. import "core:sys/posix"
  6. _Path_Separator :: '/'
  7. _Path_Separator_String :: "/"
  8. _Path_List_Separator :: ':'
  9. _is_path_separator :: proc(c: byte) -> bool {
  10. return c == _Path_Separator
  11. }
  12. _mkdir :: proc(name: string, perm: int) -> (err: Error) {
  13. temp_allocator := TEMP_ALLOCATOR_GUARD({})
  14. cname := clone_to_cstring(name, temp_allocator) or_return
  15. if posix.mkdir(cname, transmute(posix.mode_t)posix._mode_t(perm)) != .OK {
  16. return _get_platform_error()
  17. }
  18. return nil
  19. }
  20. _mkdir_all :: proc(path: string, perm: int) -> Error {
  21. if path == "" {
  22. return .Invalid_Path
  23. }
  24. temp_allocator := TEMP_ALLOCATOR_GUARD({})
  25. if exists(path) {
  26. return .Exist
  27. }
  28. clean_path := clean_path(path, temp_allocator) or_return
  29. return internal_mkdir_all(clean_path, perm)
  30. internal_mkdir_all :: proc(path: string, perm: int) -> Error {
  31. dir, file := split_path(path)
  32. if file != path && dir != "/" {
  33. if len(dir) > 1 && dir[len(dir) - 1] == '/' {
  34. dir = dir[:len(dir) - 1]
  35. }
  36. internal_mkdir_all(dir, perm) or_return
  37. }
  38. err := _mkdir(path, perm)
  39. if err == .Exist { err = nil }
  40. return err
  41. }
  42. }
  43. _remove_all :: proc(path: string) -> (err: Error) {
  44. temp_allocator := TEMP_ALLOCATOR_GUARD({})
  45. cpath := clone_to_cstring(path, temp_allocator) or_return
  46. dir := posix.opendir(cpath)
  47. if dir == nil {
  48. return _get_platform_error()
  49. }
  50. defer posix.closedir(dir)
  51. for {
  52. posix.set_errno(.NONE)
  53. entry := posix.readdir(dir)
  54. if entry == nil {
  55. if errno := posix.errno(); errno != .NONE {
  56. return _get_platform_error()
  57. } else {
  58. break
  59. }
  60. }
  61. cname := cstring(raw_data(entry.d_name[:]))
  62. if cname == "." || cname == ".." {
  63. continue
  64. }
  65. fullpath, _ := concatenate({path, "/", string(cname), "\x00"}, temp_allocator)
  66. if entry.d_type == .DIR {
  67. _remove_all(fullpath[:len(fullpath)-1]) or_return
  68. } else {
  69. if posix.unlink(cstring(raw_data(fullpath))) != .OK {
  70. return _get_platform_error()
  71. }
  72. }
  73. }
  74. if posix.rmdir(cpath) != .OK {
  75. return _get_platform_error()
  76. }
  77. return nil
  78. }
  79. _get_working_directory :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
  80. temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
  81. buf: [dynamic]byte
  82. buf.allocator = temp_allocator
  83. size := uint(posix.PATH_MAX)
  84. cwd: cstring
  85. for ; cwd == nil; size *= 2 {
  86. resize(&buf, size)
  87. cwd = posix.getcwd(raw_data(buf), len(buf))
  88. if cwd == nil && posix.errno() != .ERANGE {
  89. err = _get_platform_error()
  90. return
  91. }
  92. }
  93. return clone_string(string(cwd), allocator)
  94. }
  95. _set_working_directory :: proc(dir: string) -> (err: Error) {
  96. temp_allocator := TEMP_ALLOCATOR_GUARD({})
  97. cdir := clone_to_cstring(dir, temp_allocator) or_return
  98. if posix.chdir(cdir) != .OK {
  99. err = _get_platform_error()
  100. }
  101. return
  102. }
  103. _get_absolute_path :: proc(path: string, allocator: runtime.Allocator) -> (absolute_path: string, err: Error) {
  104. rel := path
  105. if rel == "" {
  106. rel = "."
  107. }
  108. temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
  109. rel_cstr := clone_to_cstring(rel, temp_allocator) or_return
  110. path_ptr := posix.realpath(rel_cstr, nil)
  111. if path_ptr == nil {
  112. return "", Platform_Error(posix.errno())
  113. }
  114. defer posix.free(path_ptr)
  115. path_str := clone_string(string(path_ptr), allocator) or_return
  116. return path_str, nil
  117. }