/**
   * Tests backward compatibility. Configuration can be either set with old param dfs.umask that
   * takes decimal umasks or dfs.umaskmode that takes symbolic or octal umask.
   */
  public void testBackwardCompatibility() {
    // Test 1 - old configuration key with decimal
    // umask value should be handled when set using
    // FSPermission.setUMask() API
    FsPermission perm = new FsPermission((short) 18);
    Configuration conf = new Configuration();
    FsPermission.setUMask(conf, perm);
    assertEquals(18, FsPermission.getUMask(conf).toShort());

    // Test 2 - old configuration key set with decimal
    // umask value should be handled
    perm = new FsPermission((short) 18);
    conf = new Configuration();
    conf.set(FsPermission.DEPRECATED_UMASK_LABEL, "18");
    assertEquals(18, FsPermission.getUMask(conf).toShort());

    // Test 3 - old configuration key overrides the new one
    conf = new Configuration();
    conf.set(FsPermission.DEPRECATED_UMASK_LABEL, "18");
    conf.set(FsPermission.UMASK_LABEL, "000");
    assertEquals(18, FsPermission.getUMask(conf).toShort());

    // Test 4 - new configuration key is handled
    conf = new Configuration();
    conf.set(FsPermission.UMASK_LABEL, "022");
    assertEquals(18, FsPermission.getUMask(conf).toShort());
  }
 public static boolean canAllRead(FsPermission p) {
   FsAction otherAction = p.getOtherAction();
   switch (otherAction) {
     case ALL:
     case READ:
     case READ_EXECUTE:
     case READ_WRITE:
       return true;
     default:
       return false;
   }
 }
 static FsPermission checkPermission(FileSystem fs, String path, FsPermission expected)
     throws IOException {
   FileStatus s = fs.getFileStatus(new Path(path));
   LOG.info(
       s.getPath()
           + ": "
           + s.isDirectory()
           + " "
           + s.getPermission()
           + ":"
           + s.getOwner()
           + ":"
           + s.getGroup());
   if (expected != null) {
     assertEquals(expected, s.getPermission());
     assertEquals(expected.toShort(), s.getPermission().toShort());
   }
   return s.getPermission();
 }
  public void testCreate() throws Exception {
    Configuration conf = new HdfsConfiguration();
    conf.setBoolean(DFSConfigKeys.DFS_PERMISSIONS_ENABLED_KEY, true);
    conf.set(FsPermission.UMASK_LABEL, "000");
    MiniDFSCluster cluster = null;
    FileSystem fs = null;

    try {
      cluster = new MiniDFSCluster.Builder(conf).numDataNodes(3).build();
      cluster.waitActive();
      fs = FileSystem.get(conf);
      FsPermission rootPerm = checkPermission(fs, "/", null);
      FsPermission inheritPerm = FsPermission.createImmutable((short) (rootPerm.toShort() | 0300));

      FsPermission dirPerm = new FsPermission((short) 0777);
      fs.mkdirs(new Path("/a1/a2/a3"), dirPerm);
      checkPermission(fs, "/a1", dirPerm);
      checkPermission(fs, "/a1/a2", dirPerm);
      checkPermission(fs, "/a1/a2/a3", dirPerm);

      dirPerm = new FsPermission((short) 0123);
      FsPermission permission = FsPermission.createImmutable((short) (dirPerm.toShort() | 0300));
      fs.mkdirs(new Path("/aa/1/aa/2/aa/3"), dirPerm);
      checkPermission(fs, "/aa/1", permission);
      checkPermission(fs, "/aa/1/aa/2", permission);
      checkPermission(fs, "/aa/1/aa/2/aa/3", dirPerm);

      FsPermission filePerm = new FsPermission((short) 0444);
      FSDataOutputStream out =
          fs.create(
              new Path("/b1/b2/b3.txt"),
              filePerm,
              true,
              conf.getInt(CommonConfigurationKeys.IO_FILE_BUFFER_SIZE_KEY, 4096),
              fs.getDefaultReplication(),
              fs.getDefaultBlockSize(),
              null);
      out.write(123);
      out.close();
      checkPermission(fs, "/b1", inheritPerm);
      checkPermission(fs, "/b1/b2", inheritPerm);
      checkPermission(fs, "/b1/b2/b3.txt", filePerm);

      conf.set(FsPermission.UMASK_LABEL, "022");
      permission = FsPermission.createImmutable((short) 0666);
      FileSystem.mkdirs(fs, new Path("/c1"), new FsPermission(permission));
      FileSystem.create(fs, new Path("/c1/c2.txt"), new FsPermission(permission));
      checkPermission(fs, "/c1", permission);
      checkPermission(fs, "/c1/c2.txt", permission);
    } finally {
      try {
        if (fs != null) fs.close();
      } catch (Exception e) {
        LOG.error(StringUtils.stringifyException(e));
      }
      try {
        if (cluster != null) cluster.shutdown();
      } catch (Exception e) {
        LOG.error(StringUtils.stringifyException(e));
      }
    }
  }