Example usage for java.nio.file.attribute PosixFileAttributeView readAttributes

List of usage examples for java.nio.file.attribute PosixFileAttributeView readAttributes

Introduction

In this page you can find the example usage for java.nio.file.attribute PosixFileAttributeView readAttributes.

Prototype

@Override
PosixFileAttributes readAttributes() throws IOException;

Source Link

Usage

From source file:Test.java

public static void main(String args[]) throws IOException {
    Path path = Paths.get("home/docs");
    SecureDirectoryStream<Path> sds = (SecureDirectoryStream) Files.newDirectoryStream(path);
    PosixFileAttributeView view = sds.getFileAttributeView(PosixFileAttributeView.class);
    PosixFileAttributes attributes = view.readAttributes();
    Set<PosixFilePermission> permissions = attributes.permissions();

    for (PosixFilePermission permission : permissions) {
        System.out.print(permission.toString() + ' ');
    }/*from  w ww.  j av a  2  s  . c om*/
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("home/docs/users.txt");
    PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);

    PosixFileAttributes attributes = view.readAttributes();
    System.out.println("Group: " + attributes.group());
    System.out.println("Owner: " + attributes.owner().getName());

    Set<PosixFilePermission> permissions = attributes.permissions();
    for (PosixFilePermission permission : permissions) {
        System.out.print(permission.name() + " ");
    }//  ww  w. ja va 2  s.  c o m
}

From source file:Main.java

public static void readPermissions(PosixFileAttributeView posixView) throws Exception {
    PosixFileAttributes attribs;//from  w w w  . j a  v a2  s . c  om
    attribs = posixView.readAttributes();
    Set<PosixFilePermission> permissions = attribs.permissions();
    // Convert the set of posix file permissions into rwxrwxrwx form
    String rwxFormPermissions = PosixFilePermissions.toString(permissions);
    System.out.println(rwxFormPermissions);
}

From source file:Test.java

private static void removePermission(Path path, PosixFilePermission permission) throws Exception {
    System.out.println("\nRemoving permission for " + path.getFileName());
    PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);

    PosixFileAttributes attributes = view.readAttributes();

    Set<PosixFilePermission> permissions = attributes.permissions();
    permissions.remove(permission);/*from   ww w . j  a v  a 2  s. co  m*/

    view.setPermissions(permissions);
    System.out.println();
}

From source file:Test.java

private static void listPermissions(Path path) throws Exception {
    System.out.println("Permission for " + path.getFileName());
    PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);

    PosixFileAttributes attributes = view.readAttributes();

    System.out.println("Group: " + attributes.group().getName());
    System.out.println("Owner: " + attributes.owner().getName());

    Set<PosixFilePermission> permissions = attributes.permissions();

    System.out.print("Permissions: ");
    for (PosixFilePermission permission : permissions) {
        System.out.print(permission.name() + " ");
    }/*from   w  w  w  .  j a v a 2 s  . c  o  m*/
}

From source file:Test.java

private static void setGroupPrincipal(Path path, String userName, String groupName) throws Exception {
    System.out.println("Setting owner for " + path.getFileName());
    PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);

    PosixFileAttributes attributes = view.readAttributes();
    System.out.println("Old Group: " + attributes.group().getName());
    System.out.println("Old Owner: " + attributes.owner().getName());

    UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
    UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(userName);
    GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(groupName);
    view.setGroup(groupPrincipal);/*from  ww  w . j  a v  a2s. c  o m*/
    view.setOwner(userPrincipal);

    attributes = view.readAttributes();
    System.out.println("New Group: " + attributes.group().getName());
    System.out.println("New Owner: " + attributes.owner().getName());
}

From source file:com.nsn.squirrel.tab.utils.PathUtils.java

/**
 * @param path//www . ja  v a  2 s.c o  m
 * @return
 */
public static String getPosixAttributesString(Path path) {

    PosixFileAttributeView posixView = Files.getFileAttributeView(path, PosixFileAttributeView.class);
    StringBuilder attrs = new StringBuilder();
    try {
        // + all basic attributes
        PosixFileAttributes posixAttrs = posixView.readAttributes();
        if (posixAttrs != null) {
            attrs.append(PosixFilePermissions.toString(posixAttrs.permissions()));
        }
    } catch (IOException e) {
        log.warn("unable to read Posix file attributes.", e); //$NON-NLS-1$
    }
    return attrs.toString();
}

From source file:cane.brothers.e4.commander.utils.PathUtils.java

/**
 * @param path//from w w  w  .j  a v a 2 s .  co m
 * @return
 */
public static String getPosixAttributesString(Path path) {
    PosixFileAttributeView posixView = Files.getFileAttributeView(path, PosixFileAttributeView.class);
    StringBuilder attrs = new StringBuilder();

    try {
        // + all basic attributes
        PosixFileAttributes posixAttrs = posixView.readAttributes();

        if (posixAttrs != null) {
            attrs.append(PosixFilePermissions.toString(posixAttrs.permissions()));
        }
    } catch (IOException e) {
        log.warn("unable to read Posix file attributes.", e); //$NON-NLS-1$
    }
    return attrs.toString();
}

From source file:io.jmnarloch.cd.go.plugin.gradle.GradleTaskConfigParser.java

/**
 * Adds the executable file permission./*from ww w. ja va 2  s. co  m*/
 *
 * @param file the path to the file
 */
private void addExecutablePermission(String file) {
    final Path path = Paths.get(file);
    if (Files.exists(path)) {
        try {
            PosixFileAttributeView attr = Files.getFileAttributeView(path, PosixFileAttributeView.class);
            Set<PosixFilePermission> permissions = attr.readAttributes().permissions();
            if (permissions.add(PosixFilePermission.OWNER_EXECUTE)) {
                logger.info(String.format("Added +x permission to file: %s", file));
            }
            attr.setPermissions(permissions);
        } catch (IOException e) {
            logger.error(String.format("Failed to add the executable permissions to file: %s", file));
        }
    }
}

From source file:io.hops.hopsworks.common.security.CertificatesMgmService.java

@PostConstruct
public void init() {
    masterPasswordFile = new File(settings.getHopsworksMasterEncPasswordFile());
    if (!masterPasswordFile.exists()) {
        throw new IllegalStateException("Master encryption file does not exist");
    }/*from   w ww.ja v a 2s .  c  om*/

    try {
        PosixFileAttributeView fileView = Files.getFileAttributeView(masterPasswordFile.toPath(),
                PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
        Set<PosixFilePermission> filePermissions = fileView.readAttributes().permissions();
        boolean ownerRead = filePermissions.contains(PosixFilePermission.OWNER_READ);
        boolean ownerWrite = filePermissions.contains(PosixFilePermission.OWNER_WRITE);
        boolean ownerExecute = filePermissions.contains(PosixFilePermission.OWNER_EXECUTE);

        boolean groupRead = filePermissions.contains(PosixFilePermission.GROUP_READ);
        boolean groupWrite = filePermissions.contains(PosixFilePermission.GROUP_WRITE);
        boolean groupExecute = filePermissions.contains(PosixFilePermission.GROUP_EXECUTE);

        boolean othersRead = filePermissions.contains(PosixFilePermission.OTHERS_READ);
        boolean othersWrite = filePermissions.contains(PosixFilePermission.OTHERS_WRITE);
        boolean othersExecute = filePermissions.contains(PosixFilePermission.OTHERS_EXECUTE);

        // Permissions should be 700
        if ((ownerRead && ownerWrite && ownerExecute) && (!groupRead && !groupWrite && !groupExecute)
                && (!othersRead && !othersWrite && !othersExecute)) {
            String owner = fileView.readAttributes().owner().getName();
            String group = fileView.readAttributes().group().getName();
            String permStr = PosixFilePermissions.toString(filePermissions);
            LOG.log(Level.INFO, "Passed permissions check for file " + masterPasswordFile.getAbsolutePath()
                    + ". Owner: " + owner + " Group: " + group + " Permissions: " + permStr);
        } else {
            throw new IllegalStateException("Wrong permissions for file " + masterPasswordFile.getAbsolutePath()
                    + ", it should be 700");
        }

    } catch (UnsupportedOperationException ex) {
        LOG.log(Level.WARNING,
                "Associated filesystem is not POSIX compliant. "
                        + "Continue without checking the permissions of " + masterPasswordFile.getAbsolutePath()
                        + " This might be a security problem.");
    } catch (IOException ex) {
        throw new IllegalStateException(
                "Error while getting POSIX permissions of " + masterPasswordFile.getAbsolutePath());
    }

    // Register handlers when master encryption password changes
    MasterPasswordChangeHandler<CertsFacade> psUserCertsHandler = new PSUserCertsMasterPasswordHandler(
            userFacade);
    psUserCertsHandler.setFacade(certsFacade);
    registerMasterPasswordChangeHandler(UserCerts.class, psUserCertsHandler);

    MasterPasswordChangeHandler<CertsFacade> pgUserCertsHandler = new PGUserCertsMasterPasswordHandler(
            projectFacade);
    pgUserCertsHandler.setFacade(certsFacade);
    registerMasterPasswordChangeHandler(ProjectGenericUserCerts.class, pgUserCertsHandler);

    MasterPasswordChangeHandler<ClusterCertificateFacade> delaClusterCertsHandler = new DelaCertsMasterPasswordHandler(
            settings);
    delaClusterCertsHandler.setFacade(clusterCertificateFacade);
    registerMasterPasswordChangeHandler(ClusterCertificate.class, delaClusterCertsHandler);
}