stat_windows.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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) -> (fi: File_Info, err: Error) {
  8. if f == nil || (^File_Impl)(f.impl).fd == nil {
  9. return
  10. }
  11. path := _cleanpath_from_handle(f, allocator) or_return
  12. h := _handle(f)
  13. switch win32.GetFileType(h) {
  14. case win32.FILE_TYPE_PIPE, win32.FILE_TYPE_CHAR:
  15. fi = File_Info {
  16. fullpath = path,
  17. name = basename(path),
  18. type = file_type(h),
  19. }
  20. return
  21. }
  22. return _file_info_from_get_file_information_by_handle(path, h, allocator)
  23. }
  24. _stat :: proc(name: string, allocator: runtime.Allocator) -> (File_Info, Error) {
  25. return internal_stat(name, win32.FILE_FLAG_BACKUP_SEMANTICS, allocator)
  26. }
  27. _lstat :: proc(name: string, allocator: runtime.Allocator) -> (File_Info, Error) {
  28. return internal_stat(name, win32.FILE_FLAG_BACKUP_SEMANTICS|win32.FILE_FLAG_OPEN_REPARSE_POINT, allocator)
  29. }
  30. _same_file :: proc(fi1, fi2: File_Info) -> bool {
  31. return fi1.fullpath == fi2.fullpath
  32. }
  33. full_path_from_name :: proc(name: string, allocator: runtime.Allocator) -> (path: string, err: Error) {
  34. name := name
  35. if name == "" {
  36. name = "."
  37. }
  38. temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
  39. p := win32_utf8_to_utf16(name, temp_allocator) or_return
  40. n := win32.GetFullPathNameW(raw_data(p), 0, nil, nil)
  41. if n == 0 {
  42. return "", _get_platform_error()
  43. }
  44. buf := make([]u16, n+1, temp_allocator)
  45. n = win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil)
  46. if n == 0 {
  47. return "", _get_platform_error()
  48. }
  49. return win32_utf16_to_utf8(buf[:n], allocator)
  50. }
  51. internal_stat :: proc(name: string, create_file_attributes: u32, allocator: runtime.Allocator) -> (fi: File_Info, e: Error) {
  52. if len(name) == 0 {
  53. return {}, .Not_Exist
  54. }
  55. temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
  56. wname := _fix_long_path(name, temp_allocator) or_return
  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. fi = _file_info_from_win32_file_attribute_data(&fa, name, allocator) or_return
  62. if fi.type == .Undetermined {
  63. fi.type = _file_type_from_create_file(wname, create_file_attributes)
  64. }
  65. return
  66. }
  67. err := 0 if ok else win32.GetLastError()
  68. if err == win32.ERROR_SHARING_VIOLATION {
  69. fd: win32.WIN32_FIND_DATAW
  70. sh := win32.FindFirstFileW(wname, &fd)
  71. if sh == win32.INVALID_HANDLE_VALUE {
  72. e = _get_platform_error()
  73. return
  74. }
  75. win32.FindClose(sh)
  76. fi = _file_info_from_win32_find_data(&fd, name, allocator) or_return
  77. if fi.type == .Undetermined {
  78. fi.type = _file_type_from_create_file(wname, create_file_attributes)
  79. }
  80. return
  81. }
  82. h := win32.CreateFileW(wname, 0, 0, nil, win32.OPEN_EXISTING, create_file_attributes, nil)
  83. if h == win32.INVALID_HANDLE_VALUE {
  84. e = _get_platform_error()
  85. return
  86. }
  87. defer win32.CloseHandle(h)
  88. return _file_info_from_get_file_information_by_handle(name, h, allocator)
  89. }
  90. _cleanpath_strip_prefix :: proc(buf: []u16) -> []u16 {
  91. buf := buf
  92. N := 0
  93. for c, i in buf {
  94. if c == 0 { break }
  95. N = i+1
  96. }
  97. buf = buf[:N]
  98. if len(buf) >= 4 {
  99. if buf[0] == '\\' &&
  100. buf[1] == '\\' &&
  101. buf[2] == '?' &&
  102. buf[3] == '\\' {
  103. buf = buf[4:]
  104. }
  105. }
  106. return buf
  107. }
  108. _cleanpath_from_handle :: proc(f: ^File, allocator: runtime.Allocator) -> (string, Error) {
  109. if f == nil {
  110. return "", nil
  111. }
  112. h := _handle(f)
  113. n := win32.GetFinalPathNameByHandleW(h, nil, 0, 0)
  114. if n == 0 {
  115. return "", _get_platform_error()
  116. }
  117. temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
  118. buf := make([]u16, max(n, 260)+1, temp_allocator)
  119. n = win32.GetFinalPathNameByHandleW(h, raw_data(buf), u32(len(buf)), 0)
  120. return _cleanpath_from_buf(buf[:n], allocator)
  121. }
  122. _cleanpath_from_handle_u16 :: proc(f: ^File) -> ([]u16, Error) {
  123. if f == nil {
  124. return nil, nil
  125. }
  126. h := _handle(f)
  127. n := win32.GetFinalPathNameByHandleW(h, nil, 0, 0)
  128. if n == 0 {
  129. return nil, _get_platform_error()
  130. }
  131. temp_allocator := TEMP_ALLOCATOR_GUARD({})
  132. buf := make([]u16, max(n, 260)+1, temp_allocator)
  133. n = win32.GetFinalPathNameByHandleW(h, raw_data(buf), u32(len(buf)), 0)
  134. return _cleanpath_strip_prefix(buf[:n]), nil
  135. }
  136. _cleanpath_from_buf :: proc(buf: []u16, allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) {
  137. buf := buf
  138. buf = _cleanpath_strip_prefix(buf)
  139. return win32_utf16_to_utf8(buf, allocator)
  140. }
  141. basename :: proc(name: string) -> (base: string) {
  142. name := name
  143. if len(name) > 3 && name[:3] == `\\?` {
  144. name = name[3:]
  145. }
  146. if len(name) == 2 && name[1] == ':' {
  147. return "."
  148. } else if len(name) > 2 && name[1] == ':' {
  149. name = name[2:]
  150. }
  151. i := len(name)-1
  152. for ; i > 0 && (name[i] == '/' || name[i] == '\\'); i -= 1 {
  153. name = name[:i]
  154. }
  155. for i -= 1; i >= 0; i -= 1 {
  156. if name[i] == '/' || name[i] == '\\' {
  157. name = name[i+1:]
  158. break
  159. }
  160. }
  161. return name
  162. }
  163. file_type :: proc(h: win32.HANDLE) -> File_Type {
  164. switch win32.GetFileType(h) {
  165. case win32.FILE_TYPE_PIPE: return .Named_Pipe
  166. case win32.FILE_TYPE_CHAR: return .Character_Device
  167. case win32.FILE_TYPE_DISK: return .Regular
  168. }
  169. return .Undetermined
  170. }
  171. _file_type_from_create_file :: proc(wname: win32.wstring, create_file_attributes: u32) -> File_Type {
  172. h := win32.CreateFileW(wname, 0, 0, nil, win32.OPEN_EXISTING, create_file_attributes, nil)
  173. if h == win32.INVALID_HANDLE_VALUE {
  174. return .Undetermined
  175. }
  176. defer win32.CloseHandle(h)
  177. return file_type(h)
  178. }
  179. _file_type_mode_from_file_attributes :: proc(file_attributes: win32.DWORD, h: win32.HANDLE, ReparseTag: win32.DWORD) -> (type: File_Type, mode: int) {
  180. if file_attributes & win32.FILE_ATTRIBUTE_READONLY != 0 {
  181. mode |= 0o444
  182. } else {
  183. mode |= 0o666
  184. }
  185. is_sym := false
  186. if file_attributes & win32.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
  187. is_sym = false
  188. } else {
  189. is_sym = ReparseTag == win32.IO_REPARSE_TAG_SYMLINK || ReparseTag == win32.IO_REPARSE_TAG_MOUNT_POINT
  190. }
  191. if is_sym {
  192. type = .Symlink
  193. } else if file_attributes & win32.FILE_ATTRIBUTE_DIRECTORY != 0 {
  194. type = .Directory
  195. mode |= 0o111
  196. } else if h != nil {
  197. type = file_type(h)
  198. }
  199. return
  200. }
  201. // a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC)
  202. time_as_filetime :: #force_inline proc(t: time.Time) -> (ft: win32.LARGE_INTEGER) {
  203. win := u64(t._nsec / 100) + 116444736000000000
  204. return win32.LARGE_INTEGER(win)
  205. }
  206. filetime_as_time_li :: #force_inline proc(ft: win32.LARGE_INTEGER) -> (t: time.Time) {
  207. return {_nsec=(i64(ft) - 116444736000000000) * 100}
  208. }
  209. filetime_as_time_ft :: #force_inline proc(ft: win32.FILETIME) -> (t: time.Time) {
  210. return filetime_as_time_li(win32.LARGE_INTEGER(ft.dwLowDateTime) + win32.LARGE_INTEGER(ft.dwHighDateTime) << 32)
  211. }
  212. filetime_as_time :: proc{filetime_as_time_ft, filetime_as_time_li}
  213. _file_info_from_win32_file_attribute_data :: proc(d: ^win32.WIN32_FILE_ATTRIBUTE_DATA, name: string, allocator: runtime.Allocator) -> (fi: File_Info, e: Error) {
  214. fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
  215. type, mode := _file_type_mode_from_file_attributes(d.dwFileAttributes, nil, 0)
  216. fi.type = type
  217. fi.mode |= mode
  218. fi.creation_time = filetime_as_time(d.ftCreationTime)
  219. fi.modification_time = filetime_as_time(d.ftLastWriteTime)
  220. fi.access_time = filetime_as_time(d.ftLastAccessTime)
  221. fi.fullpath, e = full_path_from_name(name, allocator)
  222. fi.name = basename(fi.fullpath)
  223. return
  224. }
  225. _file_info_from_win32_find_data :: proc(d: ^win32.WIN32_FIND_DATAW, name: string, allocator: runtime.Allocator) -> (fi: File_Info, e: Error) {
  226. fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
  227. type, mode := _file_type_mode_from_file_attributes(d.dwFileAttributes, nil, 0)
  228. fi.type = type
  229. fi.mode |= mode
  230. fi.creation_time = filetime_as_time(d.ftCreationTime)
  231. fi.modification_time = filetime_as_time(d.ftLastWriteTime)
  232. fi.access_time = filetime_as_time(d.ftLastAccessTime)
  233. fi.fullpath, e = full_path_from_name(name, allocator)
  234. fi.name = basename(fi.fullpath)
  235. return
  236. }
  237. _file_info_from_get_file_information_by_handle :: proc(path: string, h: win32.HANDLE, allocator: runtime.Allocator) -> (File_Info, Error) {
  238. d: win32.BY_HANDLE_FILE_INFORMATION
  239. if !win32.GetFileInformationByHandle(h, &d) {
  240. return {}, _get_platform_error()
  241. }
  242. ti: win32.FILE_ATTRIBUTE_TAG_INFO
  243. if !win32.GetFileInformationByHandleEx(h, .FileAttributeTagInfo, &ti, size_of(ti)) {
  244. err := win32.GetLastError()
  245. if err != win32.ERROR_INVALID_PARAMETER {
  246. return {}, Platform_Error(err)
  247. }
  248. // Indicate this is a symlink on FAT file systems
  249. ti.ReparseTag = 0
  250. }
  251. fi: File_Info
  252. fi.fullpath = path
  253. fi.name = basename(path)
  254. fi.inode = u128(u64(d.nFileIndexHigh)<<32 + u64(d.nFileIndexLow))
  255. fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
  256. type, mode := _file_type_mode_from_file_attributes(d.dwFileAttributes, h, 0)
  257. fi.type = type
  258. fi.mode |= mode
  259. fi.creation_time = filetime_as_time(d.ftCreationTime)
  260. fi.modification_time = filetime_as_time(d.ftLastWriteTime)
  261. fi.access_time = filetime_as_time(d.ftLastAccessTime)
  262. return fi, nil
  263. }
  264. reserved_names := [?]string{
  265. "CON", "PRN", "AUX", "NUL",
  266. "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
  267. "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
  268. }
  269. _is_reserved_name :: proc(path: string) -> bool {
  270. if len(path) == 0 {
  271. return false
  272. }
  273. for reserved in reserved_names {
  274. if strings.equal_fold(path, reserved) {
  275. return true
  276. }
  277. }
  278. return false
  279. }
  280. _volume_name_len :: proc(path: string) -> (length: int) {
  281. if len(path) < 2 {
  282. return 0
  283. }
  284. if path[1] == ':' {
  285. switch path[0] {
  286. case 'a'..='z', 'A'..='Z':
  287. return 2
  288. }
  289. }
  290. /*
  291. See: URL: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
  292. Further allowed paths can be of the form of:
  293. - \\server\share or \\server\share\more\path
  294. - \\?\C:\...
  295. - \\.\PhysicalDriveX
  296. */
  297. // Any remaining kind of path has to start with two slashes.
  298. if !_is_path_separator(path[0]) || !_is_path_separator(path[1]) {
  299. return 0
  300. }
  301. // Device path. The volume name is the whole string
  302. if len(path) >= 5 && path[2] == '.' && _is_path_separator(path[3]) {
  303. return len(path)
  304. }
  305. // We're a UNC share `\\host\share`, file namespace `\\?\C:` or UNC in file namespace `\\?\\host\share`
  306. prefix := 2
  307. // File namespace.
  308. if len(path) >= 5 && path[2] == '?' && _is_path_separator(path[3]) {
  309. if _is_path_separator(path[4]) {
  310. // `\\?\\` UNC path in file namespace
  311. prefix = 5
  312. }
  313. if len(path) >= 6 && path[5] == ':' {
  314. switch path[4] {
  315. case 'a'..='z', 'A'..='Z':
  316. return 6
  317. case:
  318. return 0
  319. }
  320. }
  321. }
  322. // UNC path, minimum version of the volume is `\\h\s` for host, share.
  323. // Can also contain an IP address in the host position.
  324. slash_count := 0
  325. for i in prefix..<len(path) {
  326. // Host needs to be at least 1 character
  327. if _is_path_separator(path[i]) && i > 0 {
  328. slash_count += 1
  329. if slash_count == 2 {
  330. return i
  331. }
  332. }
  333. }
  334. return len(path)
  335. }