Przeglądaj źródła

HADOOP-7378. Add -d option to ls to not expand directories. Contributed by Daryn Sharp.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1151594 13f79535-47bb-0310-9956-ffa450edef68
Suresh Srinivas 14 lat temu
rodzic
commit
0850205088

+ 3 - 0
common/CHANGES.txt

@@ -289,6 +289,9 @@ Trunk (unreleased changes)
     HADOOP-7485. Add -h option to ls to list file sizes in human readable
     format. (XieXianshan via suresh)
 
+    HADOOP-7378. Add -d option to ls to not expand directories.
+    (Daryn Sharp via suresh)
+
   OPTIMIZATIONS
   
     HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole

+ 6 - 5
common/src/docs/src/documentation/content/xdocs/file_system_shell.xml

@@ -273,7 +273,7 @@
        <section>
            <title>ls</title>
            <p>
-               <code>Usage: hdfs dfs -ls [-R] [-h] &lt;args&gt;</code>
+               <code>Usage: hdfs dfs -ls [-d] [-h] [-R] &lt;args&gt;</code>
            </p>
            <p>For a file returns stat on the file with the following format:</p>
            <p>
@@ -284,10 +284,11 @@
                <code>permissions userid groupid modification_date modification_time dirname</code>
            </p>
            <p>Options:</p>
-           <ul>
-             <li>The <code>-R</code> option will list subdirectories recursively.</li>
-             <li>The <code>-h</code> option will format file sizes in a &quot;human-readable&quot; fashion (e.g 64.0m instead of 67108864)</li>
-           </ul>
+             <ul>
+               <li><code>-d</code>  Directories are listed as plain files</li>
+               <li><code>-h</code>  Format file sizes in a &quot;human-readable&quot; fashion (e.g 64.0m instead of 67108864)</li>
+               <li><code>-R</code>  Recursively list subdirectories encountered</li>
+             </ul>
            <p>Example:</p>
            <p>
                <code>hdfs dfs -ls /user/hadoop/file1 </code>

+ 17 - 10
common/src/java/org/apache/hadoop/fs/shell/Ls.java

@@ -42,7 +42,7 @@ class Ls extends FsCommand {
   }
   
   public static final String NAME = "ls";
-  public static final String USAGE = "[-R] [-h] [<path> ...]";
+  public static final String USAGE = "[-d] [-h] [-R] [<path> ...]";
   public static final String DESCRIPTION =
     "List the contents that match the specified file pattern. If\n" + 
     "path is not specified, the contents of /user/<currentUser>\n" +
@@ -52,15 +52,17 @@ class Ls extends FsCommand {
     "\tfileName(full path) <r n> size \n" +
     "where n is the number of replicas specified for the file \n" + 
     "and size is the size of the file, in bytes.\n" +
-    "  -R  Recursively list the contents of directories.\n" +
+    "  -d  Directories are listed as plain files.\n" +
     "  -h  Formats the sizes of files in a human-readable fashion\n" +
-    "      rather than of bytes.\n\n";
+    "      rather than a number of bytes.\n" +
+    "  -R  Recursively list the contents of directories.";
 
   protected static final SimpleDateFormat dateFormat = 
     new SimpleDateFormat("yyyy-MM-dd HH:mm");
 
   protected int maxRepl = 3, maxLen = 10, maxOwner = 0, maxGroup = 0;
   protected String lineFormat;
+  protected boolean dirRecurse;
 
   protected boolean humanReadable = false;
   protected String formatSize(long size) {
@@ -72,22 +74,27 @@ class Ls extends FsCommand {
   @Override
   protected void processOptions(LinkedList<String> args)
   throws IOException {
-    CommandFormat cf = new CommandFormat(0, Integer.MAX_VALUE, "R", "h");
+    CommandFormat cf = new CommandFormat(0, Integer.MAX_VALUE, "d", "h", "R");
     cf.parse(args);
-    setRecursive(cf.getOpt("R"));
+    dirRecurse = !cf.getOpt("d");
+    setRecursive(cf.getOpt("R") && dirRecurse);
     humanReadable = cf.getOpt("h");
     if (args.isEmpty()) args.add(Path.CUR_DIR);
   }
 
   @Override
-  protected void processPaths(PathData parent, PathData ... items)
-  throws IOException {
+  protected void processPathArgument(PathData item) throws IOException {
     // implicitly recurse once for cmdline directories
-    if (parent == null && items[0].stat.isDirectory()) {
-      recursePath(items[0]);
-      return;
+    if (dirRecurse && item.stat.isDirectory()) {
+      recursePath(item);
+    } else {
+      super.processPathArgument(item);
     }
+  }
 
+  @Override
+  protected void processPaths(PathData parent, PathData ... items)
+  throws IOException {
     if (!isRecursive() && items.length != 0) {
       out.println("Found " + items.length + " items");
     }

+ 8 - 4
common/src/test/core/org/apache/hadoop/cli/testConf.xml

@@ -54,7 +54,7 @@
       <comparators>
         <comparator>
           <type>RegexpComparator</type>
-          <expected-output>^-ls \[-R\] \[-h\] \[&lt;path&gt; \.\.\.\]:( |\t)*List the contents that match the specified file pattern. If( )*</expected-output>
+          <expected-output>^-ls \[-d\] \[-h\] \[-R\] \[&lt;path&gt; \.\.\.\]:( |\t)*List the contents that match the specified file pattern. If( )*</expected-output>
         </comparator>
         <comparator>
           <type>RegexpComparator</type>
@@ -90,15 +90,19 @@
         </comparator>
         <comparator>
           <type>RegexpComparator</type>
-          <expected-output>^( |\t)*-R  Recursively list the contents of directories.( )*</expected-output>
+          <expected-output>^( |\t)*-d\s+Directories are listed as plain files\.</expected-output>
         </comparator>
         <comparator>
           <type>RegexpComparator</type>
-          <expected-output>^( |\t)*-h  Formats the sizes of files in a human-readable fashion( )*</expected-output>
+          <expected-output>^( |\t)*-h\s+Formats the sizes of files in a human-readable fashion( )*</expected-output>
         </comparator>
         <comparator>
           <type>RegexpComparator</type>
-          <expected-output>^( |\t)*rather than a number of bytes.( )*</expected-output>
+          <expected-output>^( |\t)*rather than a number of bytes\.( )*</expected-output>
+        </comparator>
+        <comparator>
+          <type>RegexpComparator</type>
+          <expected-output>^( |\t)*-R\s+Recursively list the contents of directories\.</expected-output>
         </comparator>
       </comparators>
     </test>