Skip to content

Commit

Permalink
windows: uv_fs_read to return 0 on EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Zinkovsky committed Feb 23, 2012
1 parent c0e7044 commit 46024bf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/win/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,11 @@ void fs__read(uv_fs_t* req, uv_file file, void *buf, size_t length,
if (ReadFile(handle, buf, length, &bytes, overlapped_ptr)) {
SET_REQ_RESULT(req, bytes);
} else {
SET_REQ_WIN32_ERROR(req, GetLastError());
if (GetLastError() == ERROR_HANDLE_EOF) {
SET_REQ_RESULT(req, 0);
} else {
SET_REQ_WIN32_ERROR(req, GetLastError());
}
}
}

Expand Down
56 changes: 56 additions & 0 deletions test/test-fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1659,3 +1659,59 @@ TEST_IMPL(fs_rename_to_existing_file) {

return 0;
}


TEST_IMPL(fs_read_file_eof) {
int r;

/* Setup. */
unlink("test_file");

loop = uv_default_loop();

r = uv_fs_open(loop, &open_req1, "test_file", O_WRONLY | O_CREAT,
S_IWRITE | S_IREAD, NULL);
ASSERT(r != -1);
ASSERT(open_req1.result != -1);
uv_fs_req_cleanup(&open_req1);

r = uv_fs_write(loop, &write_req, open_req1.result, test_buf,
sizeof(test_buf), -1, NULL);
ASSERT(r != -1);
ASSERT(write_req.result != -1);
uv_fs_req_cleanup(&write_req);

r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
ASSERT(r != -1);
ASSERT(close_req.result != -1);
uv_fs_req_cleanup(&close_req);

r = uv_fs_open(loop, &open_req1, "test_file", O_RDONLY, 0, NULL);
ASSERT(r != -1);
ASSERT(open_req1.result != -1);
uv_fs_req_cleanup(&open_req1);

memset(buf, 0, sizeof(buf));
r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
NULL);
ASSERT(r != -1);
ASSERT(read_req.result != -1);
ASSERT(strcmp(buf, test_buf) == 0);
uv_fs_req_cleanup(&read_req);

r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf),
read_req.result, NULL);
ASSERT(r == 0);
ASSERT(read_req.result == 0);
uv_fs_req_cleanup(&read_req);

r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
ASSERT(r != -1);
ASSERT(close_req.result != -1);
uv_fs_req_cleanup(&close_req);

/* Cleanup */
unlink("test_file");

return 0;
}
2 changes: 2 additions & 0 deletions test/test-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ TEST_DECLARE (fs_utime)
TEST_DECLARE (fs_futime)
TEST_DECLARE (fs_file_open_append)
TEST_DECLARE (fs_stat_missing_path)
TEST_DECLARE (fs_read_file_eof)
TEST_DECLARE (fs_event_watch_dir)
TEST_DECLARE (fs_event_watch_file)
TEST_DECLARE (fs_event_watch_file_current_dir)
Expand Down Expand Up @@ -290,6 +291,7 @@ TASK_LIST_START
TEST_ENTRY (fs_futime)
TEST_ENTRY (fs_symlink)
TEST_ENTRY (fs_stat_missing_path)
TEST_ENTRY (fs_read_file_eof)
TEST_ENTRY (fs_file_open_append)
TEST_ENTRY (fs_event_watch_dir)
TEST_ENTRY (fs_event_watch_file)
Expand Down

0 comments on commit 46024bf

Please sign in to comment.