Example usage for org.apache.hadoop.fs.permission FsAction implies

List of usage examples for org.apache.hadoop.fs.permission FsAction implies

Introduction

In this page you can find the example usage for org.apache.hadoop.fs.permission FsAction implies.

Prototype

public boolean implies(FsAction that) 

Source Link

Document

Return true if this action implies that action.

Usage

From source file:alluxio.underfs.hdfs.acl.SupportedHdfsAclProvider.java

License:Apache License

@Override
public Pair<AccessControlList, DefaultAccessControlList> getAcl(FileSystem hdfs, String path)
        throws IOException {
    AclStatus hdfsAcl;/* w  w  w.jav  a 2s  .  c o m*/
    Path filePath = new Path(path);
    boolean isDir = hdfs.isDirectory(filePath);
    try {
        hdfsAcl = hdfs.getAclStatus(filePath);
    } catch (AclException e) {
        // When dfs.namenode.acls.enabled is false, getAclStatus throws AclException.
        return new Pair<>(null, null);
    }
    AccessControlList acl = new AccessControlList();
    DefaultAccessControlList defaultAcl = new DefaultAccessControlList();

    acl.setOwningUser(hdfsAcl.getOwner());
    acl.setOwningGroup(hdfsAcl.getGroup());
    defaultAcl.setOwningUser(hdfsAcl.getOwner());
    defaultAcl.setOwningGroup(hdfsAcl.getGroup());
    for (AclEntry entry : hdfsAcl.getEntries()) {
        alluxio.security.authorization.AclEntry.Builder builder = new alluxio.security.authorization.AclEntry.Builder();
        builder.setType(getAclEntryType(entry));
        builder.setSubject(entry.getName() == null ? "" : entry.getName());
        FsAction permission = entry.getPermission();
        if (permission.implies(FsAction.READ)) {
            builder.addAction(AclAction.READ);
        } else if (permission.implies(FsAction.WRITE)) {
            builder.addAction(AclAction.WRITE);
        } else if (permission.implies(FsAction.EXECUTE)) {
            builder.addAction(AclAction.EXECUTE);
        }
        if (entry.getScope().equals(AclEntryScope.ACCESS)) {
            acl.setEntry(builder.build());
        } else {
            // default ACL, must be a directory
            defaultAcl.setEntry(builder.build());
        }
    }
    if (isDir) {
        return new Pair<>(acl, defaultAcl);
    } else {
        // a null defaultACL indicates this is a file
        return new Pair<>(acl, null);
    }
}

From source file:com.yahoo.storm.yarn.Util.java

License:Open Source License

/**
 * Checks for a given path whether the Other permissions on it
 * imply the permission in the passed FsAction
 * @param fs/*from  ww w  .  j  av  a2s  . c  om*/
 * @param path
 * @param action
 * @return true if the path in the uri is visible to all, false otherwise
 * @throws IOException
 */
private static boolean checkPermissionOfOther(FileSystem fs, Path path, FsAction action) throws IOException {
    FileStatus status = fs.getFileStatus(path);
    FsPermission perms = status.getPermission();
    FsAction otherAction = perms.getOtherAction();
    if (otherAction.implies(action)) {
        return true;
    }
    return false;
}

From source file:es.tid.cosmos.platform.injection.server.HadoopSshFile.java

License:Open Source License

/**
 * Answer if the current user permissions on the current path are enough to perform a requested action. This method
 * is the common basis for the following permission-related methods, such as isReadable, isWritable, etc.
 *
 * @param queriedFsAction an action such as read, write ...
 * @return                is this user allowed to perform this action?
 *///from w ww  .  j a  va 2s. co m
private boolean isAllowed(FsAction queriedFsAction) {
    try {
        String pathOwner = this.hadoopFS.getFileStatus(this.hadoopPath).getOwner();
        FsPermission permission = this.hadoopFS.getFileStatus(this.hadoopPath).getPermission();
        FsAction action = (pathOwner.equals(this.userName) ? permission.getUserAction()
                : permission.getOtherAction());
        return action.implies(queriedFsAction);
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
        return false;
    } // try catch
}

From source file:hdfs.FileUtil.java

License:Apache License

/**
 * Set permissions to the required value. Uses the java primitives instead
 * of forking if group == other.//from  ww  w .  j  a v a 2s  .co  m
 * @param f the file to change
 * @param permission the new permissions
 * @throws IOException
 */
public static void setPermission(File f, FsPermission permission) throws IOException {
    FsAction user = permission.getUserAction();
    FsAction group = permission.getGroupAction();
    FsAction other = permission.getOtherAction();

    // use the native/fork if the group/other permissions are different
    // or if the native is available    
    if (group != other || NativeIO.isAvailable()) {
        execSetPermission(f, permission);
        return;
    }

    boolean rv = true;

    // read perms
    rv = f.setReadable(group.implies(FsAction.READ), false);
    checkReturnValue(rv, f, permission);
    if (group.implies(FsAction.READ) != user.implies(FsAction.READ)) {
        f.setReadable(user.implies(FsAction.READ), true);
        checkReturnValue(rv, f, permission);
    }

    // write perms
    rv = f.setWritable(group.implies(FsAction.WRITE), false);
    checkReturnValue(rv, f, permission);
    if (group.implies(FsAction.WRITE) != user.implies(FsAction.WRITE)) {
        f.setWritable(user.implies(FsAction.WRITE), true);
        checkReturnValue(rv, f, permission);
    }

    // exec perms
    rv = f.setExecutable(group.implies(FsAction.EXECUTE), false);
    checkReturnValue(rv, f, permission);
    if (group.implies(FsAction.EXECUTE) != user.implies(FsAction.EXECUTE)) {
        f.setExecutable(user.implies(FsAction.EXECUTE), true);
        checkReturnValue(rv, f, permission);
    }
}

From source file:org.apache.hadoop.fs.FileUtil.java

License:Apache License

/**
 * Set permissions to the required value. Uses the java primitives instead
 * of forking if group == other./*  w ww.j a  v a 2  s . c  om*/
 * @param f the file to change
 * @param permission the new permissions
 * @throws java.io.IOException
 */
public static void setPermission(File f, FsPermission permission) throws IOException {
    FsAction user = permission.getUserAction();
    FsAction group = permission.getGroupAction();
    FsAction other = permission.getOtherAction();

    // use the native/fork if the group/other permissions are different
    // or if the native is available
    if (group != other || NativeIO.isAvailable()) {
        execSetPermission(f, permission);
        return;
    }

    boolean rv = true;

    // read perms
    rv = f.setReadable(group.implies(FsAction.READ), false);
    checkReturnValue(rv, f, permission);
    if (group.implies(FsAction.READ) != user.implies(FsAction.READ)) {
        f.setReadable(user.implies(FsAction.READ), true);
        checkReturnValue(rv, f, permission);
    }

    // write perms
    rv = f.setWritable(group.implies(FsAction.WRITE), false);
    checkReturnValue(rv, f, permission);
    if (group.implies(FsAction.WRITE) != user.implies(FsAction.WRITE)) {
        f.setWritable(user.implies(FsAction.WRITE), true);
        checkReturnValue(rv, f, permission);
    }

    // exec perms
    rv = f.setExecutable(group.implies(FsAction.EXECUTE), false);
    checkReturnValue(rv, f, permission);
    if (group.implies(FsAction.EXECUTE) != user.implies(FsAction.EXECUTE)) {
        f.setExecutable(user.implies(FsAction.EXECUTE), true);
        checkReturnValue(rv, f, permission);
    }
}

From source file:org.apache.nifi.processors.hadoop.GetHDFSFileInfo.java

License:Apache License

protected String getPerms(final FsPermission permission) {

    final StringBuilder sb = new StringBuilder();
    for (FsAction action : new FsAction[] { permission.getUserAction(), permission.getGroupAction(),
            permission.getOtherAction() }) {
        if (action.implies(FsAction.READ)) {
            sb.append("r");
        } else {/*from w  w  w . j av a 2  s  .c o  m*/
            sb.append("-");
        }

        if (action.implies(FsAction.WRITE)) {
            sb.append("w");
        } else {
            sb.append("-");
        }

        if (action.implies(FsAction.EXECUTE)) {
            sb.append("x");
        } else {
            sb.append("-");
        }
    }

    return sb.toString();
}

From source file:org.apache.nifi.processors.hadoop.ListHDFS.java

License:Apache License

private String getPerms(final FsAction action) {
    final StringBuilder sb = new StringBuilder();
    if (action.implies(FsAction.READ)) {
        sb.append("r");
    } else {//w ww .j  a v  a  2 s.c o m
        sb.append("-");
    }

    if (action.implies(FsAction.WRITE)) {
        sb.append("w");
    } else {
        sb.append("-");
    }

    if (action.implies(FsAction.EXECUTE)) {
        sb.append("x");
    } else {
        sb.append("-");
    }

    return sb.toString();
}

From source file:org.cgc.wfx.impl.FileInformationImpl.java

License:Open Source License

private static int fsActionToUnixVal(FsAction action) {
    int retVal = 0;
    if (action.implies(FsAction.READ)) {
        retVal |= S_IRUSR;//from w  ww . j a  va2 s .c o  m
    }
    if (action.implies(FsAction.WRITE)) {
        retVal |= S_IWUSR;
    }
    if (action.implies(FsAction.EXECUTE)) {
        retVal |= S_IXUSR;
    }
    return retVal;
}

From source file:org.shaf.lib.io.List.java

License:Apache License

/**
 * Returns information for the specified location (it could be file or
 * directory);// w w w .j av  a 2 s. c  o  m
 * 
 * @param loc
 *            the location for obtaining information.
 * @return the array containing information about the specified location.
 */
private final String[] getFileInfo(final Location loc) {
    String[] info = new String[6];

    try {
        // Sets permissions.
        info[0] = (loc.isDirectory() ? "d" : "-");

        FsPermission permission = loc.getPermission();
        if (permission == null) {
            info[0] += "rw-rw-rw-";
        } else {
            FsAction ua = permission.getUserAction();
            info[0] += (ua.implies(FsAction.READ) ? "r" : "-");
            info[0] += (ua.implies(FsAction.WRITE) ? "w" : "-");
            info[0] += (ua.implies(FsAction.EXECUTE) ? "x" : "-");

            FsAction ga = permission.getGroupAction();
            info[0] += (ga.implies(FsAction.READ) ? "r" : "-");
            info[0] += (ga.implies(FsAction.WRITE) ? "w" : "-");
            info[0] += (ga.implies(FsAction.EXECUTE) ? "x" : "-");

            FsAction oa = permission.getGroupAction();
            info[0] += (oa.implies(FsAction.READ) ? "r" : "-");
            info[0] += (oa.implies(FsAction.WRITE) ? "w" : "-");
            info[0] += (oa.implies(FsAction.EXECUTE) ? "x" : "-");
        }
        // Sets owner.

        info[1] = (loc.getOwner() == null) ? "unknown" : loc.getOwner();

        // Sets group.

        info[2] = (loc.getGroup() == null) ? "unknown" : loc.getGroup();

        // Sets size.
        info[3] = String.valueOf(loc.isFile() ? String.valueOf(loc.getSize()) : "");

        // Sets date.
        info[4] = loc.getModificationTimeAsString();

        // Sets name.
        info[5] = loc.getPath().getName();
    } catch (Exception exc) {
        LOG.error("Failed to information about file system entity: " + loc.getPath(), exc);
        info = new String[] { "----------", "unknown", "unknown", "", "unknown", loc.getPathAsString() };
    }

    return info;
}