Dear all,

Kindly look at the filealter.c file here.

http://gayatri-hitech.com/languageclasses/index.html

It is a small utility using kqueue(). I guess you don't have it in Linux.

You can use this perl module SGI::FAM and this perl code.

#!/usr/bin/perl -w
use strict;

use SGI::FAM;

my $conn = new SGI::FAM;
$conn->monitor( "/var/log/messages" );

while (1) {
my $event = $conn->next_event;
print $event->filename, " ", $event->type, "\n";
}


Basically the idea of watching files or directories is very important
for programming.

Yesterday I could not get this working with FAM. I had to instead fall
back on kqueue. There are many alternatives like dnotify on
linux.

Here is the filealter.c file.


#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
   int f, kq, nev;
   struct kevent change;
   struct kevent event;

   kq = kqueue();
   if (kq == -1)
       perror("kqueue");

   f = open("/tmp/foo", O_RDONLY);
   if (f == -1)
       perror("open");

   EV_SET(&change, f, EVFILT_VNODE,
          EV_ADD | EV_ENABLE | EV_ONESHOT,
          NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB,
          0, 0);

   for (;;) {
       nev = kevent(kq, &change, 1, & event, 1, NULL);
       if (nev == -1)
           perror("kevent");
       else if (nev > 0) {
           if (event.fflags & NOTE_DELETE) {
               printf("File deleted\n");
               break;
           }
           if (event.fflags & NOTE_EXTEND ||
               event.fflags & NOTE_WRITE)
               printf("File modified\n");
           if (event.fflags & NOTE_ATTRIB)
               printf("File attributes modified\n");
       }
   }

   close(kq);
   close(f);
   return EXIT_SUCCESS;
}

kqueue() is an event notification mechanism using which you can watch
for file and directory changes.

It can be used for many other things as well. We first register for an
event using the EV_SET() macro.

Please remember that macros are always written in uppercase. It is
just a convention and not a rule.

kqueue() is a FreeBSD construct. You have it in other BSDs too. On
OpenBSD the preferred thing to do is use libevent.

But I am not quite sure if that can be used for file monitoring.

I have to find out. But this code works on my OpenBSD machine. I did an

$ apt-cache search kqueue
libev-dev - static library, header files, and docs for libev
libev-libevent-dev - libevent event loop compatibility wrapper for libev
libev3 - high-performance event loop library modelled after libevent
libevent1 - An asynchronous event notification library

Interesting. I think you should use libev instead of inotify directly on linux.

Please try it out and let me know.

Thanks.

-Girish

-- 
Gayatri Hitech
web: http://gayatri-hitech.com

SpamCheetah Spam filter:
http://spam-cheetah.com
_______________________________________________
To unsubscribe, email ilugc-requ...@ae.iitm.ac.in with 
"unsubscribe <password> <address>"
in the subject or body of the message.  
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc

Reply via email to