stat_windows.odin 9.5 KB

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