Skip to content

Commit

Permalink
win,fs: fix error code in uv_fs_read() and uv_fs_write() (libuv#3303)
Browse files Browse the repository at this point in the history
Just like the Unix counterpart, uv_fs_read() and uv_fs_write() should
throw an EBADF instead of throwing an EPERM when the passed fd has not
been opened with the right flags.

Signed-off-by: Darshan Sen <darshan.sen@postman.com>
  • Loading branch information
RaisinTen authored Oct 16, 2021
1 parent 1cefd94 commit 9604b61
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
18 changes: 15 additions & 3 deletions src/win/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ void fs__read_filemap(uv_fs_t* req, struct uv__fd_info_s* fd_info) {
void* view;

if (rw_flags == UV_FS_O_WRONLY) {
SET_REQ_WIN32_ERROR(req, ERROR_ACCESS_DENIED);
SET_REQ_WIN32_ERROR(req, ERROR_INVALID_FLAGS);
return;
}
if (fd_info->is_directory) {
Expand Down Expand Up @@ -912,6 +912,11 @@ void fs__read(uv_fs_t* req) {
SET_REQ_RESULT(req, bytes);
} else {
error = GetLastError();

if (error == ERROR_ACCESS_DENIED) {
error = ERROR_INVALID_FLAGS;
}

if (error == ERROR_HANDLE_EOF) {
SET_REQ_RESULT(req, bytes);
} else {
Expand All @@ -936,7 +941,7 @@ void fs__write_filemap(uv_fs_t* req, HANDLE file,
FILETIME ft;

if (rw_flags == UV_FS_O_RDONLY) {
SET_REQ_WIN32_ERROR(req, ERROR_ACCESS_DENIED);
SET_REQ_WIN32_ERROR(req, ERROR_INVALID_FLAGS);
return;
}
if (fd_info->is_directory) {
Expand Down Expand Up @@ -1052,6 +1057,7 @@ void fs__write(uv_fs_t* req) {
OVERLAPPED overlapped, *overlapped_ptr;
LARGE_INTEGER offset_;
DWORD bytes;
DWORD error;
int result;
unsigned int index;
LARGE_INTEGER original_position;
Expand Down Expand Up @@ -1111,7 +1117,13 @@ void fs__write(uv_fs_t* req) {
if (result || bytes > 0) {
SET_REQ_RESULT(req, bytes);
} else {
SET_REQ_WIN32_ERROR(req, GetLastError());
error = GetLastError();

if (error == ERROR_ACCESS_DENIED) {
error = ERROR_INVALID_FLAGS;
}

SET_REQ_WIN32_ERROR(req, error);
}
}

Expand Down
38 changes: 19 additions & 19 deletions test/test-fs-open-flags.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,21 +276,21 @@ static void fs_open_flags(int add_flags) {
/* r */
flags = add_flags | UV_FS_O_RDONLY;
openFail(absent_file, UV_ENOENT);
writeFail(empty_file, UV_EPERM);
writeFail(empty_file, UV_EBADF);
readExpect(empty_file, "", 0);
writeFail(dummy_file, UV_EPERM);
writeFail(dummy_file, UV_EBADF);
readExpect(dummy_file, "a", 1);
writeFail(empty_dir, UV_EPERM);
writeFail(empty_dir, UV_EBADF);
readFail(empty_dir, UV_EISDIR);

/* rs */
flags = add_flags | UV_FS_O_RDONLY | UV_FS_O_SYNC;
openFail(absent_file, UV_ENOENT);
writeFail(empty_file, UV_EPERM);
writeFail(empty_file, UV_EBADF);
readExpect(empty_file, "", 0);
writeFail(dummy_file, UV_EPERM);
writeFail(dummy_file, UV_EBADF);
readExpect(dummy_file, "a", 1);
writeFail(empty_dir, UV_EPERM);
writeFail(empty_dir, UV_EBADF);
readFail(empty_dir, UV_EISDIR);

/* r+ */
Expand All @@ -316,18 +316,18 @@ static void fs_open_flags(int add_flags) {
/* w */
flags = add_flags | UV_FS_O_TRUNC | UV_FS_O_CREAT | UV_FS_O_WRONLY;
writeExpect(absent_file, "bc", 2);
readFail(absent_file, UV_EPERM);
readFail(absent_file, UV_EBADF);
writeExpect(empty_file, "bc", 2);
readFail(empty_file, UV_EPERM);
readFail(empty_file, UV_EBADF);
writeExpect(dummy_file, "bc", 2);
readFail(dummy_file, UV_EPERM);
readFail(dummy_file, UV_EBADF);
openFail(empty_dir, UV_EISDIR);

/* wx */
flags = add_flags | UV_FS_O_TRUNC | UV_FS_O_CREAT | UV_FS_O_WRONLY |
UV_FS_O_EXCL;
writeExpect(absent_file, "bc", 2);
readFail(absent_file, UV_EPERM);
readFail(absent_file, UV_EBADF);
openFail(empty_file, UV_EEXIST);
openFail(dummy_file, UV_EEXIST);
openFail(empty_dir, UV_EEXIST);
Expand All @@ -354,19 +354,19 @@ static void fs_open_flags(int add_flags) {
/* a */
flags = add_flags | UV_FS_O_APPEND | UV_FS_O_CREAT | UV_FS_O_WRONLY;
writeExpect(absent_file, "bc", 2);
readFail(absent_file, UV_EPERM);
readFail(absent_file, UV_EBADF);
writeExpect(empty_file, "bc", 2);
readFail(empty_file, UV_EPERM);
readFail(empty_file, UV_EBADF);
writeExpect(dummy_file, "abc", 3);
readFail(dummy_file, UV_EPERM);
readFail(dummy_file, UV_EBADF);
writeFail(empty_dir, UV_EISDIR);
readFail(empty_dir, UV_EPERM);
readFail(empty_dir, UV_EBADF);

/* ax */
flags = add_flags | UV_FS_O_APPEND | UV_FS_O_CREAT | UV_FS_O_WRONLY |
UV_FS_O_EXCL;
writeExpect(absent_file, "bc", 2);
readFail(absent_file, UV_EPERM);
readFail(absent_file, UV_EBADF);
openFail(empty_file, UV_EEXIST);
openFail(dummy_file, UV_EEXIST);
openFail(empty_dir, UV_EEXIST);
Expand All @@ -375,13 +375,13 @@ static void fs_open_flags(int add_flags) {
flags = add_flags | UV_FS_O_APPEND | UV_FS_O_CREAT | UV_FS_O_WRONLY |
UV_FS_O_SYNC;
writeExpect(absent_file, "bc", 2);
readFail(absent_file, UV_EPERM);
readFail(absent_file, UV_EBADF);
writeExpect(empty_file, "bc", 2);
readFail(empty_file, UV_EPERM);
readFail(empty_file, UV_EBADF);
writeExpect(dummy_file, "abc", 3);
readFail(dummy_file, UV_EPERM);
readFail(dummy_file, UV_EBADF);
writeFail(empty_dir, UV_EISDIR);
readFail(empty_dir, UV_EPERM);
readFail(empty_dir, UV_EBADF);

/* a+ */
flags = add_flags | UV_FS_O_APPEND | UV_FS_O_CREAT | UV_FS_O_RDWR;
Expand Down

0 comments on commit 9604b61

Please sign in to comment.