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.