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