stat_windows.odin 9.4 KB

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