Example usage for java.nio.file.attribute PosixFileAttributes group

List of usage examples for java.nio.file.attribute PosixFileAttributes group

Introduction

In this page you can find the example usage for java.nio.file.attribute PosixFileAttributes group.

Prototype

GroupPrincipal group();

Source Link

Document

Returns the group owner of the file.

Usage

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() + " ");
    }/* w  w  w.  ja  va 2  s  .  c  o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    Path path = Paths.get("c:/home/tutorial/Java/JavaFX/Topic.txt");

    PosixFileAttributes attr = Files.readAttributes(path, PosixFileAttributes.class);
    attr = Files.getFileAttributeView(path, PosixFileAttributeView.class).readAttributes();

    System.out.println("File owner: " + attr.owner().getName());
    System.out.println("File group: " + attr.group().getName());
    System.out.println("File permissions: " + attr.permissions().toString());

}

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() + " ");
    }/*w ww .j ava  2 s  . co 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. ja  v  a  2 s .  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:fr.pilato.elasticsearch.crawler.fs.framework.FsCrawlerUtil.java

/**
 * Determines the 'group' of the file. Please note that 'group' is not
 * available of Windows OS./*from  w w  w  .ja  v a 2s .  c om*/
 */
public static String getGroupName(final File file) {
    if (OsValidator.windows) {
        logger.trace("Determining 'group' is skipped for file [{}]on [{}]", file, OsValidator.OS);
        return null;
    }
    try {
        final Path path = Paths.get(file.getAbsolutePath());
        final PosixFileAttributes posixFileAttributes = Files
                .getFileAttributeView(path, PosixFileAttributeView.class).readAttributes();
        return posixFileAttributes != null ? posixFileAttributes.group().getName() : null;
    } catch (Exception e) {
        logger.warn("Failed to determine 'group' of {}: {}", file, e.getMessage());
        return null;
    }
}

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiver.java

private TarArchiveEntry createTarEntry(File tarRootDir, File source) throws IOException {
    String pathInTar = slashify(tarRootDir.toPath().relativize(source.toPath()));
    log.debug("Adding entry " + pathInTar);
    TarArchiveEntry tarEntry;//  ww w  .  j  av a 2 s . co m
    if (isSymbolicLink(source) && resolvesBelow(source, tarRootDir)) {
        // only create symlink entry if link target is inside archive
        tarEntry = new TarArchiveEntry(pathInTar, TarArchiveEntry.LF_SYMLINK);
        tarEntry.setLinkName(slashify(getRelativeSymLinkTarget(source, source.getParentFile())));
    } else {
        tarEntry = new TarArchiveEntry(source, pathInTar);
    }
    PosixFileAttributes attrs = getAttributes(source);
    if (attrs != null) {
        tarEntry.setUserName(attrs.owner().getName());
        tarEntry.setGroupName(attrs.group().getName());
        tarEntry.setMode(FilePermissionHelper.toOctalFileMode(attrs.permissions()));
    }
    tarEntry.setModTime(source.lastModified());
    return tarEntry;
}

From source file:de.jwi.jfm.FileWrapper.java

public String getAttributes() throws IOException {
    FileSystem fileSystem = FileSystems.getDefault();
    Set<String> fileSystemViews = fileSystem.supportedFileAttributeViews();

    File file = getFile();/*  ww  w  .j a  va2s .  c om*/
    Path p = file.toPath();

    try {
        if (fileSystemViews.contains("posix")) {
            Set<PosixFilePermission> posixFilePermissions = Files.getPosixFilePermissions(p,
                    LinkOption.NOFOLLOW_LINKS);

            PosixFileAttributes attrs = Files.getFileAttributeView(p, PosixFileAttributeView.class)
                    .readAttributes();

            String owner = attrs.owner().toString();
            String group = attrs.group().toString();

            String permissions = PosixFilePermissions.toString(attrs.permissions());

            String res = String.format("%s %s %s", permissions, owner, group);
            return res;
        } else if (fileSystemViews.contains("dos")) {
            StringWriter sw = new StringWriter();
            DosFileAttributeView attributeView = Files.getFileAttributeView(p, DosFileAttributeView.class);
            DosFileAttributes dosFileAttributes = null;

            dosFileAttributes = attributeView.readAttributes();
            if (dosFileAttributes.isArchive()) {
                sw.append('A');
            }
            if (dosFileAttributes.isHidden()) {
                sw.append('H');
            }
            if (dosFileAttributes.isReadOnly()) {
                sw.append('R');
            }
            if (dosFileAttributes.isSystem()) {
                sw.append('S');
            }

            return sw.toString();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java

@Test
public void testCreateArchiveOwnerAndGroupPreserved() throws Exception {
    PosixFileAttributes attrs = getPosixFileAttributes(testOwnerAndGroupFile);
    archiver.createArchive();//ww  w .j av  a 2 s. c o m
    TarArchiveEntry testOwnerAndGroupNameEntry = getTarEntries().get("dir2/testOwnerAndGroupName");
    assertEquals(attrs.owner().getName(), testOwnerAndGroupNameEntry.getUserName());
    assertEquals(attrs.group().getName(), testOwnerAndGroupNameEntry.getGroupName());
}

From source file:VOBackupFile.java

/**
 * Wrapper for metadata of backuped file.
 *
 * @param file backuped file//from   w ww  .  j  a  v a  2 s .c  o  m
 */
public VOBackupFile(File file) {
    this.path = file.getAbsolutePath();
    this.directory = file.isDirectory();
    this.symLink = Files.isSymbolicLink(file.toPath());

    this.size = ((file.isDirectory() || symLink) ? 0 : file.length());
    this.modify = new Date(file.lastModified());

    if (symLink) {
        try {
            symlinkTarget = Files.readSymbolicLink(file.toPath()).toString();
        } catch (IOException e) {
            e.printStackTrace(); //TODO Lebeda - oetit do logu
        }
    } else {
        // advanced attributes

        try {

            owner = Files.getOwner(file.toPath(), LinkOption.NOFOLLOW_LINKS).getName();

            if (Files.getFileStore(file.toPath()).supportsFileAttributeView(DosFileAttributeView.class)) {
                dosAttr = Boolean.TRUE;
                final DosFileAttributes dosFileAttributes = Files.readAttributes(file.toPath(),
                        DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);

                dosArchive = dosFileAttributes.isArchive();
                dosHidden = dosFileAttributes.isHidden();
                dosSystem = dosFileAttributes.isSystem();
                dosReadOnly = dosFileAttributes.isReadOnly();
            } else {
                dosAttr = Boolean.FALSE;
            }

            if (Files.getFileStore(file.toPath()).supportsFileAttributeView(PosixFileAttributeView.class)) {
                posixAttr = Boolean.TRUE;
                final PosixFileAttributes posixFileAttributes = Files.readAttributes(file.toPath(),
                        PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
                posixGroup = posixFileAttributes.group().getName();
                posixPermitions = PosixFilePermissions.toString(posixFileAttributes.permissions());
            } else {
                posixAttr = Boolean.FALSE;
            }

        } catch (IOException e) {
            e.printStackTrace(); //Todo implementovat
        }
    }

}

From source file:org.application.backupsync.PathName.java

public JSONObject getAttrs() throws IOException, JSONException {
    JSONObject result;/*from www .  j a  va  2  s .c  o  m*/
    BasicFileAttributes attr;
    DosFileAttributes dosAttr;
    PosixFileAttributes posixAttr;

    result = new JSONObject();
    attr = Files.readAttributes(this.path, BasicFileAttributes.class);

    result.append("ctime", attr.creationTime().toMillis());
    result.append("mtime", attr.lastModifiedTime().toMillis());
    //result.append("symlink", attr.isSymbolicLink()); //Redundant
    result.append("size", attr.size());

    if (System.getProperty("os.name").startsWith("Windows")) {
        dosAttr = Files.readAttributes(this.path, DosFileAttributes.class);

        result.append("dos:archive", dosAttr.isArchive());
        result.append("dos:hidden", dosAttr.isHidden());
        result.append("dos:readonly", dosAttr.isReadOnly());
        result.append("dos:system", dosAttr.isSystem());
    } else {
        posixAttr = Files.readAttributes(this.path, PosixFileAttributes.class);

        result.append("posix:symlink", posixAttr.isSymbolicLink());
        result.append("posix:owner", posixAttr.owner());
        result.append("posix:group", posixAttr.group());
        result.append("posix:permission", PosixFilePermissions.toString(posixAttr.permissions()));
    }

    return result;
}