Browse Source

Fix os.read / os.read_entire_file

- DWORDs are NOT i32
- os.read didn't correctly read as much as it could
Tetralux 5 years ago
parent
commit
1181d7cf90
3 changed files with 24 additions and 26 deletions
  1. 8 8
      core/os/os.odin
  2. 14 16
      core/os/os_windows.odin
  3. 2 2
      core/sys/win32/kernel32.odin

+ 8 - 8
core/os/os.odin

@@ -85,13 +85,13 @@ read_entire_file :: proc(name: string) -> (data: []byte, success: bool) {
 	if data == nil {
 	if data == nil {
 		return nil, false;
 		return nil, false;
 	}
 	}
+	defer if !success do delete(data);
 
 
 	bytes_read, read_err := read(fd, data);
 	bytes_read, read_err := read(fd, data);
-	if read_err != 0 {
-		delete(data);
+	if read_err != ERROR_NONE {
 		return nil, false;
 		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) {
 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;
 		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);
 	fd, err := open(name, flags, mode);
 	if err != 0 {
 	if err != 0 {

+ 14 - 16
core/os/os_windows.odin

@@ -123,14 +123,14 @@ close :: proc(fd: Handle) -> Errno {
 write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
 write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
 	if len(data) == 0 do return 0, ERROR_NONE;
 	if len(data) == 0 do return 0, ERROR_NONE;
 
 
-	single_write_length: i32;
+	single_write_length: u32;
 	total_write: i64;
 	total_write: i64;
 	length := i64(len(data));
 	length := i64(len(data));
 
 
 	for total_write < length {
 	for total_write < length {
 		remaining := length - total_write;
 		remaining := length - total_write;
 		MAX :: 1<<31-1;
 		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);
 		e := win32.write_file(win32.Handle(fd), &data[total_write], to_write, &single_write_length, nil);
 		if single_write_length <= 0 || !e {
 		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) {
 read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
 	if len(data) == 0 do return 0, ERROR_NONE;
 	if len(data) == 0 do return 0, ERROR_NONE;
 
 
-	single_read_length: i32;
-	total_read: i64;
-	length := i64(len(data));
-
-	for total_read < length {
-		remaining := length - total_read;
-		MAX :: 1<<32-1;
-		to_read: u32 = min(u32(remaining), MAX);
+	read := 0;
+	for {
+		to_read := u32(min(1<<29-1, len(data)-read));
+		if to_read <= 0 do break;
 
 
-		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) {
 seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {

+ 2 - 2
core/sys/win32/kernel32.odin

@@ -58,8 +58,8 @@ foreign kernel32 {
 	                      creation, flags_and_attribs: u32, template_file: Handle) -> Handle ---;
 	                      creation, flags_and_attribs: u32, template_file: Handle) -> Handle ---;
 
 
 
 
-	@(link_name="ReadFile")  read_file  :: proc(h: Handle, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> Bool ---;
-	@(link_name="WriteFile") write_file :: proc(h: Handle, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> Bool ---;
+	@(link_name="ReadFile")  read_file  :: proc(h: Handle, buf: rawptr, to_read: u32, bytes_read: ^u32, overlapped: rawptr) -> Bool ---;
+	@(link_name="WriteFile") write_file :: proc(h: Handle, buf: rawptr, len: u32, written_result: ^u32, overlapped: rawptr) -> Bool ---;
 
 
 	@(link_name="GetFileSizeEx")              get_file_size_ex               :: proc(file_handle: Handle, file_size: ^i64) -> Bool ---;
 	@(link_name="GetFileSizeEx")              get_file_size_ex               :: proc(file_handle: Handle, file_size: ^i64) -> Bool ---;
 	@(link_name="GetFileInformationByHandle") get_file_information_by_handle :: proc(file_handle: Handle, file_info: ^By_Handle_File_Information) -> Bool ---;
 	@(link_name="GetFileInformationByHandle") get_file_information_by_handle :: proc(file_handle: Handle, file_info: ^By_Handle_File_Information) -> Bool ---;