diff --git a/src/win/fs.c b/src/win/fs.c index 2037ddd6..78ccffab 100644 --- a/src/win/fs.c +++ b/src/win/fs.c @@ -542,8 +542,12 @@ void fs__fstat(uv_fs_t* req, uv_file file) { void fs__rename(uv_fs_t* req, const wchar_t* path, const wchar_t* new_path) { - int result = _wrename(path, new_path); - SET_REQ_RESULT(req, result); + if (!MoveFileExW(path, new_path, MOVEFILE_REPLACE_EXISTING)) { + SET_REQ_RESULT_WIN32_ERROR(req, GetLastError()); + return; + } + + SET_REQ_RESULT(req, 0); } diff --git a/test/test-fs.c b/test/test-fs.c index 34f891c1..763af8a7 100644 --- a/test/test-fs.c +++ b/test/test-fs.c @@ -1476,3 +1476,71 @@ TEST_IMPL(fs_file_open_append) { return 0; } + + +TEST_IMPL(fs_rename_to_existing_file) { + int r; + + /* Setup. */ + unlink("test_file"); + unlink("test_file2"); + + 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_file2", 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_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_rename(loop, &rename_req, "test_file", "test_file2", NULL); + ASSERT(r != -1); + ASSERT(rename_req.result != -1); + uv_fs_req_cleanup(&rename_req); + + r = uv_fs_open(loop, &open_req1, "test_file2", 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_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"); + unlink("test_file2"); + + return 0; +} \ No newline at end of file diff --git a/test/test-list.h b/test/test-list.h index dec51053..3efc721f 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -115,6 +115,7 @@ TEST_DECLARE (fs_event_immediate_close) TEST_DECLARE (fs_readdir_empty_dir) TEST_DECLARE (fs_readdir_file) TEST_DECLARE (fs_open_dir) +TEST_DECLARE (fs_rename_to_existing_file) TEST_DECLARE (threadpool_queue_work_simple) TEST_DECLARE (counters_init) #ifdef _WIN32 @@ -270,6 +271,7 @@ TASK_LIST_START TEST_ENTRY (fs_readdir_empty_dir) TEST_ENTRY (fs_readdir_file) TEST_ENTRY (fs_open_dir) + TEST_ENTRY (fs_rename_to_existing_file) TEST_ENTRY (threadpool_queue_work_simple) TEST_ENTRY (counters_init)