简体   繁体   中英

Find the permissions of a file in windows

I work in Linux. In Linux with stat function, we can extract the permissions of a file.

Similarly how can we extract the permissions of a file in windows.

_stat function in msdn states that permission bits are set in the stat buffer. But it does not give how to extract them.

http://msdn.microsoft.com/en-us/library/14h5k7ff%28VS.71%29.aspx

The struct stat structure does not contain any file permission info on Windows. Windows security is far more convoluted, you'd need GetFileSecurity() to retrieve the DACL for the file. That's rarely done in a Windows program, you'd typically let Windows evaluate the effective permissions and deal with the "you can't do it" error return. ERROR_ACCESS_DENIED from GetLastError().

Windows uses an ACL (Access Control List) to control access to a file (or other kernel object). You can get the ACL for a file with GetFileSecurity (you want the DACL, not the SACL). You can then get the actual permissions (rights) from that with GetEffectiveRightsFromAcl .

This has a race condition, so it's rarely a good idea though. In particular, between the time you retrieve the DACL and the time you try to do something with the file, the DACL may have changed, so what you retrieved is no longer correct.

I read in the link you provided that among others, file permissions as implemented in _stat and friends are UNIX specific and are left unused for NTFS, FAT and other Windows filesystems.

I think you will have more luck reading file permissions using the classic FindFirstFile and related functions. You'll need to process the handle returned with functions described here These are highly unportable, but as C++ has no filesystem support, they are the only good choice.

MSDN doesn't have nearly as many hyperlinks as it should, you need to search a little to find _stat Structure st_mode Field Constants .

On both Unix and Windows, you only get the basic permissions of a file, not its access control lists. Since Windows uses ACLs a lot, _stat won't give you much useful information. You need to use native Windows API functions to get the ACL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM