stat_windows.odin 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //+private
  2. package os2
  3. import "core:time"
  4. import win32 "core:sys/windows"
  5. _fstat :: proc(fd: Handle, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
  6. if fd == 0 {
  7. return {}, Path_Error{err = .Invalid_Argument}
  8. }
  9. context.allocator = allocator
  10. path, err := _cleanpath_from_handle(fd)
  11. if err != nil {
  12. return {}, err
  13. }
  14. h := win32.HANDLE(fd)
  15. switch win32.GetFileType(h) {
  16. case win32.FILE_TYPE_PIPE, win32.FILE_TYPE_CHAR:
  17. fi: File_Info
  18. fi.fullpath = path
  19. fi.name = basename(path)
  20. fi.mode |= file_type_mode(h)
  21. return fi, nil
  22. }
  23. return _file_info_from_get_file_information_by_handle(path, h)
  24. }
  25. _stat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
  26. return internal_stat(name, win32.FILE_FLAG_BACKUP_SEMANTICS)
  27. }
  28. _lstat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
  29. return internal_stat(name, win32.FILE_FLAG_BACKUP_SEMANTICS|win32.FILE_FLAG_OPEN_REPARSE_POINT)
  30. }
  31. _same_file :: proc(fi1, fi2: File_Info) -> bool {
  32. return fi1.fullpath == fi2.fullpath
  33. }
  34. _stat_errno :: proc(errno: win32.DWORD) -> Path_Error {
  35. return Path_Error{err = Platform_Error{i32(errno)}}
  36. }
  37. full_path_from_name :: proc(name: string, allocator := context.allocator) -> (path: string, err: Maybe(Path_Error)) {
  38. context.allocator = allocator
  39. name := name
  40. if name == "" {
  41. name = "."
  42. }
  43. p := win32.utf8_to_utf16(name, context.temp_allocator)
  44. buf := make([dynamic]u16, 100)
  45. for {
  46. n := win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil)
  47. if n == 0 {
  48. delete(buf)
  49. return "", _stat_errno(win32.GetLastError())
  50. }
  51. if n <= u32(len(buf)) {
  52. return win32.utf16_to_utf8(buf[:n]), nil
  53. }
  54. resize(&buf, len(buf)*2)
  55. }
  56. return
  57. }
  58. internal_stat :: proc(name: string, create_file_attributes: u32, allocator := context.allocator) -> (fi: File_Info, e: Maybe(Path_Error)) {
  59. if len(name) == 0 {
  60. return {}, Path_Error{err = .Not_Exist}
  61. }
  62. context.allocator = allocator
  63. wname := win32.utf8_to_wstring(_fix_long_path(name), context.temp_allocator)
  64. fa: win32.WIN32_FILE_ATTRIBUTE_DATA
  65. ok := win32.GetFileAttributesExW(wname, win32.GetFileExInfoStandard, &fa)
  66. if ok && fa.dwFileAttributes & win32.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
  67. // Not a symlink
  68. return _file_info_from_win32_file_attribute_data(&fa, name)
  69. }
  70. err := 0 if ok else win32.GetLastError()
  71. if err == win32.ERROR_SHARING_VIOLATION {
  72. fd: win32.WIN32_FIND_DATAW
  73. sh := win32.FindFirstFileW(wname, &fd)
  74. if sh == win32.INVALID_HANDLE_VALUE {
  75. e = Path_Error{err = Platform_Error{i32(win32.GetLastError())}}
  76. return
  77. }
  78. win32.FindClose(sh)
  79. return _file_info_from_win32_find_data(&fd, name)
  80. }
  81. h := win32.CreateFileW(wname, 0, 0, nil, win32.OPEN_EXISTING, create_file_attributes, nil)
  82. if h == win32.INVALID_HANDLE_VALUE {
  83. e = Path_Error{err = Platform_Error{i32(win32.GetLastError())}}
  84. return
  85. }
  86. defer win32.CloseHandle(h)
  87. return _file_info_from_get_file_information_by_handle(name, h)
  88. }
  89. _cleanpath_strip_prefix :: proc(buf: []u16) -> []u16 {
  90. buf := buf
  91. N := 0
  92. for c, i in buf {
  93. if c == 0 { break }
  94. N = i+1
  95. }
  96. buf = buf[:N]
  97. if len(buf) >= 4 {
  98. if buf[0] == '\\' &&
  99. buf[1] == '\\' &&
  100. buf[2] == '?' &&
  101. buf[3] == '\\' {
  102. buf = buf[4:]
  103. }
  104. }
  105. return buf
  106. }
  107. _cleanpath_from_handle :: proc(fd: Handle) -> (string, Maybe(Path_Error)) {
  108. if fd == 0 {
  109. return "", Path_Error{err = .Invalid_Argument}
  110. }
  111. h := win32.HANDLE(fd)
  112. MAX_PATH := win32.DWORD(260) + 1
  113. buf: []u16
  114. for {
  115. buf = make([]u16, MAX_PATH, context.temp_allocator)
  116. err := win32.GetFinalPathNameByHandleW(h, raw_data(buf), MAX_PATH, 0)
  117. switch err {
  118. case win32.ERROR_PATH_NOT_FOUND, win32.ERROR_INVALID_PARAMETER:
  119. return "", _stat_errno(err)
  120. case win32.ERROR_NOT_ENOUGH_MEMORY:
  121. MAX_PATH = MAX_PATH*2 + 1
  122. continue
  123. }
  124. break
  125. }
  126. return _cleanpath_from_buf(buf), nil
  127. }
  128. _cleanpath_from_handle_u16 :: proc(fd: Handle) -> ([]u16, Maybe(Path_Error)) {
  129. if fd == 0 {
  130. return nil, Path_Error{err = .Invalid_Argument}
  131. }
  132. h := win32.HANDLE(fd)
  133. MAX_PATH := win32.DWORD(260) + 1
  134. buf: []u16
  135. for {
  136. buf = make([]u16, MAX_PATH, context.temp_allocator)
  137. err := win32.GetFinalPathNameByHandleW(h, raw_data(buf), MAX_PATH, 0)
  138. switch err {
  139. case win32.ERROR_PATH_NOT_FOUND, win32.ERROR_INVALID_PARAMETER:
  140. return nil, _stat_errno(err)
  141. case win32.ERROR_NOT_ENOUGH_MEMORY:
  142. MAX_PATH = MAX_PATH*2 + 1
  143. continue
  144. }
  145. break
  146. }
  147. return _cleanpath_strip_prefix(buf), nil
  148. }
  149. _cleanpath_from_buf :: proc(buf: []u16) -> string {
  150. buf := buf
  151. buf = _cleanpath_strip_prefix(buf)
  152. return win32.utf16_to_utf8(buf, context.allocator)
  153. }
  154. basename :: proc(name: string) -> (base: string) {
  155. name := name
  156. if len(name) > 3 && name[:3] == `\\?` {
  157. name = name[3:]
  158. }
  159. if len(name) == 2 && name[1] == ':' {
  160. return "."
  161. } else if len(name) > 2 && name[1] == ':' {
  162. name = name[2:]
  163. }
  164. i := len(name)-1
  165. for ; i > 0 && (name[i] == '/' || name[i] == '\\'); i -= 1 {
  166. name = name[:i]
  167. }
  168. for i -= 1; i >= 0; i -= 1 {
  169. if name[i] == '/' || name[i] == '\\' {
  170. name = name[i+1:]
  171. break
  172. }
  173. }
  174. return name
  175. }
  176. file_type_mode :: proc(h: win32.HANDLE) -> File_Mode {
  177. switch win32.GetFileType(h) {
  178. case win32.FILE_TYPE_PIPE:
  179. return File_Mode_Named_Pipe
  180. case win32.FILE_TYPE_CHAR:
  181. return File_Mode_Device | File_Mode_Char_Device
  182. }
  183. return 0
  184. }
  185. _file_mode_from_file_attributes :: proc(FileAttributes: win32.DWORD, h: win32.HANDLE, ReparseTag: win32.DWORD) -> (mode: File_Mode) {
  186. if FileAttributes & win32.FILE_ATTRIBUTE_READONLY != 0 {
  187. mode |= 0o444
  188. } else {
  189. mode |= 0o666
  190. }
  191. is_sym := false
  192. if FileAttributes & win32.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
  193. is_sym = false
  194. } else {
  195. is_sym = ReparseTag == win32.IO_REPARSE_TAG_SYMLINK || ReparseTag == win32.IO_REPARSE_TAG_MOUNT_POINT
  196. }
  197. if is_sym {
  198. mode |= File_Mode_Sym_Link
  199. } else {
  200. if FileAttributes & win32.FILE_ATTRIBUTE_DIRECTORY != 0 {
  201. mode |= 0o111 | File_Mode_Dir
  202. }
  203. if h != nil {
  204. mode |= file_type_mode(h)
  205. }
  206. }
  207. return
  208. }
  209. _file_info_from_win32_file_attribute_data :: proc(d: ^win32.WIN32_FILE_ATTRIBUTE_DATA, name: string) -> (fi: File_Info, e: Maybe(Path_Error)) {
  210. fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
  211. fi.mode |= _file_mode_from_file_attributes(d.dwFileAttributes, nil, 0)
  212. fi.is_dir = fi.mode & File_Mode_Dir != 0
  213. fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
  214. fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
  215. fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
  216. fi.fullpath, e = full_path_from_name(name)
  217. fi.name = basename(fi.fullpath)
  218. return
  219. }
  220. _file_info_from_win32_find_data :: proc(d: ^win32.WIN32_FIND_DATAW, name: string) -> (fi: File_Info, e: Maybe(Path_Error)) {
  221. fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
  222. fi.mode |= _file_mode_from_file_attributes(d.dwFileAttributes, nil, 0)
  223. fi.is_dir = fi.mode & File_Mode_Dir != 0
  224. fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
  225. fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
  226. fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
  227. fi.fullpath, e = full_path_from_name(name)
  228. fi.name = basename(fi.fullpath)
  229. return
  230. }
  231. _file_info_from_get_file_information_by_handle :: proc(path: string, h: win32.HANDLE) -> (File_Info, Maybe(Path_Error)) {
  232. d: win32.BY_HANDLE_FILE_INFORMATION
  233. if !win32.GetFileInformationByHandle(h, &d) {
  234. return {}, _stat_errno(win32.GetLastError())
  235. }
  236. ti: win32.FILE_ATTRIBUTE_TAG_INFO
  237. if !win32.GetFileInformationByHandleEx(h, .FileAttributeTagInfo, &ti, size_of(ti)) {
  238. err := win32.GetLastError()
  239. if err != win32.ERROR_INVALID_PARAMETER {
  240. return {}, _stat_errno(err)
  241. }
  242. // Indicate this is a symlink on FAT file systems
  243. ti.ReparseTag = 0
  244. }
  245. fi: File_Info
  246. fi.fullpath = path
  247. fi.name = basename(path)
  248. fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
  249. fi.mode |= _file_mode_from_file_attributes(ti.FileAttributes, h, ti.ReparseTag)
  250. fi.is_dir = fi.mode & File_Mode_Dir != 0
  251. fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
  252. fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
  253. fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
  254. return fi, nil
  255. }
  256. _is_abs :: proc(path: string) -> bool {
  257. if len(path) > 0 && path[0] == '/' {
  258. return true
  259. }
  260. if len(path) > 2 {
  261. switch path[0] {
  262. case 'A'..='Z', 'a'..='z':
  263. return path[1] == ':' && is_path_separator(path[2])
  264. }
  265. }
  266. return false
  267. }
  268. _fix_long_path :: proc(path: string) -> string {
  269. if len(path) < 248 {
  270. return path
  271. }
  272. if len(path) >= 2 && path[:2] == `\\` {
  273. return path
  274. }
  275. if !_is_abs(path) {
  276. return path
  277. }
  278. prefix :: `\\?`
  279. path_buf := make([]byte, len(prefix)+len(path)+len(`\`), context.temp_allocator)
  280. copy(path_buf, prefix)
  281. n := len(path)
  282. r, w := 0, len(prefix)
  283. for r < n {
  284. switch {
  285. case is_path_separator(path[r]):
  286. r += 1
  287. case path[r] == '.' && (r+1 == n || is_path_separator(path[r+1])):
  288. r += 1
  289. case r+1 < n && path[r] == '.' && path[r+1] == '.' && (r+2 == n || is_path_separator(path[r+2])):
  290. return path
  291. case:
  292. path_buf[w] = '\\'
  293. w += 1
  294. for ; r < n && !is_path_separator(path[r]); r += 1 {
  295. path_buf[w] = path[r]
  296. w += 1
  297. }
  298. }
  299. }
  300. if w == len(`\\?\c:`) {
  301. path_buf[w] = '\\'
  302. w += 1
  303. }
  304. return string(path_buf[:w])
  305. }