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 cabb7e352a..3f52203d32 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 @@ private Path computeSourceRootPath(FileStatus sourceStatus, 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(); + } } } 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 a3e8afe116..cd8656074c 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.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 void testPreserveUserNonEmptyDirWithUpdate() throws Exception { 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