Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get numeric groupid/userid using java7 file attribute apis?

Tags:

java

java-7

I can use the following code to get the name of the owner of a file;

    final PosixFileAttributes basicFileAttributes =
        Files.readAttributes( path, PosixFileAttributes.class, 
                                    LinkOption.NOFOLLOW_LINKS );
    String ownerName = basicFileAttributes.owner().getName();

But I'm also trying to get hold of the numeric unix id of the user in question. In the debugger I can see it's hiding inside "UnixFileAttributes" (subclass of PosixFileAttributes), but is there any reasonably standard way to get hold of it ?

like image 276
krosenvold Avatar asked Jun 22 '11 20:06

krosenvold


2 Answers

There's actually a "unix" view you can get access to such Unix-specific attributes through:

int uid = (int) Files.getAttribute(path, "unix:uid", NOFOLLOW_LINKS);
like image 127
ColinD Avatar answered Oct 15 '22 22:10

ColinD


For some strange reason the Java team refuses to document this.

But from jdk/test/java/nio/file/Files/FileAttributes.java...

int mode = (Integer)Files.getAttribute(file, "unix:mode");
long ino = (Long)Files.getAttribute(file, "unix:ino");
long dev = (Long)Files.getAttribute(file, "unix:dev");
long rdev = (Long)Files.getAttribute(file, "unix:rdev");
int nlink = (Integer)Files.getAttribute(file, "unix:nlink");
int uid = (Integer)Files.getAttribute(file, "unix:uid");
int gid = (Integer)Files.getAttribute(file, "unix:gid");
FileTime ctime = (FileTime)Files.getAttribute(file, "unix:ctime");
map = Files.readAttributes(file, "unix:*");
map = Files.readAttributes(file, "unix:size,uid,gid");
like image 32
kervin Avatar answered Oct 15 '22 20:10

kervin