123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- package os
- import win32 "core:sys/windows"
- import "intrinsics"
- is_path_separator :: proc(c: byte) -> bool {
- return c == '/' || c == '\\';
- }
- open :: proc(path: string, mode: int = O_RDONLY, perm: int = 0) -> (Handle, Errno) {
- if len(path) == 0 {
- return INVALID_HANDLE, ERROR_FILE_NOT_FOUND;
- }
- access: u32;
- switch mode & (O_RDONLY|O_WRONLY|O_RDWR) {
- case O_RDONLY: access = win32.FILE_GENERIC_READ;
- case O_WRONLY: access = win32.FILE_GENERIC_WRITE;
- case O_RDWR: access = win32.FILE_GENERIC_READ | win32.FILE_GENERIC_WRITE;
- }
- if mode&O_APPEND != 0 {
- access &~= win32.FILE_GENERIC_WRITE;
- access |= win32.FILE_APPEND_DATA;
- }
- if mode&O_CREATE != 0 {
- access |= win32.FILE_GENERIC_WRITE;
- }
- share_mode := win32.FILE_SHARE_READ|win32.FILE_SHARE_WRITE;
- sa: ^win32.SECURITY_ATTRIBUTES = nil;
- sa_inherit := win32.SECURITY_ATTRIBUTES{nLength = size_of(win32.SECURITY_ATTRIBUTES), bInheritHandle = true};
- if mode&O_CLOEXEC == 0 {
- sa = &sa_inherit;
- }
- create_mode: u32;
- switch {
- case mode&(O_CREATE|O_EXCL) == (O_CREATE | O_EXCL):
- create_mode = win32.CREATE_NEW;
- case mode&(O_CREATE|O_TRUNC) == (O_CREATE | O_TRUNC):
- create_mode = win32.CREATE_ALWAYS;
- case mode&O_CREATE == O_CREATE:
- create_mode = win32.OPEN_ALWAYS;
- case mode&O_TRUNC == O_TRUNC:
- create_mode = win32.TRUNCATE_EXISTING;
- case:
- create_mode = win32.OPEN_EXISTING;
- }
- wide_path := win32.utf8_to_wstring(path);
- handle := Handle(win32.CreateFileW(wide_path, access, share_mode, sa, create_mode, win32.FILE_ATTRIBUTE_NORMAL|win32.FILE_FLAG_BACKUP_SEMANTICS, nil));
- if handle != INVALID_HANDLE {
- return handle, ERROR_NONE;
- }
- err := Errno(win32.GetLastError());
- return INVALID_HANDLE, err;
- }
- close :: proc(fd: Handle) -> Errno {
- if !win32.CloseHandle(win32.HANDLE(fd)) {
- return Errno(win32.GetLastError());
- }
- return ERROR_NONE;
- }
- flush :: proc(fd: Handle) -> (err: Errno) {
- if !win32.FlushFileBuffers(win32.HANDLE(fd)) {
- err = Errno(win32.GetLastError());
- }
- return;
- }
- write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
- if len(data) == 0 {
- return 0, ERROR_NONE;
- }
- single_write_length: win32.DWORD;
- total_write: i64;
- length := i64(len(data));
- for total_write < length {
- remaining := length - total_write;
- to_write := win32.DWORD(min(i32(remaining), MAX_RW));
- e := win32.WriteFile(win32.HANDLE(fd), &data[total_write], to_write, &single_write_length, nil);
- if single_write_length <= 0 || !e {
- err := Errno(win32.GetLastError());
- return int(total_write), err;
- }
- total_write += i64(single_write_length);
- }
- return int(total_write), ERROR_NONE;
- }
- read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
- if len(data) == 0 {
- return 0, ERROR_NONE;
- }
- single_read_length: win32.DWORD;
- total_read: i64;
- length := i64(len(data));
- for total_read < length {
- remaining := length - total_read;
- to_read := min(win32.DWORD(remaining), MAX_RW);
- e := win32.ReadFile(win32.HANDLE(fd), &data[total_read], to_read, &single_read_length, nil);
- if single_read_length <= 0 || !e {
- err := Errno(win32.GetLastError());
- return int(total_read), err;
- }
- total_read += i64(single_read_length);
- }
- return int(total_read), ERROR_NONE;
- }
- seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
- w: u32;
- switch whence {
- case 0: w = win32.FILE_BEGIN;
- case 1: w = win32.FILE_CURRENT;
- case 2: w = win32.FILE_END;
- }
- hi := i32(offset>>32);
- lo := i32(offset);
- ft := win32.GetFileType(win32.HANDLE(fd));
- if ft == win32.FILE_TYPE_PIPE {
- return 0, ERROR_FILE_IS_PIPE;
- }
- dw_ptr := win32.SetFilePointer(win32.HANDLE(fd), lo, &hi, w);
- if dw_ptr == win32.INVALID_SET_FILE_POINTER {
- err := Errno(win32.GetLastError());
- return 0, err;
- }
- return i64(hi)<<32 + i64(dw_ptr), ERROR_NONE;
- }
- file_size :: proc(fd: Handle) -> (i64, Errno) {
- length: win32.LARGE_INTEGER;
- err: Errno;
- if !win32.GetFileSizeEx(win32.HANDLE(fd), &length) {
- err = Errno(win32.GetLastError());
- }
- return i64(length), err;
- }
- @(private)
- MAX_RW :: 1<<30;
- @(private)
- pread :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
- buf := data;
- if len(buf) > MAX_RW {
- buf = buf[:MAX_RW];
- }
- curr_offset, e := seek(fd, offset, 1);
- if e != 0 {
- return 0, e;
- }
- defer seek(fd, curr_offset, 0);
- o := win32.OVERLAPPED{
- OffsetHigh = u32(offset>>32),
- Offset = u32(offset),
- };
- h := win32.HANDLE(fd);
- done: win32.DWORD;
- if !win32.ReadFile(h, raw_data(buf), u32(len(buf)), &done, &o) {
- e = Errno(win32.GetLastError());
- done = 0;
- }
- return int(done), e;
- }
- @(private)
- pwrite :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
- buf := data;
- if len(buf) > MAX_RW {
- buf = buf[:MAX_RW];
- }
- curr_offset, e := seek(fd, offset, 1);
- if e != 0 {
- return 0, e;
- }
- defer seek(fd, curr_offset, 0);
- o := win32.OVERLAPPED{
- OffsetHigh = u32(offset>>32),
- Offset = u32(offset),
- };
- h := win32.HANDLE(fd);
- done: win32.DWORD;
- if !win32.WriteFile(h, raw_data(buf), u32(len(buf)), &done, &o) {
- e = Errno(win32.GetLastError());
- done = 0;
- }
- return int(done), e;
- }
- read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Errno) {
- if offset < 0 {
- return 0, ERROR_NEGATIVE_OFFSET;
- }
- b, offset := data, offset;
- for len(b) > 0 {
- m, e := pread(fd, b, offset);
- if e != 0 {
- err = e;
- break;
- }
- n += m;
- b = b[m:];
- offset += i64(m);
- }
- return;
- }
- write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Errno) {
- if offset < 0 {
- return 0, ERROR_NEGATIVE_OFFSET;
- }
- b, offset := data, offset;
- for len(b) > 0 {
- m, e := pwrite(fd, b, offset);
- if e != 0 {
- err = e;
- break;
- }
- n += m;
- b = b[m:];
- offset += i64(m);
- }
- return;
- }
- // NOTE(bill): Uses startup to initialize it
- stdin := get_std_handle(uint(win32.STD_INPUT_HANDLE));
- stdout := get_std_handle(uint(win32.STD_OUTPUT_HANDLE));
- stderr := get_std_handle(uint(win32.STD_ERROR_HANDLE));
- get_std_handle :: proc "contextless" (h: uint) -> Handle {
- fd := win32.GetStdHandle(win32.DWORD(h));
- when size_of(uintptr) == 8 {
- win32.SetHandleInformation(fd, win32.HANDLE_FLAG_INHERIT, 0);
- }
- return Handle(fd);
- }
- exists :: proc(path: string) -> bool {
- wpath := win32.utf8_to_wstring(path, context.temp_allocator);
- attribs := win32.GetFileAttributesW(wpath);
- return i32(attribs) != win32.INVALID_FILE_ATTRIBUTES;
- }
- is_file :: proc(path: string) -> bool {
- wpath := win32.utf8_to_wstring(path, context.temp_allocator);
- attribs := win32.GetFileAttributesW(wpath);
- if i32(attribs) != win32.INVALID_FILE_ATTRIBUTES {
- return attribs & win32.FILE_ATTRIBUTE_DIRECTORY == 0;
- }
- return false;
- }
- is_dir :: proc(path: string) -> bool {
- wpath := win32.utf8_to_wstring(path, context.temp_allocator);
- attribs := win32.GetFileAttributesW(wpath);
- if i32(attribs) != win32.INVALID_FILE_ATTRIBUTES {
- return attribs & win32.FILE_ATTRIBUTE_DIRECTORY != 0;
- }
- return false;
- }
- // NOTE(tetra): GetCurrentDirectory is not thread safe with SetCurrentDirectory and GetFullPathName
- @private cwd_lock := win32.SRWLOCK{}; // zero is initialized
- get_current_directory :: proc(allocator := context.allocator) -> string {
- win32.AcquireSRWLockExclusive(&cwd_lock);
- sz_utf16 := win32.GetCurrentDirectoryW(0, nil);
- dir_buf_wstr := make([]u16, sz_utf16, context.temp_allocator); // the first time, it _includes_ the NUL.
- sz_utf16 = win32.GetCurrentDirectoryW(win32.DWORD(len(dir_buf_wstr)), raw_data(dir_buf_wstr));
- assert(int(sz_utf16)+1 == len(dir_buf_wstr)); // the second time, it _excludes_ the NUL.
- win32.ReleaseSRWLockExclusive(&cwd_lock);
- return win32.utf16_to_utf8(dir_buf_wstr, allocator);
- }
- set_current_directory :: proc(path: string) -> (err: Errno) {
- wstr := win32.utf8_to_wstring(path);
- win32.AcquireSRWLockExclusive(&cwd_lock);
- if !win32.SetCurrentDirectoryW(wstr) {
- err = Errno(win32.GetLastError());
- }
- win32.ReleaseSRWLockExclusive(&cwd_lock);
- return;
- }
- change_directory :: proc(path: string) -> Errno {
- wpath := win32.utf8_to_wstring(path, context.temp_allocator);
- return Errno(win32.SetCurrentDirectoryW(wpath));
- }
- make_directory :: proc(path: string, mode: u32) -> Errno {
- wpath := win32.utf8_to_wstring(path, context.temp_allocator);
- return Errno(win32.CreateDirectoryW(wpath, nil));
- }
- remove_directory :: proc(path: string) -> Errno {
- wpath := win32.utf8_to_wstring(path, context.temp_allocator);
- return Errno(win32.RemoveDirectoryW(wpath));
- }
- @(private)
- is_abs :: proc(path: string) -> bool {
- if len(path) > 0 && path[0] == '/' {
- return true;
- }
- when ODIN_OS == "windows" {
- if len(path) > 2 {
- switch path[0] {
- case 'A'..='Z', 'a'..='z':
- return path[1] == ':' && is_path_separator(path[2]);
- }
- }
- }
- return false;
- }
- @(private)
- fix_long_path :: proc(path: string) -> string {
- if len(path) < 248 {
- return path;
- }
- if len(path) >= 2 && path[:2] == `\\` {
- return path;
- }
- if !is_abs(path) {
- return path;
- }
- prefix :: `\\?`;
- path_buf := make([]byte, len(prefix)+len(path)+len(`\`), context.temp_allocator);
- copy(path_buf, prefix);
- n := len(path);
- r, w := 0, len(prefix);
- for r < n {
- switch {
- case is_path_separator(path[r]):
- r += 1;
- case path[r] == '.' && (r+1 == n || is_path_separator(path[r+1])):
- r += 1;
- case r+1 < n && path[r] == '.' && path[r+1] == '.' && (r+2 == n || is_path_separator(path[r+2])):
- return path;
- case:
- path_buf[w] = '\\';
- w += 1;
- for ; r < n && !is_path_separator(path[r]); r += 1 {
- path_buf[w] = path[r];
- w += 1;
- }
- }
- }
- if w == len(`\\?\c:`) {
- path_buf[w] = '\\';
- w += 1;
- }
- return string(path_buf[:w]);
- }
- link :: proc(old_name, new_name: string) -> Errno {
- n := win32.utf8_to_wstring(fix_long_path(new_name));
- o := win32.utf8_to_wstring(fix_long_path(old_name));
- return Errno(win32.CreateHardLinkW(n, o, nil));
- }
- unlink :: proc(path: string) -> Errno {
- wpath := win32.utf8_to_wstring(path, context.temp_allocator);
- return Errno(win32.DeleteFileW(wpath));
- }
- rename :: proc(old_path, new_path: string) -> Errno {
- from := win32.utf8_to_wstring(old_path, context.temp_allocator);
- to := win32.utf8_to_wstring(new_path, context.temp_allocator);
- return Errno(win32.MoveFileExW(from, to, win32.MOVEFILE_REPLACE_EXISTING));
- }
- ftruncate :: proc(fd: Handle, length: i64) -> (err: Errno) {
- curr_off, e := seek(fd, 0, 1);
- if e != 0 {
- return e;
- }
- defer seek(fd, curr_off, 0);
- _, e = seek(fd, length, 0);
- if e != 0 {
- return e;
- }
- ok := win32.SetEndOfFile(win32.HANDLE(fd));
- if !ok {
- return Errno(win32.GetLastError());
- }
- return ERROR_NONE;
- }
- truncate :: proc(path: string, length: i64) -> (err: Errno) {
- fd: Handle;
- fd, err = open(path, O_WRONLY|O_CREATE, 0o666);
- if err != 0 {
- return;
- }
- defer close(fd);
- err = ftruncate(fd, length);
- return;
- }
- remove :: proc(name: string) -> Errno {
- p := win32.utf8_to_wstring(fix_long_path(name));
- err, err1: win32.DWORD;
- if !win32.DeleteFileW(p) {
- err = win32.GetLastError();
- }
- if err == 0 {
- return 0;
- }
- if !win32.RemoveDirectoryW(p) {
- err1 = win32.GetLastError();
- }
- if err1 == 0 {
- return 0;
- }
- if err != err1 {
- a := win32.GetFileAttributesW(p);
- if a == ~u32(0) {
- err = win32.GetLastError();
- } else {
- if a & win32.FILE_ATTRIBUTE_DIRECTORY != 0 {
- err = err1;
- } else if a & win32.FILE_ATTRIBUTE_READONLY != 0 {
- if win32.SetFileAttributesW(p, a &~ win32.FILE_ATTRIBUTE_READONLY) {
- err = 0;
- if !win32.DeleteFileW(p) {
- err = win32.GetLastError();
- }
- }
- }
- }
- }
- return Errno(err);
- }
- pipe :: proc() -> (r, w: Handle, err: Errno) {
- sa: win32.SECURITY_ATTRIBUTES;
- sa.nLength = size_of(win32.SECURITY_ATTRIBUTES);
- sa.bInheritHandle = true;
- if !win32.CreatePipe((^win32.HANDLE)(&r), (^win32.HANDLE)(&w), &sa, 0) {
- err = Errno(win32.GetLastError());
- }
- return;
- }
|