private static void setCmRootPermissions(Path cmroot) throws IOException{
    FileSystem cmFs = cmroot.getFileSystem(conf);
    cmFs.setPermission(cmroot, new FsPermission("770"));
    try {
      FileStatus warehouseStatus = cmFs.getFileStatus(new Path(MetastoreConf.get(conf, ConfVars.WAREHOUSE.getVarname())));
      String warehouseOwner = warehouseStatus.getOwner();
      String warehouseGroup = warehouseStatus.getGroup();
      if (warehouseOwner.equals(cmFs.getFileStatus(cmroot).getOwner())) {
        FsAction whOwnerAction = warehouseStatus.getPermission().getUserAction();
        FsAction whGroupAction = warehouseStatus.getPermission().getGroupAction();
        FsAction whOtherAction = warehouseStatus.getPermission().getOtherAction();
        if(!warehouseGroup.equals(cmFs.getFileStatus(cmroot).getGroup())) {
          //change group to wh-group.
          //since cmRoot owner is already part of wh-group, this can be done.
          cmFs.setOwner(cmroot, null, warehouseGroup);
          cmFs.setPermission(cmroot, new FsPermission(whOwnerAction, whGroupAction, whOtherAction));
        }
      } else {
        LOG.warn("Metastore-user is not same as owner of warehouse.");
        if(!warehouseGroup.equals(cmFs.getFileStatus(cmroot).getGroup())) {
          List<AclEntry> aclList = Lists.newArrayList(
                  new AclEntry.Builder().setScope(ACCESS).setType(USER).setPermission(FsAction.ALL).build(),
                  new AclEntry.Builder().setScope(ACCESS).setType(GROUP).setPermission(FsAction.ALL).build(),
                  new AclEntry.Builder().setScope(ACCESS).setType(OTHER).setPermission(FsAction.NONE).build());
          aclList.add(new AclEntry.Builder().setScope(ACCESS).setType(GROUP).setName(warehouseGroup).
                  setPermission(warehouseStatus.getPermission().getGroupAction()).build());
          cmFs.setAcl(cmroot, aclList);
        }
      }
    } catch (RuntimeException | IOException e) {
      LOG.error("Unable to set permissions corresponding to hive-warehouse on CMRoot: ", e);
    }
  }
Thread to clear old files of cmroot recursively
standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/ReplChangeManager.java