2012-01-30 18 views
9

Tengo una carpeta llamada "Datas". Esta carpeta tiene una subcarpeta llamada "Bandeja de entrada" dentro de la cual hay varios archivos ".txt". Esta carpeta "Datas" se puede modificar y al final habrá varias subcarpetas con subcarpetas "Inbox" y archivos ".txt". Necesito controlar la carpeta "Datas" y el archivo ".txt" de la carpeta "Bandeja de entrada". ¿Cómo puedo hacer eso?¿Cómo controlar una carpeta con todas las subcarpetas y archivos dentro?

INotify solo está supervisando una carpeta y muestra eventos cuando se crean subcarpetas. ¿Cómo mostrar eventos cuando se crean archivos ".txt" (en qué carpeta)?

Necesito el código C o C++ pero estoy atascado. No sé cómo resolver esto.

+3

cual sistema operativo? –

+2

@VJovic inotify es un linux específico. –

+1

@AbhijeetRastogi Entonces, ¿esta es la pregunta para Linux? –

Respuesta

11

Desde la página de manual inotify:

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

Se puede hacer por la captura de este evento.

nuevo desde la página de manual:

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. 

Por lo tanto, tendrá que hacer la parte recursiva a sí mismo. Puede comenzar buscando un ejemplo en here. También debe tener una mirada en el proyecto notify-tools

ejemplo, como se le preguntó en los comentarios: Supervisa /tmp/inotify1 & /tmp/inotify2 para los nuevos archivos creados & muestra los eventos

#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); 
} 

Prueba de funcionamiento:

[email protected] ~ $ ./a.out 
In /tmp/inotify1: 
The file abhijeet was created. 
In /tmp/inotify2: 
The file rastogi was created. 
^C 
[email protected] ~ $ 
+0

Thx, pero necesito un ejemplo de código c o C++. ¿Puedes ayudarme? –

+1

@justAngela Además del enlace de IBM que di, aquí hay uno más que viene en la búsqueda de Google. http://www.thegeekstuff.com/2010/04/inotify-c-program-example/. Además, ¿por qué no miras el código fuente de inotiwatch https://github.com/rvoicilas/inotify-tools/blob/master/src/inotify? –

+0

lo tomé en la fuente ... pero nada funciona para mí: ((Por favor, imprima un ejemplo mejor que el mío si lo tiene. Por favor. El segundo enlace no funciona para mí –

1

También hay fanotify. Con él puedes configurar un reloj en un punto de montaje completo. Vea el código de ejemplo here.

Cuestiones relacionadas