Reputation: 131
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
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