Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core:os clean up of interface stuff #4021

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/os/dir_unix.odin
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package os
import "core:strings"

@(require_results)
read_dir :: proc(fd: Handle, n: int, allocator := context.allocator) -> (fi: []File_Info, err: Error) {
_read_dir :: proc(fd: Handle, n: int, allocator := context.allocator) -> (fi: []File_Info, err: Error) {
dirp := _fdopendir(fd) or_return
defer _closedir(dirp)

Expand Down
2 changes: 1 addition & 1 deletion core/os/dir_windows.odin
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "core:strings"
import "base:runtime"

@(require_results)
read_dir :: proc(fd: Handle, n: int, allocator := context.allocator) -> (fi: []File_Info, err: Error) {
_read_dir :: proc(fd: Handle, n: int, allocator := context.allocator) -> (fi: []File_Info, err: Error) {
@(require_results)
find_data_to_file_info :: proc(base_path: string, d: ^win32.WIN32_FIND_DATAW) -> (fi: File_Info) {
// Ignore "." and ".."
Expand Down
12 changes: 6 additions & 6 deletions core/os/env_windows.odin
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "base:runtime"
// Otherwise the returned value will be empty and the boolean will be false
// NOTE: the value will be allocated with the supplied allocator
@(require_results)
lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
_lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
if key == "" {
return
}
Expand All @@ -35,13 +35,13 @@ lookup_env :: proc(key: string, allocator := context.allocator) -> (value: strin
// To distinguish between an empty value and an unset value, use lookup_env
// NOTE: the value will be allocated with the supplied allocator
@(require_results)
get_env :: proc(key: string, allocator := context.allocator) -> (value: string) {
_get_env :: proc(key: string, allocator := context.allocator) -> (value: string) {
value, _ = lookup_env(key, allocator)
return
}

// set_env sets the value of the environment variable named by the key
set_env :: proc(key, value: string) -> Error {
_set_env :: proc(key, value: string) -> Error {
k := win32.utf8_to_wstring(key)
v := win32.utf8_to_wstring(value)

Expand All @@ -52,7 +52,7 @@ set_env :: proc(key, value: string) -> Error {
}

// unset_env unsets a single environment variable
unset_env :: proc(key: string) -> Error {
_unset_env :: proc(key: string) -> Error {
k := win32.utf8_to_wstring(key)
if !win32.SetEnvironmentVariableW(k, nil) {
return get_last_error()
Expand All @@ -63,7 +63,7 @@ unset_env :: proc(key: string) -> Error {
// environ returns a copy of strings representing the environment, in the form "key=value"
// NOTE: the slice of strings and the strings with be allocated using the supplied allocator
@(require_results)
environ :: proc(allocator := context.allocator) -> []string {
_environ :: proc(allocator := context.allocator) -> []string {
envs := ([^]win32.WCHAR)(win32.GetEnvironmentStringsW())
if envs == nil {
return nil
Expand All @@ -89,7 +89,7 @@ environ :: proc(allocator := context.allocator) -> []string {


// clear_env deletes all environment variables
clear_env :: proc() {
_clear_env :: proc() {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
envs := environ(context.temp_allocator)
for env in envs {
Expand Down
59 changes: 34 additions & 25 deletions core/os/file_windows.odin
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import "base:runtime"
import "core:unicode/utf16"

@(require_results)
is_path_separator :: proc(c: byte) -> bool {
_is_path_separator :: proc "contextless" (c: rune) -> bool {
return c == '/' || c == '\\'
}

@(require_results)
open :: proc(path: string, mode: int = O_RDONLY, perm: int = 0) -> (Handle, Error) {
_open :: proc(path: string, mode: int = O_RDONLY, perm: int = 0) -> (Handle, Error) {
if len(path) == 0 {
return INVALID_HANDLE, General_Error.Not_Exist
}
Expand Down Expand Up @@ -60,14 +60,14 @@ open :: proc(path: string, mode: int = O_RDONLY, perm: int = 0) -> (Handle, Erro
return INVALID_HANDLE, get_last_error()
}

close :: proc(fd: Handle) -> Error {
_close :: proc(fd: Handle) -> Error {
if !win32.CloseHandle(win32.HANDLE(fd)) {
return get_last_error()
}
return nil
}

flush :: proc(fd: Handle) -> (err: Error) {
_flush :: proc(fd: Handle) -> (err: Error) {
if !win32.FlushFileBuffers(win32.HANDLE(fd)) {
err = get_last_error()
}
Expand All @@ -76,7 +76,7 @@ flush :: proc(fd: Handle) -> (err: Error) {



write :: proc(fd: Handle, data: []byte) -> (int, Error) {
_write :: proc(fd: Handle, data: []byte) -> (int, Error) {
if len(data) == 0 {
return 0, nil
}
Expand Down Expand Up @@ -149,7 +149,7 @@ read_console :: proc(handle: win32.HANDLE, b: []byte) -> (n: int, err: Error) {
return
}

read :: proc(fd: Handle, data: []byte) -> (total_read: int, err: Error) {
_read :: proc(fd: Handle, data: []byte) -> (total_read: int, err: Error) {
if len(data) == 0 {
return 0, nil
}
Expand Down Expand Up @@ -186,7 +186,7 @@ read :: proc(fd: Handle, data: []byte) -> (total_read: int, err: Error) {
return total_read, nil
}

seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
_seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
w: u32
switch whence {
case 0: w = win32.FILE_BEGIN
Expand All @@ -209,7 +209,7 @@ seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
}

@(require_results)
file_size :: proc(fd: Handle) -> (i64, Error) {
_file_size :: proc(fd: Handle) -> (i64, Error) {
length: win32.LARGE_INTEGER
err: Error
if !win32.GetFileSizeEx(win32.HANDLE(fd), &length) {
Expand Down Expand Up @@ -279,7 +279,7 @@ on Windows, read_at changes the position of the file cursor, on *nix, it does no

will read from the location twice on *nix, and from two different locations on Windows
*/
read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
_read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
if offset < 0 {
return 0, .Invalid_Offset
}
Expand Down Expand Up @@ -311,7 +311,7 @@ on Windows, write_at changes the position of the file cursor, on *nix, it does n

will write to the location twice on *nix, and to two different locations on Windows
*/
write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
_write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
if offset < 0 {
return 0, .Invalid_Offset
}
Expand Down Expand Up @@ -341,7 +341,7 @@ get_std_handle :: proc "contextless" (h: uint) -> Handle {
}


exists :: proc(path: string) -> bool {
_exists :: proc(path: string) -> bool {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
wpath := win32.utf8_to_wstring(path, context.temp_allocator)
attribs := win32.GetFileAttributesW(wpath)
Expand All @@ -350,7 +350,7 @@ exists :: proc(path: string) -> bool {
}

@(require_results)
is_file :: proc(path: string) -> bool {
_is_file_path :: proc(path: string, _: bool) -> bool {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
wpath := win32.utf8_to_wstring(path, context.temp_allocator)
attribs := win32.GetFileAttributesW(wpath)
Expand All @@ -362,7 +362,7 @@ is_file :: proc(path: string) -> bool {
}

@(require_results)
is_dir :: proc(path: string) -> bool {
_is_dir_path :: proc(path: string, _: bool) -> bool {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
wpath := win32.utf8_to_wstring(path, context.temp_allocator)
attribs := win32.GetFileAttributesW(wpath)
Expand All @@ -373,11 +373,21 @@ is_dir :: proc(path: string) -> bool {
return false
}

@(require_results)
_is_file_handle :: proc(fd: Handle) -> bool {
return false
}

@(require_results)
_is_dir_handle :: proc(fd: Handle) -> bool {
return false
}

// NOTE(tetra): GetCurrentDirectory is not thread safe with SetCurrentDirectory and GetFullPathName
@private cwd_lock := win32.SRWLOCK{} // zero is initialized

@(require_results)
get_current_directory :: proc(allocator := context.allocator) -> string {
_get_current_directory :: proc(allocator := context.allocator) -> string {
win32.AcquireSRWLockExclusive(&cwd_lock)

runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == allocator)
Expand All @@ -393,7 +403,7 @@ get_current_directory :: proc(allocator := context.allocator) -> string {
return win32.utf16_to_utf8(dir_buf_wstr, allocator) or_else ""
}

set_current_directory :: proc(path: string) -> (err: Error) {
_set_current_directory :: proc(path: string) -> (err: Error) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
wstr := win32.utf8_to_wstring(path, context.temp_allocator)

Expand All @@ -407,9 +417,8 @@ set_current_directory :: proc(path: string) -> (err: Error) {

return
}
change_directory :: set_current_directory

make_directory :: proc(path: string, mode: u32 = 0) -> (err: Error) {
_make_directory :: proc(path: string, mode: u32 = 0) -> (err: Error) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
// Mode is unused on Windows, but is needed on *nix
wpath := win32.utf8_to_wstring(path, context.temp_allocator)
Expand All @@ -421,7 +430,7 @@ make_directory :: proc(path: string, mode: u32 = 0) -> (err: Error) {
}


remove_directory :: proc(path: string) -> (err: Error) {
_remove_directory :: proc(path: string) -> (err: Error) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
wpath := win32.utf8_to_wstring(path, context.temp_allocator)

Expand Down Expand Up @@ -494,14 +503,14 @@ fix_long_path :: proc(path: string) -> string {
}


link :: proc(old_name, new_name: string) -> (err: Error) {
_link :: proc(old_name, new_name: string) -> (err: Error) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
n := win32.utf8_to_wstring(fix_long_path(new_name))
o := win32.utf8_to_wstring(fix_long_path(old_name))
return Platform_Error(win32.CreateHardLinkW(n, o, nil))
}

unlink :: proc(path: string) -> (err: Error) {
_unlink :: proc(path: string) -> (err: Error) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
wpath := win32.utf8_to_wstring(path, context.temp_allocator)

Expand All @@ -513,7 +522,7 @@ unlink :: proc(path: string) -> (err: Error) {



rename :: proc(old_path, new_path: string) -> (err: Error) {
_rename :: proc(old_path, new_path: string) -> (err: Error) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
from := win32.utf8_to_wstring(old_path, context.temp_allocator)
to := win32.utf8_to_wstring(new_path, context.temp_allocator)
Expand All @@ -525,7 +534,7 @@ rename :: proc(old_path, new_path: string) -> (err: Error) {
}


ftruncate :: proc(fd: Handle, length: i64) -> (err: Error) {
_ftruncate :: proc(fd: Handle, length: i64) -> (err: Error) {
curr_off := seek(fd, 0, 1) or_return
defer seek(fd, curr_off, 0)
_= seek(fd, length, 0) or_return
Expand All @@ -536,14 +545,14 @@ ftruncate :: proc(fd: Handle, length: i64) -> (err: Error) {
return nil
}

truncate :: proc(path: string, length: i64) -> (err: Error) {
_truncate :: proc(path: string, length: i64) -> (err: Error) {
fd := open(path, O_WRONLY|O_CREATE, 0o666) or_return
defer close(fd)
return ftruncate(fd, length)
}


remove :: proc(name: string) -> Error {
_remove :: proc(name: string) -> Error {
p := win32.utf8_to_wstring(fix_long_path(name))
err, err1: win32.DWORD
if !win32.DeleteFileW(p) {
Expand Down Expand Up @@ -582,7 +591,7 @@ remove :: proc(name: string) -> Error {


@(require_results)
pipe :: proc() -> (r, w: Handle, err: Error) {
_pipe :: proc() -> (r, w: Handle, err: Error) {
sa: win32.SECURITY_ATTRIBUTES
sa.nLength = size_of(win32.SECURITY_ATTRIBUTES)
sa.bInheritHandle = true
Expand Down
Loading
Loading