浏览代码

HDFS-4784. NPE in FSDirectory.resolvePath(). Contributed by Brandon Li.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1478276 13f79535-47bb-0310-9956-ffa450edef68
Suresh Srinivas 12 年之前
父节点
当前提交
a4bae51b7d

+ 2 - 0
hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt

@@ -273,6 +273,8 @@ Trunk (Unreleased)
     HDFS-4785. Concat operation does not remove concatenated files from
     HDFS-4785. Concat operation does not remove concatenated files from
     InodeMap. (suresh)
     InodeMap. (suresh)
 
 
+    HDFS-4784. NPE in FSDirectory.resolvePath(). (Brandon Li via suresh)
+
   BREAKDOWN OF HADOOP-8562 and HDFS-3602 SUBTASKS AND RELATED JIRAS
   BREAKDOWN OF HADOOP-8562 and HDFS-3602 SUBTASKS AND RELATED JIRAS
 
 
     HDFS-4145. Merge hdfs cmd line scripts from branch-1-win. (David Lao,
     HDFS-4145. Merge hdfs cmd line scripts from branch-1-win. (David Lao,

+ 10 - 6
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirectory.java

@@ -1090,7 +1090,7 @@ public class FSDirectory implements Closeable {
       NameNode.stateChangeLog.debug("DIR* FSDirectory.unprotectedDelete: "
       NameNode.stateChangeLog.debug("DIR* FSDirectory.unprotectedDelete: "
           +src+" is removed");
           +src+" is removed");
     }
     }
-    remvoedAllFromInodesFromMap(targetNode);
+    removeAllFromInodesFromMap(targetNode);
     return filesRemoved;
     return filesRemoved;
   }
   }
   
   
@@ -1783,14 +1783,14 @@ public class FSDirectory implements Closeable {
   }
   }
   
   
   /** Remove all the inodes under given inode from the map */
   /** Remove all the inodes under given inode from the map */
-  private void remvoedAllFromInodesFromMap(INode inode) {
+  private void removeAllFromInodesFromMap(INode inode) {
     removeFromInodeMap(inode);
     removeFromInodeMap(inode);
     if (!inode.isDirectory()) {
     if (!inode.isDirectory()) {
       return;
       return;
     }
     }
     INodeDirectory dir = (INodeDirectory) inode;
     INodeDirectory dir = (INodeDirectory) inode;
     for (INode child : dir.getChildrenList()) {
     for (INode child : dir.getChildrenList()) {
-      remvoedAllFromInodesFromMap(child);
+      removeAllFromInodesFromMap(child);
     }
     }
     dir.clearChildren();
     dir.clearChildren();
   }
   }
@@ -2258,14 +2258,18 @@ public class FSDirectory implements Closeable {
     try {
     try {
       id = Long.valueOf(inodeId);
       id = Long.valueOf(inodeId);
     } catch (NumberFormatException e) {
     } catch (NumberFormatException e) {
-      throw new FileNotFoundException(
-          "File for given inode path does not exist: " + src);
+      throw new FileNotFoundException("Invalid inode path: " + src);
     }
     }
     if (id == INodeId.ROOT_INODE_ID && pathComponents.length == 4) {
     if (id == INodeId.ROOT_INODE_ID && pathComponents.length == 4) {
       return Path.SEPARATOR;
       return Path.SEPARATOR;
     }
     }
+    INode inode = fsd.getInode(id);
+    if (inode == null) {
+      throw new FileNotFoundException(
+          "File for given inode path does not exist: " + src);
+    }
     StringBuilder path = id == INodeId.ROOT_INODE_ID ? new StringBuilder()
     StringBuilder path = id == INodeId.ROOT_INODE_ID ? new StringBuilder()
-        : new StringBuilder(fsd.getInode(id).getFullPathName());
+        : new StringBuilder(inode.getFullPathName());
     for (int i = 4; i < pathComponents.length; i++) {
     for (int i = 4; i < pathComponents.length; i++) {
       path.append(Path.SEPARATOR).append(DFSUtil.bytes2String(pathComponents[i]));
       path.append(Path.SEPARATOR).append(DFSUtil.bytes2String(pathComponents[i]));
     }
     }

+ 11 - 0
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestINodeFile.java

@@ -909,6 +909,17 @@ public class TestINodeFile {
     components = INode.getPathComponents(testPath);
     components = INode.getPathComponents(testPath);
     resolvedPath = FSDirectory.resolvePath(testPath, components, fsd);
     resolvedPath = FSDirectory.resolvePath(testPath, components, fsd);
     assertEquals(testPath, resolvedPath);
     assertEquals(testPath, resolvedPath);
+    
+    // Test path with nonexistent(deleted or wrong id) inode
+    Mockito.doReturn(null).when(fsd).getInode(Mockito.anyLong());
+    testPath = "/.reserved/.inodes/1234";
+    components = INode.getPathComponents(testPath);
+    try {
+      String realPath = FSDirectory.resolvePath(testPath, components, fsd);
+      fail("Path should not be resolved:" + realPath);
+    } catch (IOException e) {
+      assertTrue(e instanceof FileNotFoundException);
+    }
   }
   }
   
   
   /**
   /**