dir_windows.odin 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #+private
  2. package os2
  3. import "base:runtime"
  4. import "core:time"
  5. import win32 "core:sys/windows"
  6. @(private="file")
  7. find_data_to_file_info :: proc(base_path: string, d: ^win32.WIN32_FIND_DATAW, allocator: runtime.Allocator) -> (fi: File_Info, err: Error) {
  8. // Ignore "." and ".."
  9. if d.cFileName[0] == '.' && d.cFileName[1] == 0 {
  10. return
  11. }
  12. if d.cFileName[0] == '.' && d.cFileName[1] == '.' && d.cFileName[2] == 0 {
  13. return
  14. }
  15. temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
  16. path := concatenate({base_path, `\`, win32_wstring_to_utf8(cstring16(raw_data(d.cFileName[:])), temp_allocator) or_else ""}, allocator) or_return
  17. handle := win32.HANDLE(_open_internal(path, {.Read}, 0o666) or_else 0)
  18. defer win32.CloseHandle(handle)
  19. fi.fullpath = path
  20. fi.name = basename(path)
  21. fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
  22. fi.type, fi.mode = _file_type_mode_from_file_attributes(d.dwFileAttributes, handle, d.dwReserved0)
  23. fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
  24. fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
  25. fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
  26. if file_id_info: win32.FILE_ID_INFO; handle != nil && win32.GetFileInformationByHandleEx(handle, .FileIdInfo, &file_id_info, size_of(file_id_info)) {
  27. #assert(size_of(fi.inode) == size_of(file_id_info.FileId))
  28. #assert(size_of(fi.inode) == 16)
  29. runtime.mem_copy_non_overlapping(&fi.inode, &file_id_info.FileId, 16)
  30. }
  31. return
  32. }
  33. Read_Directory_Iterator_Impl :: struct {
  34. find_data: win32.WIN32_FIND_DATAW,
  35. find_handle: win32.HANDLE,
  36. path: string,
  37. prev_fi: File_Info,
  38. no_more_files: bool,
  39. }
  40. @(require_results)
  41. _read_directory_iterator :: proc(it: ^Read_Directory_Iterator) -> (fi: File_Info, index: int, ok: bool) {
  42. for !it.impl.no_more_files {
  43. err: Error
  44. file_info_delete(it.impl.prev_fi, file_allocator())
  45. it.impl.prev_fi = {}
  46. fi, err = find_data_to_file_info(it.impl.path, &it.impl.find_data, file_allocator())
  47. if err != nil {
  48. read_directory_iterator_set_error(it, it.impl.path, err)
  49. return
  50. }
  51. if fi.name != "" {
  52. it.impl.prev_fi = fi
  53. ok = true
  54. index = it.index
  55. it.index += 1
  56. }
  57. if !win32.FindNextFileW(it.impl.find_handle, &it.impl.find_data) {
  58. e := _get_platform_error()
  59. if pe, _ := is_platform_error(e); pe != i32(win32.ERROR_NO_MORE_FILES) {
  60. read_directory_iterator_set_error(it, it.impl.path, e)
  61. }
  62. it.impl.no_more_files = true
  63. }
  64. if ok {
  65. return
  66. }
  67. }
  68. return
  69. }
  70. _read_directory_iterator_init :: proc(it: ^Read_Directory_Iterator, f: ^File) {
  71. it.impl.no_more_files = false
  72. if f == nil || f.impl == nil {
  73. read_directory_iterator_set_error(it, "", .Invalid_File)
  74. return
  75. }
  76. it.f = f
  77. impl := (^File_Impl)(f.impl)
  78. // NOTE: Allow calling `init` to target a new directory with the same iterator - reset idx.
  79. if it.impl.find_handle != nil {
  80. win32.FindClose(it.impl.find_handle)
  81. }
  82. if it.impl.path != "" {
  83. delete(it.impl.path, file_allocator())
  84. }
  85. if !is_directory(impl.name) {
  86. read_directory_iterator_set_error(it, impl.name, .Invalid_Dir)
  87. return
  88. }
  89. wpath := string16(impl.wname)
  90. temp_allocator := TEMP_ALLOCATOR_GUARD({})
  91. wpath_search := make([]u16, len(wpath)+3, temp_allocator)
  92. copy(wpath_search, wpath)
  93. wpath_search[len(wpath)+0] = '\\'
  94. wpath_search[len(wpath)+1] = '*'
  95. wpath_search[len(wpath)+2] = 0
  96. it.impl.find_handle = win32.FindFirstFileW(cstring16(raw_data(wpath_search)), &it.impl.find_data)
  97. if it.impl.find_handle == win32.INVALID_HANDLE_VALUE {
  98. read_directory_iterator_set_error(it, impl.name, _get_platform_error())
  99. return
  100. }
  101. defer if it.err.err != nil {
  102. win32.FindClose(it.impl.find_handle)
  103. }
  104. err: Error
  105. it.impl.path, err = _cleanpath_from_buf(wpath, file_allocator())
  106. if err != nil {
  107. read_directory_iterator_set_error(it, impl.name, err)
  108. }
  109. return
  110. }
  111. _read_directory_iterator_destroy :: proc(it: ^Read_Directory_Iterator) {
  112. if it.f == nil {
  113. return
  114. }
  115. file_info_delete(it.impl.prev_fi, file_allocator())
  116. delete(it.impl.path, file_allocator())
  117. win32.FindClose(it.impl.find_handle)
  118. }