win,fs: fix error code in uv_fs_read() and uv_fs_write() (#3303)

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>
This commit is contained in:
Darshan Sen 2021-10-16 13:11:52 +00:00 committed by GitHub
parent 1cefd94d56
commit 9604b61db6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 22 deletions

View File

@ -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) {
@ -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 {
@ -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) {
@ -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;
@ -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);
}
}

View File

@ -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+ */
@ -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);
@ -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);
@ -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;