$search
00001 00002 /*************************************************************************** 00003 * fam.h - File Alteration Monitor 00004 * 00005 * Created: Fri May 23 11:38:41 2008 00006 * Copyright 2006-2010 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 <sys/types.h> 00027 #include <map> 00028 #include <list> 00029 #include <string> 00030 #include <regex.h> 00031 00032 namespace fawkes { 00033 00034 class FamListener 00035 { 00036 public: 00037 virtual ~FamListener(); 00038 00039 static const unsigned int FAM_ACCESS; 00040 static const unsigned int FAM_MODIFY; 00041 static const unsigned int FAM_ATTRIB; 00042 static const unsigned int FAM_CLOSE_WRITE; 00043 static const unsigned int FAM_CLOSE_NOWRITE; 00044 static const unsigned int FAM_CLOSE; 00045 static const unsigned int FAM_OPEN; 00046 static const unsigned int FAM_MOVED_FROM; 00047 static const unsigned int FAM_MOVED_TO; 00048 static const unsigned int FAM_MOVE; 00049 static const unsigned int FAM_CREATE; 00050 static const unsigned int FAM_DELETE; 00051 static const unsigned int FAM_DELETE_SELF; 00052 static const unsigned int FAM_MOVE_SELF; 00053 00054 static const unsigned int FAM_UNMOUNT; 00055 static const unsigned int FAM_Q_OVERFLOW; 00056 static const unsigned int FAM_IGNORED; 00057 00058 static const unsigned int FAM_ONLYDIR; 00059 static const unsigned int FAM_DONT_FOLLOW; 00060 static const unsigned int FAM_MASK_ADD; 00061 static const unsigned int FAM_ISDIR; 00062 static const unsigned int FAM_ONESHOT; 00063 00064 static const unsigned int FAM_ALL_EVENTS; 00065 00066 00067 virtual void fam_event(const char *filename, unsigned int mask) = 0; 00068 }; 00069 00070 class FileAlterationMonitor 00071 { 00072 public: 00073 FileAlterationMonitor(); 00074 ~FileAlterationMonitor(); 00075 00076 void watch_dir(const char *dirpath); 00077 void watch_file(const char *filepath); 00078 void add_filter(const char *regex); 00079 00080 void process_events(int timeout = 0); 00081 void interrupt(); 00082 00083 void add_listener(FamListener *listener); 00084 void remove_listener(FamListener *listener); 00085 00086 private: 00087 std::list<FamListener *> __listeners; 00088 std::list<FamListener *>::iterator __lit; 00089 std::list<regex_t *> __regexes; 00090 std::list<regex_t *>::iterator __rxit; 00091 00092 int __inotify_fd; 00093 char *__inotify_buf; 00094 size_t __inotify_bufsize; 00095 std::map<int, std::string> __inotify_watches; 00096 std::map<int, std::string>::iterator __inotify_wit; 00097 00098 bool __interrupted; 00099 bool __interruptible; 00100 int __pipe_fds[2]; 00101 }; 00102 00103 } // end of namespace fawkes 00104 00105 #endif