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?
Files.setOwner()
.Files.setOwner()
requires a UserPrincipal
argument.UserPrincipal
interface has a getName()
method.tar
archive contains only numerical ids.UserPrincipal
object.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 getpwuid
to retrieve user name from UID.