Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#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/user/Documents", IN_MODIFY);
  wd2 = inotify_add_watch(fd, "/home/user/Desktop", IN_MODIFY);


  length = read(fd, buffer, EVENT_BUF_LEN ); 
  while(i < length){     
      struct inotify_event *event = ( struct inotify_event *)&buffer[i];
      if(event->len){
          if(event->mask & IN_MODIFY){
            if(event->wd1){
              printf("Pathname1 file '%s' modified.\n",event->name);
            }
            if(event->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);
}


What I have tried:

In this code, I am trying to watch two paths at the same time and see that which path is modified. But I am facing a problem. I declared two variables called wd1 and wd2 and when I used them in if-statement like this event->wd1 and event->wd2, it gives me the following error.

error: use of undeclared identifier 'wd1'; did you mean 'wd'?
error: use of undeclared identifier 'wd2'; did you mean 'wd1'?

error: no member named 'wd1' in 'struct inotify_event'; did you mean 'wd'?
error: no member named 'wd2' in 'struct inotify_event'; did you mean 'wd1'?


Quote:
Note: Although I declared the same wd two times for two inotify_add_watch also but it still didn't solve the problem.
Posted
Updated 4-Feb-21 23:55pm

This is the same issue as What condition to put that which event is triggered/modified?[^], where I told you that the values of the wd field in the returned event will tell you which one has fired. You just need to compare that against the value of wd1 and wd2 thus:
C++
if (event->wd == wd1)
{
// the event refers to pathname1
}
else if (event->wd == wd2)
{
// the event refers to pathname2
}
else
{
// this should not happen
}
 
Share this answer
 
Comments
ibilalkayy 5-Feb-21 6:01am    
Thank you, Richard! But here is one problem that pathname1/pathname2 is modified. This message appears two times. Although I put a break statement but it is still giving me the result two times.
Richard MacCutchan 5-Feb-21 6:37am    
You need to use the debugger to examine all the data in the returned structure.
Richard MacCutchan 5-Feb-21 6:50am    
I have just run a test and it only prints one notification, for the correct pathname that I modified.
We don't have access to your struct definition, but the error is saying that "wd1" and "wd2" are not a part of the inotify_event struct:
C++
if(event->wd1){
...
if(event->wd2){
Check the struct definition, and see what is available.
 
Share this answer
 

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