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

(cherry picked from commit a749ba0cea)
This commit is contained in:
Yongjun Zhang 2016-04-21 11:36:26 -07:00
parent 0b008dc21c
commit a1beb5fe1e
2 changed files with 40 additions and 2 deletions

View File

@ -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();
}
}
}

View File

@ -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();
}
}
}
}