diff --git a/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/nativeio/NativeIO.c b/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/nativeio/NativeIO.c index a7d4b55c65c..242a45676b0 100644 --- a/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/nativeio/NativeIO.c +++ b/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/nativeio/NativeIO.c @@ -1204,7 +1204,7 @@ done: if (!src) goto done; // exception was thrown dst = (LPCWSTR) (*env)->GetStringChars(env, jdst, NULL); if (!dst) goto done; // exception was thrown - if (!MoveFile(src, dst)) { + if (!MoveFileEx(src, dst, MOVEFILE_REPLACE_EXISTING)) { throw_ioe(env, GetLastError()); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/nativeio/TestNativeIO.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/nativeio/TestNativeIO.java index b2445a33312..436f10a3d12 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/nativeio/TestNativeIO.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/nativeio/TestNativeIO.java @@ -510,7 +510,7 @@ public void testRenameTo() throws Exception { Assert.assertEquals(Errno.ENOENT, e.getErrno()); } } - + // Test renaming a file to itself. It should succeed and do nothing. File sourceFile = new File(TEST_DIR, "source"); Assert.assertTrue(sourceFile.createNewFile()); @@ -536,7 +536,9 @@ public void testRenameTo() throws Exception { } } - FileUtils.deleteQuietly(TEST_DIR); + // Test renaming to an existing file + assertTrue(targetFile.exists()); + NativeIO.renameTo(sourceFile, targetFile); } @Test(timeout=10000)