stat_windows.odin 9.3 KB

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