Skip to content
Merged
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
3 changes: 2 additions & 1 deletion base/intrinsics/intrinsics.odin
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ type_polymorphic_record_parameter_value :: proc($T: typeid, index: int) -> $V --
type_is_specialized_polymorphic_record :: proc($T: typeid) -> bool ---
type_is_unspecialized_polymorphic_record :: proc($T: typeid) -> bool ---

type_is_subtype_of :: proc($T, $U: typeid) -> bool ---
type_is_subtype_of :: proc($T, $U: typeid) -> bool ---
type_is_superset_of :: proc($Super, $Sub: typeid) -> bool ---

type_field_index_of :: proc($T: typeid, $name: string) -> uintptr ---

Expand Down
61 changes: 51 additions & 10 deletions core/os/os2/file.odin
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import "base:runtime"
*/
File :: struct {
impl: rawptr,
stream: io.Stream,
fstat: Fstat_Callback,
stream: File_Stream,
}

/*
Expand Down Expand Up @@ -218,7 +217,11 @@ name :: proc(f: ^File) -> string {
*/
close :: proc(f: ^File) -> Error {
if f != nil {
return io.close(f.stream)
if f.stream.procedure == nil {
return .Unsupported
}
_, err := f.stream.procedure(f, .Close, nil, 0, nil, runtime.nil_allocator())
return err
}
return nil
}
Expand All @@ -235,7 +238,10 @@ close :: proc(f: ^File) -> Error {
*/
seek :: proc(f: ^File, offset: i64, whence: io.Seek_From) -> (ret: i64, err: Error) {
if f != nil {
return io.seek(f.stream, offset, whence)
if f.stream.procedure == nil {
return 0, .Unsupported
}
return f.stream.procedure(f, .Seek, nil, offset, whence, runtime.nil_allocator())
}
return 0, .Invalid_File
}
Expand All @@ -247,7 +253,12 @@ seek :: proc(f: ^File, offset: i64, whence: io.Seek_From) -> (ret: i64, err: Err
*/
read :: proc(f: ^File, p: []byte) -> (n: int, err: Error) {
if f != nil {
return io.read(f.stream, p)
if f.stream.procedure == nil {
return 0, .Unsupported
}
n64: i64
n64, err = f.stream.procedure(f, .Read, p, 0, nil, runtime.nil_allocator())
return int(n64), err
}
return 0, .Invalid_File
}
Expand All @@ -260,7 +271,12 @@ read :: proc(f: ^File, p: []byte) -> (n: int, err: Error) {
*/
read_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: int, err: Error) {
if f != nil {
return io.read_at(f.stream, p, offset)
if f.stream.procedure == nil {
return 0, .Unsupported
}
n64: i64
n64, err = f.stream.procedure(f, .Read_At, p, offset, nil, runtime.nil_allocator())
return int(n64), err
}
return 0, .Invalid_File
}
Expand All @@ -272,7 +288,12 @@ read_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: int, err: Error) {
*/
write :: proc(f: ^File, p: []byte) -> (n: int, err: Error) {
if f != nil {
return io.write(f.stream, p)
if f.stream.procedure == nil {
return 0, .Unsupported
}
n64: i64
n64, err = f.stream.procedure(f, .Write, p, 0, nil, runtime.nil_allocator())
return int(n64), err
}
return 0, .Invalid_File
}
Expand All @@ -284,7 +305,12 @@ write :: proc(f: ^File, p: []byte) -> (n: int, err: Error) {
*/
write_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: int, err: Error) {
if f != nil {
return io.write_at(f.stream, p, offset)
if f.stream.procedure == nil {
return 0, .Unsupported
}
n64: i64
n64, err = f.stream.procedure(f, .Write_At, p, offset, nil, runtime.nil_allocator())
return int(n64), err
}
return 0, .Invalid_File
}
Expand All @@ -294,7 +320,18 @@ write_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: int, err: Error) {
*/
file_size :: proc(f: ^File) -> (n: i64, err: Error) {
if f != nil {
return io.size(f.stream)
if f.stream.procedure == nil {
return 0, .Unsupported
}
n, err = f.stream.procedure(f, .Size, nil, 0, nil, runtime.nil_allocator())
if err == .Unsupported {
n = 0
curr := seek(f, 0, .Current) or_return
end := seek(f, 0, .End) or_return
seek(f, curr, .Start) or_return
n = end
}
return
}
return 0, .Invalid_File
}
Expand All @@ -304,7 +341,11 @@ file_size :: proc(f: ^File) -> (n: i64, err: Error) {
*/
flush :: proc(f: ^File) -> Error {
if f != nil {
return io.flush(f.stream)
if f.stream.procedure == nil {
return .Unsupported
}
_, err := f.stream.procedure(f, .Flush, nil, 0, nil, runtime.nil_allocator())
return err
}
return nil
}
Expand Down
65 changes: 24 additions & 41 deletions core/os/os2/file_linux.odin
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,16 @@ _stdin := File{
stream = {
procedure = _file_stream_proc,
},
fstat = _fstat,
}
_stdout := File{
stream = {
procedure = _file_stream_proc,
},
fstat = _fstat,
}
_stderr := File{
stream = {
procedure = _file_stream_proc,
},
fstat = _fstat,
}

@init
Expand All @@ -55,7 +52,6 @@ _standard_stream_init :: proc "contextless" () {
data = impl,
procedure = _file_stream_proc,
}
impl.file.fstat = _fstat
return &impl.file
}

Expand Down Expand Up @@ -109,7 +105,6 @@ _new_file :: proc(fd: uintptr, _: string, allocator: runtime.Allocator) -> (f: ^
data = impl,
procedure = _file_stream_proc,
}
impl.file.fstat = _fstat
return &impl.file, nil
}

Expand Down Expand Up @@ -476,88 +471,76 @@ _read_entire_pseudo_file_cstring :: proc(name: cstring, allocator: runtime.Alloc
}

@(private="package")
_file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
_file_stream_proc :: proc(stream_data: rawptr, mode: File_Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From, allocator: runtime.Allocator) -> (n: i64, err: Error) {
f := (^File_Impl)(stream_data)
ferr: Error
switch mode {
case .Read:
n, ferr = _read(f, p)
err = error_to_io_error(ferr)
n, err = _read(f, p)
return
case .Read_At:
n, ferr = _read_at(f, p, offset)
err = error_to_io_error(ferr)
n, err = _read_at(f, p, offset)
return
case .Write:
n, ferr = _write(f, p)
err = error_to_io_error(ferr)
n, err = _write(f, p)
return
case .Write_At:
n, ferr = _write_at(f, p, offset)
err = error_to_io_error(ferr)
n, err = _write_at(f, p, offset)
return
case .Seek:
n, ferr = _seek(f, offset, whence)
err = error_to_io_error(ferr)
n, err = _seek(f, offset, whence)
return
case .Size:
n, ferr = _file_size(f)
err = error_to_io_error(ferr)
n, err = _file_size(f)
return
case .Flush:
ferr = _flush(f)
err = error_to_io_error(ferr)
err = _flush(f)
return
case .Close, .Destroy:
ferr = _close(f)
err = error_to_io_error(ferr)
err = _close(f)
return
case .Query:
return io.query_utility({.Read, .Read_At, .Write, .Write_At, .Seek, .Size, .Flush, .Close, .Destroy, .Query})
case .Fstat:
err = file_stream_fstat_utility(f, p, allocator)
return
}
return 0, .Unsupported
}


@(private="package")
_file_stream_buffered_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
_file_stream_buffered_proc :: proc(stream_data: rawptr, mode: File_Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From, allocator: runtime.Allocator) -> (n: i64, err: Error) {
f := (^File_Impl)(stream_data)
ferr: Error
switch mode {
case .Read:
n, ferr = _read(f, p)
err = error_to_io_error(ferr)
n, err = _read(f, p)
return
case .Read_At:
n, ferr = _read_at(f, p, offset)
err = error_to_io_error(ferr)
n, err = _read_at(f, p, offset)
return
case .Write:
n, ferr = _write(f, p)
err = error_to_io_error(ferr)
n, err = _write(f, p)
return
case .Write_At:
n, ferr = _write_at(f, p, offset)
err = error_to_io_error(ferr)
n, err = _write_at(f, p, offset)
return
case .Seek:
n, ferr = _seek(f, offset, whence)
err = error_to_io_error(ferr)
n, err = _seek(f, offset, whence)
return
case .Size:
n, ferr = _file_size(f)
err = error_to_io_error(ferr)
n, err = _file_size(f)
return
case .Flush:
ferr = _flush(f)
err = error_to_io_error(ferr)
err = _flush(f)
return
case .Close, .Destroy:
ferr = _close(f)
err = error_to_io_error(ferr)
err = _close(f)
return
case .Query:
return io.query_utility({.Read, .Read_At, .Write, .Write_At, .Seek, .Size, .Flush, .Close, .Destroy, .Query})
case .Fstat:
err = file_stream_fstat_utility(f, p, allocator)
return
}
return 0, .Unsupported
}
Expand Down
13 changes: 6 additions & 7 deletions core/os/os2/file_posix.odin
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ init_std_files :: proc "contextless" () {
data = impl,
procedure = _file_stream_proc,
}
impl.file.fstat = _fstat
return &impl.file
}

Expand Down Expand Up @@ -110,7 +109,6 @@ __new_file :: proc(handle: posix.FD, allocator: runtime.Allocator) -> ^File {
data = impl,
procedure = _file_stream_proc,
}
impl.file.fstat = _fstat
return &impl.file
}

Expand Down Expand Up @@ -371,7 +369,7 @@ _exists :: proc(path: string) -> bool {
return posix.access(cpath) == .OK
}

_file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
_file_stream_proc :: proc(stream_data: rawptr, mode: File_Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From, allocator: runtime.Allocator) -> (n: i64, err: Error) {
f := (^File_Impl)(stream_data)
fd := f.fd

Expand Down Expand Up @@ -489,18 +487,19 @@ _file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte,
return

case .Flush:
ferr := _sync(&f.file)
err = error_to_io_error(ferr)
err = _sync(&f.file)
return

case .Close, .Destroy:
ferr := _close(f)
err = error_to_io_error(ferr)
err = _close(f)
return

case .Query:
return io.query_utility({.Read, .Read_At, .Write, .Write_At, .Seek, .Size, .Flush, .Close, .Destroy, .Query})

case .Fstat:
err = file_stream_fstat_utility(f, p, allocator)
return
case:
return 0, .Unsupported
}
Expand Down
Loading
Loading