file_windows.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. package os
  2. import win32 "core:sys/windows"
  3. import "intrinsics"
  4. is_path_separator :: proc(c: byte) -> bool {
  5. return c == '/' || c == '\\';
  6. }
  7. open :: proc(path: string, mode: int = O_RDONLY, perm: int = 0) -> (Handle, Errno) {
  8. if len(path) == 0 {
  9. return INVALID_HANDLE, ERROR_FILE_NOT_FOUND;
  10. }
  11. access: u32;
  12. switch mode & (O_RDONLY|O_WRONLY|O_RDWR) {
  13. case O_RDONLY: access = win32.FILE_GENERIC_READ;
  14. case O_WRONLY: access = win32.FILE_GENERIC_WRITE;
  15. case O_RDWR: access = win32.FILE_GENERIC_READ | win32.FILE_GENERIC_WRITE;
  16. }
  17. if mode&O_APPEND != 0 {
  18. access &~= win32.FILE_GENERIC_WRITE;
  19. access |= win32.FILE_APPEND_DATA;
  20. }
  21. if mode&O_CREATE != 0 {
  22. access |= win32.FILE_GENERIC_WRITE;
  23. }
  24. share_mode := win32.FILE_SHARE_READ|win32.FILE_SHARE_WRITE;
  25. sa: ^win32.SECURITY_ATTRIBUTES = nil;
  26. sa_inherit := win32.SECURITY_ATTRIBUTES{nLength = size_of(win32.SECURITY_ATTRIBUTES), bInheritHandle = true};
  27. if mode&O_CLOEXEC == 0 {
  28. sa = &sa_inherit;
  29. }
  30. create_mode: u32;
  31. switch {
  32. case mode&(O_CREATE|O_EXCL) == (O_CREATE | O_EXCL):
  33. create_mode = win32.CREATE_NEW;
  34. case mode&(O_CREATE|O_TRUNC) == (O_CREATE | O_TRUNC):
  35. create_mode = win32.CREATE_ALWAYS;
  36. case mode&O_CREATE == O_CREATE:
  37. create_mode = win32.OPEN_ALWAYS;
  38. case mode&O_TRUNC == O_TRUNC:
  39. create_mode = win32.TRUNCATE_EXISTING;
  40. case:
  41. create_mode = win32.OPEN_EXISTING;
  42. }
  43. wide_path := win32.utf8_to_wstring(path);
  44. handle := Handle(win32.CreateFileW(wide_path, access, share_mode, sa, create_mode, win32.FILE_ATTRIBUTE_NORMAL|win32.FILE_FLAG_BACKUP_SEMANTICS, nil));
  45. if handle != INVALID_HANDLE {
  46. return handle, ERROR_NONE;
  47. }
  48. err := Errno(win32.GetLastError());
  49. return INVALID_HANDLE, err;
  50. }
  51. close :: proc(fd: Handle) -> Errno {
  52. if !win32.CloseHandle(win32.HANDLE(fd)) {
  53. return Errno(win32.GetLastError());
  54. }
  55. return ERROR_NONE;
  56. }
  57. flush :: proc(fd: Handle) -> (err: Errno) {
  58. if !win32.FlushFileBuffers(win32.HANDLE(fd)) {
  59. err = Errno(win32.GetLastError());
  60. }
  61. return;
  62. }
  63. write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  64. if len(data) == 0 {
  65. return 0, ERROR_NONE;
  66. }
  67. single_write_length: win32.DWORD;
  68. total_write: i64;
  69. length := i64(len(data));
  70. for total_write < length {
  71. remaining := length - total_write;
  72. to_write := win32.DWORD(min(i32(remaining), MAX_RW));
  73. e := win32.WriteFile(win32.HANDLE(fd), &data[total_write], to_write, &single_write_length, nil);
  74. if single_write_length <= 0 || !e {
  75. err := Errno(win32.GetLastError());
  76. return int(total_write), err;
  77. }
  78. total_write += i64(single_write_length);
  79. }
  80. return int(total_write), ERROR_NONE;
  81. }
  82. read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  83. if len(data) == 0 {
  84. return 0, ERROR_NONE;
  85. }
  86. single_read_length: win32.DWORD;
  87. total_read: i64;
  88. length := i64(len(data));
  89. for total_read < length {
  90. remaining := length - total_read;
  91. to_read := min(win32.DWORD(remaining), MAX_RW);
  92. e := win32.ReadFile(win32.HANDLE(fd), &data[total_read], to_read, &single_read_length, nil);
  93. if single_read_length <= 0 || !e {
  94. err := Errno(win32.GetLastError());
  95. return int(total_read), err;
  96. }
  97. total_read += i64(single_read_length);
  98. }
  99. return int(total_read), ERROR_NONE;
  100. }
  101. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
  102. w: u32;
  103. switch whence {
  104. case 0: w = win32.FILE_BEGIN;
  105. case 1: w = win32.FILE_CURRENT;
  106. case 2: w = win32.FILE_END;
  107. }
  108. hi := i32(offset>>32);
  109. lo := i32(offset);
  110. ft := win32.GetFileType(win32.HANDLE(fd));
  111. if ft == win32.FILE_TYPE_PIPE {
  112. return 0, ERROR_FILE_IS_PIPE;
  113. }
  114. dw_ptr := win32.SetFilePointer(win32.HANDLE(fd), lo, &hi, w);
  115. if dw_ptr == win32.INVALID_SET_FILE_POINTER {
  116. err := Errno(win32.GetLastError());
  117. return 0, err;
  118. }
  119. return i64(hi)<<32 + i64(dw_ptr), ERROR_NONE;
  120. }
  121. file_size :: proc(fd: Handle) -> (i64, Errno) {
  122. length: win32.LARGE_INTEGER;
  123. err: Errno;
  124. if !win32.GetFileSizeEx(win32.HANDLE(fd), &length) {
  125. err = Errno(win32.GetLastError());
  126. }
  127. return i64(length), err;
  128. }
  129. @(private)
  130. MAX_RW :: 1<<30;
  131. @(private)
  132. pread :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
  133. buf := data;
  134. if len(buf) > MAX_RW {
  135. buf = buf[:MAX_RW];
  136. }
  137. curr_offset, e := seek(fd, offset, 1);
  138. if e != 0 {
  139. return 0, e;
  140. }
  141. defer seek(fd, curr_offset, 0);
  142. o := win32.OVERLAPPED{
  143. OffsetHigh = u32(offset>>32),
  144. Offset = u32(offset),
  145. };
  146. h := win32.HANDLE(fd);
  147. done: win32.DWORD;
  148. if !win32.ReadFile(h, raw_data(buf), u32(len(buf)), &done, &o) {
  149. e = Errno(win32.GetLastError());
  150. done = 0;
  151. }
  152. return int(done), e;
  153. }
  154. @(private)
  155. pwrite :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
  156. buf := data;
  157. if len(buf) > MAX_RW {
  158. buf = buf[:MAX_RW];
  159. }
  160. curr_offset, e := seek(fd, offset, 1);
  161. if e != 0 {
  162. return 0, e;
  163. }
  164. defer seek(fd, curr_offset, 0);
  165. o := win32.OVERLAPPED{
  166. OffsetHigh = u32(offset>>32),
  167. Offset = u32(offset),
  168. };
  169. h := win32.HANDLE(fd);
  170. done: win32.DWORD;
  171. if !win32.WriteFile(h, raw_data(buf), u32(len(buf)), &done, &o) {
  172. e = Errno(win32.GetLastError());
  173. done = 0;
  174. }
  175. return int(done), e;
  176. }
  177. read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Errno) {
  178. if offset < 0 {
  179. return 0, ERROR_NEGATIVE_OFFSET;
  180. }
  181. b, offset := data, offset;
  182. for len(b) > 0 {
  183. m, e := pread(fd, b, offset);
  184. if e != 0 {
  185. err = e;
  186. break;
  187. }
  188. n += m;
  189. b = b[m:];
  190. offset += i64(m);
  191. }
  192. return;
  193. }
  194. write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Errno) {
  195. if offset < 0 {
  196. return 0, ERROR_NEGATIVE_OFFSET;
  197. }
  198. b, offset := data, offset;
  199. for len(b) > 0 {
  200. m, e := pwrite(fd, b, offset);
  201. if e != 0 {
  202. err = e;
  203. break;
  204. }
  205. n += m;
  206. b = b[m:];
  207. offset += i64(m);
  208. }
  209. return;
  210. }
  211. // NOTE(bill): Uses startup to initialize it
  212. stdin := get_std_handle(uint(win32.STD_INPUT_HANDLE));
  213. stdout := get_std_handle(uint(win32.STD_OUTPUT_HANDLE));
  214. stderr := get_std_handle(uint(win32.STD_ERROR_HANDLE));
  215. get_std_handle :: proc "contextless" (h: uint) -> Handle {
  216. fd := win32.GetStdHandle(win32.DWORD(h));
  217. when size_of(uintptr) == 8 {
  218. win32.SetHandleInformation(fd, win32.HANDLE_FLAG_INHERIT, 0);
  219. }
  220. return Handle(fd);
  221. }
  222. exists :: proc(path: string) -> bool {
  223. wpath := win32.utf8_to_wstring(path, context.temp_allocator);
  224. attribs := win32.GetFileAttributesW(wpath);
  225. return i32(attribs) != win32.INVALID_FILE_ATTRIBUTES;
  226. }
  227. is_file :: proc(path: string) -> bool {
  228. wpath := win32.utf8_to_wstring(path, context.temp_allocator);
  229. attribs := win32.GetFileAttributesW(wpath);
  230. if i32(attribs) != win32.INVALID_FILE_ATTRIBUTES {
  231. return attribs & win32.FILE_ATTRIBUTE_DIRECTORY == 0;
  232. }
  233. return false;
  234. }
  235. is_dir :: proc(path: string) -> bool {
  236. wpath := win32.utf8_to_wstring(path, context.temp_allocator);
  237. attribs := win32.GetFileAttributesW(wpath);
  238. if i32(attribs) != win32.INVALID_FILE_ATTRIBUTES {
  239. return attribs & win32.FILE_ATTRIBUTE_DIRECTORY != 0;
  240. }
  241. return false;
  242. }
  243. // NOTE(tetra): GetCurrentDirectory is not thread safe with SetCurrentDirectory and GetFullPathName
  244. @private cwd_lock := win32.SRWLOCK{}; // zero is initialized
  245. get_current_directory :: proc(allocator := context.allocator) -> string {
  246. win32.AcquireSRWLockExclusive(&cwd_lock);
  247. sz_utf16 := win32.GetCurrentDirectoryW(0, nil);
  248. dir_buf_wstr := make([]u16, sz_utf16, context.temp_allocator); // the first time, it _includes_ the NUL.
  249. sz_utf16 = win32.GetCurrentDirectoryW(win32.DWORD(len(dir_buf_wstr)), raw_data(dir_buf_wstr));
  250. assert(int(sz_utf16)+1 == len(dir_buf_wstr)); // the second time, it _excludes_ the NUL.
  251. win32.ReleaseSRWLockExclusive(&cwd_lock);
  252. return win32.utf16_to_utf8(dir_buf_wstr, allocator);
  253. }
  254. set_current_directory :: proc(path: string) -> (err: Errno) {
  255. wstr := win32.utf8_to_wstring(path);
  256. win32.AcquireSRWLockExclusive(&cwd_lock);
  257. if !win32.SetCurrentDirectoryW(wstr) {
  258. err = Errno(win32.GetLastError());
  259. }
  260. win32.ReleaseSRWLockExclusive(&cwd_lock);
  261. return;
  262. }
  263. change_directory :: proc(path: string) -> Errno {
  264. wpath := win32.utf8_to_wstring(path, context.temp_allocator);
  265. return Errno(win32.SetCurrentDirectoryW(wpath));
  266. }
  267. make_directory :: proc(path: string, mode: u32) -> Errno {
  268. wpath := win32.utf8_to_wstring(path, context.temp_allocator);
  269. return Errno(win32.CreateDirectoryW(wpath, nil));
  270. }
  271. remove_directory :: proc(path: string) -> Errno {
  272. wpath := win32.utf8_to_wstring(path, context.temp_allocator);
  273. return Errno(win32.RemoveDirectoryW(wpath));
  274. }
  275. @(private)
  276. is_abs :: proc(path: string) -> bool {
  277. if len(path) > 0 && path[0] == '/' {
  278. return true;
  279. }
  280. when ODIN_OS == "windows" {
  281. if len(path) > 2 {
  282. switch path[0] {
  283. case 'A'..='Z', 'a'..='z':
  284. return path[1] == ':' && is_path_separator(path[2]);
  285. }
  286. }
  287. }
  288. return false;
  289. }
  290. @(private)
  291. fix_long_path :: proc(path: string) -> string {
  292. if len(path) < 248 {
  293. return path;
  294. }
  295. if len(path) >= 2 && path[:2] == `\\` {
  296. return path;
  297. }
  298. if !is_abs(path) {
  299. return path;
  300. }
  301. prefix :: `\\?`;
  302. path_buf := make([]byte, len(prefix)+len(path)+len(`\`), context.temp_allocator);
  303. copy(path_buf, prefix);
  304. n := len(path);
  305. r, w := 0, len(prefix);
  306. for r < n {
  307. switch {
  308. case is_path_separator(path[r]):
  309. r += 1;
  310. case path[r] == '.' && (r+1 == n || is_path_separator(path[r+1])):
  311. r += 1;
  312. case r+1 < n && path[r] == '.' && path[r+1] == '.' && (r+2 == n || is_path_separator(path[r+2])):
  313. return path;
  314. case:
  315. path_buf[w] = '\\';
  316. w += 1;
  317. for ; r < n && !is_path_separator(path[r]); r += 1 {
  318. path_buf[w] = path[r];
  319. w += 1;
  320. }
  321. }
  322. }
  323. if w == len(`\\?\c:`) {
  324. path_buf[w] = '\\';
  325. w += 1;
  326. }
  327. return string(path_buf[:w]);
  328. }
  329. link :: proc(old_name, new_name: string) -> Errno {
  330. n := win32.utf8_to_wstring(fix_long_path(new_name));
  331. o := win32.utf8_to_wstring(fix_long_path(old_name));
  332. return Errno(win32.CreateHardLinkW(n, o, nil));
  333. }
  334. unlink :: proc(path: string) -> Errno {
  335. wpath := win32.utf8_to_wstring(path, context.temp_allocator);
  336. return Errno(win32.DeleteFileW(wpath));
  337. }
  338. rename :: proc(old_path, new_path: string) -> Errno {
  339. from := win32.utf8_to_wstring(old_path, context.temp_allocator);
  340. to := win32.utf8_to_wstring(new_path, context.temp_allocator);
  341. return Errno(win32.MoveFileExW(from, to, win32.MOVEFILE_REPLACE_EXISTING));
  342. }
  343. ftruncate :: proc(fd: Handle, length: i64) -> (err: Errno) {
  344. curr_off, e := seek(fd, 0, 1);
  345. if e != 0 {
  346. return e;
  347. }
  348. defer seek(fd, curr_off, 0);
  349. _, e = seek(fd, length, 0);
  350. if e != 0 {
  351. return e;
  352. }
  353. ok := win32.SetEndOfFile(win32.HANDLE(fd));
  354. if !ok {
  355. return Errno(win32.GetLastError());
  356. }
  357. return ERROR_NONE;
  358. }
  359. truncate :: proc(path: string, length: i64) -> (err: Errno) {
  360. fd: Handle;
  361. fd, err = open(path, O_WRONLY|O_CREATE, 0o666);
  362. if err != 0 {
  363. return;
  364. }
  365. defer close(fd);
  366. err = ftruncate(fd, length);
  367. return;
  368. }
  369. remove :: proc(name: string) -> Errno {
  370. p := win32.utf8_to_wstring(fix_long_path(name));
  371. err, err1: win32.DWORD;
  372. if !win32.DeleteFileW(p) {
  373. err = win32.GetLastError();
  374. }
  375. if err == 0 {
  376. return 0;
  377. }
  378. if !win32.RemoveDirectoryW(p) {
  379. err1 = win32.GetLastError();
  380. }
  381. if err1 == 0 {
  382. return 0;
  383. }
  384. if err != err1 {
  385. a := win32.GetFileAttributesW(p);
  386. if a == ~u32(0) {
  387. err = win32.GetLastError();
  388. } else {
  389. if a & win32.FILE_ATTRIBUTE_DIRECTORY != 0 {
  390. err = err1;
  391. } else if a & win32.FILE_ATTRIBUTE_READONLY != 0 {
  392. if win32.SetFileAttributesW(p, a &~ win32.FILE_ATTRIBUTE_READONLY) {
  393. err = 0;
  394. if !win32.DeleteFileW(p) {
  395. err = win32.GetLastError();
  396. }
  397. }
  398. }
  399. }
  400. }
  401. return Errno(err);
  402. }
  403. pipe :: proc() -> (r, w: Handle, err: Errno) {
  404. sa: win32.SECURITY_ATTRIBUTES;
  405. sa.nLength = size_of(win32.SECURITY_ATTRIBUTES);
  406. sa.bInheritHandle = true;
  407. if !win32.CreatePipe((^win32.HANDLE)(&r), (^win32.HANDLE)(&w), &sa, 0) {
  408. err = Errno(win32.GetLastError());
  409. }
  410. return;
  411. }