简体   繁体   English

C ++ / Linux如何获得其他用户的读取权限统计信息?

[英]C++/Linux How can I get read permission stat for other users?

I'm writing a simple C++ webserver assignment and I want to check if the file is readable by other users. 我正在编写一个简单的C ++ Web服务器分配,我想检查该文件是否可被其他用户读取。 If not, the server will send back 403 Forbidden. 否则,服务器将发回403禁止访问。

I already have a statbuf.st_mode using stat(FILE,&statbuf) but I don't know how to retrieve the read permission for other users. 我已经有一个statbuf.st_mode使用stat(FILE,&statbuf)但我不知道如何获取其他用户的读取权限。 I know there is "S_IROTH" but I don't how to use it. 我知道有“ S_IROTH”,但我不知道如何使用它。 I tried to print it to terminal using cout<<S_IROTH<<endl; 我试图使用cout<<S_IROTH<<endl;将其打印到终端cout<<S_IROTH<<endl; and it was 4 but "FILE" has a permission of 0440 so I guess I was not printing the S_IROTH of "FILE". 它是4,但是“ FILE”具有0440的权限,所以我想我不是在打印“ FILE”的S_IROTH。

So my question is: How to get read permission stat for other users? 所以我的问题是:如何获取其他用户的读取权限统计信息? Or am I making any mistake in concept here? 还是我在概念上犯了任何错误?

Thank you. 谢谢。

You need to mask the mode of the file against S_IROTH . 您需要针对S_IROTH屏蔽文件的模式。 Also, you're passing statbuf incorrectly (and you should be getting a warning for it). 另外,您错误地传递了statbuf (并且您应该收到警告)。 The correct code should look like: 正确的代码应如下所示:

int result = stat(path, &statbuf);
if (result != 0) {
    return NOT_FOUND;
}
if (!(statbuf.st_mode & S_IROTH)) {
    return FORBIDDEN;
}
... success, continue ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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