windows: uv_fs_rename to replace the new file if it exists

fixes #283
This commit is contained in:
Igor Zinkovsky 2011-12-30 15:46:17 -08:00
parent 4d1d02fd18
commit 43e3ac5798
3 changed files with 76 additions and 2 deletions

View File

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

View File

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

View File

@ -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)