I'm reading a tar archive which has numeric owner and group ids.
I want to set the file ownership to those given in the archive. Assuming of course that the name to id mapping is the same on the target machine as on the source of the archive, how can I do this?
- The only way to do this in Java is
Files.setOwner(). Files.setOwner()requires aUserPrincipalargument.- The
UserPrincipalinterface has agetName()method. - The
tararchive contains only numerical ids. - If I have a name, I can create a
UserPrincipalobject.
but there seems to be no 'clean' method of obtaining the name of the user associated with a particular numerical uid, if one exist. By 'clean' I mean a JVM method and not reading and parsing /etc/passwd.
Java doesn't provide any direct APIs for retrieving the OS level user name from a numeric UID. The UserPrincipal object, as you correctly pointed out, works with a user's name, not a user's numeric identifier.
The most thorough solution would be to incorporate a JNI (Java Native Interface) or JNA (Java Native Access) solution. JNI and JNA can call C/C++ functions. You could use JNI or JNA to call the system function
getpwuidto retrieve user name from UID.