Fix broken cases of Seek usage in _file_stream_proc

Handles `EINVAL`, among other fixes.
This commit is contained in:
Feoramund
2024-08-19 04:35:55 -04:00
committed by Laytan
parent e83b982afe
commit 1ced76cdd1
4 changed files with 15 additions and 0 deletions

View File

@@ -192,6 +192,8 @@ seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
case 0: w = win32.FILE_BEGIN
case 1: w = win32.FILE_CURRENT
case 2: w = win32.FILE_END
case:
return 0, .Invalid_Whence
}
hi := i32(offset>>32)
lo := i32(offset)

View File

@@ -43,6 +43,7 @@ ERROR_BUFFER_OVERFLOW :: _Platform_Error(111)
ERROR_INSUFFICIENT_BUFFER :: _Platform_Error(122)
ERROR_MOD_NOT_FOUND :: _Platform_Error(126)
ERROR_PROC_NOT_FOUND :: _Platform_Error(127)
ERROR_NEGATIVE_SEEK :: _Platform_Error(131)
ERROR_DIR_NOT_EMPTY :: _Platform_Error(145)
ERROR_ALREADY_EXISTS :: _Platform_Error(183)
ERROR_ENVVAR_NOT_FOUND :: _Platform_Error(203)
@@ -91,6 +92,9 @@ get_last_error :: proc "contextless" () -> Error {
case win32.ERROR_INVALID_HANDLE:
return .Invalid_File
case win32.ERROR_NEGATIVE_SEEK:
return .Invalid_Offset
case
win32.ERROR_BAD_ARGUMENTS,
win32.ERROR_INVALID_PARAMETER,

View File

@@ -47,6 +47,14 @@ _file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte,
}
case .Seek:
n, os_err = seek(fd, offset, int(whence))
if os_err != nil {
switch whence {
case .Start, .Current, .End:
return 0, .Invalid_Offset
case:
return 0, .Invalid_Whence
}
}
case .Size:
n, os_err = file_size(fd)
case .Destroy:

View File

@@ -213,6 +213,7 @@ ERROR_BROKEN_PIPE : DWORD : 109
ERROR_CALL_NOT_IMPLEMENTED : DWORD : 120
ERROR_INSUFFICIENT_BUFFER : DWORD : 122
ERROR_INVALID_NAME : DWORD : 123
ERROR_NEGATIVE_SEEK : DWORD : 131
ERROR_BAD_ARGUMENTS : DWORD : 160
ERROR_LOCK_FAILED : DWORD : 167
ERROR_ALREADY_EXISTS : DWORD : 183