From 483ca92d9b0a8c590089f5704db987b0977d7c8b Mon Sep 17 00:00:00 2001 From: Tsz-wo Sze Date: Fri, 16 Mar 2012 01:28:38 +0000 Subject: [PATCH] HADOOP-8175. Add -p option to mkdir in FsShell. Contributed by Daryn Sharp git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1301303 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-common-project/hadoop-common/CHANGES.txt | 2 ++ .../java/org/apache/hadoop/fs/shell/Mkdir.java | 15 +++++++++++---- .../hadoop-common/src/test/resources/testConf.xml | 6 +++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index eba60c1315..80a0df2ffc 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -376,6 +376,8 @@ Release 0.23.2 - UNRELEASED HADOOP-8173. FsShell needs to handle quoted metachars. (Daryn Sharp via szetszwo) + HADOOP-8175. Add -p option to mkdir in FsShell. (Daryn Sharp via szetszwo) + Release 0.23.1 - 2012-02-17 INCOMPATIBLE CHANGES 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 30ce5ed4df..eb0fd22562 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 @@ -39,20 +39,26 @@ public static void registerCommands(CommandFactory factory) { } public static final String NAME = "mkdir"; - public static final String USAGE = " ..."; + public static final String USAGE = "[-p] ..."; public static final String DESCRIPTION = - "Create a directory in specified location."; + "Create a directory in specified location.\n" + + " -p Do not fail if the directory already exists"; + private boolean createParents; + @Override protected void processOptions(LinkedList args) { - CommandFormat cf = new CommandFormat(1, Integer.MAX_VALUE); + CommandFormat cf = new CommandFormat(1, Integer.MAX_VALUE, "p"); cf.parse(args); + createParents = cf.getOpt("p"); } @Override protected void processPath(PathData item) throws IOException { if (item.stat.isDirectory()) { - throw new PathExistsException(item.toString()); + if (!createParents) { + throw new PathExistsException(item.toString()); + } } else { throw new PathIsNotDirectoryException(item.toString()); } @@ -60,6 +66,7 @@ protected void processPath(PathData item) throws IOException { @Override protected void processNonexistentPath(PathData item) throws IOException { + // TODO: should use createParents to control intermediate dir creation if (!item.fs.mkdirs(item.path)) { throw new PathIOException(item.toString()); } diff --git a/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml b/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml index b1ce87e888..ade6e1a04b 100644 --- a/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml +++ b/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml @@ -526,7 +526,11 @@ RegexpComparator - ^-mkdir <path> \.\.\.:( |\t)*Create a directory in specified location.( )* + ^-mkdir \[-p\] <path> \.\.\.:( |\t)*Create a directory in specified location.( )* + + + TokenComparator + -p Do not fail if the directory already exists