Why C function _stat does not set errno properly when access is denied to the folder
I'm developing a C-program with VS2005 and I fou开发者_高级运维nd out that when folder access is restricted in a way that I can't create folders or delete anything from a folder the _stat function for the folder does not set errno correctly. errno is set to value ENOENT. I absolutely cannot modify the permissions so I could get value EACCES. Either _stat returns 0 (no error) or the errno is set to ENOENT. Can u tell me how to make distinction between folder really existing and user not having enough permissions for folder? Another thing i don't uderstand is that what particular permission results _stat to return -1. When I look at the Effective permissions of the folder I've been testing _stat with it has nothing in 'Deny'-column. Even 'Create folders / Append data' is checked while i cannot create folders into it.
Thanks & BR -Matti
Windows security attributes are too fine grained to test this with _stat(). Its implementation uses the FindFirstFile() API function, that will only fail if the right to enumerate files is not granted. That's rarely the case, the more restrictive one that typically is turned off is Write or Modify.
Hacking the security API to test the ACLs is often avoided and highly unportable. You just find out that you don't have the necessary privilege when you try to open the file. Quite acceptable because there's nothing you can do in your code to gain the right to access the file.
I am not100% sure of this, but have you tried _doserrno? According to MSDN documentation:
For I/O operations, use _doserrno to access the operating-system error-code
equivalents of errno codes. For most non-I/O operations the value of _doserrno is
undefined.
精彩评论