?
Solved

Directory and SubDirectory watcher

Posted on 2010-01-07
12
Medium Priority
?
568 Views
Last Modified: 2012-05-08
HI
I am linux Admin, and i last touched  C when i was 3rd year Computer science student!! after that from last good couple of year did not touch C

anyway ..
i am looking for some solution which i explained to this questions :
http://www.experts-exchange.com/OS/Linux/Q_25026679.html?cid=1576

while looking in google, i found this C code which works as directory watcher
http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html

Now,i tryed this code, its only works the directory, it does not go inside another directory
Example, if i set :

wd = inotify_add_watch( fd, "/htdocs",
it will just keep watch first 1st level of this directory, any directory bellow this will not watch
is there any way to add this facilities on this code ???


also : how can i run this code always?? if i run this code
./watcher  &

it will just fire onece, but after that it will exit, so how can i run this code always???

thanks
 
0
Comment
Question by:fosiul01
  • 5
  • 4
  • 3
12 Comments
 
LVL 5

Accepted Solution

by:
nuke3dd earned 1500 total points
ID: 26199461
This does not directly answer your question, but maybe it will help you to avoid having to reinvent something:
http://iwatch.sourceforge.net/index.html
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 26201332
>>it will just keep watch first 1st level of this directory, any directory bellow this
>>will not watch

According to the manpages for that API (http://linux.die.net/man/7/inotify and http://linux.die.net/man/2/inotify_add_watch), it seems that watching a subtree is not implemented, you'd have to set a watch for each subdirectory individually. Maybe 'fileschanged (http://fileschanged.sourceforge.net/) could serve you better here - similar to the one suggested by nuke3dd.

>>also : how can i run this code always??

That'd be the simpler part, e.g.
#include <stdio.h>
#include <stdlib.h>
#include <errno.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;
  char buffer[BUF_LEN];

  fd = inotify_init();

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

  wd = inotify_add_watch( fd, "/home/strike", 
                         IN_MODIFY | IN_CREATE | IN_DELETE );
  length = read( fd, buffer, BUF_LEN );  

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

  while (1) { // <-------------- START loop to continuously monitor files

  while ( i < length ) {
    struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
    if ( event->len ) {
      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 );
        }
      }
      else if ( event->mask & IN_DELETE ) {
        if ( event->mask & IN_ISDIR ) {
          printf( "The directory %s was deleted.\n", event->name );       
        }
        else {
          printf( "The file %s was deleted.\n", event->name );
        }
      }
      else if ( event->mask & IN_MODIFY ) {
        if ( event->mask & IN_ISDIR ) {
          printf( "The directory %s was modified.\n", event->name );
        }
        else {
          printf( "The file %s was modified.\n", event->name );
        }
      }
    }
    i += EVENT_SIZE + event->len;
  }

  } // <-------------- END loop to continuously monitor files

  ( void ) inotify_rm_watch( fd, wd );
  ( void ) close( fd );

  exit( 0 );
}

Open in new window

0
 
LVL 29

Author Comment

by:fosiul01
ID: 26201471
HI
thanks @jkr and @nuke3dd

@jkr :
i compiled as your code, then run it , but still it does not continue .. after first file watch, it does not do anything

@nuke3dd..
my purpose was to monitor a directory, if programm see, there is any changes of that directory it will fire rsync programm to copy data from that directory to anotehr server...

so i am not thinking of security measure.. its mainly file transfer  .... ( near to real time)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:jkr
ID: 26201553
>>i compiled as your code, then run it , but still it does not continue ..

Sorry, overhurried change on my side, the loop should be different and enclose the 'read()' call:


#include <stdio.h>
#include <stdlib.h>
#include <errno.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;
  char buffer[BUF_LEN];

  fd = inotify_init();

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

  wd = inotify_add_watch( fd, "/home/strike", 
                         IN_MODIFY | IN_CREATE | IN_DELETE );


  while (1) { // <-------------- START loop to continuously monitor files

  length = read( fd, buffer, BUF_LEN );  

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

  while ( i < length ) {
    struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
    if ( event->len ) {
      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 );
        }
      }
      else if ( event->mask & IN_DELETE ) {
        if ( event->mask & IN_ISDIR ) {
          printf( "The directory %s was deleted.\n", event->name );       
        }
        else {
          printf( "The file %s was deleted.\n", event->name );
        }
      }
      else if ( event->mask & IN_MODIFY ) {
        if ( event->mask & IN_ISDIR ) {
          printf( "The directory %s was modified.\n", event->name );
        }
        else {
          printf( "The file %s was modified.\n", event->name );
        }
      }
    }
    i += EVENT_SIZE + event->len;
  }

  } // <-------------- END loop to continuously monitor files

  ( void ) inotify_rm_watch( fd, wd );
  ( void ) close( fd );

  exit( 0 );
}

Open in new window

0
 
LVL 5

Assisted Solution

by:nuke3dd
nuke3dd earned 1500 total points
ID: 26201566
Yes, iWatch can execute a command if an event occures. If it's not flexible enough there's always the possibility to modify it, since it's opensource.
0
 
LVL 29

Author Comment

by:fosiul01
ID: 26201614
Sorry , nop, still its,just taking the first modification,

like if create or delete anything from directory first time, it will work, but after that, it does not do anything
0
 
LVL 29

Author Comment

by:fosiul01
ID: 26201637
@nuke3dd, iwatch is comming as file binary...

its saying its perl module, but author is not providing any perl code to modify it
0
 
LVL 86

Expert Comment

by:jkr
ID: 26201638
OK, let's take that to the extreme then ;o)
#include <stdio.h>
#include <stdlib.h>
#include <errno.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;
  char buffer[BUF_LEN];

  while (1) { // <-------------- START loop to continuously monitor files

  fd = inotify_init();

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

  wd = inotify_add_watch( fd, "/home/strike", 
                         IN_MODIFY | IN_CREATE | IN_DELETE );


  length = read( fd, buffer, BUF_LEN );  

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

  while ( i < length ) {
    struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
    if ( event->len ) {
      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 );
        }
      }
      else if ( event->mask & IN_DELETE ) {
        if ( event->mask & IN_ISDIR ) {
          printf( "The directory %s was deleted.\n", event->name );       
        }
        else {
          printf( "The file %s was deleted.\n", event->name );
        }
      }
      else if ( event->mask & IN_MODIFY ) {
        if ( event->mask & IN_ISDIR ) {
          printf( "The directory %s was modified.\n", event->name );
        }
        else {
          printf( "The file %s was modified.\n", event->name );
        }
      }
    }
    i += EVENT_SIZE + event->len;
  }

  ( void ) inotify_rm_watch( fd, wd );
  ( void ) close( fd );

  } // <-------------- END loop to continuously monitor files

  exit( 0 );
}

Open in new window

0
 
LVL 29

Author Comment

by:fosiul01
ID: 26201681
hahaa, sorry,... still no luck....

i am running the programme by ./iwatch



0
 
LVL 5

Expert Comment

by:nuke3dd
ID: 26201689
It's hosted at sourceforge, so it has to be opensource. The file included in the archive is pure perl, not a binary.
0
 
LVL 29

Author Comment

by:fosiul01
ID: 26201884
@nuke3dd, Sorry i cant find it, can you please give me the right link to download the source code??
0
 
LVL 5

Assisted Solution

by:nuke3dd
nuke3dd earned 1500 total points
ID: 26204661
Ok, here's the direct link to the archive containing the iwatch perl source (which also the executable):
http://downloads.sourceforge.net/project/iwatch/iwatch/0.2.2/iwatch-0.2.2.tgz?use_mirror=heanet
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question