user3238850
user3238850

Reputation: 131

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. If not, the server will send back 403 Forbidden.

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. I know there is "S_IROTH" but I don't how to use it. I tried to print it to terminal using 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".

So my question is: How to get read permission stat for other users? Or am I making any mistake in concept here?

Thank you.

Upvotes: 0

Views: 244

Answers (1)

user149341
user149341

Reputation:

You need to mask the mode of the file against S_IROTH. Also, you're passing statbuf incorrectly (and you should be getting a warning for it). 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 ...

Upvotes: 1

Related Questions