Browse Source

HADOOP-3239. getFileInfo() returns null for non-existing files instead of throwing FileNotFoundException. Contributed by Lohit Vijayarenu.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/branches/branch-0.17@648049 13f79535-47bb-0310-9956-ffa450edef68
Konstantin Shvachko 17 năm trước cách đây
mục cha
commit
ee1d483706

+ 3 - 0
CHANGES.txt

@@ -86,6 +86,9 @@ Release 0.17.0 - Unreleased
     HADOOP-2826. Deprecated FileSplit.getFile(), LineRecordReader.readLine().
     (Amareshwari Sriramadasu via ddas)
 
+    HADOOP-3239. getFileInfo() returns null for non-existing files instead
+    of throwing FileNotFoundException. (Lohit Vijayarenu via shv)
+
   NEW FEATURES
 
     HADOOP-1398.  Add HBase in-memory block cache.  (tomwhite)

+ 4 - 3
src/java/org/apache/hadoop/dfs/ClientProtocol.java

@@ -36,9 +36,9 @@ interface ClientProtocol extends VersionedProtocol {
    * Compared to the previous version the following changes have been introduced:
    * (Only the latest change is reflected.
    * The log of historical changes can be retrieved from the svn).
-   * 27 : removed getContentLength(String), open(String, long, long) and isDir(String)
+   * 29 : getFileInfo returns null instead of throwing FileNotFoundException
    */
-  public static final long versionID = 27L;
+  public static final long versionID = 29L;
   
   ///////////////////////////////////////
   // File contents
@@ -418,8 +418,9 @@ interface ClientProtocol extends VersionedProtocol {
   /**
    * Get the file info for a specific file or directory.
    * @param src The string representation of the path to the file
-   * @throws IOException if file does not exist
+   * @throws IOException if permission to access file is denied by the system 
    * @return object containing information regarding the file
+   *         or null if file not found
    */
   public DFSFileInfo getFileInfo(String src) throws IOException;
 

+ 7 - 12
src/java/org/apache/hadoop/dfs/DFSClient.java

@@ -542,21 +542,17 @@ class DFSClient implements FSConstants {
    */
   public boolean exists(String src) throws IOException {
     checkOpen();
-    try {
-      return getFileInfo(src) != null;
-    } catch (FileNotFoundException e) {
-      return false;
-    }
+    return getFileInfo(src) != null;
   }
 
   /** @deprecated Use getFileStatus() instead */
   @Deprecated
   public boolean isDirectory(String src) throws IOException {
-    try {
-      return getFileInfo(src).isDir();
-    } catch (FileNotFoundException e) {
-      return false;               // f does not exist
-    }
+    FileStatus fs = getFileInfo(src);
+    if (fs != null)
+      return fs.isDir();
+    else
+      throw new FileNotFoundException("File does not exist: " + src);
   }
 
   /**
@@ -575,8 +571,7 @@ class DFSClient implements FSConstants {
     try {
       return namenode.getFileInfo(src);
     } catch(RemoteException re) {
-      throw re.unwrapRemoteException(AccessControlException.class,
-                                     FileNotFoundException.class);
+      throw re.unwrapRemoteException(AccessControlException.class);
     }
   }
 

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

@@ -362,8 +362,11 @@ public class DistributedFileSystem extends FileSystem {
       DfsPath p = (DfsPath) f;
       return p.info;
     }
-    
-    return dfs.getFileInfo(getPathName(f));
+    FileStatus fs = dfs.getFileInfo(getPathName(f));
+    if (fs != null)
+      return fs;
+    else
+      throw new FileNotFoundException("File does not exist: " + f);
   }
 
   /** {@inheritDoc }*/

+ 4 - 4
src/java/org/apache/hadoop/dfs/FSDirectory.java

@@ -565,17 +565,17 @@ class FSDirectory implements FSConstants {
     }
   }
 
-  /* Get the file info for a specific file.
+  /** Get the file info for a specific file.
    * @param src The string representation of the path to the file
-   * @throws IOException if file does not exist
    * @return object containing information regarding the file
+   *         or null if file not found
    */
-  DFSFileInfo getFileInfo(String src) throws FileNotFoundException {
+  DFSFileInfo getFileInfo(String src) {
     String srcs = normalizePath(src);
     synchronized (rootDir) {
       INode targetNode = rootDir.getNode(srcs);
       if (targetNode == null) {
-        throw new FileNotFoundException("File does not exist: " + srcs);
+        return null;
       }
       else {
         return new DFSFileInfo(srcs, targetNode);

+ 4 - 9
src/java/org/apache/hadoop/dfs/FSNamesystem.java

@@ -1543,20 +1543,15 @@ class FSNamesystem implements FSConstants, FSNamesystemMBean {
    * Return whether the given filename exists
    */
   @Deprecated
-  public boolean exists(String src) throws AccessControlException {
-    try {
-      getFileInfo(src);
-      return true;
-    } catch (IOException e) {
-      //getFileInfo throws IOException for file not found
-      return false;
-    }
+  public boolean exists(String src) throws IOException {
+      return getFileInfo(src) != null;
   }
 
   /** Get the file info for a specific file.
    * @param src The string representation of the path to the file
-   * @throws IOException if file does not exist
+   * @throws IOException if permission to access file is denied by the system 
    * @return object containing information regarding the file
+   *         or null if file not found
    */
   DFSFileInfo getFileInfo(String src) throws IOException {
     if (isPermissionEnabled) {

+ 3 - 1
src/java/org/apache/hadoop/dfs/FileDataServlet.java

@@ -85,9 +85,11 @@ public class FileDataServlet extends DfsServlet {
       final String path = request.getPathInfo() != null
         ? request.getPathInfo() : "/";
       DFSFileInfo info = nnproxy.getFileInfo(path);
-      if (!info.isDir()) {
+      if ((info != null) && !info.isDir()) {
         response.sendRedirect(createUri(info, ugi, nnproxy,
               request.getScheme()).toURL().toString());
+      } else if (info == null){
+        response.sendError(400, "cat: File not found " + path);
       } else {
         response.sendError(400, "cat: " + path + ": is a directory");
       }

+ 1 - 1
src/java/org/apache/hadoop/dfs/ListPathsServlet.java

@@ -134,7 +134,7 @@ public class ListPathsServlet extends DfsServlet {
       }
 
       DFSFileInfo base = nnproxy.getFileInfo(path);
-      if (base.isDir()) {
+      if ((base != null) && base.isDir()) {
         writeInfo(base, doc);
       }
 

+ 3 - 7
src/java/org/apache/hadoop/dfs/NameNode.java

@@ -386,12 +386,7 @@ public class NameNode implements ClientProtocol, DatanodeProtocol,
    */
   @Deprecated
   public boolean exists(String src) throws IOException {
-    try {
-      getFileInfo(src);
-      return true;
-    } catch (FileNotFoundException e) {
-      return false;
-    }
+    return getFileInfo(src) != null;
   }
 
   /**
@@ -437,8 +432,9 @@ public class NameNode implements ClientProtocol, DatanodeProtocol,
   /**
    * Get the file info for a specific file.
    * @param src The string representation of the path to the file
-   * @throws IOException if file does not exist
+   * @throws IOException if permission to access file is denied by the system
    * @return object containing information regarding the file
+   *         or null if file not found
    */
   public DFSFileInfo getFileInfo(String src)  throws IOException {
     return namesystem.getFileInfo(src);

+ 5 - 0
src/test/org/apache/hadoop/dfs/TestFileStatus.java

@@ -82,6 +82,7 @@ public class TestFileStatus extends TestCase {
     Configuration conf = new Configuration();
     MiniDFSCluster cluster = new MiniDFSCluster(conf, 1, true, null);
     FileSystem fs = cluster.getFileSystem();
+    DFSClient dfsClient = new DFSClient(conf);
     try {
 
       //
@@ -94,6 +95,10 @@ public class TestFileStatus extends TestCase {
       assertTrue("/ should be a directory", 
                  fs.getFileStatus(path).isDir() == true);
       
+      // make sure getFileInfo returns null for files which do not exist
+      DFSFileInfo fileInfo = dfsClient.getFileInfo("/noSuchFile");
+      assertTrue(fileInfo == null);
+
       // create a file in home directory
       //
       Path file1 = new Path("filestatus.dat");