In these two functions, I am using inotify instance
to monitor the paths and I want to monitor two paths at the same time but after giving the locations of the source and destination, the program gives me a message that both locations are being monitored but this time the inotifyFunc
that I call, does not work and the program closes. I don't why?
Although I put while(1)
loop above read(monitor.fd, monitor.buffer, BUFFER_LEN);
also but it had a problem that whenever I enter the source path, it stops there and didn't ask me the destination path. That's why I remove the while(1)
.
Is there any other solution to it?
void inotifyFunc(char *path, uint32_t *maskPtr)
{
int i = 0;
monitor.fd = inotify_init();
if(fcntl(monitor.fd, F_SETFL, O_NONBLOCK)){
perror("inotify not initialized: ");
exit(0);
}
monitor.wd = inotify_add_watch(monitor.fd, path, *maskPtr);
if(monitor.wd < 0){
perror("Sorry");
exit(1);
}
monitor.length = read(monitor.fd, monitor.buffer, BUFFER_LEN);
while(i<monitor.length){
struct inotify_event *event = (struct inotify_event *)&monitor.buffer[i];
if(event->len){
if(event->mask & *maskPtr){
if(event->mask & IN_ISDIR){
printf("Directory is created\n");
break;
}
else{
printf("File is created\n");
break;
}
}
}
i+= EVENT_SIZE + event->len;
}
}
void monitoringSystem(char *pathname1, char *pathname2){
monitor.mask[0] = ENOENT;
monitor.mask[1] = IN_CREATE;
printf("Choose the source path: ");
scanf("%s", pathname1);
inotifyFunc(pathname1, &monitor.mask[0]);
printf("Choose the destination path: ");
scanf("%s", pathname2);
inotifyFunc(pathname2, &monitor.mask[0]);
printf("\nBoth locations are being monitored\n");
inotifyFunc(pathname1, &monitor.mask[1]);
inotifyFunc(pathname2, &monitor.mask[1]);
}