fam.h

00001 
00002 /***************************************************************************
00003  *  fam.h - File Alteration Monitor
00004  *
00005  *  Created: Fri May 23 11:38:41 2008
00006  *  Copyright  2006-2008  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #ifndef __UTILS_SYSTEM_FAM_H_
00024 #define __UTILS_SYSTEM_FAM_H_
00025 
00026 #include <core/utils/lock_list.h>
00027 
00028 #include <sys/types.h>
00029 #include <map>
00030 #include <string>
00031 #include <regex.h>
00032 
00033 namespace fawkes {
00034 
00035 class FamListener
00036 {
00037  public:
00038   virtual ~FamListener();
00039 
00040   static const unsigned int FAM_ACCESS;
00041   static const unsigned int FAM_MODIFY;
00042   static const unsigned int FAM_ATTRIB;
00043   static const unsigned int FAM_CLOSE_WRITE;
00044   static const unsigned int FAM_CLOSE_NOWRITE;
00045   static const unsigned int FAM_CLOSE;
00046   static const unsigned int FAM_OPEN;
00047   static const unsigned int FAM_MOVED_FROM;
00048   static const unsigned int FAM_MOVED_TO;
00049   static const unsigned int FAM_MOVE;
00050   static const unsigned int FAM_CREATE;
00051   static const unsigned int FAM_DELETE;
00052   static const unsigned int FAM_DELETE_SELF;
00053   static const unsigned int FAM_MOVE_SELF;
00054 
00055   static const unsigned int FAM_UNMOUNT;
00056   static const unsigned int FAM_Q_OVERFLOW;
00057   static const unsigned int FAM_IGNORED;
00058 
00059   static const unsigned int FAM_ONLYDIR;
00060   static const unsigned int FAM_DONT_FOLLOW;
00061   static const unsigned int FAM_MASK_ADD;
00062   static const unsigned int FAM_ISDIR;
00063   static const unsigned int FAM_ONESHOT;
00064 
00065   static const unsigned int FAM_ALL_EVENTS;
00066 
00067 
00068   virtual void fam_event(const char *filename, unsigned int mask) = 0;
00069 };
00070 
00071 class FileAlterationMonitor
00072 {
00073  public:
00074   FileAlterationMonitor();
00075   ~FileAlterationMonitor();
00076 
00077   void watch_dir(const char *dirpath);
00078   void watch_file(const char *filepath);
00079   void add_filter(const char *regex);
00080 
00081   void process_events(int timeout = 0);
00082   void interrupt();
00083 
00084   void add_listener(FamListener *listener);
00085   void remove_listener(FamListener *listener);
00086 
00087  private:
00088   LockList<FamListener *>            __listeners;
00089   LockList<FamListener *>::iterator  __lit;
00090   LockList<regex_t *>                __regexes;
00091   LockList<regex_t *>::iterator      __rxit;
00092 
00093   int     __inotify_fd;
00094   char   *__inotify_buf;
00095   size_t  __inotify_bufsize;
00096   std::map<int, std::string> __inotify_watches;
00097   std::map<int, std::string>::iterator __inotify_wit;
00098 
00099   bool __interrupted;
00100   bool __interruptible;
00101   int  __pipe_fds[2];
00102 };
00103 
00104 } // end of namespace fawkes
00105 
00106 #endif