Skip to content

Commit

Permalink
Do not set File._errno when reading less than requested bytes. (#2785)
Browse files Browse the repository at this point in the history
When requesting more bytes than available from a File the _errno was set to
although reading less than available bytes (> 0) is no error condition.
If anything then FileEOF should be set in this case to signal that we reached the end of file.
Nonetheless i kept it as it is right now, as i think it is more reliable to detect FileEOF when reading 0 bytes.
  • Loading branch information
mfelsche authored and jemc committed Jun 19, 2018
1 parent 54ab0aa commit b596b7b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
25 changes: 25 additions & 0 deletions packages/files/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ actor Main is TestList
test(_TestFileMixedWriteQueue)
test(_TestFileWritevLarge)
test(_TestFileFlush)
test(_TestFileReadMore)

primitive _FileHelper
fun make_files(h: TestHelper, files: Array[String]): FilePath ? =>
Expand Down Expand Up @@ -811,3 +812,27 @@ class iso _TestFileFlush is UnitTest
else
h.fail("Unhandled error!")
end

class iso _TestFileReadMore is UnitTest
fun name(): String => "files/File.read-more"
fun apply(h: TestHelper)? =>
let path = FilePath(h.env.root as AmbientAuth, "tmp-read-more")?
with file = CreateFile(path) as File do
h.assert_true(file.write("foobar"))
end

with read_file = OpenFile(path) as File do
let content = read_file.read(10)
h.assert_eq[USize](6, content.size())
h.assert_is[FileErrNo](
read_file.errno(),
FileOK,
"File errno is not OK after reading fewer bytes than requested")
h.assert_eq[USize](0, read_file.read(10).size())
h.assert_is[FileErrNo](
read_file.errno(),
FileEOF,
"File errno is not EOF after reading past the last byte")
end
path.remove()

33 changes: 11 additions & 22 deletions packages/files/file.pony
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,11 @@ class File
end)
.isize()

if r < bytes_to_read.isize() then
_errno =
if r == 0 then
FileEOF
else
_get_error() // error
error
end
match r
| 0 => _errno = FileEOF
| -1 =>
_errno = _get_error()
error
else
// truncate at offset in order to adjust size of string after ffi call
// and to avoid scanning full array via recalc
Expand Down Expand Up @@ -312,13 +309,9 @@ class File
end)
.isize()

if r < len.isize() then
_errno =
if r == 0 then
FileEOF
else
_get_error() // error
end
match r
| 0 => _errno = FileEOF
| -1 => _errno = _get_error()
end

result.truncate(r.usize())
Expand All @@ -341,13 +334,9 @@ class File
@read(_fd, result.cpointer(), result.space())
end).isize()

if r < len.isize() then
_errno =
if r == 0 then
FileEOF
else
_get_error() // error
end
match r
| 0 => _errno = FileEOF
| -1 => _errno = _get_error()
end

result.truncate(r.usize())
Expand Down

0 comments on commit b596b7b

Please sign in to comment.