dir_windows.odin 3.5 KB

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