stat_windows.odin 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package os
  2. import "core:time"
  3. import win32 "core:sys/windows"
  4. @(private)
  5. full_path_from_name :: proc(name: string, allocator := context.allocator) -> (path: string, err: Errno) {
  6. context.allocator = allocator
  7. name := name
  8. if name == "" {
  9. name = "."
  10. }
  11. p := win32.utf8_to_utf16(name, context.temp_allocator)
  12. buf := make([dynamic]u16, 100)
  13. defer delete(buf)
  14. for {
  15. n := win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil)
  16. if n == 0 {
  17. return "", Errno(win32.GetLastError())
  18. }
  19. if n <= u32(len(buf)) {
  20. return win32.utf16_to_utf8(buf[:n], allocator), ERROR_NONE
  21. }
  22. resize(&buf, len(buf)*2)
  23. }
  24. return
  25. }
  26. @(private)
  27. _stat :: proc(name: string, create_file_attributes: u32, allocator := context.allocator) -> (fi: File_Info, e: Errno) {
  28. if len(name) == 0 {
  29. return {}, ERROR_PATH_NOT_FOUND
  30. }
  31. context.allocator = allocator
  32. wname := win32.utf8_to_wstring(fix_long_path(name), context.temp_allocator)
  33. fa: win32.WIN32_FILE_ATTRIBUTE_DATA
  34. ok := win32.GetFileAttributesExW(wname, win32.GetFileExInfoStandard, &fa)
  35. if ok && fa.dwFileAttributes & win32.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
  36. // Not a symlink
  37. return file_info_from_win32_file_attribute_data(&fa, name)
  38. }
  39. err := 0 if ok else win32.GetLastError()
  40. if err == win32.ERROR_SHARING_VIOLATION {
  41. fd: win32.WIN32_FIND_DATAW
  42. sh := win32.FindFirstFileW(wname, &fd)
  43. if sh == win32.INVALID_HANDLE_VALUE {
  44. e = Errno(win32.GetLastError())
  45. return
  46. }
  47. win32.FindClose(sh)
  48. return file_info_from_win32_find_data(&fd, name)
  49. }
  50. h := win32.CreateFileW(wname, 0, 0, nil, win32.OPEN_EXISTING, create_file_attributes, nil)
  51. if h == win32.INVALID_HANDLE_VALUE {
  52. e = Errno(win32.GetLastError())
  53. return
  54. }
  55. defer win32.CloseHandle(h)
  56. return file_info_from_get_file_information_by_handle(name, h)
  57. }
  58. lstat :: proc(name: string, allocator := context.allocator) -> (File_Info, Errno) {
  59. attrs := win32.FILE_FLAG_BACKUP_SEMANTICS
  60. attrs |= win32.FILE_FLAG_OPEN_REPARSE_POINT
  61. return _stat(name, attrs, allocator)
  62. }
  63. stat :: proc(name: string, allocator := context.allocator) -> (File_Info, Errno) {
  64. attrs := win32.FILE_FLAG_BACKUP_SEMANTICS
  65. return _stat(name, attrs, allocator)
  66. }
  67. fstat :: proc(fd: Handle, allocator := context.allocator) -> (File_Info, Errno) {
  68. if fd == 0 {
  69. return {}, ERROR_INVALID_HANDLE
  70. }
  71. context.allocator = allocator
  72. path, err := cleanpath_from_handle(fd)
  73. if err != ERROR_NONE {
  74. return {}, err
  75. }
  76. h := win32.HANDLE(fd)
  77. switch win32.GetFileType(h) {
  78. case win32.FILE_TYPE_PIPE, win32.FILE_TYPE_CHAR:
  79. fi: File_Info
  80. fi.fullpath = path
  81. fi.name = basename(path)
  82. fi.mode |= file_type_mode(h)
  83. return fi, ERROR_NONE
  84. }
  85. return file_info_from_get_file_information_by_handle(path, h)
  86. }
  87. @(private)
  88. cleanpath_strip_prefix :: proc(buf: []u16) -> []u16 {
  89. buf := buf
  90. N := 0
  91. for c, i in buf {
  92. if c == 0 { break }
  93. N = i+1
  94. }
  95. buf = buf[:N]
  96. if len(buf) >= 4 && buf[0] == '\\' && buf[1] == '\\' && buf[2] == '?' && buf[3] == '\\' {
  97. buf = buf[4:]
  98. /*
  99. NOTE(Jeroen): Properly handle UNC paths.
  100. We need to turn `\\?\UNC\synology.local` into `\\synology.local`.
  101. */
  102. if len(buf) >= 3 && buf[0] == 'U' && buf[1] == 'N' && buf[2] == 'C' {
  103. buf = buf[2:]
  104. buf[0] = '\\'
  105. }
  106. }
  107. return buf
  108. }
  109. @(private)
  110. cleanpath_from_handle :: proc(fd: Handle) -> (string, Errno) {
  111. if fd == 0 {
  112. return "", ERROR_INVALID_HANDLE
  113. }
  114. h := win32.HANDLE(fd)
  115. MAX_PATH := win32.DWORD(260) + 1
  116. buf: []u16
  117. for {
  118. buf = make([]u16, MAX_PATH, context.temp_allocator)
  119. err := win32.GetFinalPathNameByHandleW(h, raw_data(buf), MAX_PATH, 0)
  120. switch Errno(err) {
  121. case ERROR_PATH_NOT_FOUND, ERROR_INVALID_PARAMETER:
  122. return "", Errno(err)
  123. case ERROR_NOT_ENOUGH_MEMORY:
  124. MAX_PATH = MAX_PATH*2 + 1
  125. continue
  126. }
  127. break
  128. }
  129. return cleanpath_from_buf(buf), ERROR_NONE
  130. }
  131. @(private)
  132. cleanpath_from_handle_u16 :: proc(fd: Handle) -> ([]u16, Errno) {
  133. if fd == 0 {
  134. return nil, ERROR_INVALID_HANDLE
  135. }
  136. h := win32.HANDLE(fd)
  137. MAX_PATH := win32.DWORD(260) + 1
  138. buf: []u16
  139. for {
  140. buf = make([]u16, MAX_PATH, context.temp_allocator)
  141. err := win32.GetFinalPathNameByHandleW(h, raw_data(buf), MAX_PATH, 0)
  142. switch Errno(err) {
  143. case ERROR_PATH_NOT_FOUND, ERROR_INVALID_PARAMETER:
  144. return nil, Errno(err)
  145. case ERROR_NOT_ENOUGH_MEMORY:
  146. MAX_PATH = MAX_PATH*2 + 1
  147. continue
  148. }
  149. break
  150. }
  151. return cleanpath_strip_prefix(buf), ERROR_NONE
  152. }
  153. @(private)
  154. cleanpath_from_buf :: proc(buf: []u16) -> string {
  155. buf := buf
  156. buf = cleanpath_strip_prefix(buf)
  157. return win32.utf16_to_utf8(buf, context.allocator)
  158. }
  159. @(private)
  160. basename :: proc(name: string) -> (base: string) {
  161. name := name
  162. if len(name) > 3 && name[:3] == `\\?` {
  163. name = name[3:]
  164. }
  165. if len(name) == 2 && name[1] == ':' {
  166. return "."
  167. } else if len(name) > 2 && name[1] == ':' {
  168. name = name[2:]
  169. }
  170. i := len(name)-1
  171. for ; i > 0 && (name[i] == '/' || name[i] == '\\'); i -= 1 {
  172. name = name[:i]
  173. }
  174. for i -= 1; i >= 0; i -= 1 {
  175. if name[i] == '/' || name[i] == '\\' {
  176. name = name[i+1:]
  177. break
  178. }
  179. }
  180. return name
  181. }
  182. @(private)
  183. file_type_mode :: proc(h: win32.HANDLE) -> File_Mode {
  184. switch win32.GetFileType(h) {
  185. case win32.FILE_TYPE_PIPE:
  186. return File_Mode_Named_Pipe
  187. case win32.FILE_TYPE_CHAR:
  188. return File_Mode_Device | File_Mode_Char_Device
  189. }
  190. return 0
  191. }
  192. @(private)
  193. file_mode_from_file_attributes :: proc(FileAttributes: win32.DWORD, h: win32.HANDLE, ReparseTag: win32.DWORD) -> (mode: File_Mode) {
  194. if FileAttributes & win32.FILE_ATTRIBUTE_READONLY != 0 {
  195. mode |= 0o444
  196. } else {
  197. mode |= 0o666
  198. }
  199. is_sym := false
  200. if FileAttributes & win32.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
  201. is_sym = false
  202. } else {
  203. is_sym = ReparseTag == win32.IO_REPARSE_TAG_SYMLINK || ReparseTag == win32.IO_REPARSE_TAG_MOUNT_POINT
  204. }
  205. if is_sym {
  206. mode |= File_Mode_Sym_Link
  207. } else {
  208. if FileAttributes & win32.FILE_ATTRIBUTE_DIRECTORY != 0 {
  209. mode |= 0o111 | File_Mode_Dir
  210. }
  211. if h != nil {
  212. mode |= file_type_mode(h)
  213. }
  214. }
  215. return
  216. }
  217. @(private)
  218. file_info_from_win32_file_attribute_data :: proc(d: ^win32.WIN32_FILE_ATTRIBUTE_DATA, name: string) -> (fi: File_Info, e: Errno) {
  219. fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
  220. fi.mode |= file_mode_from_file_attributes(d.dwFileAttributes, nil, 0)
  221. fi.is_dir = fi.mode & File_Mode_Dir != 0
  222. fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
  223. fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
  224. fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
  225. fi.fullpath, e = full_path_from_name(name)
  226. fi.name = basename(fi.fullpath)
  227. return
  228. }
  229. @(private)
  230. file_info_from_win32_find_data :: proc(d: ^win32.WIN32_FIND_DATAW, name: string) -> (fi: File_Info, e: Errno) {
  231. fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
  232. fi.mode |= file_mode_from_file_attributes(d.dwFileAttributes, nil, 0)
  233. fi.is_dir = fi.mode & File_Mode_Dir != 0
  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. fi.fullpath, e = full_path_from_name(name)
  238. fi.name = basename(fi.fullpath)
  239. return
  240. }
  241. @(private)
  242. file_info_from_get_file_information_by_handle :: proc(path: string, h: win32.HANDLE) -> (File_Info, Errno) {
  243. d: win32.BY_HANDLE_FILE_INFORMATION
  244. if !win32.GetFileInformationByHandle(h, &d) {
  245. err := Errno(win32.GetLastError())
  246. return {}, err
  247. }
  248. ti: win32.FILE_ATTRIBUTE_TAG_INFO
  249. if !win32.GetFileInformationByHandleEx(h, .FileAttributeTagInfo, &ti, size_of(ti)) {
  250. err := win32.GetLastError()
  251. if err != u32(ERROR_INVALID_PARAMETER) {
  252. return {}, Errno(err)
  253. }
  254. // Indicate this is a symlink on FAT file systems
  255. ti.ReparseTag = 0
  256. }
  257. fi: File_Info
  258. fi.fullpath = path
  259. fi.name = basename(path)
  260. fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
  261. fi.mode |= file_mode_from_file_attributes(ti.FileAttributes, h, ti.ReparseTag)
  262. fi.is_dir = fi.mode & File_Mode_Dir != 0
  263. fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
  264. fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
  265. fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
  266. return fi, ERROR_NONE
  267. }