Skip to content
This repository has been archived by the owner on Nov 20, 2022. It is now read-only.

Commit

Permalink
Fix fs_read return value handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilm committed Feb 2, 2015
1 parent faa8910 commit 9f2521c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
13 changes: 6 additions & 7 deletions code/uvcat/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ void on_write(uv_fs_t *req) {

void on_read(uv_fs_t *req) {
if (req->result < 0) {
if (req->result == UV_EOF) {
uv_fs_t close_req;
// synchronous
uv_fs_close(uv_default_loop(), &close_req, open_req.result, NULL);
} else {
fprintf(stderr, "Read error: %s\n", uv_strerror(req->result));
}
fprintf(stderr, "Read error: %s\n", uv_strerror(req->result));
}
else if (req->result == 0) {
uv_fs_t close_req;
// synchronous
uv_fs_close(uv_default_loop(), &close_req, open_req.result, NULL);
}
else if (req->result > 0) {
iov.len = req->result;
Expand Down
7 changes: 5 additions & 2 deletions source/filesystem.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ The ``result`` field of a ``uv_fs_t`` is the file descriptor in case of the
.. literalinclude:: ../code/uvcat/main.c
:linenos:
:lines: 26-40
:emphasize-lines: 2,6,13
:emphasize-lines: 2,8,12

In the case of a read call, you should pass an *initialized* buffer which will
be filled with data before the read callback is triggered.
be filled with data before the read callback is triggered. The ``uv_fs_*``
operations map almost directly to certain POSIX functions, so EOF is indicated
in this case by ``result`` being 0. In the case of streams or pipes, the
``UV_EOF`` constant would have been passed as a status instead.

Here you see a common pattern when writing asynchronous programs. The
``uv_fs_close()`` call is performed synchronously. *Usually tasks which are
Expand Down

0 comments on commit 9f2521c

Please sign in to comment.