9

I have a folder called "Datas". This folder has a subfolder called "Inbox" inside of which there are multiple ".txt" files. This "Datas" folder can be modified and in the end there will be multiple subfolders with "Inbox" subfolders and ".txt" files. I need to monitor the "Datas" folder and the ".txt" file from the "Inbox" folder. How can I do that?

INotify is just monitoring a folder and pops events when subfolders are created. How to pop events when ".txt" files are created (in which folder)?

I need C or C++ code but I am stuck. I do not know how to solve this.

Deanna
  • 23,876
  • 7
  • 71
  • 156
just ME
  • 1,817
  • 6
  • 32
  • 53

2 Answers2

11

From the inotify manpage:

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

It can be done by catching this event.

Again from the manpage:

  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.

So, you will need to do the recursive part yourself. You can start by looking an example from here. You should also have a look at the project notify-tools

EXAMPLE as asked in comments: It monitors /tmp/inotify1 & /tmp/inotify2 for new files created & displays the events

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

Test run:

shadyabhi@archlinux ~ $ ./a.out 
In /tmp/inotify1: 
The file abhijeet was created.
In /tmp/inotify2: 
The file rastogi was created.
^C
shadyabhi@archlinux ~ $
Sven Almgren
  • 183
  • 10
shadyabhi
  • 16,675
  • 26
  • 80
  • 131
  • 1
    @justAngela Apart from the IBM link I gave, here is one more that comes in google search. http://www.thegeekstuff.com/2010/04/inotify-c-program-example/ . Also, why don't you look at the source code of inotiwatch https://github.com/rvoicilas/inotify-tools/blob/master/src/inotify? – shadyabhi Jan 31 '12 at 07:45
  • i did took at the source..but nothing works for me:((. Please print an example better than mine if you have. Please. The second link doesn't work for me – just ME Jan 31 '12 at 08:57
  • @justAngela https://github.com/rvoicilas/inotify-tools/blob/master/src/inotifywatch.c does what you want. LINE 213. – shadyabhi Jan 31 '12 at 14:19
  • could you please write a short example? please. i cant seem to work it out! – just ME Feb 01 '12 at 16:12
  • @justAngela Check the example. It monitors two directories at once. – shadyabhi Feb 01 '12 at 17:51
  • This won't monitor new subdirectories of /tmp/inotify if they are created while the program is running. – Rich Nov 24 '15 at 19:22
1

There is also fanotify. With it you can set a watch on a whole mountpoint. Check out the example code here.

Neox
  • 2,000
  • 13
  • 12