1 #ifndef INOTIFYWATCHTOWERDRIVER_H 2 #define INOTIFYWATCHTOWERDRIVER_H 3 4 #include <memory> 5 #include <mutex> 6 #include <vector> 7 8 template <typename Driver> 9 class ObservedFile; 10 template <typename Driver> 11 class ObservedFileList; 12 13 class INotifyWatchTowerDriver { 14 public: 15 class FileId { 16 public: 17 friend class INotifyWatchTowerDriver; 18 19 FileId() { wd_ = -1; } 20 bool operator==( const FileId& other ) const 21 { return wd_ == other.wd_; } 22 private: 23 FileId( int wd ) { wd_ = wd; } 24 int wd_; 25 }; 26 class DirId { 27 public: 28 friend class INotifyWatchTowerDriver; 29 30 DirId() { wd_ = -1; } 31 bool operator==( const DirId& other ) const 32 { return wd_ == other.wd_; } 33 private: 34 DirId( int wd ) { wd_ = wd; } 35 int wd_; 36 }; 37 class SymlinkId { 38 public: 39 friend class INotifyWatchTowerDriver; 40 41 SymlinkId() { wd_ = -1; } 42 bool operator==( const SymlinkId& other ) const 43 { return wd_ == other.wd_; } 44 private: 45 SymlinkId( int wd ) { wd_ = wd; } 46 int wd_; 47 }; 48 49 #ifdef HAS_TEMPLATE_ALIASES 50 using INotifyObservedFile = ObservedFile<INotifyWatchTowerDriver>; 51 using INotifyObservedFileList = ObservedFileList<INotifyWatchTowerDriver>; 52 #else 53 typedef ObservedFile<INotifyWatchTowerDriver> INotifyObservedFile; 54 typedef ObservedFileList<INotifyWatchTowerDriver> INotifyObservedFileList; 55 #endif 56 57 // Default constructor 58 INotifyWatchTowerDriver(); 59 ~INotifyWatchTowerDriver(); 60 61 // No copy/assign/move please 62 INotifyWatchTowerDriver( const INotifyWatchTowerDriver& ) = delete; 63 INotifyWatchTowerDriver& operator=( const INotifyWatchTowerDriver& ) = delete; 64 INotifyWatchTowerDriver( const INotifyWatchTowerDriver&& ) = delete; 65 INotifyWatchTowerDriver& operator=( const INotifyWatchTowerDriver&& ) = delete; 66 67 FileId addFile( const std::string& file_name ); 68 SymlinkId addSymlink( const std::string& file_name ); 69 DirId addDir( const std::string& file_name ); 70 71 void removeFile( const FileId& file_id ); 72 void removeSymlink( const SymlinkId& symlink_id ); 73 74 std::vector<INotifyObservedFile*> waitAndProcessEvents( 75 INotifyObservedFileList* list, 76 std::mutex* list_mutex ); 77 void interruptWait(); 78 79 private: 80 // Only written at initialisation so no protection needed. 81 const int inotify_fd_; 82 83 // Breaking pipe 84 int breaking_pipe_read_fd_; 85 int breaking_pipe_write_fd_; 86 87 // Private member functions 88 size_t processINotifyEvent( const struct inotify_event* event, 89 INotifyObservedFileList* list, 90 std::mutex* list_mutex, 91 std::vector<INotifyObservedFile*>* files_to_notify ); 92 }; 93 94 #endif 95