mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Merge pull request #3318 from flysand7/os2-file-pipe-read
[core/os2]: Fix read_entire_file for reads from pipe and console.
This commit is contained in:
@@ -13,6 +13,12 @@ General_Error :: enum u32 {
|
|||||||
|
|
||||||
Timeout,
|
Timeout,
|
||||||
|
|
||||||
|
Broken_Pipe,
|
||||||
|
|
||||||
|
// Indicates that an attempt to retrieve a file's size was made, but the
|
||||||
|
// file doesn't have a size.
|
||||||
|
No_Size,
|
||||||
|
|
||||||
Invalid_File,
|
Invalid_File,
|
||||||
Invalid_Dir,
|
Invalid_Dir,
|
||||||
Invalid_Path,
|
Invalid_Path,
|
||||||
@@ -51,6 +57,8 @@ error_string :: proc(ferr: Error) -> string {
|
|||||||
case .Not_Exist: return "file does not exist"
|
case .Not_Exist: return "file does not exist"
|
||||||
case .Closed: return "file already closed"
|
case .Closed: return "file already closed"
|
||||||
case .Timeout: return "i/o timeout"
|
case .Timeout: return "i/o timeout"
|
||||||
|
case .Broken_Pipe: return "Broken pipe"
|
||||||
|
case .No_Size: return "file has no definite size"
|
||||||
case .Invalid_File: return "invalid file"
|
case .Invalid_File: return "invalid file"
|
||||||
case .Invalid_Dir: return "invalid directory"
|
case .Invalid_Dir: return "invalid directory"
|
||||||
case .Invalid_Path: return "invalid path"
|
case .Invalid_Path: return "invalid path"
|
||||||
|
|||||||
+35
-11
@@ -87,26 +87,50 @@ read_entire_file_from_path :: proc(name: string, allocator: runtime.Allocator) -
|
|||||||
|
|
||||||
read_entire_file_from_file :: proc(f: ^File, allocator: runtime.Allocator) -> (data: []byte, err: Error) {
|
read_entire_file_from_file :: proc(f: ^File, allocator: runtime.Allocator) -> (data: []byte, err: Error) {
|
||||||
size: int
|
size: int
|
||||||
|
has_size := true
|
||||||
if size64, err := file_size(f); err == nil {
|
if size64, err := file_size(f); err == nil {
|
||||||
if i64(int(size64)) != size64 {
|
if i64(int(size64)) != size64 {
|
||||||
size = int(size64)
|
size = int(size64)
|
||||||
}
|
}
|
||||||
|
} else if err == .No_Size {
|
||||||
|
has_size = false
|
||||||
|
} else {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
size += 1 // for EOF
|
size += 1 // for EOF
|
||||||
|
|
||||||
// TODO(bill): Is this correct logic?
|
// TODO(bill): Is this correct logic?
|
||||||
total: int
|
if has_size {
|
||||||
data = make([]byte, size, allocator) or_return
|
total: int
|
||||||
for {
|
data = make([]byte, size, allocator) or_return
|
||||||
n: int
|
for {
|
||||||
n, err = read(f, data[total:])
|
n: int
|
||||||
total += n
|
n, err = read(f, data[total:])
|
||||||
if err != nil {
|
total += n
|
||||||
if err == .EOF {
|
if err != nil {
|
||||||
err = nil
|
if err == .EOF {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
data = data[:total]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
buffer: [1024]u8
|
||||||
|
out_buffer := make([dynamic]u8, 0, 0, allocator)
|
||||||
|
total := 0
|
||||||
|
for {
|
||||||
|
n: int = ---
|
||||||
|
n, err = read(f, buffer[:])
|
||||||
|
total += n
|
||||||
|
append_elems(&out_buffer, ..buffer[:total])
|
||||||
|
if err != nil {
|
||||||
|
if err == .EOF || err == .Broken_Pipe {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
data = out_buffer[:total]
|
||||||
|
return
|
||||||
}
|
}
|
||||||
data = data[:total]
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -434,6 +434,9 @@ _write_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: i64, err: Error) {
|
|||||||
|
|
||||||
_file_size :: proc(f: ^File) -> (n: i64, err: Error) {
|
_file_size :: proc(f: ^File) -> (n: i64, err: Error) {
|
||||||
length: win32.LARGE_INTEGER
|
length: win32.LARGE_INTEGER
|
||||||
|
if f.impl.kind == .Pipe {
|
||||||
|
return 0, .No_Size
|
||||||
|
}
|
||||||
handle := _handle(f)
|
handle := _handle(f)
|
||||||
if !win32.GetFileSizeEx(handle, &length) {
|
if !win32.GetFileSizeEx(handle, &length) {
|
||||||
err = _get_platform_error()
|
err = _get_platform_error()
|
||||||
@@ -766,7 +769,6 @@ _is_dir :: proc(path: string) -> bool {
|
|||||||
_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: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
|
||||||
f := (^File)(stream_data)
|
f := (^File)(stream_data)
|
||||||
ferr: Error
|
ferr: Error
|
||||||
i: int
|
|
||||||
switch mode {
|
switch mode {
|
||||||
case .Read:
|
case .Read:
|
||||||
n, ferr = _read(f, p)
|
n, ferr = _read(f, p)
|
||||||
|
|||||||
Reference in New Issue
Block a user