mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-07 16:18:52 +00:00
Fix os.read / os.read_entire_file
- DWORDs are NOT i32 - os.read didn't correctly read as much as it could
This commit is contained in:
+8
-8
@@ -85,13 +85,13 @@ read_entire_file :: proc(name: string) -> (data: []byte, success: bool) {
|
||||
if data == nil {
|
||||
return nil, false;
|
||||
}
|
||||
defer if !success do delete(data);
|
||||
|
||||
bytes_read, read_err := read(fd, data);
|
||||
if read_err != 0 {
|
||||
delete(data);
|
||||
if read_err != ERROR_NONE {
|
||||
return nil, false;
|
||||
}
|
||||
return data[0:bytes_read], true;
|
||||
return data[:bytes_read], true;
|
||||
}
|
||||
|
||||
write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) {
|
||||
@@ -100,11 +100,11 @@ write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (succ
|
||||
flags |= O_TRUNC;
|
||||
}
|
||||
|
||||
mode: int = 0;
|
||||
when OS == "linux" {
|
||||
// NOTE(justasd): 644 (owner read, write; group read; others read)
|
||||
mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
|
||||
}
|
||||
mode: int = 0;
|
||||
when OS == "linux" {
|
||||
// NOTE(justasd): 644 (owner read, write; group read; others read)
|
||||
mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
|
||||
}
|
||||
|
||||
fd, err := open(name, flags, mode);
|
||||
if err != 0 {
|
||||
|
||||
+14
-16
@@ -123,14 +123,14 @@ close :: proc(fd: Handle) -> Errno {
|
||||
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
||||
if len(data) == 0 do return 0, ERROR_NONE;
|
||||
|
||||
single_write_length: i32;
|
||||
single_write_length: u32;
|
||||
total_write: i64;
|
||||
length := i64(len(data));
|
||||
|
||||
for total_write < length {
|
||||
remaining := length - total_write;
|
||||
MAX :: 1<<31-1;
|
||||
to_write: i32 = min(i32(remaining), MAX);
|
||||
to_write: u32 = min(u32(remaining), MAX);
|
||||
|
||||
e := win32.write_file(win32.Handle(fd), &data[total_write], to_write, &single_write_length, nil);
|
||||
if single_write_length <= 0 || !e {
|
||||
@@ -145,23 +145,21 @@ write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
||||
read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
||||
if len(data) == 0 do return 0, ERROR_NONE;
|
||||
|
||||
single_read_length: i32;
|
||||
total_read: i64;
|
||||
length := i64(len(data));
|
||||
read := 0;
|
||||
for {
|
||||
to_read := u32(min(1<<29-1, len(data)-read));
|
||||
if to_read <= 0 do break;
|
||||
|
||||
for total_read < length {
|
||||
remaining := length - total_read;
|
||||
MAX :: 1<<32-1;
|
||||
to_read: u32 = min(u32(remaining), MAX);
|
||||
|
||||
e := win32.read_file(win32.Handle(fd), &data[total_read], to_read, &single_read_length, nil);
|
||||
if single_read_length <= 0 || !e {
|
||||
err := Errno(win32.get_last_error());
|
||||
return int(total_read), err;
|
||||
n: u32;
|
||||
ok := win32.read_file(win32.Handle(fd), &data[to_read], to_read, &n, nil);
|
||||
if !ok {
|
||||
return int(read), Errno(win32.get_last_error());
|
||||
}
|
||||
total_read += i64(single_read_length);
|
||||
|
||||
read += int(n);
|
||||
}
|
||||
return int(total_read), ERROR_NONE;
|
||||
|
||||
return int(read), ERROR_NONE;
|
||||
}
|
||||
|
||||
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
|
||||
|
||||
Reference in New Issue
Block a user