1c540156cSNicolas Bonnefon #ifndef INOTIFYWATCHTOWERDRIVER_H 2c540156cSNicolas Bonnefon #define INOTIFYWATCHTOWERDRIVER_H 3c540156cSNicolas Bonnefon 4*b278d183SNicolas Bonnefon #include <memory> 5*b278d183SNicolas Bonnefon #include <mutex> 6*b278d183SNicolas Bonnefon #include <vector> 7c540156cSNicolas Bonnefon 8*b278d183SNicolas Bonnefon class ObservedFile; 9*b278d183SNicolas Bonnefon class ObservedFileList; 10*b278d183SNicolas Bonnefon 11*b278d183SNicolas Bonnefon class INotifyWatchTowerDriver { 12c540156cSNicolas Bonnefon public: 13*b278d183SNicolas Bonnefon class FileId { 14*b278d183SNicolas Bonnefon public: 15*b278d183SNicolas Bonnefon friend class INotifyWatchTowerDriver; 16*b278d183SNicolas Bonnefon 17*b278d183SNicolas Bonnefon FileId() {} 18*b278d183SNicolas Bonnefon bool operator==( const FileId& other ) const 19*b278d183SNicolas Bonnefon { return wd_ == other.wd_; } 20*b278d183SNicolas Bonnefon private: 21*b278d183SNicolas Bonnefon FileId( int wd ) { wd_ = wd; } 22*b278d183SNicolas Bonnefon int wd_ = -1; 23c540156cSNicolas Bonnefon }; 24*b278d183SNicolas Bonnefon class DirId { 25*b278d183SNicolas Bonnefon public: 26*b278d183SNicolas Bonnefon friend class INotifyWatchTowerDriver; 27*b278d183SNicolas Bonnefon 28*b278d183SNicolas Bonnefon DirId() {} 29*b278d183SNicolas Bonnefon bool operator==( const DirId& other ) const 30*b278d183SNicolas Bonnefon { return wd_ == other.wd_; } 31*b278d183SNicolas Bonnefon private: 32*b278d183SNicolas Bonnefon DirId( int wd ) { wd_ = wd; } 33*b278d183SNicolas Bonnefon int wd_ = -1; 34c540156cSNicolas Bonnefon }; 35*b278d183SNicolas Bonnefon class SymlinkId { 36*b278d183SNicolas Bonnefon public: 37*b278d183SNicolas Bonnefon friend class INotifyWatchTowerDriver; 38*b278d183SNicolas Bonnefon 39*b278d183SNicolas Bonnefon SymlinkId() {} 40*b278d183SNicolas Bonnefon bool operator==( const SymlinkId& other ) const 41*b278d183SNicolas Bonnefon { return wd_ == other.wd_; } 42*b278d183SNicolas Bonnefon private: 43*b278d183SNicolas Bonnefon SymlinkId( int wd ) { wd_ = wd; } 44*b278d183SNicolas Bonnefon int wd_ = -1; 45c540156cSNicolas Bonnefon }; 46c540156cSNicolas Bonnefon 47c540156cSNicolas Bonnefon INotifyWatchTowerDriver(); 48c540156cSNicolas Bonnefon 49*b278d183SNicolas Bonnefon FileId addFile( const std::string& file_name ); 50*b278d183SNicolas Bonnefon SymlinkId addSymlink( const std::string& file_name ); 51*b278d183SNicolas Bonnefon DirId addDir( const std::string& file_name ); 52c540156cSNicolas Bonnefon 53*b278d183SNicolas Bonnefon void removeFile( const FileId& file_id ); 54*b278d183SNicolas Bonnefon void removeSymlink( const SymlinkId& symlink_id ); 55c540156cSNicolas Bonnefon 56*b278d183SNicolas Bonnefon std::vector<ObservedFile*> waitAndProcessEvents( 57*b278d183SNicolas Bonnefon ObservedFileList* list, 58*b278d183SNicolas Bonnefon std::mutex* list_mutex ); 59*b278d183SNicolas Bonnefon 60*b278d183SNicolas Bonnefon private: 61*b278d183SNicolas Bonnefon // Only written at initialisation so no protection needed. 62*b278d183SNicolas Bonnefon const int inotify_fd_; 63*b278d183SNicolas Bonnefon 64*b278d183SNicolas Bonnefon // Private member functions 65*b278d183SNicolas Bonnefon size_t processINotifyEvent( const struct inotify_event* event, 66*b278d183SNicolas Bonnefon ObservedFileList* list, 67*b278d183SNicolas Bonnefon std::mutex* list_mutex, 68*b278d183SNicolas Bonnefon std::vector<ObservedFile*>* files_to_notify ); 69c540156cSNicolas Bonnefon }; 70c540156cSNicolas Bonnefon 71c540156cSNicolas Bonnefon #endif 72