You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by to...@apache.org on 2011/05/13 20:49:17 UTC

svn commit: r1102861 - in /hadoop/common/trunk: CHANGES.txt src/java/org/apache/hadoop/fs/FsShell.java src/java/org/apache/hadoop/fs/shell/FsCommand.java src/java/org/apache/hadoop/fs/shell/Test.java src/test/core/org/apache/hadoop/cli/testConf.xml

Author: todd
Date: Fri May 13 18:49:16 2011
New Revision: 1102861

URL: http://svn.apache.org/viewvc?rev=1102861&view=rev
Log:
HADOOP-7285. Refactor the test command to conform to new FsCommand class. Contributed by Daryn Sharp.

Added:
    hadoop/common/trunk/src/java/org/apache/hadoop/fs/shell/Test.java
Modified:
    hadoop/common/trunk/CHANGES.txt
    hadoop/common/trunk/src/java/org/apache/hadoop/fs/FsShell.java
    hadoop/common/trunk/src/java/org/apache/hadoop/fs/shell/FsCommand.java
    hadoop/common/trunk/src/test/core/org/apache/hadoop/cli/testConf.xml

Modified: hadoop/common/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/CHANGES.txt?rev=1102861&r1=1102860&r2=1102861&view=diff
==============================================================================
--- hadoop/common/trunk/CHANGES.txt (original)
+++ hadoop/common/trunk/CHANGES.txt Fri May 13 18:49:16 2011
@@ -150,6 +150,9 @@ Trunk (unreleased changes)
     HADOOP-7267. Refactor the rm/rmr/expunge commands to conform to new
     FsCommand class.  (Daryn Sharp via szetszwo)
 
+    HADOOP-7285. Refactor the test command to conform to new FsCommand
+    class. (Daryn Sharp via todd)
+
   OPTIMIZATIONS
 
   BUG FIXES

Modified: hadoop/common/trunk/src/java/org/apache/hadoop/fs/FsShell.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/java/org/apache/hadoop/fs/FsShell.java?rev=1102861&r1=1102860&r2=1102861&view=diff
==============================================================================
--- hadoop/common/trunk/src/java/org/apache/hadoop/fs/FsShell.java (original)
+++ hadoop/common/trunk/src/java/org/apache/hadoop/fs/FsShell.java Fri May 13 18:49:16 2011
@@ -433,32 +433,6 @@ public class FsShell extends Configured 
   }
 
   /**
-   * Check file types.
-   */
-  int test(String argv[], int i) throws IOException {
-    if (!argv[i].startsWith("-") || argv[i].length() > 2)
-      throw new IOException("Not a flag: " + argv[i]);
-    char flag = argv[i].toCharArray()[1];
-    PathData item = new PathData(argv[++i], getConf());
-    
-    if ((flag != 'e') && !item.exists) { 
-      // TODO: it's backwards compat, but why is this throwing an exception?
-      // it's not like the shell test cmd
-      throw new PathNotFoundException(item.toString());
-    }
-    switch(flag) {
-      case 'e':
-        return item.exists ? 0 : 1;
-      case 'z':
-        return (item.stat.getLen() == 0) ? 0 : 1;
-      case 'd':
-        return item.stat.isDirectory() ? 0 : 1;
-      default:
-        throw new IOException("Unknown flag: " + flag);
-    }
-  }
-
-  /**
    * Move files that match the file pattern <i>srcf</i>
    * to a destination file.
    * When moving mutiple files, the destination must be a directory. 
@@ -671,8 +645,7 @@ public class FsShell extends Configured 
       "[-moveFromLocal <localsrc> ... <dst>] [" + 
       GET_SHORT_USAGE + "\n\t" +
       "[" + COPYTOLOCAL_SHORT_USAGE + "] [-moveToLocal <src> <localdst>]\n\t" +
-      "[-report]\n\t" +
-      "[-test -[ezd] <path>]";
+      "[-report]";
 
     String conf ="-conf <configuration file>:  Specify an application configuration file.";
  
@@ -735,9 +708,6 @@ public class FsShell extends Configured 
 
     String moveToLocal = "-moveToLocal <src> <localdst>:  Not implemented yet \n";
         
-    String test = "-test -[ezd] <path>: If file { exists, has zero length, is a directory\n" +
-      "\t\tthen return 0, else return 1.\n";
-
     String help = "-help [cmd]: \tDisplays help for given command or all commands if none\n" +
       "\t\tis specified.\n";
 
@@ -774,8 +744,6 @@ public class FsShell extends Configured 
       System.out.println(moveToLocal);
     } else if ("get".equals(cmd)) {
       System.out.println(get);
-    } else if ("test".equals(cmd)) {
-      System.out.println(test);
     } else if ("help".equals(cmd)) {
       System.out.println(help);
     } else {
@@ -798,7 +766,6 @@ public class FsShell extends Configured 
       System.out.println(get);
       System.out.println(copyToLocal);
       System.out.println(moveToLocal);
-      System.out.println(test);
 
       for (String thisCmdName : commandFactory.getNames()) {
         printHelp(commandFactory.getInstance(thisCmdName));
@@ -891,9 +858,6 @@ public class FsShell extends Configured 
     } else if ("-moveToLocal".equals(cmd)) {
       System.err.println("Usage: java FsShell" + 
                          " [" + cmd + " [-crc] <src> <localdst>]");
-    } else if ("-test".equals(cmd)) {
-      System.err.println("Usage: java FsShell" +
-                         " [-test -[ezd] <path>]");
     } else {
       System.err.println("Usage: java FsShell");
       System.err.println("           [-df [<path>]]");
@@ -907,7 +871,6 @@ public class FsShell extends Configured 
       System.err.println("           [" + GET_SHORT_USAGE + "]");
       System.err.println("           [" + COPYTOLOCAL_SHORT_USAGE + "]");
       System.err.println("           [-moveToLocal [-crc] <src> <localdst>]");
-      System.err.println("           [-test -[ezd] <path>]");
       for (String name : commandFactory.getNames()) {
       	instance = commandFactory.getInstance(name);
         System.err.println("           [" + instance.getUsage() + "]");
@@ -939,7 +902,7 @@ public class FsShell extends Configured 
     //
     // verify that we have enough command line parameters
     //
-    if ("-put".equals(cmd) || "-test".equals(cmd) ||
+    if ("-put".equals(cmd) ||
         "-copyFromLocal".equals(cmd) || "-moveFromLocal".equals(cmd)) {
       if (argv.length < 3) {
         printUsage(cmd);
@@ -1014,8 +977,6 @@ public class FsShell extends Configured 
         du(argv, i);
       } else if ("-dus".equals(cmd)) {
         dus(argv, i);
-      } else if ("-test".equals(cmd)) {
-        exitCode = test(argv, i);
       } else if ("-help".equals(cmd)) {
         if (i < argv.length) {
           printHelp(argv[i]);
@@ -1082,30 +1043,7 @@ public class FsShell extends Configured 
     }
     System.exit(res);
   }
-
-  /**
-   * Accumulate exceptions if there is any.  Throw them at last.
-   */
-  private abstract class DelayedExceptionThrowing {
-    abstract void process(Path p, FileSystem srcFs) throws IOException;
-
-    final void globAndProcess(Path srcPattern, FileSystem srcFs
-        ) throws IOException {
-      List<IOException> exceptions = new ArrayList<IOException>();
-      for(Path p : FileUtil.stat2Paths(srcFs.globStatus(srcPattern), 
-                                       srcPattern))
-        try { process(p, srcFs); } 
-        catch(IOException ioe) { exceptions.add(ioe); }
-    
-      if (!exceptions.isEmpty())
-        if (exceptions.size() == 1)
-          throw exceptions.get(0);
-        else 
-          throw new IOException("Multiple IOExceptions: " + exceptions);
-    }
-  }
-
-
+  
   /**
    * Utility class for a line of du output
    */

Modified: hadoop/common/trunk/src/java/org/apache/hadoop/fs/shell/FsCommand.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/java/org/apache/hadoop/fs/shell/FsCommand.java?rev=1102861&r1=1102860&r2=1102861&view=diff
==============================================================================
--- hadoop/common/trunk/src/java/org/apache/hadoop/fs/shell/FsCommand.java (original)
+++ hadoop/common/trunk/src/java/org/apache/hadoop/fs/shell/FsCommand.java Fri May 13 18:49:16 2011
@@ -53,6 +53,7 @@ abstract public class FsCommand extends 
     factory.registerCommands(SetReplication.class);
     factory.registerCommands(Stat.class);
     factory.registerCommands(Tail.class);
+    factory.registerCommands(Test.class);
     factory.registerCommands(Touch.class);
   }
 

Added: hadoop/common/trunk/src/java/org/apache/hadoop/fs/shell/Test.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/java/org/apache/hadoop/fs/shell/Test.java?rev=1102861&view=auto
==============================================================================
--- hadoop/common/trunk/src/java/org/apache/hadoop/fs/shell/Test.java (added)
+++ hadoop/common/trunk/src/java/org/apache/hadoop/fs/shell/Test.java Fri May 13 18:49:16 2011
@@ -0,0 +1,87 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.fs.shell;
+
+import java.io.IOException;
+import java.util.LinkedList;
+
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.classification.InterfaceStability;
+import org.apache.hadoop.fs.shell.PathExceptions.PathNotFoundException;
+
+/**
+ * Perform shell-like file tests 
+ */
+@InterfaceAudience.Private
+@InterfaceStability.Unstable
+
+class Test extends FsCommand {  
+  public static void registerCommands(CommandFactory factory) {
+    factory.addClass(Test.class, "-test");
+  }
+
+  public static final String NAME = "test";
+  public static final String USAGE = "-[ezd] <path>";
+  public static final String DESCRIPTION =
+    "If file exists, has zero length, is a directory\n" +
+    "then return 0, else return 1.";
+
+  private char flag;
+  
+  @Override
+  protected void processOptions(LinkedList<String> args) {
+    CommandFormat cf = new CommandFormat(null, 1, 1, "e", "d", "z");
+    cf.parse(args);
+    
+    String[] opts = cf.getOpts().toArray(new String[0]);
+    switch (opts.length) {
+      case 0:
+        throw new IllegalArgumentException("No test flag given");
+      case 1:
+        flag = opts[0].charAt(0);
+        break;
+      default:
+        throw new IllegalArgumentException("Only one test flag is allowed");
+    }
+  }
+
+  @Override
+  protected void processPath(PathData item) throws IOException {
+    boolean test = false;
+    switch (flag) {
+      case 'e':
+        test = true;
+        break;
+      case 'd':
+        test = item.stat.isDirectory();
+        break;
+      case 'z':
+        test = (item.stat.getLen() == 0);
+        break;
+    }
+    if (!test) exitCode = 1;
+  }
+
+  @Override
+  protected void processNonexistentPath(PathData item) throws IOException {
+    // NOTE: errors for FNF is not how the shell works!
+    if (flag != 'e') displayError(new PathNotFoundException(item.toString()));
+    exitCode = 1;
+  }
+}

Modified: hadoop/common/trunk/src/test/core/org/apache/hadoop/cli/testConf.xml
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/test/core/org/apache/hadoop/cli/testConf.xml?rev=1102861&r1=1102860&r2=1102861&view=diff
==============================================================================
--- hadoop/common/trunk/src/test/core/org/apache/hadoop/cli/testConf.xml (original)
+++ hadoop/common/trunk/src/test/core/org/apache/hadoop/cli/testConf.xml Fri May 13 18:49:16 2011
@@ -564,7 +564,7 @@
       <comparators>
         <comparator>
           <type>RegexpComparator</type>
-          <expected-output>^-test -\[ezd\] &lt;path&gt;: If file \{ exists, has zero length, is a directory( )*</expected-output>
+          <expected-output>^-test -\[ezd\] &lt;path&gt;:\s+If file exists, has zero length, is a directory( )*</expected-output>
         </comparator>
         <comparator>
           <type>RegexpComparator</type>