Browse Source

HADOOP-3108. Fix NPE in setPermission and setOwner. Contributed by Konstantin Shvachko.

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

+ 2 - 0
CHANGES.txt

@@ -1257,6 +1257,8 @@ Release 0.16.0 - 2008-02-07
     HADOOP-2768. Fix performance regression caused by HADOOP-1707.
     (dhruba borthakur via nigel)
 
+    HADOOP-3108. Fix NPE in setPermission and setOwner. (shv)
+
 Release 0.15.3 - 2008-01-18
 
   BUG FIXES

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

@@ -396,7 +396,13 @@ public class DistributedFileSystem extends FileSystem {
   /** {@inheritDoc }*/
   public void setPermission(Path p, FsPermission permission
       ) throws IOException {
-    dfs.namenode.setPermission(getPathName(p), permission);
+    try {
+      dfs.namenode.setPermission(getPathName(p), permission);
+    } catch(RemoteException re) {
+      if(FileNotFoundException.class.getName().equals(re.getClassName())) {
+        throw new FileNotFoundException("File does not exist: " + p);
+      }
+    }
   }
 
   /** {@inheritDoc }*/
@@ -405,6 +411,12 @@ public class DistributedFileSystem extends FileSystem {
     if (username == null && groupname == null) {
       throw new IOException("username == null && groupname == null");
     }
-    dfs.namenode.setOwner(getPathName(p), username, groupname);
+    try {
+      dfs.namenode.setOwner(getPathName(p), username, groupname);
+    } catch(RemoteException re) {
+      if(FileNotFoundException.class.getName().equals(re.getClassName())) {
+        throw new FileNotFoundException("File does not exist: " + p);
+      }
+    }
   }
 }

+ 8 - 3
src/java/org/apache/hadoop/dfs/FSDirectory.java

@@ -415,9 +415,12 @@ class FSDirectory implements FSConstants {
     fsImage.getEditLog().logSetPermissions(src, permission);
   }
 
-  void unprotectedSetPermission(String src, FsPermission permissions) {
+  void unprotectedSetPermission(String src, FsPermission permissions) throws FileNotFoundException {
     synchronized(rootDir) {
-      rootDir.getNode(src).setPermission(permissions);
+        INode inode = rootDir.getNode(src);
+        if(inode == null)
+            throw new FileNotFoundException("File does not exist: " + src);
+        inode.setPermission(permissions);
     }
   }
 
@@ -427,9 +430,11 @@ class FSDirectory implements FSConstants {
     fsImage.getEditLog().logSetOwner(src, username, groupname);
   }
 
-  void unprotectedSetOwner(String src, String username, String groupname) {
+  void unprotectedSetOwner(String src, String username, String groupname) throws FileNotFoundException {
     synchronized(rootDir) {
       INode inode = rootDir.getNode(src);
+      if(inode == null)
+          throw new FileNotFoundException("File does not exist: " + src);
       if (username != null) {
         inode.setUser(username);
       }

+ 16 - 0
src/test/org/apache/hadoop/security/TestPermission.java

@@ -119,6 +119,22 @@ public class TestPermission extends TestCase {
     FileSystem fs = FileSystem.get(conf);
 
     try {
+      // test permissions on files that do not exist
+      assertFalse(fs.exists(CHILD_FILE1));
+      try {
+        fs.setOwner(CHILD_FILE1, "foo", "bar");
+        assertTrue(false);
+      }
+      catch(java.io.FileNotFoundException e) {
+        LOG.info("GOOD: got " + e);
+      }
+      try {
+        fs.setPermission(CHILD_FILE1, new FsPermission((short)0777));
+        assertTrue(false);
+      }
+      catch(java.io.FileNotFoundException e) {
+        LOG.info("GOOD: got " + e);
+      }
       // following dir/file creations are legal
       fs.mkdirs(CHILD_DIR1);
       FSDataOutputStream out = fs.create(CHILD_FILE1);