2012-01-30 2 views
9

"Datas"라는 폴더가 있습니다. 이 폴더에는 여러 개의 ".txt"파일이있는 "받은 파일 함"이라는 하위 폴더가 있습니다. 이 "Datas"폴더는 수정할 수 있으며 결국에는 "Inbox"하위 폴더와 ".txt"파일이있는 여러 하위 폴더가 생깁니다. "Datas"폴더와 ".txt"파일을 "Inbox"폴더에서 모니터해야합니다. 어떻게해야합니까?내부에있는 모든 하위 폴더와 파일이있는 폴더를 모니터링하는 방법은 무엇입니까?

INotify는 폴더를 모니터링하고 하위 폴더를 만들 때 이벤트를 팝합니다. ".txt"파일이 생성 될 때 (폴더에서) 이벤트를 팝하는 방법?

C 또는 C++ 코드가 필요하지만 막혔습니다. 이 문제를 해결하는 방법을 모르겠습니다. inotify를 맨 페이지에서

+3

운영 체제는 무엇입니까? –

+2

@VJovic inotify는 리눅스와 관련이 있습니다. –

+1

@AbhijeetRastogi 그럼,이 질문은 리눅스 용입니까? –

답변

11

은 :

IN_CREATE   File/directory created in watched directory (*). 

는이 이벤트를 잡기하여 수행 할 수 있습니다. 맨 페이지에서 다시

:

Limitations and caveats 
     Inotify monitoring of directories is not recursive: to monitor subdirectories under a directory, additional watches must be created. This can take a significant 
     amount time for large directory trees. 

그래서, 당신은 재귀 부분을 직접 수행해야합니다. here에서 예를 찾아 볼 수 있습니다. 의견에 질문으로 당신은 또한 프로젝트 notify-tools

예를 살펴이 있어야합니다

[email protected] ~ $ ./a.out 
In /tmp/inotify1: 
The file abhijeet was created. 
In /tmp/inotify2: 
The file rastogi was created. 
^C 
[email protected] ~ $ 
: 그것은 새로운 파일에 대한 /tmp/inotify1 & /tmp/inotify2이 & 표시에게 이벤트

#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/inotify.h> 

#define EVENT_SIZE (sizeof (struct inotify_event)) 
#define BUF_LEN  (1024 * (EVENT_SIZE + 16)) 

int main(int argc, char **argv) 
{ 
    int length, i = 0; 
    int fd; 
    int wd[2]; 
    char buffer[BUF_LEN]; 

    fd = inotify_init(); 

    if (fd < 0) { 
     perror("inotify_init"); 
    } 

    wd[0] = inotify_add_watch(fd, "/tmp/inotify1", IN_CREATE); 
    wd[1] = inotify_add_watch (fd, "/tmp/inotify2", IN_CREATE); 

    while (1){ 
     struct inotify_event *event; 

     length = read(fd, buffer, BUF_LEN); 

     if (length < 0) { 
      perror("read"); 
     } 

     event = (struct inotify_event *) &buffer[ i ]; 

     if (event->len) { 
      if (event->wd == wd[0]) printf("%s\n", "In /tmp/inotify1: "); 
      else printf("%s\n", "In /tmp/inotify2: "); 
      if (event->mask & IN_CREATE) { 
       if (event->mask & IN_ISDIR) { 
        printf("The directory %s was created.\n", event->name);  
       } 
       else { 
        printf("The file %s was created.\n", event->name); 
       } 
      } 
     } 
    } 
    (void) inotify_rm_watch(fd, wd[0]); 
    (void) inotify_rm_watch(fd, wd[1]); 
    (void) close(fd); 

    exit(0); 
} 

테스트 실행을 생성 모니터링

+0

Thx하지만 C 또는 C++ 코드 예제가 필요합니다. 도움이 될까요? –

+1

@justAngela 내가 준 IBM 링크 외에도 여기에 Google 검색에서 제공되는 또 하나가 있습니다. http://www.thegeekstuff.com/2010/04/inotify-c-program-example/. 또한, 당신은 inotiwatch https://github.com/rvoicilas/inotify-tools/blob/master/src/inotify의 소스 코드를 보지 않으시겠습니까? –

+0

나는 출처에서 취한 ..하지만 나에게는 아무런 효과가 없다. ((제발, 내게는 제게 좋은 것보다 인쇄하십시오.) 제 2의 링크가 저에게 효과적이지 않습니다. –

1

fanotify도 있습니다. 그것으로 전체 마운트 지점에서 시계를 설정할 수 있습니다. 예제 코드 here을 확인하십시오.