为什么当文件夹访问被拒绝时,C 函数 _stat 没有正确设置 errno
我正在使用 VS2005 开发一个 C 程序,我发现当文件夹访问受到限制而无法创建文件夹或从文件夹中删除任何内容时,该文件夹的 _stat 函数不会正确设置 errno。 errno 设置为值 ENOENT。我绝对无法修改权限以便获得 EACCES 值。 _stat 返回 0(无错误)或 errno 设置为 ENOENT。你能告诉我如何区分文件夹确实存在和用户没有足够的文件夹权限吗?我不明白的另一件事是,什么特定权限会导致 _stat 返回 -1。当我查看文件夹的有效权限时,我一直在测试 _stat,它在“拒绝”列中没有任何内容。即使“创建文件夹/附加数据”也会被选中,但我无法在其中创建文件夹。
谢谢& BR-马蒂
I'm developing a C-program with VS2005 and I found 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Windows 安全属性太细粒度,无法使用 _stat() 进行测试。它的实现使用 FindFirstFile() API 函数,只有在未授予枚举文件的权限时才会失败。这种情况很少发生,通常被关闭的限制性更强的一项是“写入”或“修改”。
侵入安全 API 来测试 ACL 通常是可以避免的,而且非常不可移植。当您尝试打开该文件时,您会发现您没有必要的权限。完全可以接受,因为您无法在代码中执行任何操作来获得访问该文件的权限。
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.
我对此不是百分百确定,但是您尝试过 _doserrno?根据MSDN文档:
I am not100% sure of this, but have you tried _doserrno? According to MSDN documentation: