From fe97c4dc63552ac830a82340f1c59c2a8bdfafe1 Mon Sep 17 00:00:00 2001 From: Igor Zinkovsky Date: Fri, 4 Nov 2011 12:41:23 -0700 Subject: [PATCH] windows: honor O_APPEND in uv_fs_open --- src/win/fs.c | 11 +++++--- test/test-fs.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ test/test-list.h | 2 ++ 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/src/win/fs.c b/src/win/fs.c index a2c572d6..09ff145c 100644 --- a/src/win/fs.c +++ b/src/win/fs.c @@ -173,19 +173,24 @@ void fs__open(uv_fs_t* req, const wchar_t* path, int flags, int mode) { /* convert flags and mode to CreateFile parameters */ switch (flags & (_O_RDONLY | _O_WRONLY | _O_RDWR)) { case _O_RDONLY: - access = GENERIC_READ; + access = FILE_GENERIC_READ; break; case _O_WRONLY: - access = GENERIC_WRITE; + access = FILE_GENERIC_WRITE; break; case _O_RDWR: - access = GENERIC_READ | GENERIC_WRITE; + access = FILE_GENERIC_READ | FILE_GENERIC_WRITE; break; default: result = -1; goto end; } + if (flags & _O_APPEND) { + access &= ~FILE_WRITE_DATA; + access |= FILE_APPEND_DATA; + } + /* * Here is where we deviate significantly from what CRT's _open() * does. We indiscriminately use all the sharing modes, to match diff --git a/test/test-fs.c b/test/test-fs.c index a8065192..5d6e11f0 100644 --- a/test/test-fs.c +++ b/test/test-fs.c @@ -1408,3 +1408,69 @@ TEST_IMPL(fs_open_dir) { return 0; } + + +TEST_IMPL(fs_file_open_append) { + 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_RDWR | O_APPEND, 0, 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, S_IREAD, NULL); + ASSERT(r != -1); + ASSERT(open_req1.result != -1); + uv_fs_req_cleanup(&open_req1); + + r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1, + NULL); + printf("read = %d\n", r); + ASSERT(r == 26); + ASSERT(read_req.result == 26); + ASSERT(memcmp(buf, "test-buffer\n\0test-buffer\n\0", sizeof(buf)) == 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; +} \ No newline at end of file diff --git a/test/test-list.h b/test/test-list.h index d602def3..b38f9536 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -103,6 +103,7 @@ TEST_DECLARE (fs_link) TEST_DECLARE (fs_symlink) TEST_DECLARE (fs_utime) TEST_DECLARE (fs_futime) +TEST_DECLARE (fs_file_open_append) TEST_DECLARE (fs_stat_missing_path) TEST_DECLARE (fs_event_watch_dir) TEST_DECLARE (fs_event_watch_file) @@ -253,6 +254,7 @@ TASK_LIST_START TEST_ENTRY (fs_futime) TEST_ENTRY (fs_symlink) TEST_ENTRY (fs_stat_missing_path) + TEST_ENTRY (fs_file_open_append) TEST_ENTRY (fs_event_watch_dir) TEST_ENTRY (fs_event_watch_file) TEST_ENTRY (fs_event_watch_file_current_dir)