Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
windows: uv_fs_rename to replace the new file if it exists
fixes #283
  • Loading branch information
Igor Zinkovsky committed Dec 30, 2011
1 parent 4d1d02f commit 43e3ac5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/win/fs.c
Expand Up @@ -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);
}


Expand Down
68 changes: 68 additions & 0 deletions test/test-fs.c
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions test/test-list.h
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 43e3ac5

Please sign in to comment.