Przeglądaj źródła

HADOOP-2194. dfs cat on a non-existent file throws FileNotFoundException.
(Mahadev Konar via dhruba)



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

Dhruba Borthakur 17 lat temu
rodzic
commit
7d8b4712b2

+ 3 - 0
CHANGES.txt

@@ -26,6 +26,9 @@ Trunk (unreleased changes)
     HADOOP-1188. fstime file is updated when a storage directory containing
     namespace image becomes inaccessible. (shv)
 
+    HADOOP-2194. dfs cat on a non-existent file throws FileNotFoundException.
+    (Mahadev Konar via dhruba)
+
 Release 0.16.0 - 2008-02-07
 
   INCOMPATIBLE CHANGES

+ 15 - 2
src/java/org/apache/hadoop/dfs/DistributedFileSystem.java

@@ -112,7 +112,19 @@ public class DistributedFileSystem extends FileSystem {
   }
 
   public FSDataInputStream open(Path f, int bufferSize) throws IOException {
-    return new DFSClient.DFSDataInputStream(dfs.open(getPathName(f),bufferSize));
+    try {
+      return new DFSClient.DFSDataInputStream(dfs.open(getPathName(f),bufferSize));
+    } catch(RemoteException e) {
+      if (IOException.class.getName().equals(e.getClassName()) &&
+          e.getMessage().startsWith(
+              "java.io.IOException: Cannot open filename")) {
+          // non-existent path
+          FileNotFoundException ne = new FileNotFoundException("File " + f + " does not exist.");
+          throw (FileNotFoundException) ne.initCause(e);
+      } else {
+        throw e;      // unexpected exception
+      }
+    }
   }
 
   public FSDataOutputStream create(Path f, FsPermission permission,
@@ -338,7 +350,8 @@ public class DistributedFileSystem extends FileSystem {
           e.getMessage().startsWith(
               "java.io.IOException: File does not exist: ")) {
         // non-existent path
-        throw new FileNotFoundException("File " + f + " does not exist.");
+        FileNotFoundException fe = new FileNotFoundException("File " + f + " does not exist.");
+        throw (FileNotFoundException) fe.initCause(e); 
       } else {
         throw e;      // unexpected exception
       }

+ 1 - 1
src/java/org/apache/hadoop/fs/FsShell.java

@@ -300,7 +300,7 @@ public class FsShell extends Configured implements Tool {
     new DelayedExceptionThrowing() {
       @Override
       void process(Path p) throws IOException {
-        if (fs.isDirectory(p)) {
+        if (fs.getFileStatus(p).isDir()) {
           throw new IOException("Source must be a file.");
         }
         printToStdout(fs.open(p));

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

@@ -174,6 +174,37 @@ public class TestDFSShell extends TestCase {
     }
   }
 
+
+  /** check if we have any exceptions in cat command output */
+  public void testCatException() throws Exception {
+    Configuration conf = new Configuration();
+    MiniDFSCluster cluster = null;
+    PrintStream bak = null;
+    try {
+      cluster = new MiniDFSCluster(conf, 2, true, null);
+      Path root = new Path("/nonexistentfile");
+      bak = System.err;
+      ByteArrayOutputStream out = new ByteArrayOutputStream();
+      PrintStream tmp = new PrintStream(out);
+      System.setErr(tmp);
+      String[] argv = new String[2];
+      argv[0] = "-cat";
+      argv[1] = root.toUri().getPath();
+      int ret = ToolRunner.run(new FsShell(), argv);
+      assertTrue(" -cat returned -1 ", 0>=ret);
+      String returned = out.toString();
+      assertTrue("cat does not print exceptions ",
+          (returned.lastIndexOf("Exception") == -1));
+    } finally {
+      if (bak != null) {
+        System.setErr(bak);
+      }
+      if (cluster != null) {
+        cluster.shutdown();
+      }
+    }
+  }
+  
   public void testText() throws Exception {
     Configuration conf = new Configuration();
     MiniDFSCluster cluster = null;