Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
int getNotified(char *pathname1, char *pathname2){ 
    int i = 0;
    inotify.fd = inotify_init();
    inotify.wd1 = inotify_add_watch(inotify.fd, pathname1, IN_MODIFY);
    inotify.wd2 = inotify_add_watch(inotify.fd, pathname2, IN_MODIFY);

    while(1){
        inotify.length = read(inotify.fd, inotify.buffer, EVENT_BUF_LEN); 
        while(i < inotify.length){     
            struct inotify_event *event = (struct inotify_event *)&inotify.buffer[i];
            if(event->len){
                if(event->mask & IN_MODIFY){
                    if(event->wd == inotify.wd1){
                        printf("Pathname1 '%s' is modified\n", event->name);
                        break;
                    }
                    else if(event->wd == inotify.wd2){
                        printf("Pathname2 '%s' is modified.\n", event->name);
                        break;
                    }
                }
            }
            i += EVENT_SIZE + event->len;
        }
    }
    inotify_rm_watch(inotify.fd, inotify.wd1);
    inotify_rm_watch(inotify.fd, inotify.wd2);

    close(inotify.fd);
    exit(0);
}


What I have tried:

In this code, I am monitoring two paths at the same time. The paths are monitored correctly but the problem is in the result. It means that when I run this code and give it the paths and when I modify a file. The result appears two times like this.

Pathname1 'filename' is modified.
Pathname1 'filename' is modified.


The same is happening with Pathname2. How to deal with this problem? Although I put break statement also.
Posted
Updated 5-Feb-21 23:06pm

This is the third time you have posted this question. Please do not repost, you can edit the original or respond to existing solutions or comments. And as I told you in the previous version that code works correctly. You need to use your debugger to find out why your code is (maybe) repeating the inner while loop.

[edit]
Corrected solution added below
[/edit]
 
Share this answer
 
v3
Comments
ibilalkayy 5-Feb-21 23:00pm    
I wrote a while(1) at the top of the length = read(....). The purpose of that is to not stop monitoring until I myself close the program. But after doing that, it only print the result one time even if I modify the path again.
Richard MacCutchan 6-Feb-21 5:07am    
See my Solution below.
This code works correctly:
C++
#include <errno.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <string.h>

#define EVENT_SIZE  (sizeof(struct inotify_event))
#define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16))

int main(){
  int length, i = 0, fd, wd1, wd2;
  char buffer[EVENT_BUF_LEN];

  fd = inotify_init();
  wd1 = inotify_add_watch(fd, "/home/rjmacc/dirA", IN_MODIFY);
  wd2 = inotify_add_watch(fd, "/home/rjmacc/dirB", IN_MODIFY);

while(true)
{
  printf("calling read function\n");
  i = 0;
  length = read(fd, buffer, EVENT_BUF_LEN ); 
  while(i < length){     
    printf("length read = %d\n", length);
      struct inotify_event *event = ( struct inotify_event *)&buffer[i];
      if(event->len){
        printf ("event:\n");
        printf ("\t    wd: %d\n", event->wd);
        printf ("\t  mask: %d\n", event->mask);
        printf ("\tcookie: %d\n", event->cookie);
        printf ("\t   len: %d\n", event->len);
        printf ("\t  name: %s\n", event->name);
          if(event->mask & IN_MODIFY){
            if(event->wd == wd1){
              printf("Pathname1 file '%s' modified.\n",event->name);
            }
            if(event->wd == wd2){
              printf("Pathname2 file '%s' modified.\n",event->name);
            }
          }
      }
      i += EVENT_SIZE + event->len;
  }
}
  inotify_rm_watch(fd, wd1);
  inotify_rm_watch(fd, wd2);
  close(fd);
}


In the cases where more than one event is seen, that is caused by the actions occurring in the directory. Some actions (e.g. using vi) will cause more than one event to occur in the directory, as reflected in the pathname that is returned from the watcher.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900