浏览代码

HADOOP-2190. bring behaviour '-ls' and '-du' closer to Linux shell
commands in case of errors. (Mahadev Konar via rangadi)


git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@627593 13f79535-47bb-0310-9956-ffa450edef68

Raghu Angadi 17 年之前
父节点
当前提交
ec1d3cda32
共有 3 个文件被更改,包括 43 次插入5 次删除
  1. 3 0
      CHANGES.txt
  2. 13 5
      src/java/org/apache/hadoop/fs/FsShell.java
  3. 27 0
      src/test/org/apache/hadoop/dfs/TestDFSShell.java

+ 3 - 0
CHANGES.txt

@@ -15,6 +15,9 @@ Trunk (unreleased changes)
 
   BUG FIXES
 
+    HADOOP-2190. bring behaviour '-ls' and '-du' closer to Linux shell 
+                 commands in case of errors. (Mahadev Konar via rangadi)
+    
     HADOOP-2193. 'fs -rm' and 'fs -rmr' show error message when the target
     file does not exist. (Mahadev Konar via rangadi)
             

+ 13 - 5
src/java/org/apache/hadoop/fs/FsShell.java

@@ -568,10 +568,15 @@ public class FsShell extends Configured implements Tool {
   void ls(String srcf, boolean recursive) throws IOException {
     Path srcPath = new Path(srcf);
     FileSystem srcFs = srcPath.getFileSystem(this.getConf());
-    Path[] srcs = srcFs.globPaths(srcPath);
+    FileStatus[] srcs = srcFs.globStatus(srcPath);
+    if (srcs==null || srcs.length==0) {
+      throw new FileNotFoundException("Cannot access " + srcf + 
+          ": No such file or directory.");
+    }
+ 
     boolean printHeader = (srcs.length == 1) ? true: false;
     for(int i=0; i<srcs.length; i++) {
-      ls(srcs[i], srcFs, recursive, printHeader);
+      ls(srcs[i].getPath(), srcFs, recursive, printHeader);
     }
   }
 
@@ -580,11 +585,14 @@ public class FsShell extends Configured implements Tool {
    */
   private void ls(Path src, FileSystem srcFs, boolean recursive, boolean printHeader) throws IOException {
     FileStatus items[] = srcFs.listStatus(src);
-    if (items == null) {
-      throw new IOException("Could not get listing for " + src);
+    if ((items == null) || ((items.length == 0) 
+        && (!srcFs.exists(src)))) {
+      throw new FileNotFoundException(src + ": No such file or directory.");
     } else {
       if (!recursive && printHeader) {
-        System.out.println("Found " + items.length + " items");
+        if (items.length != 0) {
+          System.out.println("Found " + items.length + " items");
+        }
       }
       for (int i = 0; i < items.length; i++) {
         FileStatus stat = items[i];

+ 27 - 0
src/test/org/apache/hadoop/dfs/TestDFSShell.java

@@ -182,6 +182,7 @@ public class TestDFSShell extends TestCase {
     PrintStream bak = null;
     try {
       cluster = new MiniDFSCluster(conf, 2, true, null);
+      FileSystem srcFs = cluster.getFileSystem();
       Path root = new Path("/nonexistentfile");
       bak = System.err;
       ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -228,6 +229,32 @@ public class TestDFSShell extends TestCase {
       assertTrue(" -dus prints reasonable error",
           (returned.lastIndexOf("No such file or directory") != -1));
       out.reset();
+      argv[0] = "-ls";
+      argv[1] = "/nonexistenfile";
+      ret = ToolRunner.run(shell, argv);
+      returned = out.toString();
+      assertTrue(" -ls does not return Found 0 items",
+          (returned.lastIndexOf("Found 0") == -1));
+      out.reset();
+      argv[0] = "-ls";
+      argv[1] = "/nonexistentfile";
+      ret = ToolRunner.run(shell, argv);
+      assertTrue(" -lsr should fail ",
+          (ret < 0));
+      out.reset();
+      srcFs.mkdirs(new Path("/testdir"));
+      argv[0] = "-ls";
+      argv[1] = "/testdir";
+      ret = ToolRunner.run(shell, argv);
+      returned = out.toString();
+      assertTrue(" -ls does not print out anything ",
+          (returned.lastIndexOf("Found 0") == -1));
+      out.reset();
+      argv[0] = "-ls";
+      argv[1] = "/user/nonxistant/*";
+      ret = ToolRunner.run(shell, argv);
+      assertTrue(" -ls on nonexistent glob returns -1",
+          (ret < 0));
     } finally {
       if (bak != null) {
         System.setErr(bak);