From 2b58fb8b03ceeb930c2788052443f065ea007af4 Mon Sep 17 00:00:00 2001 From: Chris Douglas Date: Wed, 17 May 2017 17:21:03 -0700 Subject: [PATCH] HADOOP-14434. Use MoveFileEx to allow renaming a file when the destination exists. Contributed by Lukas Majercak (cherry picked from commit ef9e536a7137d209985d25a4769712c2db2c52bf) (cherry picked from commit 4bb056544341726dd051353f1cc8430b801bf391) (cherry picked from commit 475b50b44cebf5e5b4622e4092f4a447569b1b2b) --- hadoop-common-project/hadoop-common/CHANGES.txt | 3 +++ .../native/src/org/apache/hadoop/io/nativeio/NativeIO.c | 2 +- .../java/org/apache/hadoop/io/nativeio/TestNativeIO.java | 6 ++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 2ddc0c430b9..515a93e73e5 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -148,6 +148,9 @@ Release 2.7.4 - UNRELEASED HADOOP-12173. NetworkTopology::add calls toString always. (Inigo Goiri via cdouglas) + HADOOP-14434. Use MoveFileEx to allow renaming a file when the destination + exists. (Lukas Majercak via cdouglas) + Release 2.7.3 - 2016-08-25 INCOMPATIBLE CHANGES 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 071d8300026..6f07fbe09a2 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 @@ -1162,7 +1162,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 bf3ece7894c..513465f2024 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 @@ -543,7 +543,7 @@ public class TestNativeIO { 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()); @@ -569,7 +569,9 @@ public class TestNativeIO { } } - FileUtils.deleteQuietly(TEST_DIR); + // Test renaming to an existing file + assertTrue(targetFile.exists()); + NativeIO.renameTo(sourceFile, targetFile); } @Test(timeout=10000)