mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 22:58:46 +00:00
Begin mapping os.Error in the rest of the codebase
This commit is contained in:
@@ -254,7 +254,7 @@ parse_and_set_pointer_by_named_type :: proc(ptr: rawptr, str: string, data_type:
|
|||||||
}
|
}
|
||||||
|
|
||||||
handle, errno := os.open(str, mode, perms)
|
handle, errno := os.open(str, mode, perms)
|
||||||
if errno != 0 {
|
if errno != nil {
|
||||||
// NOTE(Feoramund): os.Error is system-dependent, and there's
|
// NOTE(Feoramund): os.Error is system-dependent, and there's
|
||||||
// currently no good way to translate them all into strings.
|
// currently no good way to translate them all into strings.
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -53,9 +53,9 @@ _create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (so
|
|||||||
unreachable()
|
unreachable()
|
||||||
}
|
}
|
||||||
|
|
||||||
sock, ok := os.socket(c_family, c_type, c_protocol)
|
sock, sock_err := os.socket(c_family, c_type, c_protocol)
|
||||||
if ok != os.ERROR_NONE {
|
if sock_err != nil {
|
||||||
err = Create_Socket_Error(ok)
|
err = Create_Socket_Error(os.is_platform_error(sock_err) or_else -1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ _dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_optio
|
|||||||
|
|
||||||
sockaddr := _endpoint_to_sockaddr(endpoint)
|
sockaddr := _endpoint_to_sockaddr(endpoint)
|
||||||
res := os.connect(os.Socket(skt), (^os.SOCKADDR)(&sockaddr), i32(sockaddr.len))
|
res := os.connect(os.Socket(skt), (^os.SOCKADDR)(&sockaddr), i32(sockaddr.len))
|
||||||
if res != os.ERROR_NONE {
|
if res != nil {
|
||||||
err = Dial_Error(res)
|
err = Dial_Error(res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -100,7 +100,7 @@ _bind :: proc(skt: Any_Socket, ep: Endpoint) -> (err: Network_Error) {
|
|||||||
sockaddr := _endpoint_to_sockaddr(ep)
|
sockaddr := _endpoint_to_sockaddr(ep)
|
||||||
s := any_socket_to_socket(skt)
|
s := any_socket_to_socket(skt)
|
||||||
res := os.bind(os.Socket(s), (^os.SOCKADDR)(&sockaddr), i32(sockaddr.len))
|
res := os.bind(os.Socket(s), (^os.SOCKADDR)(&sockaddr), i32(sockaddr.len))
|
||||||
if res != os.ERROR_NONE {
|
if res != nil {
|
||||||
if res == os.EACCES && ep.port <= MAX_PRIVILEGED_PORT {
|
if res == os.EACCES && ep.port <= MAX_PRIVILEGED_PORT {
|
||||||
err = .Privileged_Port_Without_Root
|
err = .Privileged_Port_Without_Root
|
||||||
} else {
|
} else {
|
||||||
@@ -128,7 +128,7 @@ _listen_tcp :: proc(interface_endpoint: Endpoint, backlog := 1000) -> (skt: TCP_
|
|||||||
bind(sock, interface_endpoint) or_return
|
bind(sock, interface_endpoint) or_return
|
||||||
|
|
||||||
res := os.listen(os.Socket(skt), backlog)
|
res := os.listen(os.Socket(skt), backlog)
|
||||||
if res != os.ERROR_NONE {
|
if res != nil {
|
||||||
err = Listen_Error(res)
|
err = Listen_Error(res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -141,9 +141,9 @@ _accept_tcp :: proc(sock: TCP_Socket, options := default_tcp_options) -> (client
|
|||||||
sockaddr: os.SOCKADDR_STORAGE_LH
|
sockaddr: os.SOCKADDR_STORAGE_LH
|
||||||
sockaddrlen := c.int(size_of(sockaddr))
|
sockaddrlen := c.int(size_of(sockaddr))
|
||||||
|
|
||||||
client_sock, ok := os.accept(os.Socket(sock), cast(^os.SOCKADDR) &sockaddr, &sockaddrlen)
|
client_sock, client_sock_err := os.accept(os.Socket(sock), cast(^os.SOCKADDR) &sockaddr, &sockaddrlen)
|
||||||
if ok != os.ERROR_NONE {
|
if client_sock_err != nil {
|
||||||
err = Accept_Error(ok)
|
err = Accept_Error(os.is_platform_error(client_sock_err) or_else -1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
client = TCP_Socket(client_sock)
|
client = TCP_Socket(client_sock)
|
||||||
|
|||||||
@@ -635,6 +635,7 @@ foreign dl {
|
|||||||
@(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
|
@(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@(require_results, no_instrumentation)
|
||||||
get_last_error :: proc "contextless" () -> Error {
|
get_last_error :: proc "contextless" () -> Error {
|
||||||
return Platform_Error(__error()^)
|
return Platform_Error(__error()^)
|
||||||
}
|
}
|
||||||
|
|||||||
+50
-49
@@ -369,7 +369,7 @@ R_OK :: 4 // Test for read permission
|
|||||||
F_KINFO :: 22
|
F_KINFO :: 22
|
||||||
|
|
||||||
foreign libc {
|
foreign libc {
|
||||||
@(link_name="__error") __errno_location :: proc() -> ^c.int ---
|
@(link_name="__error") __Error_location :: proc() -> ^c.int ---
|
||||||
|
|
||||||
@(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle ---
|
@(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle ---
|
||||||
@(link_name="close") _unix_close :: proc(fd: Handle) -> c.int ---
|
@(link_name="close") _unix_close :: proc(fd: Handle) -> c.int ---
|
||||||
@@ -419,24 +419,25 @@ is_path_separator :: proc(r: rune) -> bool {
|
|||||||
return r == '/'
|
return r == '/'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@(require_results, no_instrumentation)
|
||||||
get_last_error :: proc "contextless" () -> Error {
|
get_last_error :: proc "contextless" () -> Error {
|
||||||
return Platform_Error(__errno_location()^)
|
return Platform_Error(__Error_location()^)
|
||||||
}
|
}
|
||||||
|
|
||||||
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
|
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
handle := _unix_open(cstr, c.int(flags), c.int(mode))
|
handle := _unix_open(cstr, c.int(flags), c.int(mode))
|
||||||
if handle == -1 {
|
if handle == -1 {
|
||||||
return INVALID_HANDLE, Errno(get_last_error())
|
return INVALID_HANDLE, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return handle, nil
|
return handle, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
close :: proc(fd: Handle) -> Errno {
|
close :: proc(fd: Handle) -> Error {
|
||||||
result := _unix_close(fd)
|
result := _unix_close(fd)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -449,16 +450,16 @@ close :: proc(fd: Handle) -> Errno {
|
|||||||
@(private)
|
@(private)
|
||||||
MAX_RW :: 1 << 30
|
MAX_RW :: 1 << 30
|
||||||
|
|
||||||
read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
read :: proc(fd: Handle, data: []byte) -> (int, Error) {
|
||||||
to_read := min(c.size_t(len(data)), MAX_RW)
|
to_read := min(c.size_t(len(data)), MAX_RW)
|
||||||
bytes_read := _unix_read(fd, &data[0], to_read)
|
bytes_read := _unix_read(fd, &data[0], to_read)
|
||||||
if bytes_read == -1 {
|
if bytes_read == -1 {
|
||||||
return -1, Errno(get_last_error())
|
return -1, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return int(bytes_read), nil
|
return int(bytes_read), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
write :: proc(fd: Handle, data: []byte) -> (int, Error) {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
@@ -466,63 +467,63 @@ write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
|||||||
to_write := min(c.size_t(len(data)), MAX_RW)
|
to_write := min(c.size_t(len(data)), MAX_RW)
|
||||||
bytes_written := _unix_write(fd, &data[0], to_write)
|
bytes_written := _unix_write(fd, &data[0], to_write)
|
||||||
if bytes_written == -1 {
|
if bytes_written == -1 {
|
||||||
return -1, Errno(get_last_error())
|
return -1, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return int(bytes_written), nil
|
return int(bytes_written), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
|
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
|
||||||
res := _unix_seek(fd, offset, c.int(whence))
|
res := _unix_seek(fd, offset, c.int(whence))
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return -1, Errno(get_last_error())
|
return -1, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
file_size :: proc(fd: Handle) -> (size: i64, err: Errno) {
|
file_size :: proc(fd: Handle) -> (size: i64, err: Error) {
|
||||||
size = -1
|
size = -1
|
||||||
s := _fstat(fd) or_return
|
s := _fstat(fd) or_return
|
||||||
size = s.size
|
size = s.size
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rename :: proc(old_path, new_path: string) -> Errno {
|
rename :: proc(old_path, new_path: string) -> Error {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
old_path_cstr := strings.clone_to_cstring(old_path, context.temp_allocator)
|
old_path_cstr := strings.clone_to_cstring(old_path, context.temp_allocator)
|
||||||
new_path_cstr := strings.clone_to_cstring(new_path, context.temp_allocator)
|
new_path_cstr := strings.clone_to_cstring(new_path, context.temp_allocator)
|
||||||
res := _unix_rename(old_path_cstr, new_path_cstr)
|
res := _unix_rename(old_path_cstr, new_path_cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
remove :: proc(path: string) -> Errno {
|
remove :: proc(path: string) -> Error {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_unlink(path_cstr)
|
res := _unix_unlink(path_cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
make_directory :: proc(path: string, mode: mode_t = 0o775) -> Errno {
|
make_directory :: proc(path: string, mode: mode_t = 0o775) -> Error {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_mkdir(path_cstr, mode)
|
res := _unix_mkdir(path_cstr, mode)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
remove_directory :: proc(path: string) -> Errno {
|
remove_directory :: proc(path: string) -> Error {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_rmdir(path_cstr)
|
res := _unix_rmdir(path_cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -537,7 +538,7 @@ is_file_handle :: proc(fd: Handle) -> bool {
|
|||||||
|
|
||||||
is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
|
is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
|
||||||
s: OS_Stat
|
s: OS_Stat
|
||||||
err: Errno
|
err: Error
|
||||||
if follow_links {
|
if follow_links {
|
||||||
s, err = _stat(path)
|
s, err = _stat(path)
|
||||||
} else {
|
} else {
|
||||||
@@ -559,7 +560,7 @@ is_dir_handle :: proc(fd: Handle) -> bool {
|
|||||||
|
|
||||||
is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
|
is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
|
||||||
s: OS_Stat
|
s: OS_Stat
|
||||||
err: Errno
|
err: Error
|
||||||
if follow_links {
|
if follow_links {
|
||||||
s, err = _stat(path)
|
s, err = _stat(path)
|
||||||
} else {
|
} else {
|
||||||
@@ -584,7 +585,7 @@ stderr: Handle = 2
|
|||||||
last_write_time :: proc(fd: Handle) -> File_Time {}
|
last_write_time :: proc(fd: Handle) -> File_Time {}
|
||||||
last_write_time_by_name :: proc(name: string) -> File_Time {}
|
last_write_time_by_name :: proc(name: string) -> File_Time {}
|
||||||
*/
|
*/
|
||||||
last_write_time :: proc(fd: Handle) -> (File_Time, Errno) {
|
last_write_time :: proc(fd: Handle) -> (File_Time, Error) {
|
||||||
s, err := _fstat(fd)
|
s, err := _fstat(fd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -593,7 +594,7 @@ last_write_time :: proc(fd: Handle) -> (File_Time, Errno) {
|
|||||||
return File_Time(modified), nil
|
return File_Time(modified), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
|
last_write_time_by_name :: proc(name: string) -> (File_Time, Error) {
|
||||||
s, err := _stat(name)
|
s, err := _stat(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -603,19 +604,19 @@ last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_stat :: proc(path: string) -> (OS_Stat, Errno) {
|
_stat :: proc(path: string) -> (OS_Stat, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
s: OS_Stat = ---
|
s: OS_Stat = ---
|
||||||
result := _unix_lstat(cstr, &s)
|
result := _unix_lstat(cstr, &s)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return s, Errno(get_last_error())
|
return s, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
_lstat :: proc(path: string) -> (OS_Stat, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
|
|
||||||
@@ -623,35 +624,35 @@ _lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
|||||||
s: OS_Stat = ---
|
s: OS_Stat = ---
|
||||||
res := _unix_lstat(cstr, &s)
|
res := _unix_lstat(cstr, &s)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return s, Errno(get_last_error())
|
return s, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
|
_fstat :: proc(fd: Handle) -> (OS_Stat, Error) {
|
||||||
s: OS_Stat = ---
|
s: OS_Stat = ---
|
||||||
result := _unix_fstat(fd, &s)
|
result := _unix_fstat(fd, &s)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return s, Errno(get_last_error())
|
return s, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
|
_fdopendir :: proc(fd: Handle) -> (Dir, Error) {
|
||||||
dirp := _unix_fdopendir(fd)
|
dirp := _unix_fdopendir(fd)
|
||||||
if dirp == cast(Dir)nil {
|
if dirp == cast(Dir)nil {
|
||||||
return nil, Errno(get_last_error())
|
return nil, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return dirp, nil
|
return dirp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_closedir :: proc(dirp: Dir) -> Errno {
|
_closedir :: proc(dirp: Dir) -> Error {
|
||||||
rc := _unix_closedir(dirp)
|
rc := _unix_closedir(dirp)
|
||||||
if rc != 0 {
|
if rc != 0 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -662,12 +663,12 @@ _rewinddir :: proc(dirp: Dir) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
|
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Error, end_of_stream: bool) {
|
||||||
result: ^Dirent
|
result: ^Dirent
|
||||||
rc := _unix_readdir_r(dirp, &entry, &result)
|
rc := _unix_readdir_r(dirp, &entry, &result)
|
||||||
|
|
||||||
if rc != 0 {
|
if rc != 0 {
|
||||||
err = Errno(get_last_error())
|
err = Error(get_last_error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -680,7 +681,7 @@ _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool)
|
|||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_readlink :: proc(path: string) -> (string, Errno) {
|
_readlink :: proc(path: string) -> (string, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
|
||||||
|
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
@@ -691,7 +692,7 @@ _readlink :: proc(path: string) -> (string, Errno) {
|
|||||||
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
|
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
|
||||||
if rc == -1 {
|
if rc == -1 {
|
||||||
delete(buf)
|
delete(buf)
|
||||||
return "", Errno(get_last_error())
|
return "", Error(get_last_error())
|
||||||
} else if rc == int(bufsz) {
|
} else if rc == int(bufsz) {
|
||||||
bufsz += MAX_PATH
|
bufsz += MAX_PATH
|
||||||
delete(buf)
|
delete(buf)
|
||||||
@@ -701,10 +702,10 @@ _readlink :: proc(path: string) -> (string, Errno) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", Errno{}
|
return "", Error{}
|
||||||
}
|
}
|
||||||
|
|
||||||
absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
|
absolute_path_from_handle :: proc(fd: Handle) -> (string, Error) {
|
||||||
// NOTE(Feoramund): The situation isn't ideal, but this was the best way I
|
// NOTE(Feoramund): The situation isn't ideal, but this was the best way I
|
||||||
// could find to implement this. There are a couple outstanding bug reports
|
// could find to implement this. There are a couple outstanding bug reports
|
||||||
// regarding the desire to retrieve an absolute path from a handle, but to
|
// regarding the desire to retrieve an absolute path from a handle, but to
|
||||||
@@ -719,14 +720,14 @@ absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
|
|||||||
|
|
||||||
res := _unix_fcntl(fd, F_KINFO, cast(uintptr)&kinfo)
|
res := _unix_fcntl(fd, F_KINFO, cast(uintptr)&kinfo)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return "", Errno(get_last_error())
|
return "", Error(get_last_error())
|
||||||
}
|
}
|
||||||
|
|
||||||
path := strings.clone_from_cstring_bounded(cast(cstring)&kinfo.path[0], len(kinfo.path))
|
path := strings.clone_from_cstring_bounded(cast(cstring)&kinfo.path[0], len(kinfo.path))
|
||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) {
|
||||||
rel := rel
|
rel := rel
|
||||||
if rel == "" {
|
if rel == "" {
|
||||||
rel = "."
|
rel = "."
|
||||||
@@ -737,7 +738,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
|||||||
|
|
||||||
path_ptr := _unix_realpath(rel_cstr, nil)
|
path_ptr := _unix_realpath(rel_cstr, nil)
|
||||||
if path_ptr == nil {
|
if path_ptr == nil {
|
||||||
return "", Errno(get_last_error())
|
return "", Error(get_last_error())
|
||||||
}
|
}
|
||||||
defer _unix_free(path_ptr)
|
defer _unix_free(path_ptr)
|
||||||
|
|
||||||
@@ -747,13 +748,13 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
|||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
access :: proc(path: string, mask: int) -> (bool, Errno) {
|
access :: proc(path: string, mask: int) -> (bool, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
|
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
result := _unix_access(cstr, c.int(mask))
|
result := _unix_access(cstr, c.int(mask))
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return false, Errno(get_last_error())
|
return false, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
@@ -785,7 +786,7 @@ get_current_directory :: proc() -> string {
|
|||||||
if cwd != nil {
|
if cwd != nil {
|
||||||
return string(cwd)
|
return string(cwd)
|
||||||
}
|
}
|
||||||
if Errno(get_last_error()) != ERANGE {
|
if Error(get_last_error()) != ERANGE {
|
||||||
delete(buf)
|
delete(buf)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -794,12 +795,12 @@ get_current_directory :: proc() -> string {
|
|||||||
unreachable()
|
unreachable()
|
||||||
}
|
}
|
||||||
|
|
||||||
set_current_directory :: proc(path: string) -> (err: Errno) {
|
set_current_directory :: proc(path: string) -> (err: Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_chdir(cstr)
|
res := _unix_chdir(cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+35
-34
@@ -119,7 +119,7 @@ S_ISSOCK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFSOCK
|
|||||||
|
|
||||||
|
|
||||||
foreign libc {
|
foreign libc {
|
||||||
@(link_name="_errnop") __error :: proc() -> ^c.int ---
|
@(link_name="_Errorp") __error :: proc() -> ^c.int ---
|
||||||
|
|
||||||
@(link_name="fork") _unix_fork :: proc() -> pid_t ---
|
@(link_name="fork") _unix_fork :: proc() -> pid_t ---
|
||||||
@(link_name="getthrid") _unix_getthrid :: proc() -> int ---
|
@(link_name="getthrid") _unix_getthrid :: proc() -> int ---
|
||||||
@@ -181,32 +181,33 @@ is_path_separator :: proc(r: rune) -> bool {
|
|||||||
return r == '/'
|
return r == '/'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@(require_results, no_instrumentation)
|
||||||
get_last_error :: proc "contextless" () -> Error {
|
get_last_error :: proc "contextless" () -> Error {
|
||||||
return Platform_Error(__error()^)
|
return Platform_Error(__error()^)
|
||||||
}
|
}
|
||||||
|
|
||||||
fork :: proc() -> (Pid, Errno) {
|
fork :: proc() -> (Pid, Error) {
|
||||||
pid := _unix_fork()
|
pid := _unix_fork()
|
||||||
if pid == -1 {
|
if pid == -1 {
|
||||||
return Pid(-1), Errno(get_last_error())
|
return Pid(-1), Error(get_last_error())
|
||||||
}
|
}
|
||||||
return Pid(pid), nil
|
return Pid(pid), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
|
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
handle := _unix_open(cstr, c.int(flags), c.int(mode))
|
handle := _unix_open(cstr, c.int(flags), c.int(mode))
|
||||||
if handle == -1 {
|
if handle == -1 {
|
||||||
return INVALID_HANDLE, Errno(get_last_error())
|
return INVALID_HANDLE, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return handle, nil
|
return handle, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
close :: proc(fd: Handle) -> Errno {
|
close :: proc(fd: Handle) -> Error {
|
||||||
result := _unix_close(fd)
|
result := _unix_close(fd)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -218,16 +219,16 @@ close :: proc(fd: Handle) -> Errno {
|
|||||||
@(private)
|
@(private)
|
||||||
MAX_RW :: 1 << 30
|
MAX_RW :: 1 << 30
|
||||||
|
|
||||||
read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
read :: proc(fd: Handle, data: []byte) -> (int, Error) {
|
||||||
to_read := min(c.size_t(len(data)), MAX_RW)
|
to_read := min(c.size_t(len(data)), MAX_RW)
|
||||||
bytes_read := _unix_read(fd, &data[0], to_read)
|
bytes_read := _unix_read(fd, &data[0], to_read)
|
||||||
if bytes_read == -1 {
|
if bytes_read == -1 {
|
||||||
return -1, Errno(get_last_error())
|
return -1, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return int(bytes_read), nil
|
return int(bytes_read), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
write :: proc(fd: Handle, data: []byte) -> (int, Error) {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
@@ -235,20 +236,20 @@ write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
|||||||
to_write := min(c.size_t(len(data)), MAX_RW)
|
to_write := min(c.size_t(len(data)), MAX_RW)
|
||||||
bytes_written := _unix_write(fd, &data[0], to_write)
|
bytes_written := _unix_write(fd, &data[0], to_write)
|
||||||
if bytes_written == -1 {
|
if bytes_written == -1 {
|
||||||
return -1, Errno(get_last_error())
|
return -1, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return int(bytes_written), nil
|
return int(bytes_written), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
|
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
|
||||||
res := _unix_seek(fd, offset, c.int(whence))
|
res := _unix_seek(fd, offset, c.int(whence))
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return -1, Errno(get_last_error())
|
return -1, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
file_size :: proc(fd: Handle) -> (i64, Errno) {
|
file_size :: proc(fd: Handle) -> (i64, Error) {
|
||||||
s, err := _fstat(fd)
|
s, err := _fstat(fd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return -1, err
|
||||||
@@ -268,7 +269,7 @@ _alloc_command_line_arguments :: proc() -> []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_stat :: proc(path: string) -> (OS_Stat, Errno) {
|
_stat :: proc(path: string) -> (OS_Stat, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
|
|
||||||
@@ -276,13 +277,13 @@ _stat :: proc(path: string) -> (OS_Stat, Errno) {
|
|||||||
s: OS_Stat = ---
|
s: OS_Stat = ---
|
||||||
res := _unix_stat(cstr, &s)
|
res := _unix_stat(cstr, &s)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return s, Errno(get_last_error())
|
return s, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
_lstat :: proc(path: string) -> (OS_Stat, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
|
|
||||||
@@ -290,36 +291,36 @@ _lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
|||||||
s: OS_Stat = ---
|
s: OS_Stat = ---
|
||||||
res := _unix_lstat(cstr, &s)
|
res := _unix_lstat(cstr, &s)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return s, Errno(get_last_error())
|
return s, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
|
_fstat :: proc(fd: Handle) -> (OS_Stat, Error) {
|
||||||
// deliberately uninitialized
|
// deliberately uninitialized
|
||||||
s: OS_Stat = ---
|
s: OS_Stat = ---
|
||||||
res := _unix_fstat(fd, &s)
|
res := _unix_fstat(fd, &s)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return s, Errno(get_last_error())
|
return s, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
|
_fdopendir :: proc(fd: Handle) -> (Dir, Error) {
|
||||||
dirp := _unix_fdopendir(fd)
|
dirp := _unix_fdopendir(fd)
|
||||||
if dirp == cast(Dir)nil {
|
if dirp == cast(Dir)nil {
|
||||||
return nil, Errno(get_last_error())
|
return nil, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return dirp, nil
|
return dirp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_closedir :: proc(dirp: Dir) -> Errno {
|
_closedir :: proc(dirp: Dir) -> Error {
|
||||||
rc := _unix_closedir(dirp)
|
rc := _unix_closedir(dirp)
|
||||||
if rc != 0 {
|
if rc != 0 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -330,12 +331,12 @@ _rewinddir :: proc(dirp: Dir) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
|
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Error, end_of_stream: bool) {
|
||||||
result: ^Dirent
|
result: ^Dirent
|
||||||
rc := _unix_readdir_r(dirp, &entry, &result)
|
rc := _unix_readdir_r(dirp, &entry, &result)
|
||||||
|
|
||||||
if rc != 0 {
|
if rc != 0 {
|
||||||
err = Errno(get_last_error())
|
err = Error(get_last_error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -348,7 +349,7 @@ _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool)
|
|||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_readlink :: proc(path: string) -> (string, Errno) {
|
_readlink :: proc(path: string) -> (string, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
|
|
||||||
@@ -358,7 +359,7 @@ _readlink :: proc(path: string) -> (string, Errno) {
|
|||||||
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
|
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
|
||||||
if rc == -1 {
|
if rc == -1 {
|
||||||
delete(buf)
|
delete(buf)
|
||||||
return "", Errno(get_last_error())
|
return "", Error(get_last_error())
|
||||||
} else if rc == int(bufsz) {
|
} else if rc == int(bufsz) {
|
||||||
bufsz += MAX_PATH
|
bufsz += MAX_PATH
|
||||||
delete(buf)
|
delete(buf)
|
||||||
@@ -369,11 +370,11 @@ _readlink :: proc(path: string) -> (string, Errno) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
|
absolute_path_from_handle :: proc(fd: Handle) -> (string, Error) {
|
||||||
return "", Errno(ENOSYS)
|
return "", Error(ENOSYS)
|
||||||
}
|
}
|
||||||
|
|
||||||
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) {
|
||||||
rel := rel
|
rel := rel
|
||||||
if rel == "" {
|
if rel == "" {
|
||||||
rel = "."
|
rel = "."
|
||||||
@@ -384,7 +385,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
|||||||
|
|
||||||
path_ptr := _unix_realpath(rel_cstr, nil)
|
path_ptr := _unix_realpath(rel_cstr, nil)
|
||||||
if path_ptr == nil {
|
if path_ptr == nil {
|
||||||
return "", Errno(get_last_error())
|
return "", Error(get_last_error())
|
||||||
}
|
}
|
||||||
defer _unix_free(path_ptr)
|
defer _unix_free(path_ptr)
|
||||||
|
|
||||||
@@ -394,12 +395,12 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
|||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
access :: proc(path: string, mask: int) -> (bool, Errno) {
|
access :: proc(path: string, mask: int) -> (bool, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_access(cstr, c.int(mask))
|
res := _unix_access(cstr, c.int(mask))
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return false, Errno(get_last_error())
|
return false, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -520,6 +520,7 @@ _get_errno :: proc(res: int) -> Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get errno from libc
|
// get errno from libc
|
||||||
|
@(require_results, no_instrumentation)
|
||||||
get_last_error :: proc "contextless" () -> Error {
|
get_last_error :: proc "contextless" () -> Error {
|
||||||
err := Platform_Error(__errno_location()^)
|
err := Platform_Error(__errno_location()^)
|
||||||
#partial switch err {
|
#partial switch err {
|
||||||
|
|||||||
+56
-53
@@ -150,7 +150,7 @@ _Platform_Error :: enum i32 {
|
|||||||
EOWNERDEAD = 97, /* Previous owner died */
|
EOWNERDEAD = 97, /* Previous owner died */
|
||||||
ENOTRECOVERABLE = 98, /* State not recoverable */
|
ENOTRECOVERABLE = 98, /* State not recoverable */
|
||||||
|
|
||||||
ELAST = 98, /* Must equal largest errno */
|
ELAST = 98, /* Must equal largest Error */
|
||||||
}
|
}
|
||||||
|
|
||||||
EPERM :: Platform_Error.EPERM /* Operation not permitted */
|
EPERM :: Platform_Error.EPERM /* Operation not permitted */
|
||||||
@@ -289,9 +289,9 @@ EPROTO :: Platform_Error.EPROTO /* Protocol error */
|
|||||||
EOWNERDEAD :: Platform_Error.EOWNERDEAD /* Previous owner died */
|
EOWNERDEAD :: Platform_Error.EOWNERDEAD /* Previous owner died */
|
||||||
ENOTRECOVERABLE :: Platform_Error.ENOTRECOVERABLE /* State not recoverable */
|
ENOTRECOVERABLE :: Platform_Error.ENOTRECOVERABLE /* State not recoverable */
|
||||||
|
|
||||||
ELAST :: Platform_Error.ELAST /* Must equal largest errno */
|
ELAST :: Platform_Error.ELAST /* Must equal largest Error */
|
||||||
|
|
||||||
/* end of errno */
|
/* end of Error */
|
||||||
|
|
||||||
O_RDONLY :: 0x000000000
|
O_RDONLY :: 0x000000000
|
||||||
O_WRONLY :: 0x000000001
|
O_WRONLY :: 0x000000001
|
||||||
@@ -421,7 +421,7 @@ W_OK :: 2 // Test for write permission
|
|||||||
R_OK :: 4 // Test for read permission
|
R_OK :: 4 // Test for read permission
|
||||||
|
|
||||||
foreign libc {
|
foreign libc {
|
||||||
@(link_name="__errno") __errno_location :: proc() -> ^c.int ---
|
@(link_name="__Error") __Error_location :: proc() -> ^c.int ---
|
||||||
|
|
||||||
@(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle ---
|
@(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle ---
|
||||||
@(link_name="close") _unix_close :: proc(fd: Handle) -> c.int ---
|
@(link_name="close") _unix_close :: proc(fd: Handle) -> c.int ---
|
||||||
@@ -473,28 +473,31 @@ foreign libc {
|
|||||||
|
|
||||||
// NOTE(phix): Perhaps share the following functions with FreeBSD if they turn out to be the same in the end.
|
// NOTE(phix): Perhaps share the following functions with FreeBSD if they turn out to be the same in the end.
|
||||||
|
|
||||||
|
@(require_results)
|
||||||
is_path_separator :: proc(r: rune) -> bool {
|
is_path_separator :: proc(r: rune) -> bool {
|
||||||
return r == '/'
|
return r == '/'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@(require_results, no_instrumentation)
|
||||||
get_last_error :: proc "contextless" () -> Error {
|
get_last_error :: proc "contextless" () -> Error {
|
||||||
return Platform_Error(__errno_location()^)
|
return Platform_Error(__Error_location()^)
|
||||||
}
|
}
|
||||||
|
|
||||||
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
|
@(require_results)
|
||||||
|
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
handle := _unix_open(cstr, c.int(flags), c.int(mode))
|
handle := _unix_open(cstr, c.int(flags), c.int(mode))
|
||||||
if handle == -1 {
|
if handle == -1 {
|
||||||
return INVALID_HANDLE, Errno(get_last_error())
|
return INVALID_HANDLE, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return handle, nil
|
return handle, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
close :: proc(fd: Handle) -> Errno {
|
close :: proc(fd: Handle) -> Error {
|
||||||
result := _unix_close(fd)
|
result := _unix_close(fd)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -503,16 +506,16 @@ close :: proc(fd: Handle) -> Errno {
|
|||||||
@(private)
|
@(private)
|
||||||
MAX_RW :: 1 << 30
|
MAX_RW :: 1 << 30
|
||||||
|
|
||||||
read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
read :: proc(fd: Handle, data: []byte) -> (int, Error) {
|
||||||
to_read := min(c.size_t(len(data)), MAX_RW)
|
to_read := min(c.size_t(len(data)), MAX_RW)
|
||||||
bytes_read := _unix_read(fd, &data[0], to_read)
|
bytes_read := _unix_read(fd, &data[0], to_read)
|
||||||
if bytes_read == -1 {
|
if bytes_read == -1 {
|
||||||
return -1, Errno(get_last_error())
|
return -1, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return int(bytes_read), nil
|
return int(bytes_read), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
write :: proc(fd: Handle, data: []byte) -> (int, Error) {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
@@ -520,63 +523,63 @@ write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
|||||||
to_write := min(c.size_t(len(data)), MAX_RW)
|
to_write := min(c.size_t(len(data)), MAX_RW)
|
||||||
bytes_written := _unix_write(fd, &data[0], to_write)
|
bytes_written := _unix_write(fd, &data[0], to_write)
|
||||||
if bytes_written == -1 {
|
if bytes_written == -1 {
|
||||||
return -1, Errno(get_last_error())
|
return -1, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return int(bytes_written), nil
|
return int(bytes_written), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
|
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
|
||||||
res := _unix_seek(fd, offset, c.int(whence))
|
res := _unix_seek(fd, offset, c.int(whence))
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return -1, Errno(get_last_error())
|
return -1, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
file_size :: proc(fd: Handle) -> (size: i64, err: Errno) {
|
file_size :: proc(fd: Handle) -> (size: i64, err: Error) {
|
||||||
size = -1
|
size = -1
|
||||||
s := _fstat(fd) or_return
|
s := _fstat(fd) or_return
|
||||||
size = s.size
|
size = s.size
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rename :: proc(old_path, new_path: string) -> Errno {
|
rename :: proc(old_path, new_path: string) -> Error {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
old_path_cstr := strings.clone_to_cstring(old_path, context.temp_allocator)
|
old_path_cstr := strings.clone_to_cstring(old_path, context.temp_allocator)
|
||||||
new_path_cstr := strings.clone_to_cstring(new_path, context.temp_allocator)
|
new_path_cstr := strings.clone_to_cstring(new_path, context.temp_allocator)
|
||||||
res := _unix_rename(old_path_cstr, new_path_cstr)
|
res := _unix_rename(old_path_cstr, new_path_cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
remove :: proc(path: string) -> Errno {
|
remove :: proc(path: string) -> Error {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_unlink(path_cstr)
|
res := _unix_unlink(path_cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
make_directory :: proc(path: string, mode: mode_t = 0o775) -> Errno {
|
make_directory :: proc(path: string, mode: mode_t = 0o775) -> Error {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_mkdir(path_cstr, mode)
|
res := _unix_mkdir(path_cstr, mode)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
remove_directory :: proc(path: string) -> Errno {
|
remove_directory :: proc(path: string) -> Error {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_rmdir(path_cstr)
|
res := _unix_rmdir(path_cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -591,7 +594,7 @@ is_file_handle :: proc(fd: Handle) -> bool {
|
|||||||
|
|
||||||
is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
|
is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
|
||||||
s: OS_Stat
|
s: OS_Stat
|
||||||
err: Errno
|
err: Error
|
||||||
if follow_links {
|
if follow_links {
|
||||||
s, err = _stat(path)
|
s, err = _stat(path)
|
||||||
} else {
|
} else {
|
||||||
@@ -613,7 +616,7 @@ is_dir_handle :: proc(fd: Handle) -> bool {
|
|||||||
|
|
||||||
is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
|
is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
|
||||||
s: OS_Stat
|
s: OS_Stat
|
||||||
err: Errno
|
err: Error
|
||||||
if follow_links {
|
if follow_links {
|
||||||
s, err = _stat(path)
|
s, err = _stat(path)
|
||||||
} else {
|
} else {
|
||||||
@@ -635,10 +638,10 @@ exists :: proc(path: string) -> bool {
|
|||||||
return res == 0
|
return res == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fcntl :: proc(fd: int, cmd: int, arg: int) -> (int, Errno) {
|
fcntl :: proc(fd: int, cmd: int, arg: int) -> (int, Error) {
|
||||||
result := _unix_fcntl(Handle(fd), c.int(cmd), uintptr(arg))
|
result := _unix_fcntl(Handle(fd), c.int(cmd), uintptr(arg))
|
||||||
if result < 0 {
|
if result < 0 {
|
||||||
return 0, Errno(get_last_error())
|
return 0, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return int(result), nil
|
return int(result), nil
|
||||||
}
|
}
|
||||||
@@ -649,32 +652,32 @@ stdin: Handle = 0
|
|||||||
stdout: Handle = 1
|
stdout: Handle = 1
|
||||||
stderr: Handle = 2
|
stderr: Handle = 2
|
||||||
|
|
||||||
last_write_time :: proc(fd: Handle) -> (time: File_Time, err: Errno) {
|
last_write_time :: proc(fd: Handle) -> (time: File_Time, err: Error) {
|
||||||
s := _fstat(fd) or_return
|
s := _fstat(fd) or_return
|
||||||
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
|
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
|
||||||
return File_Time(modified), nil
|
return File_Time(modified), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
last_write_time_by_name :: proc(name: string) -> (time: File_Time, err: Errno) {
|
last_write_time_by_name :: proc(name: string) -> (time: File_Time, err: Error) {
|
||||||
s := _stat(name) or_return
|
s := _stat(name) or_return
|
||||||
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
|
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
|
||||||
return File_Time(modified), nil
|
return File_Time(modified), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_stat :: proc(path: string) -> (OS_Stat, Errno) {
|
_stat :: proc(path: string) -> (OS_Stat, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
s: OS_Stat = ---
|
s: OS_Stat = ---
|
||||||
result := _unix_lstat(cstr, &s)
|
result := _unix_lstat(cstr, &s)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return s, Errno(get_last_error())
|
return s, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
_lstat :: proc(path: string) -> (OS_Stat, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
|
|
||||||
@@ -682,35 +685,35 @@ _lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
|||||||
s: OS_Stat = ---
|
s: OS_Stat = ---
|
||||||
res := _unix_lstat(cstr, &s)
|
res := _unix_lstat(cstr, &s)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return s, Errno(get_last_error())
|
return s, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
|
_fstat :: proc(fd: Handle) -> (OS_Stat, Error) {
|
||||||
s: OS_Stat = ---
|
s: OS_Stat = ---
|
||||||
result := _unix_fstat(fd, &s)
|
result := _unix_fstat(fd, &s)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return s, Errno(get_last_error())
|
return s, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
|
_fdopendir :: proc(fd: Handle) -> (Dir, Error) {
|
||||||
dirp := _unix_fdopendir(fd)
|
dirp := _unix_fdopendir(fd)
|
||||||
if dirp == cast(Dir)nil {
|
if dirp == cast(Dir)nil {
|
||||||
return nil, Errno(get_last_error())
|
return nil, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return dirp, nil
|
return dirp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_closedir :: proc(dirp: Dir) -> Errno {
|
_closedir :: proc(dirp: Dir) -> Error {
|
||||||
rc := _unix_closedir(dirp)
|
rc := _unix_closedir(dirp)
|
||||||
if rc != 0 {
|
if rc != 0 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -721,12 +724,12 @@ _rewinddir :: proc(dirp: Dir) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
|
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Error, end_of_stream: bool) {
|
||||||
result: ^Dirent
|
result: ^Dirent
|
||||||
rc := _unix_readdir_r(dirp, &entry, &result)
|
rc := _unix_readdir_r(dirp, &entry, &result)
|
||||||
|
|
||||||
if rc != 0 {
|
if rc != 0 {
|
||||||
err = Errno(get_last_error())
|
err = Error(get_last_error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = nil
|
err = nil
|
||||||
@@ -740,7 +743,7 @@ _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool)
|
|||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_readlink :: proc(path: string) -> (string, Errno) {
|
_readlink :: proc(path: string) -> (string, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
|
||||||
|
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
@@ -751,7 +754,7 @@ _readlink :: proc(path: string) -> (string, Errno) {
|
|||||||
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
|
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
|
||||||
if rc == -1 {
|
if rc == -1 {
|
||||||
delete(buf)
|
delete(buf)
|
||||||
return "", Errno(get_last_error())
|
return "", Error(get_last_error())
|
||||||
} else if rc == int(bufsz) {
|
} else if rc == int(bufsz) {
|
||||||
bufsz += MAX_PATH
|
bufsz += MAX_PATH
|
||||||
delete(buf)
|
delete(buf)
|
||||||
@@ -761,16 +764,16 @@ _readlink :: proc(path: string) -> (string, Errno) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", Errno{}
|
return "", Error{}
|
||||||
}
|
}
|
||||||
|
|
||||||
absolute_path_from_handle :: proc(fd: Handle) -> (path: string, err: Errno) {
|
absolute_path_from_handle :: proc(fd: Handle) -> (path: string, err: Error) {
|
||||||
buf: [MAX_PATH]byte
|
buf: [MAX_PATH]byte
|
||||||
_ = fcntl(int(fd), F_GETPATH, int(uintptr(&buf[0]))) or_return
|
_ = fcntl(int(fd), F_GETPATH, int(uintptr(&buf[0]))) or_return
|
||||||
return strings.clone_from_cstring(cstring(&buf[0]))
|
return strings.clone_from_cstring(cstring(&buf[0]))
|
||||||
}
|
}
|
||||||
|
|
||||||
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) {
|
||||||
rel := rel
|
rel := rel
|
||||||
if rel == "" {
|
if rel == "" {
|
||||||
rel = "."
|
rel = "."
|
||||||
@@ -781,7 +784,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
|||||||
|
|
||||||
path_ptr := _unix_realpath(rel_cstr, nil)
|
path_ptr := _unix_realpath(rel_cstr, nil)
|
||||||
if path_ptr == nil {
|
if path_ptr == nil {
|
||||||
return "", Errno(get_last_error())
|
return "", Error(get_last_error())
|
||||||
}
|
}
|
||||||
defer _unix_free(path_ptr)
|
defer _unix_free(path_ptr)
|
||||||
|
|
||||||
@@ -790,13 +793,13 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
|||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
access :: proc(path: string, mask: int) -> (bool, Errno) {
|
access :: proc(path: string, mask: int) -> (bool, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
|
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
result := _unix_access(cstr, c.int(mask))
|
result := _unix_access(cstr, c.int(mask))
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return false, Errno(get_last_error())
|
return false, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
@@ -828,7 +831,7 @@ get_current_directory :: proc() -> string {
|
|||||||
if cwd != nil {
|
if cwd != nil {
|
||||||
return string(cwd)
|
return string(cwd)
|
||||||
}
|
}
|
||||||
if Errno(get_last_error()) != ERANGE {
|
if Error(get_last_error()) != ERANGE {
|
||||||
delete(buf)
|
delete(buf)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -837,12 +840,12 @@ get_current_directory :: proc() -> string {
|
|||||||
unreachable()
|
unreachable()
|
||||||
}
|
}
|
||||||
|
|
||||||
set_current_directory :: proc(path: string) -> (err: Errno) {
|
set_current_directory :: proc(path: string) -> (err: Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_chdir(cstr)
|
res := _unix_chdir(cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+52
-48
@@ -388,36 +388,40 @@ foreign libc {
|
|||||||
@(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
|
@(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@(require_results)
|
||||||
is_path_separator :: proc(r: rune) -> bool {
|
is_path_separator :: proc(r: rune) -> bool {
|
||||||
return r == '/'
|
return r == '/'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@(require_results, no_instrumentation)
|
||||||
get_last_error :: proc "contextless" () -> Error {
|
get_last_error :: proc "contextless" () -> Error {
|
||||||
return Platform_Error(__error()^)
|
return Platform_Error(__error()^)
|
||||||
}
|
}
|
||||||
|
|
||||||
fork :: proc() -> (Pid, Errno) {
|
@(require_results)
|
||||||
|
fork :: proc() -> (Pid, Error) {
|
||||||
pid := _unix_fork()
|
pid := _unix_fork()
|
||||||
if pid == -1 {
|
if pid == -1 {
|
||||||
return Pid(-1), Errno(get_last_error())
|
return Pid(-1), Error(get_last_error())
|
||||||
}
|
}
|
||||||
return Pid(pid), nil
|
return Pid(pid), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
|
@(require_results)
|
||||||
|
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
handle := _unix_open(cstr, c.int(flags), c.int(mode))
|
handle := _unix_open(cstr, c.int(flags), c.int(mode))
|
||||||
if handle == -1 {
|
if handle == -1 {
|
||||||
return INVALID_HANDLE, Errno(get_last_error())
|
return INVALID_HANDLE, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return handle, nil
|
return handle, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
close :: proc(fd: Handle) -> Errno {
|
close :: proc(fd: Handle) -> Error {
|
||||||
result := _unix_close(fd)
|
result := _unix_close(fd)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -430,16 +434,16 @@ close :: proc(fd: Handle) -> Errno {
|
|||||||
@(private)
|
@(private)
|
||||||
MAX_RW :: 1 << 30
|
MAX_RW :: 1 << 30
|
||||||
|
|
||||||
read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
read :: proc(fd: Handle, data: []byte) -> (int, Error) {
|
||||||
to_read := min(c.size_t(len(data)), MAX_RW)
|
to_read := min(c.size_t(len(data)), MAX_RW)
|
||||||
bytes_read := _unix_read(fd, &data[0], to_read)
|
bytes_read := _unix_read(fd, &data[0], to_read)
|
||||||
if bytes_read == -1 {
|
if bytes_read == -1 {
|
||||||
return -1, Errno(get_last_error())
|
return -1, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return int(bytes_read), nil
|
return int(bytes_read), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
write :: proc(fd: Handle, data: []byte) -> (int, Error) {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
@@ -447,63 +451,63 @@ write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
|||||||
to_write := min(c.size_t(len(data)), MAX_RW)
|
to_write := min(c.size_t(len(data)), MAX_RW)
|
||||||
bytes_written := _unix_write(fd, &data[0], to_write)
|
bytes_written := _unix_write(fd, &data[0], to_write)
|
||||||
if bytes_written == -1 {
|
if bytes_written == -1 {
|
||||||
return -1, Errno(get_last_error())
|
return -1, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return int(bytes_written), nil
|
return int(bytes_written), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
|
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
|
||||||
res := _unix_seek(fd, offset, c.int(whence))
|
res := _unix_seek(fd, offset, c.int(whence))
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return -1, Errno(get_last_error())
|
return -1, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
file_size :: proc(fd: Handle) -> (size: i64, err: Errno) {
|
file_size :: proc(fd: Handle) -> (size: i64, err: Error) {
|
||||||
size = -1
|
size = -1
|
||||||
s := _fstat(fd) or_return
|
s := _fstat(fd) or_return
|
||||||
size = s.size
|
size = s.size
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rename :: proc(old_path, new_path: string) -> Errno {
|
rename :: proc(old_path, new_path: string) -> Error {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
old_path_cstr := strings.clone_to_cstring(old_path, context.temp_allocator)
|
old_path_cstr := strings.clone_to_cstring(old_path, context.temp_allocator)
|
||||||
new_path_cstr := strings.clone_to_cstring(new_path, context.temp_allocator)
|
new_path_cstr := strings.clone_to_cstring(new_path, context.temp_allocator)
|
||||||
res := _unix_rename(old_path_cstr, new_path_cstr)
|
res := _unix_rename(old_path_cstr, new_path_cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
remove :: proc(path: string) -> Errno {
|
remove :: proc(path: string) -> Error {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_unlink(path_cstr)
|
res := _unix_unlink(path_cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
make_directory :: proc(path: string, mode: mode_t = 0o775) -> Errno {
|
make_directory :: proc(path: string, mode: mode_t = 0o775) -> Error {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_mkdir(path_cstr, mode)
|
res := _unix_mkdir(path_cstr, mode)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
remove_directory :: proc(path: string) -> Errno {
|
remove_directory :: proc(path: string) -> Error {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_rmdir(path_cstr)
|
res := _unix_rmdir(path_cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -518,7 +522,7 @@ is_file_handle :: proc(fd: Handle) -> bool {
|
|||||||
|
|
||||||
is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
|
is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
|
||||||
s: OS_Stat
|
s: OS_Stat
|
||||||
err: Errno
|
err: Error
|
||||||
if follow_links {
|
if follow_links {
|
||||||
s, err = _stat(path)
|
s, err = _stat(path)
|
||||||
} else {
|
} else {
|
||||||
@@ -540,7 +544,7 @@ is_dir_handle :: proc(fd: Handle) -> bool {
|
|||||||
|
|
||||||
is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
|
is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
|
||||||
s: OS_Stat
|
s: OS_Stat
|
||||||
err: Errno
|
err: Error
|
||||||
if follow_links {
|
if follow_links {
|
||||||
s, err = _stat(path)
|
s, err = _stat(path)
|
||||||
} else {
|
} else {
|
||||||
@@ -565,20 +569,20 @@ stderr: Handle = 2
|
|||||||
last_write_time :: proc(fd: Handle) -> File_Time {}
|
last_write_time :: proc(fd: Handle) -> File_Time {}
|
||||||
last_write_time_by_name :: proc(name: string) -> File_Time {}
|
last_write_time_by_name :: proc(name: string) -> File_Time {}
|
||||||
*/
|
*/
|
||||||
last_write_time :: proc(fd: Handle) -> (time: File_Time, err: Errno) {
|
last_write_time :: proc(fd: Handle) -> (time: File_Time, err: Error) {
|
||||||
s := _fstat(fd) or_return
|
s := _fstat(fd) or_return
|
||||||
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
|
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
|
||||||
return File_Time(modified), nil
|
return File_Time(modified), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
last_write_time_by_name :: proc(name: string) -> (time: File_Time, err: Errno) {
|
last_write_time_by_name :: proc(name: string) -> (time: File_Time, err: Error) {
|
||||||
s := _stat(name) or_return
|
s := _stat(name) or_return
|
||||||
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
|
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
|
||||||
return File_Time(modified), nil
|
return File_Time(modified), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_stat :: proc(path: string) -> (OS_Stat, Errno) {
|
_stat :: proc(path: string) -> (OS_Stat, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
|
|
||||||
@@ -586,13 +590,13 @@ _stat :: proc(path: string) -> (OS_Stat, Errno) {
|
|||||||
s: OS_Stat = ---
|
s: OS_Stat = ---
|
||||||
res := _unix_stat(cstr, &s)
|
res := _unix_stat(cstr, &s)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return s, Errno(get_last_error())
|
return s, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
_lstat :: proc(path: string) -> (OS_Stat, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
|
|
||||||
@@ -600,36 +604,36 @@ _lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
|||||||
s: OS_Stat = ---
|
s: OS_Stat = ---
|
||||||
res := _unix_lstat(cstr, &s)
|
res := _unix_lstat(cstr, &s)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return s, Errno(get_last_error())
|
return s, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
|
_fstat :: proc(fd: Handle) -> (OS_Stat, Error) {
|
||||||
// deliberately uninitialized
|
// deliberately uninitialized
|
||||||
s: OS_Stat = ---
|
s: OS_Stat = ---
|
||||||
res := _unix_fstat(fd, &s)
|
res := _unix_fstat(fd, &s)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return s, Errno(get_last_error())
|
return s, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
|
_fdopendir :: proc(fd: Handle) -> (Dir, Error) {
|
||||||
dirp := _unix_fdopendir(fd)
|
dirp := _unix_fdopendir(fd)
|
||||||
if dirp == cast(Dir)nil {
|
if dirp == cast(Dir)nil {
|
||||||
return nil, Errno(get_last_error())
|
return nil, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return dirp, nil
|
return dirp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_closedir :: proc(dirp: Dir) -> Errno {
|
_closedir :: proc(dirp: Dir) -> Error {
|
||||||
rc := _unix_closedir(dirp)
|
rc := _unix_closedir(dirp)
|
||||||
if rc != 0 {
|
if rc != 0 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -640,12 +644,12 @@ _rewinddir :: proc(dirp: Dir) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
|
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Error, end_of_stream: bool) {
|
||||||
result: ^Dirent
|
result: ^Dirent
|
||||||
rc := _unix_readdir_r(dirp, &entry, &result)
|
rc := _unix_readdir_r(dirp, &entry, &result)
|
||||||
|
|
||||||
if rc != 0 {
|
if rc != 0 {
|
||||||
err = Errno(get_last_error())
|
err = Error(get_last_error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = nil
|
err = nil
|
||||||
@@ -659,7 +663,7 @@ _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool)
|
|||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_readlink :: proc(path: string) -> (string, Errno) {
|
_readlink :: proc(path: string) -> (string, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
|
|
||||||
@@ -669,7 +673,7 @@ _readlink :: proc(path: string) -> (string, Errno) {
|
|||||||
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
|
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
|
||||||
if rc == -1 {
|
if rc == -1 {
|
||||||
delete(buf)
|
delete(buf)
|
||||||
return "", Errno(get_last_error())
|
return "", Error(get_last_error())
|
||||||
} else if rc == int(bufsz) {
|
} else if rc == int(bufsz) {
|
||||||
bufsz += MAX_PATH
|
bufsz += MAX_PATH
|
||||||
delete(buf)
|
delete(buf)
|
||||||
@@ -681,11 +685,11 @@ _readlink :: proc(path: string) -> (string, Errno) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// XXX OpenBSD
|
// XXX OpenBSD
|
||||||
absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
|
absolute_path_from_handle :: proc(fd: Handle) -> (string, Error) {
|
||||||
return "", Errno(ENOSYS)
|
return "", Error(ENOSYS)
|
||||||
}
|
}
|
||||||
|
|
||||||
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) {
|
||||||
rel := rel
|
rel := rel
|
||||||
if rel == "" {
|
if rel == "" {
|
||||||
rel = "."
|
rel = "."
|
||||||
@@ -696,7 +700,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
|||||||
|
|
||||||
path_ptr := _unix_realpath(rel_cstr, nil)
|
path_ptr := _unix_realpath(rel_cstr, nil)
|
||||||
if path_ptr == nil {
|
if path_ptr == nil {
|
||||||
return "", Errno(get_last_error())
|
return "", Error(get_last_error())
|
||||||
}
|
}
|
||||||
defer _unix_free(path_ptr)
|
defer _unix_free(path_ptr)
|
||||||
|
|
||||||
@@ -705,12 +709,12 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
|||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
access :: proc(path: string, mask: int) -> (bool, Errno) {
|
access :: proc(path: string, mask: int) -> (bool, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_access(cstr, c.int(mask))
|
res := _unix_access(cstr, c.int(mask))
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return false, Errno(get_last_error())
|
return false, Error(get_last_error())
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
@@ -737,7 +741,7 @@ get_current_directory :: proc() -> string {
|
|||||||
if cwd != nil {
|
if cwd != nil {
|
||||||
return string(cwd)
|
return string(cwd)
|
||||||
}
|
}
|
||||||
if Errno(get_last_error()) != ERANGE {
|
if Error(get_last_error()) != ERANGE {
|
||||||
delete(buf)
|
delete(buf)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -746,12 +750,12 @@ get_current_directory :: proc() -> string {
|
|||||||
unreachable()
|
unreachable()
|
||||||
}
|
}
|
||||||
|
|
||||||
set_current_directory :: proc(path: string) -> (err: Errno) {
|
set_current_directory :: proc(path: string) -> (err: Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_chdir(cstr)
|
res := _unix_chdir(cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error())
|
return Error(get_last_error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ ERROR_NEGATIVE_OFFSET :: _Platform_Error(1<<29 + 2)
|
|||||||
// "Argv" arguments converted to Odin strings
|
// "Argv" arguments converted to Odin strings
|
||||||
args := _alloc_command_line_arguments()
|
args := _alloc_command_line_arguments()
|
||||||
|
|
||||||
|
@(require_results, no_instrumentation)
|
||||||
get_last_error :: proc "contextless" () -> Error {
|
get_last_error :: proc "contextless" () -> Error {
|
||||||
err := win32.GetLastError()
|
err := win32.GetLastError()
|
||||||
if err == 0 {
|
if err == 0 {
|
||||||
@@ -114,6 +115,7 @@ get_last_error :: proc "contextless" () -> Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@(require_results)
|
||||||
last_write_time :: proc(fd: Handle) -> (File_Time, Error) {
|
last_write_time :: proc(fd: Handle) -> (File_Time, Error) {
|
||||||
file_info: win32.BY_HANDLE_FILE_INFORMATION
|
file_info: win32.BY_HANDLE_FILE_INFORMATION
|
||||||
if !win32.GetFileInformationByHandle(win32.HANDLE(fd), &file_info) {
|
if !win32.GetFileInformationByHandle(win32.HANDLE(fd), &file_info) {
|
||||||
@@ -124,6 +126,7 @@ last_write_time :: proc(fd: Handle) -> (File_Time, Error) {
|
|||||||
return lo | hi << 32, nil
|
return lo | hi << 32, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@(require_results)
|
||||||
last_write_time_by_name :: proc(name: string) -> (File_Time, Error) {
|
last_write_time_by_name :: proc(name: string) -> (File_Time, Error) {
|
||||||
data: win32.WIN32_FILE_ATTRIBUTE_DATA
|
data: win32.WIN32_FILE_ATTRIBUTE_DATA
|
||||||
|
|
||||||
@@ -138,6 +141,7 @@ last_write_time_by_name :: proc(name: string) -> (File_Time, Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@(require_results)
|
||||||
get_page_size :: proc() -> int {
|
get_page_size :: proc() -> int {
|
||||||
// NOTE(tetra): The page size never changes, so why do anything complicated
|
// NOTE(tetra): The page size never changes, so why do anything complicated
|
||||||
// if we don't have to.
|
// if we don't have to.
|
||||||
|
|||||||
@@ -63,17 +63,17 @@ temp_full_path :: proc(name: string) -> (path: string, err: os.Error) {
|
|||||||
p := win32.utf8_to_utf16(name, ta)
|
p := win32.utf8_to_utf16(name, ta)
|
||||||
n := win32.GetFullPathNameW(raw_data(p), 0, nil, nil)
|
n := win32.GetFullPathNameW(raw_data(p), 0, nil, nil)
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return "", os.Platform_Error(win32.GetLastError())
|
return "", os.get_last_error()
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]u16, n, ta)
|
buf := make([]u16, n, ta)
|
||||||
n = win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil)
|
n = win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil)
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
delete(buf)
|
delete(buf)
|
||||||
return "", os.Platform_Error(win32.GetLastError())
|
return "", os.get_last_error()
|
||||||
}
|
}
|
||||||
|
|
||||||
return win32.utf16_to_utf8(buf[:n], ta) or_else "", os.ERROR_NONE
|
return win32.utf16_to_utf8(buf[:n], ta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ BUFFER_DEFAULT_SIZE :: 0x10_0000
|
|||||||
|
|
||||||
context_create_with_scale :: proc(filename: string, precise_time: bool, timestamp_scale: f64) -> (ctx: Context, ok: bool) #optional_ok {
|
context_create_with_scale :: proc(filename: string, precise_time: bool, timestamp_scale: f64) -> (ctx: Context, ok: bool) #optional_ok {
|
||||||
fd, err := os.open(filename, os.O_WRONLY | os.O_APPEND | os.O_CREATE | os.O_TRUNC, 0o600)
|
fd, err := os.open(filename, os.O_WRONLY | os.O_APPEND | os.O_CREATE | os.O_TRUNC, 0o600)
|
||||||
if err != os.ERROR_NONE {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,19 +12,19 @@ MAX_RW :: 0x7fffffff
|
|||||||
@(no_instrumentation)
|
@(no_instrumentation)
|
||||||
_write :: proc "contextless" (fd: os.Handle, data: []byte) -> (n: int, err: os.Error) #no_bounds_check /* bounds check would segfault instrumentation */ {
|
_write :: proc "contextless" (fd: os.Handle, data: []byte) -> (n: int, err: os.Error) #no_bounds_check /* bounds check would segfault instrumentation */ {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return 0, os.ERROR_NONE
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for n < len(data) {
|
for n < len(data) {
|
||||||
chunk := data[:min(len(data), MAX_RW)]
|
chunk := data[:min(len(data), MAX_RW)]
|
||||||
written, errno := linux.write(linux.Fd(fd), chunk)
|
written, errno := linux.write(linux.Fd(fd), chunk)
|
||||||
if errno != .NONE {
|
if errno != nil {
|
||||||
return n, os.Error(errno)
|
return n, os.Platform_Error(errno)
|
||||||
}
|
}
|
||||||
n += written
|
n += written
|
||||||
}
|
}
|
||||||
|
|
||||||
return n, os.ERROR_NONE
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
CLOCK_MONOTONIC_RAW :: 4 // NOTE(tetra): "RAW" means: Not adjusted by NTP.
|
CLOCK_MONOTONIC_RAW :: 4 // NOTE(tetra): "RAW" means: Not adjusted by NTP.
|
||||||
|
|||||||
@@ -22,29 +22,24 @@ foreign libc {
|
|||||||
@(link_name="clock_gettime") _unix_clock_gettime :: proc(clock_id: u64, timespec: ^timespec) -> i32 ---
|
@(link_name="clock_gettime") _unix_clock_gettime :: proc(clock_id: u64, timespec: ^timespec) -> i32 ---
|
||||||
}
|
}
|
||||||
|
|
||||||
@(no_instrumentation)
|
|
||||||
get_last_error :: proc "contextless" () -> os.Platform_Error {
|
|
||||||
return os.Platform_Error(__error()^)
|
|
||||||
}
|
|
||||||
|
|
||||||
MAX_RW :: 0x7fffffff
|
MAX_RW :: 0x7fffffff
|
||||||
|
|
||||||
@(no_instrumentation)
|
@(no_instrumentation)
|
||||||
_write :: proc "contextless" (fd: os.Handle, data: []byte) -> (n: int, err: os.Error) #no_bounds_check /* bounds check would segfault instrumentation */ {
|
_write :: proc "contextless" (fd: os.Handle, data: []byte) -> (n: int, err: os.Error) #no_bounds_check /* bounds check would segfault instrumentation */ {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return 0, os.ERROR_NONE
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for n < len(data) {
|
for n < len(data) {
|
||||||
chunk := data[:min(len(data), MAX_RW)]
|
chunk := data[:min(len(data), MAX_RW)]
|
||||||
written := _unix_write(fd, raw_data(chunk), len(chunk))
|
written := _unix_write(fd, raw_data(chunk), len(chunk))
|
||||||
if written < 0 {
|
if written < 0 {
|
||||||
return n, os.Error(get_last_error())
|
return n, os.get_last_error()
|
||||||
}
|
}
|
||||||
n += written
|
n += written
|
||||||
}
|
}
|
||||||
|
|
||||||
return n, os.ERROR_NONE
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
CLOCK_MONOTONIC_RAW :: 4 // NOTE(tetra): "RAW" means: Not adjusted by NTP.
|
CLOCK_MONOTONIC_RAW :: 4 // NOTE(tetra): "RAW" means: Not adjusted by NTP.
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ MAX_RW :: 1<<30
|
|||||||
@(no_instrumentation)
|
@(no_instrumentation)
|
||||||
_write :: proc "contextless" (fd: os.Handle, data: []byte) -> (int, os.Error) #no_bounds_check /* bounds check would segfault instrumentation */ {
|
_write :: proc "contextless" (fd: os.Handle, data: []byte) -> (int, os.Error) #no_bounds_check /* bounds check would segfault instrumentation */ {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return 0, os.ERROR_NONE
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
single_write_length: win32.DWORD
|
single_write_length: win32.DWORD
|
||||||
@@ -25,12 +25,11 @@ _write :: proc "contextless" (fd: os.Handle, data: []byte) -> (int, os.Error) #n
|
|||||||
|
|
||||||
e := win32.WriteFile(win32.HANDLE(fd), &data[total_write], to_write, &single_write_length, nil)
|
e := win32.WriteFile(win32.HANDLE(fd), &data[total_write], to_write, &single_write_length, nil)
|
||||||
if single_write_length <= 0 || !e {
|
if single_write_length <= 0 || !e {
|
||||||
err := os.Platform_Error(win32.GetLastError())
|
return int(total_write), os.get_last_error()
|
||||||
return int(total_write), err
|
|
||||||
}
|
}
|
||||||
total_write += i64(single_write_length)
|
total_write += i64(single_write_length)
|
||||||
}
|
}
|
||||||
return int(total_write), os.ERROR_NONE
|
return int(total_write), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@(no_instrumentation)
|
@(no_instrumentation)
|
||||||
|
|||||||
@@ -868,8 +868,8 @@ To partly mitigate this, redirect STDERR to a file or use the -define:ODIN_TEST_
|
|||||||
when ODIN_OS != .Windows {
|
when ODIN_OS != .Windows {
|
||||||
mode = os.S_IRUSR|os.S_IWUSR|os.S_IRGRP|os.S_IROTH
|
mode = os.S_IRUSR|os.S_IWUSR|os.S_IRGRP|os.S_IROTH
|
||||||
}
|
}
|
||||||
json_fd, errno := os.open(JSON_REPORT, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
|
json_fd, err := os.open(JSON_REPORT, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
|
||||||
fmt.assertf(errno == os.ERROR_NONE, "unable to open file %q for writing of JSON report, error: %v", JSON_REPORT, errno)
|
fmt.assertf(err == nil, "unable to open file %q for writing of JSON report, error: %v", JSON_REPORT, err)
|
||||||
defer os.close(json_fd)
|
defer os.close(json_fd)
|
||||||
|
|
||||||
for test, i in report.all_tests {
|
for test, i in report.all_tests {
|
||||||
|
|||||||
@@ -1117,7 +1117,7 @@ test_os_handle :: proc(t: ^testing.T) {
|
|||||||
// Delete the file now that we're done.
|
// Delete the file now that we're done.
|
||||||
//
|
//
|
||||||
// This is not done all the time, just in case the file is useful to debugging.
|
// This is not done all the time, just in case the file is useful to debugging.
|
||||||
testing.expect_value(t, os.remove(TEMPORARY_FILENAME), os.ERROR_NONE)
|
testing.expect_value(t, os.remove(TEMPORARY_FILENAME), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
TEMPORARY_FILENAME :: "test_core_flags_write_test_output_data"
|
TEMPORARY_FILENAME :: "test_core_flags_write_test_output_data"
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ import "core:testing"
|
|||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
read_dir :: proc(t: ^testing.T) {
|
read_dir :: proc(t: ^testing.T) {
|
||||||
fd, errno := os.open(#directory + "/dir")
|
fd, err := os.open(#directory + "/dir")
|
||||||
testing.expect_value(t, errno, os.ERROR_NONE)
|
testing.expect_value(t, err, nil)
|
||||||
defer os.close(fd)
|
defer os.close(fd)
|
||||||
|
|
||||||
dir, errno2 := os.read_dir(fd, -1)
|
dir, err2 := os.read_dir(fd, -1)
|
||||||
testing.expect_value(t, errno2, os.ERROR_NONE)
|
testing.expect_value(t, err2, nil)
|
||||||
defer os.file_info_slice_delete(dir)
|
defer os.file_info_slice_delete(dir)
|
||||||
|
|
||||||
slice.sort_by_key(dir, proc(fi: os.File_Info) -> string { return fi.name })
|
slice.sort_by_key(dir, proc(fi: os.File_Info) -> string { return fi.name })
|
||||||
|
|||||||
@@ -439,7 +439,7 @@ main :: proc() {
|
|||||||
}
|
}
|
||||||
save_path := fmt.tprintf("verify/test_%v_%v.odin", test.package_name, code_test_name)
|
save_path := fmt.tprintf("verify/test_%v_%v.odin", test.package_name, code_test_name)
|
||||||
|
|
||||||
test_file_handle, err := os.open(save_path, os.O_WRONLY | os.O_CREATE); if err != 0 {
|
test_file_handle, err := os.open(save_path, os.O_WRONLY | os.O_CREATE); if err != nil {
|
||||||
fmt.eprintf("We could not open the file to the path %q for writing\n", save_path)
|
fmt.eprintf("We could not open the file to the path %q for writing\n", save_path)
|
||||||
g_bad_doc = true
|
g_bad_doc = true
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user