Browse Source

HDFS-10650. DFSClient#mkdirs and DFSClient#primitiveMkdir should use default directory permission. Contributed by John Zhuge.

Xiao Chen 8 years ago
parent
commit
cb5af0012e

+ 10 - 4
hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSClient.java

@@ -1209,6 +1209,13 @@ public class DFSClient implements java.io.Closeable, RemotePeerFactory,
     return permission.applyUMask(dfsClientConf.getUMask());
     return permission.applyUMask(dfsClientConf.getUMask());
   }
   }
 
 
+  private FsPermission applyUMaskDir(FsPermission permission) {
+    if (permission == null) {
+      permission = FsPermission.getDirDefault();
+    }
+    return permission.applyUMask(dfsClientConf.getUMask());
+  }
+
   /**
   /**
    * Same as {@link #create(String, FsPermission, EnumSet, boolean, short, long,
    * Same as {@link #create(String, FsPermission, EnumSet, boolean, short, long,
    * Progressable, int, ChecksumOpt)} with the addition of favoredNodes that is
    * Progressable, int, ChecksumOpt)} with the addition of favoredNodes that is
@@ -2458,7 +2465,7 @@ public class DFSClient implements java.io.Closeable, RemotePeerFactory,
    *
    *
    * @param src The path of the directory being created
    * @param src The path of the directory being created
    * @param permission The permission of the directory being created.
    * @param permission The permission of the directory being created.
-   * If permission == null, use {@link FsPermission#getDefault()}.
+   * If permission == null, use {@link FsPermission#getDirDefault()}.
    * @param createParent create missing parent directory if true
    * @param createParent create missing parent directory if true
    *
    *
    * @return True if the operation success.
    * @return True if the operation success.
@@ -2467,7 +2474,7 @@ public class DFSClient implements java.io.Closeable, RemotePeerFactory,
    */
    */
   public boolean mkdirs(String src, FsPermission permission,
   public boolean mkdirs(String src, FsPermission permission,
       boolean createParent) throws IOException {
       boolean createParent) throws IOException {
-    final FsPermission masked = applyUMask(permission);
+    final FsPermission masked = applyUMaskDir(permission);
     return primitiveMkdir(src, masked, createParent);
     return primitiveMkdir(src, masked, createParent);
   }
   }
 
 
@@ -2488,9 +2495,8 @@ public class DFSClient implements java.io.Closeable, RemotePeerFactory,
       boolean createParent) throws IOException {
       boolean createParent) throws IOException {
     checkOpen();
     checkOpen();
     if (absPermission == null) {
     if (absPermission == null) {
-      absPermission = applyUMask(null);
+      absPermission = applyUMaskDir(null);
     }
     }
-
     LOG.debug("{}: masked={}", src, absPermission);
     LOG.debug("{}: masked={}", src, absPermission);
     try (TraceScope ignored = tracer.newScope("mkdir")) {
     try (TraceScope ignored = tracer.newScope("mkdir")) {
       return namenode.mkdirs(src, absPermission, createParent);
       return namenode.mkdirs(src, absPermission, createParent);

+ 10 - 0
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/security/TestPermission.java

@@ -53,6 +53,7 @@ public class TestPermission {
   final private static Path ROOT_PATH = new Path("/data");
   final private static Path ROOT_PATH = new Path("/data");
   final private static Path CHILD_DIR1 = new Path(ROOT_PATH, "child1");
   final private static Path CHILD_DIR1 = new Path(ROOT_PATH, "child1");
   final private static Path CHILD_DIR2 = new Path(ROOT_PATH, "child2");
   final private static Path CHILD_DIR2 = new Path(ROOT_PATH, "child2");
+  final private static Path CHILD_DIR3 = new Path(ROOT_PATH, "child3");
   final private static Path CHILD_FILE1 = new Path(ROOT_PATH, "file1");
   final private static Path CHILD_FILE1 = new Path(ROOT_PATH, "file1");
   final private static Path CHILD_FILE2 = new Path(ROOT_PATH, "file2");
   final private static Path CHILD_FILE2 = new Path(ROOT_PATH, "file2");
   final private static Path CHILD_FILE3 = new Path(ROOT_PATH, "file3");
   final private static Path CHILD_FILE3 = new Path(ROOT_PATH, "file3");
@@ -237,6 +238,9 @@ public class TestPermission {
       
       
       // following dir/file creations are legal
       // following dir/file creations are legal
       nnfs.mkdirs(CHILD_DIR1);
       nnfs.mkdirs(CHILD_DIR1);
+      status = nnfs.getFileStatus(CHILD_DIR1);
+      assertThat("Expect 755 = 777 (default dir) - 022 (default umask)",
+          status.getPermission().toString(), is("rwxr-xr-x"));
       out = nnfs.create(CHILD_FILE1);
       out = nnfs.create(CHILD_FILE1);
       status = nnfs.getFileStatus(CHILD_FILE1);
       status = nnfs.getFileStatus(CHILD_FILE1);
       assertTrue(status.getPermission().toString().equals("rw-r--r--"));
       assertTrue(status.getPermission().toString().equals("rw-r--r--"));
@@ -248,6 +252,12 @@ public class TestPermission {
       status = nnfs.getFileStatus(CHILD_FILE1);
       status = nnfs.getFileStatus(CHILD_FILE1);
       assertTrue(status.getPermission().toString().equals("rwx------"));
       assertTrue(status.getPermission().toString().equals("rwx------"));
 
 
+      // mkdirs with null permission
+      nnfs.mkdirs(CHILD_DIR3, null);
+      status = nnfs.getFileStatus(CHILD_DIR3);
+      assertThat("Expect 755 = 777 (default dir) - 022 (default umask)",
+          status.getPermission().toString(), is("rwxr-xr-x"));
+
       // following read is legal
       // following read is legal
       byte dataIn[] = new byte[FILE_LEN];
       byte dataIn[] = new byte[FILE_LEN];
       FSDataInputStream fin = nnfs.open(CHILD_FILE1);
       FSDataInputStream fin = nnfs.open(CHILD_FILE1);