Click here to Skip to main content
15,883,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
int main(){
  int length, i = 0, fd, wd;
  char buffer[EVENT_BUF_LEN];

  fd = inotify_init();
  if (fd < 0){
      perror( "inotify_init" );
  }
  wd = inotify_add_watch(fd, pathname1, IN_MODIFY);
  length = read( fd, buffer, EVENT_BUF_LEN); 
  if(length < 0){
      perror("read");
  }  

  while(i < length){     
    struct inotify_event *event = ( struct inotify_event *)&buffer[i];
      if(event->len){
          if(event->mask & IN_MODIFY){
              if(event->mask & IN_ISDIR){
                if(event->wd == wd){
                  // if directory name changed. I don't know what condition to put
                  printf("The directory name '%s' is changed with %s\n", event->name);
                }
              }
              else{
                if(event->wd == wd){
                  // if file name changed. I don't know what condition to put
                  printf("The file name '%s' is changed with %s\n", event->name);
                }
              }
          }
      }
      i += EVENT_SIZE + event->len;
  }
    inotify_rm_watch(fd, wd);
  close(fd);
}


What I have tried:

In this code, I am monitoring a file and a directory. But I have a problem that I want to solve and that is, what condition to put that which file or a directory is renamed? Although the IN_MODIFY only works with the file when that file text is modified. But I want to monitor that if the file or directory is renamed
Is it possible with inotify or with C?
Posted
Updated 27-Feb-21 6:44am

1 solution

See the options at inotify(7) - Linux manual page[^]
 
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