I'm trying to write a program that checks files in a directory. When a new file is created I have to check if it executable and if so I have to execute it.
I'm using the inotify interface and it works well, but I have some problems when I try to check whether a file is executable using stat(2). I found that I don't have permission to execute it.
Does passing to the program an absolute path of the directory that I want to check create permissions problems?
int main(int argc,char * argv []){
int fd,wd,len,i=0;
char buffer [EVENT_SIZE_BUF];
if(argc != 2)
usage();
if((fd = inotify_init()) == -1)
perror("inotify_init()");
if((wd = inotify_add_watch(fd,argv[1],IN_CREATE) -1))
perror("inotify_add_watch()");
while(1){
if((len = read(fd,buffer,EVENT_SIZE_BUF)) < 0)
perror("read()");
struct inotify_event * ev = (struct inotify_event *)&buffer;
if(ev->len > 0){
if(ev->mask & IN_CREATE && ((ev-> mask & IN_ISDIR) == 0x00)){
printf("SPY; new file is created %s\n",ev->name);
char * path = strcat(argv[1],ev->name);
printf("%s\n",path);
struct stat sb;
if(!stat(path,&sb)){
printf( (S_ISDIR(sb.st_mode)) ? "d" : "-");
printf( (sb.st_mode & S_IRUSR) ? "r" : "-");
printf( (sb.st_mode & S_IWUSR) ? "w" : "-");
printf( (sb.st_mode & S_IXUSR) ? "x" : "-");
printf( (sb.st_mode & S_IRGRP) ? "r" : "-");
printf( (sb.st_mode & S_IWGRP) ? "w" : "-");
printf( (sb.st_mode & S_IXGRP) ? "x" : "-");
printf( (sb.st_mode & S_IROTH) ? "r" : "-");
printf( (sb.st_mode & S_IWOTH) ? "w" : "-");
printf( (sb.st_mode & S_IXOTH) ? "x" : "-");
fflush(stdout);
printf("\n");
}
}else{ printf("dir\n"); }
}
}
inotify_rm_watch(fd,wd);
close(fd);
return 0;
}