The std Unix APIs for getting the UID of a file, which I can then use with the bash id command to get the username.
I can't seem to find any equivalent in the Windows APIs, and I'm not having any luck with third party crates. Is there some standard way to find the username of the owner of a file in Windows with Rust?
I'd create an issue against the Rust repo asking if they can add the user of a file. The MetadataExt trait is already in a Windows-specific module so there's no cross-platform compatibility to worry about, plus I feel like this sort of thing would belong in the standard library.
In the meantime, you could call into the Windows API directly:
The trouble is getting the owner requires additional Windows API calls. Currently the Windows MetadataExt simply returns information that's already been retrieved. Additionally, the owner of a file is generally not particularly interesting information on Windows. So it's a more niche operation.
Which is all to say it should perhaps be part of the standard library but perhaps not as part of the MetadataExt trait and may even be better off in a third party crate.
I think they mean that most operations where you might need to know the identity of a file's owner (e.g. read/write/execute permissions) will be handled automatically by the OS's security system.
Can you provide an example for where it matters on Unix? About the only thing I can think of is to provide a personalised message ("hello mr_rustbot"). Even things like finding the home directory are normally done via $HOME or std::env::home_dir() and don't require knowing who the user is.
I think they mean that most operations where you might need to know the identity of a file's owner (e.g. read/write/execute permissions) will be handled automatically by the OS's security system.
Okay, so he's assuming I'm trying to read / write / execute and that I'm just overthinking the complexity of it.
I'm just doing some analysis of a filesystem and want to collect the owner of the file and see who else has permissions to it, not necessarily doing anything with the file myself.
About the only thing I can think of is to provide a personalised message ("hello mr_rustbot").
I'm not trying to get the current user running my program.
To get all users with access to the file you'll have to query the effective access rights for each user.
All being the owner means is that user has the right to change the permissions, even if they would otherwise be denied. This owner may not even have read permissions on the file itself. Though of course they could grant themselves read permissions by changing the permissions.
Note also that getting the owner can fail if the current user does not have permission to read permissions.