HDFS-9670. DistCp throws NPE when source is root. (John Zhuge via Yongjun Zhang)

(cherry picked from commit a749ba0ceaa843aa83146b6bea19e031c8dc3296)


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/a1beb5fe
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/a1beb5fe
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/a1beb5fe

Branch: refs/heads/branch-2.8
Commit: a1beb5fe1e7de47f4dd6a5959334d0a8ff41853b
Parents: 0b008dc
Author: Yongjun Zhang <[email protected]>
Authored: Thu Apr 21 11:36:26 2016 -0700
Committer: Yongjun Zhang <[email protected]>
Committed: Thu Apr 21 12:29:49 2016 -0700

----------------------------------------------------------------------
 .../apache/hadoop/tools/SimpleCopyListing.java  |  8 +++--
 .../apache/hadoop/tools/TestDistCpSystem.java   | 34 ++++++++++++++++++++
 2 files changed, 40 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/a1beb5fe/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/SimpleCopyListing.java
----------------------------------------------------------------------
diff --git 
a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/SimpleCopyListing.java
 
b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/SimpleCopyListing.java
index d2598a4..fef0e04 100644
--- 
a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/SimpleCopyListing.java
+++ 
b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/SimpleCopyListing.java
@@ -369,8 +369,12 @@ public class SimpleCopyListing extends CopyListing {
       boolean specialHandling = (options.getSourcePaths().size() == 1 && 
!targetPathExists) ||
           options.shouldSyncFolder() || options.shouldOverwrite();
 
-      return specialHandling && sourceStatus.isDirectory() ? 
sourceStatus.getPath() :
-          sourceStatus.getPath().getParent();
+      if ((specialHandling && sourceStatus.isDirectory()) ||
+          sourceStatus.getPath().isRoot()) {
+        return sourceStatus.getPath();
+      } else {
+        return sourceStatus.getPath().getParent();
+      }
     }
   }
 

http://git-wip-us.apache.org/repos/asf/hadoop/blob/a1beb5fe/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestDistCpSystem.java
----------------------------------------------------------------------
diff --git 
a/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestDistCpSystem.java
 
b/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestDistCpSystem.java
index a3e8afe..cd86560 100644
--- 
a/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestDistCpSystem.java
+++ 
b/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestDistCpSystem.java
@@ -18,6 +18,8 @@
 
 package org.apache.hadoop.tools;
 
+import static org.hamcrest.core.Is.is;
+
 import java.io.IOException;
 import java.io.OutputStream;
 import java.net.URI;
@@ -32,6 +34,8 @@ import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.hdfs.MiniDFSCluster;
 import org.apache.hadoop.util.ToolRunner;
+import org.junit.Assert;
+import org.junit.Test;
 
 /**
  * A JUnit test for copying files recursively.
@@ -201,4 +205,34 @@ public class TestDistCpSystem extends TestCase {
     testPreserveUserHelper(srcfiles, dstfiles, true, true, true);
   }
 
+  @Test
+  public void testSourceRoot() throws Exception {
+    MiniDFSCluster cluster = null;
+    Configuration conf = new Configuration();
+    try {
+      cluster = new MiniDFSCluster.Builder(conf).numDataNodes(2).build();
+      cluster.waitActive();
+      FileSystem fs = cluster.getFileSystem();
+
+      String rootStr = fs.makeQualified(new Path("/")).toString();
+
+      // Case 1. The target does not exist.
+
+      String tgtStr = fs.makeQualified(new Path("/nodir")).toString();
+      String[] args = new String[]{ rootStr, tgtStr };
+      Assert.assertThat(ToolRunner.run(conf, new DistCp(), args), is(0));
+
+      // Case 2. The target exists.
+
+      Path tgtPath2 = new Path("/dir");
+      assertTrue(fs.mkdirs(tgtPath2));
+      String tgtStr2 = fs.makeQualified(tgtPath2).toString();
+      String[] args2 = new String[]{ rootStr, tgtStr2 };
+      Assert.assertThat(ToolRunner.run(conf, new DistCp(), args2), is(0));
+    } finally {
+      if (cluster != null) {
+        cluster.shutdown();
+      }
+    }
+  }
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to