1

I want to monitor the creation of new files in some directories and read this link about inotify. I liked the implementation and used it. However, in my case, I want to monitor a directory which has up to level-3 sub-dirs.

My thought is to add a watch each time a new directory is created, but in order to do this, I need to know the path of the created directory. Unfortunately, the event struct of inotify can give me only the name of the file-directory created and not its path. Can anyone suggest an idea for this?

add_watch(fd,root);
  if ( event->mask & IN_CREATE) {
        if (event->mask & IN_ISDIR){
          printf("%d DIR::%s CREATED\n", event->wd,event->name );
          strcpy(new_dir,root);
          strcat(new_dir,"/");
          strcat(new_dir,event->name);
          add_watch(fd,new_dir);

where add_watch is :

void add_watch(int fd, char *root)
  {
  int wd;
  struct dirent *entry;
  DIR *dp;

  dp = opendir(root);
  if (dp == NULL)
  {
  perror("Error opening the starting directory");
  exit(0);
  }

  /* add watch to starting directory */
  wd = inotify_add_watch(fd, root, IN_CREATE | IN_MODIFY | IN_MOVED_TO);

This is ok for the root directory,the level-1 sub-dir gets watched also,but when i try to add a watch to the level-2 sub-dir the path is not right.

writing in c++ with netbeans7.2,ubuntu12.

Community
  • 1
  • 1
  • possible duplicate of http://stackoverflow.com/questions/9313206/retrieve-the-full-path-name-from-inotify-event – us2012 Jan 08 '13 at 13:32
  • not really a duplicate since this question refers to monitoring new subdirectories, while the other is only interested in determining the names of files (with paths). – Peter Krnjevic Jul 17 '13 at 01:12

1 Answers1

0

I have a working sample on Github that supports inotify directory create/delete events. A small Watch class takes care of mapping wd (watch descriptors) to file/folder names. Here is a snippet showing how to handle inotify CREATE and DELETE events. The full sample is on Github.

            if ( event->mask & IN_CREATE ) {
                current_dir = watch.get(event->wd);
                if ( event->mask & IN_ISDIR ) {
                    new_dir = current_dir + "/" + event->name;
                    wd = inotify_add_watch( fd, new_dir.c_str(), WATCH_FLAGS );
                    watch.insert( event->wd, event->name, wd );
                    total_dir_events++;
                    printf( "New directory %s created.\n", new_dir.c_str() );
                } else {
                    total_file_events++;
                    printf( "New file %s/%s created.\n", current_dir.c_str(), event->name );
                }
            } else if ( event->mask & IN_DELETE ) {
                if ( event->mask & IN_ISDIR ) {
                    new_dir = watch.erase( event->wd, event->name, &wd );
                    inotify_rm_watch( fd, wd );
                    total_dir_events--;
                    printf( "Directory %s deleted.\n", new_dir.c_str() );
                } else {
                    current_dir = watch.get(event->wd);
                    total_file_events--;
                    printf( "File %s/%s deleted.\n", current_dir.c_str(), event->name );
                }
            }
Peter Krnjevic
  • 1,070
  • 15
  • 20