diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Mkdir.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Mkdir.java index 9f39da29ce..5828b0bbf4 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Mkdir.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Mkdir.java @@ -68,11 +68,14 @@ protected void processPath(PathData item) throws IOException { @Override protected void processNonexistentPath(PathData item) throws IOException { - // check if parent exists. this is complicated because getParent(a/b/c/) returns a/b/c, but - // we want a/b - if (!createParents && - !item.fs.exists(new Path(item.path.toString()).getParent())) { - throw new PathNotFoundException(item.toString()); + if (!createParents) { + // check if parent exists. this is complicated because getParent(a/b/c/) returns a/b/c, but + // we want a/b + final Path itemPath = new Path(item.path.toString()); + final Path itemParentPath = itemPath.getParent(); + if (!item.fs.exists(itemParentPath)) { + throw new PathNotFoundException(itemParentPath.toString()); + } } if (!item.fs.mkdirs(item.path)) { throw new PathIOException(item.toString()); diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java index b19bdeab57..1d2042ec3e 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java @@ -721,6 +721,14 @@ public void testErrOutPut() throws Exception { assertTrue(" -mkdir returned this is a file ", (returned.lastIndexOf("not a directory") != -1)); out.reset(); + argv[0] = "-mkdir"; + argv[1] = "/testParent/testChild"; + ret = ToolRunner.run(shell, argv); + returned = out.toString(); + assertEquals(" -mkdir returned 1", 1, ret); + assertTrue(" -mkdir returned there is No file or directory but has testChild in the path", + (returned.lastIndexOf("testChild") == -1)); + out.reset(); argv = new String[3]; argv[0] = "-mv"; argv[1] = "/testfile"; diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/resources/testHDFSConf.xml b/hadoop-hdfs-project/hadoop-hdfs/src/test/resources/testHDFSConf.xml index a13c441052..4ab093bd15 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/resources/testHDFSConf.xml +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/resources/testHDFSConf.xml @@ -6183,11 +6183,11 @@ RegexpComparator - mkdir: `dir0/dir1': No such file or directory + .*mkdir:.*dir0': No such file or directory$ - + mkdir: Test recreate of existing directory fails