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:
parent
91c6e1a65b
commit
d624ad50a7
@ -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
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user