From c417477a2f1e8e5892a10edda3dc230887410f3a Mon Sep 17 00:00:00 2001
From: Dmitry Timoshkov <dmitry@baikal.ru>
Date: Sun, 14 Oct 2018 14:36:56 -0600
Subject: [PATCH 3/5] kernel32: MoveFile(source, source) should succeed.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=14901
Signed-off-by: Alex Henrie <alexhenrie24@gmail.com>
---
Patch from Wine Staging with minor modifictions.
---
 dlls/kernel32/path.c                     | 14 +++++++++-----
 dlls/kernel32/tests/file.c               |  2 +-
 dlls/msvcp120/tests/msvcp120.c           |  2 +-
 programs/cmd/tests/test_builtins.cmd.exp |  4 ++--
 4 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/dlls/kernel32/path.c b/dlls/kernel32/path.c
index 48a3e336ec..93b202559a 100644
--- a/dlls/kernel32/path.c
+++ b/dlls/kernel32/path.c
@@ -1341,17 +1341,21 @@ BOOL WINAPI MoveFileWithProgressW( LPCWSTR source, LPCWSTR dest,
     status = NtOpenFile( &dest_handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &attr, &io, 0, options );
     if (status == STATUS_SUCCESS)  /* destination exists */
     {
-        NtClose( dest_handle );
-        if (!(flag & MOVEFILE_REPLACE_EXISTING))
+        if (!(flag & MOVEFILE_REPLACE_EXISTING) && !is_same_file( source_handle, dest_handle ))
         {
+            NtClose( dest_handle );
             SetLastError( ERROR_ALREADY_EXISTS );
             RtlFreeUnicodeString( &nt_name );
             goto error;
         }
-        else if (info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) /* cannot replace directory */
+        else
         {
-            SetLastError( ERROR_ACCESS_DENIED );
-            goto error;
+            NtClose( dest_handle );
+            if (info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) /* cannot replace directory */
+            {
+                SetLastError( ERROR_ACCESS_DENIED );
+                goto error;
+            }
         }
     }
     else if (status != STATUS_OBJECT_NAME_NOT_FOUND)
diff --git a/dlls/kernel32/tests/file.c b/dlls/kernel32/tests/file.c
index bec91601af..592cec5a0c 100644
--- a/dlls/kernel32/tests/file.c
+++ b/dlls/kernel32/tests/file.c
@@ -1886,7 +1886,7 @@ static void test_MoveFileA(void)
     ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
 
     ret = MoveFileA(source, source);
-    todo_wine ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
+    ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
 
     ret = MoveFileA(source, dest);
     ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
diff --git a/dlls/msvcp120/tests/msvcp120.c b/dlls/msvcp120/tests/msvcp120.c
index 2e63555f2e..ac38aa61a0 100644
--- a/dlls/msvcp120/tests/msvcp120.c
+++ b/dlls/msvcp120/tests/msvcp120.c
@@ -1529,7 +1529,7 @@ static void test_tr2_sys__Rename(void)
     CloseHandle(file);
 
     ret = p_tr2_sys__Rename("tr2_test_dir\\f1", "tr2_test_dir\\f1");
-    todo_wine ok(ERROR_SUCCESS == ret, "test_tr2_sys__Rename(): expect: ERROR_SUCCESS, got %d\n", ret);
+    ok(ERROR_SUCCESS == ret, "test_tr2_sys__Rename(): expect: ERROR_SUCCESS, got %d\n", ret);
     for(i=0; i<ARRAY_SIZE(tests); i++) {
         errno = 0xdeadbeef;
         if(tests[i].val == ERROR_SUCCESS) {
diff --git a/programs/cmd/tests/test_builtins.cmd.exp b/programs/cmd/tests/test_builtins.cmd.exp
index 797a9cc8ae..b7ef05d76c 100644
--- a/programs/cmd/tests/test_builtins.cmd.exp
+++ b/programs/cmd/tests/test_builtins.cmd.exp
@@ -1313,8 +1313,8 @@ file move succeeded
 @todo_wine@bar@or_broken@baz
 read-only files are moveable
 file moved in subdirectory
-@todo_wine@moving a file to itself is a no-op@or_broken@moving a file to itself should be a no-op!
-@todo_wine@ErrorLevel: 0@or_broken@ErrorLevel: 1
+moving a file to itself is a no-op
+ErrorLevel: 0
 --- directory move
 simple directory move succeeded
 moving a directory to itself gives error; errlevel 1
-- 
2.19.1

