Reputation: 383
I want to open a file in read only mode in kernel but before i do that i want to check if the file has permission to read, how can i check it? Because to even check that i would need a file pointer pointing to the input file.
filp_open(args->inputfile, O_RDONLY, 0);
Is there any way to check it before opening it? I tried using, but it always fails
if (!fileptr->f_op->read)
{
error = -EACCES;
printk("reading input file failed\n");
}
Upvotes: 0
Views: 371
Reputation: 11537
You should use access(char *filepath,int mode)
that checks file access rights.
mode
describes what you want to check: F_OK (existence), or an OR combination of R_OK(read), W_OK(write) or X_OK(execute).
So for your problem, you could use:
#include <unistd.h>
...
if( access( filename, R_OK ) != -1 ) {
// can read file
} else {
// cannot read file
}
Upvotes: 1