2010-01-19 3 views
2

디렉토리 (/ test)를 모니터링하고 알려주는 프로그램이 있습니다. 다른 디렉토리 (/ opt)를 모니터하기 위해이 기능을 향상시키고 싶습니다. 또한 하위 디렉토리를 모니터링하는 방법도 있지만 현재/test 아래의 파일에 변경 사항이있을 경우 알림을 받게됩니다. 변경/시험의 하위 디렉토리를 만든 경우하지만 난 어떤 inotifcation을받지 못했습니다, 그 ..inotify를 사용하여 하위 디렉토리와 함께 여러 디렉토리를 모니터링하는 C 프로그램?

여기

내 현재 코드 터치 /test/sub-dir/files.txt - 이것은 도움이되기를 바랍니다

/* 

Simple example for inotify in Linux. 

inotify has 3 main functions. 
inotify_init1 to initialize 
inotify_add_watch to add monitor 
then inotify_??_watch to rm monitor.you the what to replace with ??. 
yes third one is inotify_rm_watch() 
*/ 


#include <sys/inotify.h> 

int main(){ 
    int fd,wd,wd1,i=0,len=0; 
    char pathname[100],buf[1024]; 
    struct inotify_event *event; 

    fd=inotify_init1(IN_NONBLOCK); 
    /* watch /test directory for any activity and report it back to me */ 
    wd=inotify_add_watch(fd,"/test",IN_ALL_EVENTS); 

    while(1){ 
     //read 1024 bytes of events from fd into buf 
     i=0; 
     len=read(fd,buf,1024); 
     while(i<len){ 
      event=(struct inotify_event *) &buf[i]; 


      /* check for changes */ 
      if(event->mask & IN_OPEN) 
       printf("%s :was opened\n",event->name); 

      if(event->mask & IN_MODIFY) 
       printf("%s : modified\n",event->name); 

      if(event->mask & IN_ATTRIB) 
       printf("%s :meta data changed\n",event->name); 

      if(event->mask & IN_ACCESS) 
       printf("%s :was read\n",event->name); 

      if(event->mask & IN_CLOSE_WRITE) 
       printf("%s :file opened for writing was closed\n",event->name); 

      if(event->mask & IN_CLOSE_NOWRITE) 
       printf("%s :file opened not for writing was closed\n",event->name); 

      if(event->mask & IN_DELETE_SELF) 
       printf("%s :deleted\n",event->name); 

      if(event->mask & IN_DELETE) 
       printf("%s :deleted\n",event->name); 

      /* update index to start of next event */ 
      i+=sizeof(struct inotify_event)+event->len; 
     } 

    } 

} 

답변

0

inotify에서 디렉토리 당 하나의 시계가 필요합니다. 글로벌 알림의 경우 fanotify가 있습니다.

0

하위 폴더를 삭제하려고 시도 할 수 있으며 그 폴더에 무언가를 추가해야 할 때마다 다시 만들려고 할 수 있습니다.

4

inotify_add_watch 하위 디렉토리의 변경 내용을 수신하지 않습니다. 이 하위 디렉토리가 생성되면이를 감지해야하고 inotify_add_watch도 검색해야합니다.

가장 중요한 것은 하위 디렉토리를 만든 후에 그에 따라 알림을 받게되지만 알림을받을 때 파일과 하위 디렉토리가 이미 만들어져있을 수 있으므로 유의해야합니다. 새 하위 디렉토리에 대한 시계를 추가 할 기회가 없으므로 이러한 이벤트를 "잃게됩니다."

이 문제를 피하는 한 가지 방법은 통지를받은 후에 디렉토리 내용을 스캔하여 실제로 무엇이 있는지 확인할 수있게하는 것입니다. 이렇게하면 더 많은 시계를 추가 할 수 있습니다.

관련 문제