1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: fix file.read() (#15861)

This commit is contained in:
yuyi 2022-09-24 13:58:55 +08:00 committed by GitHub
parent 91c6e1a65b
commit d624ad50a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 7 deletions

View File

@ -237,11 +237,18 @@ pub fn (f &File) read(mut buf []u8) !int {
if buf.len == 0 {
return IError(Eof{})
}
nbytes := fread(buf.data, 1, buf.len, f.cfile) or {
return IError(NotExpected{
cause: 'unexpected error from fread'
code: -1
})
nbytes := int(C.fread(buf.data, 1, buf.len, f.cfile))
// if no bytes were read, check for errors and end-of-file.
if nbytes <= 0 {
if C.feof(f.cfile) != 0 {
return IError(Eof{})
}
if C.ferror(f.cfile) != 0 {
return IError(NotExpected{
cause: 'unexpected error from fread'
code: -1
})
}
}
return nbytes
}

View File

@ -144,7 +144,7 @@ fn test_read_eof_last_read_partial_buffer_fill() {
assert false
} else {
// Expected an error when received end-of-file.
assert err !is none
assert err == IError(os.Eof{})
}
f.close()
}
@ -176,7 +176,7 @@ fn test_read_eof_last_read_full_buffer_fill() {
assert false
} else {
// Expect an error at EOF.
assert err !is none
assert err == IError(os.Eof{})
}
f.close()
}