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,12 +237,19 @@ pub fn (f &File) read(mut buf []u8) !int {
|
|||||||
if buf.len == 0 {
|
if buf.len == 0 {
|
||||||
return IError(Eof{})
|
return IError(Eof{})
|
||||||
}
|
}
|
||||||
nbytes := fread(buf.data, 1, buf.len, f.cfile) or {
|
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{
|
return IError(NotExpected{
|
||||||
cause: 'unexpected error from fread'
|
cause: 'unexpected error from fread'
|
||||||
code: -1
|
code: -1
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nbytes
|
return nbytes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,7 +144,7 @@ fn test_read_eof_last_read_partial_buffer_fill() {
|
|||||||
assert false
|
assert false
|
||||||
} else {
|
} else {
|
||||||
// Expected an error when received end-of-file.
|
// Expected an error when received end-of-file.
|
||||||
assert err !is none
|
assert err == IError(os.Eof{})
|
||||||
}
|
}
|
||||||
f.close()
|
f.close()
|
||||||
}
|
}
|
||||||
@ -176,7 +176,7 @@ fn test_read_eof_last_read_full_buffer_fill() {
|
|||||||
assert false
|
assert false
|
||||||
} else {
|
} else {
|
||||||
// Expect an error at EOF.
|
// Expect an error at EOF.
|
||||||
assert err !is none
|
assert err == IError(os.Eof{})
|
||||||
}
|
}
|
||||||
f.close()
|
f.close()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user