stat_windows.odin 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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_GUARD()
  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_GUARD()
  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. 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 {
  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. TEMP_ALLOCATOR_GUARD()
  110. buf := make([]u16, max(n, 260)+1, temp_allocator())
  111. n = win32.GetFinalPathNameByHandleW(h, raw_data(buf), u32(len(buf)), 0)
  112. return _cleanpath_from_buf(buf[:n], allocator)
  113. }
  114. _cleanpath_from_handle_u16 :: proc(f: ^File) -> ([]u16, Error) {
  115. if f == nil {
  116. return nil, nil
  117. }
  118. h := _handle(f)
  119. n := win32.GetFinalPathNameByHandleW(h, nil, 0, 0)
  120. if n == 0 {
  121. return nil, _get_platform_error()
  122. }
  123. TEMP_ALLOCATOR_GUARD()
  124. buf := make([]u16, max(n, 260)+1, temp_allocator())
  125. n = win32.GetFinalPathNameByHandleW(h, raw_data(buf), u32(len(buf)), 0)
  126. return _cleanpath_strip_prefix(buf[:n]), nil
  127. }
  128. _cleanpath_from_buf :: proc(buf: []u16, allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) {
  129. buf := buf
  130. buf = _cleanpath_strip_prefix(buf)
  131. return win32_utf16_to_utf8(buf, allocator)
  132. }
  133. basename :: proc(name: string) -> (base: string) {
  134. name := name
  135. if len(name) > 3 && name[:3] == `\\?` {
  136. name = name[3:]
  137. }
  138. if len(name) == 2 && name[1] == ':' {
  139. return "."
  140. } else if len(name) > 2 && name[1] == ':' {
  141. name = name[2:]
  142. }
  143. i := len(name)-1
  144. for ; i > 0 && (name[i] == '/' || name[i] == '\\'); i -= 1 {
  145. name = name[:i]
  146. }
  147. for i -= 1; i >= 0; i -= 1 {
  148. if name[i] == '/' || name[i] == '\\' {
  149. name = name[i+1:]
  150. break
  151. }
  152. }
  153. return name
  154. }
  155. file_type :: proc(h: win32.HANDLE) -> File_Type {
  156. switch win32.GetFileType(h) {
  157. case win32.FILE_TYPE_PIPE: return .Named_Pipe
  158. case win32.FILE_TYPE_CHAR: return .Character_Device
  159. case win32.FILE_TYPE_DISK: return .Regular
  160. }
  161. return .Undetermined
  162. }
  163. _file_type_mode_from_file_attributes :: proc(file_attributes: win32.DWORD, h: win32.HANDLE, ReparseTag: win32.DWORD) -> (type: File_Type, mode: int) {
  164. if file_attributes & win32.FILE_ATTRIBUTE_READONLY != 0 {
  165. mode |= 0o444
  166. } else {
  167. mode |= 0o666
  168. }
  169. is_sym := false
  170. if file_attributes & win32.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
  171. is_sym = false
  172. } else {
  173. is_sym = ReparseTag == win32.IO_REPARSE_TAG_SYMLINK || ReparseTag == win32.IO_REPARSE_TAG_MOUNT_POINT
  174. }
  175. if is_sym {
  176. type = .Symlink
  177. } else {
  178. if file_attributes & win32.FILE_ATTRIBUTE_DIRECTORY != 0 {
  179. type = .Directory
  180. mode |= 0o111
  181. }
  182. if h != nil {
  183. type = file_type(h)
  184. }
  185. }
  186. return
  187. }
  188. _file_info_from_win32_file_attribute_data :: proc(d: ^win32.WIN32_FILE_ATTRIBUTE_DATA, name: string, allocator: runtime.Allocator) -> (fi: File_Info, e: Error) {
  189. fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
  190. type, mode := _file_type_mode_from_file_attributes(d.dwFileAttributes, nil, 0)
  191. fi.type = type
  192. fi.mode |= mode
  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. type, mode := _file_type_mode_from_file_attributes(d.dwFileAttributes, nil, 0)
  203. fi.type = type
  204. fi.mode |= mode
  205. fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
  206. fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
  207. fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
  208. fi.fullpath, e = full_path_from_name(name, allocator)
  209. fi.name = basename(fi.fullpath)
  210. return
  211. }
  212. _file_info_from_get_file_information_by_handle :: proc(path: string, h: win32.HANDLE, allocator: runtime.Allocator) -> (File_Info, Error) {
  213. d: win32.BY_HANDLE_FILE_INFORMATION
  214. if !win32.GetFileInformationByHandle(h, &d) {
  215. return {}, _get_platform_error()
  216. }
  217. ti: win32.FILE_ATTRIBUTE_TAG_INFO
  218. if !win32.GetFileInformationByHandleEx(h, .FileAttributeTagInfo, &ti, size_of(ti)) {
  219. err := win32.GetLastError()
  220. if err != win32.ERROR_INVALID_PARAMETER {
  221. return {}, Platform_Error(err)
  222. }
  223. // Indicate this is a symlink on FAT file systems
  224. ti.ReparseTag = 0
  225. }
  226. fi: File_Info
  227. fi.fullpath = path
  228. fi.name = basename(path)
  229. fi.inode = u128(u64(d.nFileIndexHigh)<<32 + u64(d.nFileIndexLow))
  230. fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
  231. type, mode := _file_type_mode_from_file_attributes(d.dwFileAttributes, nil, 0)
  232. fi.type = type
  233. fi.mode |= mode
  234. fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
  235. fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
  236. fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
  237. return fi, nil
  238. }
  239. reserved_names := [?]string{
  240. "CON", "PRN", "AUX", "NUL",
  241. "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
  242. "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
  243. }
  244. _is_reserved_name :: proc(path: string) -> bool {
  245. if len(path) == 0 {
  246. return false
  247. }
  248. for reserved in reserved_names {
  249. if strings.equal_fold(path, reserved) {
  250. return true
  251. }
  252. }
  253. return false
  254. }
  255. _is_UNC :: proc(path: string) -> bool {
  256. return _volume_name_len(path) > 2
  257. }
  258. _volume_name_len :: proc(path: string) -> int {
  259. if ODIN_OS == .Windows {
  260. if len(path) < 2 {
  261. return 0
  262. }
  263. c := path[0]
  264. if path[1] == ':' {
  265. switch c {
  266. case 'a'..='z', 'A'..='Z':
  267. return 2
  268. }
  269. }
  270. // URL: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
  271. if l := len(path); l >= 5 && _is_path_separator(path[0]) && _is_path_separator(path[1]) &&
  272. !_is_path_separator(path[2]) && path[2] != '.' {
  273. for n := 3; n < l-1; n += 1 {
  274. if _is_path_separator(path[n]) {
  275. n += 1
  276. if !_is_path_separator(path[n]) {
  277. if path[n] == '.' {
  278. break
  279. }
  280. }
  281. for ; n < l; n += 1 {
  282. if _is_path_separator(path[n]) {
  283. break
  284. }
  285. }
  286. return n
  287. }
  288. break
  289. }
  290. }
  291. }
  292. return 0
  293. }
  294. _is_abs :: proc(path: string) -> bool {
  295. if _is_reserved_name(path) {
  296. return true
  297. }
  298. l := _volume_name_len(path)
  299. if l == 0 {
  300. return false
  301. }
  302. path := path
  303. path = path[l:]
  304. if path == "" {
  305. return false
  306. }
  307. return is_path_separator(path[0])
  308. }