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 17*dc7f5916SNicolas 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; } 22*dc7f5916SNicolas Bonnefon int wd_; 23c540156cSNicolas Bonnefon }; 24b278d183SNicolas Bonnefon class DirId { 25b278d183SNicolas Bonnefon public: 26b278d183SNicolas Bonnefon friend class INotifyWatchTowerDriver; 27b278d183SNicolas Bonnefon 28*dc7f5916SNicolas 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; } 33*dc7f5916SNicolas Bonnefon int wd_; 34c540156cSNicolas Bonnefon }; 35b278d183SNicolas Bonnefon class SymlinkId { 36b278d183SNicolas Bonnefon public: 37b278d183SNicolas Bonnefon friend class INotifyWatchTowerDriver; 38b278d183SNicolas Bonnefon 39*dc7f5916SNicolas 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; } 44*dc7f5916SNicolas Bonnefon int wd_; 45c540156cSNicolas Bonnefon }; 46c540156cSNicolas Bonnefon 47c540156cSNicolas Bonnefon INotifyWatchTowerDriver(); 48c540156cSNicolas Bonnefon 49b278d183SNicolas Bonnefon FileId addFile( const std::string& file_name ); 50b278d183SNicolas Bonnefon SymlinkId addSymlink( const std::string& file_name ); 51b278d183SNicolas Bonnefon DirId addDir( const std::string& file_name ); 52c540156cSNicolas Bonnefon 53b278d183SNicolas Bonnefon void removeFile( const FileId& file_id ); 54b278d183SNicolas Bonnefon void removeSymlink( const SymlinkId& symlink_id ); 55c540156cSNicolas Bonnefon 56b278d183SNicolas Bonnefon std::vector<ObservedFile*> waitAndProcessEvents( 57b278d183SNicolas Bonnefon ObservedFileList* list, 58b278d183SNicolas Bonnefon std::mutex* list_mutex ); 59b278d183SNicolas Bonnefon 60b278d183SNicolas Bonnefon private: 61b278d183SNicolas Bonnefon // Only written at initialisation so no protection needed. 62b278d183SNicolas Bonnefon const int inotify_fd_; 63b278d183SNicolas Bonnefon 64b278d183SNicolas Bonnefon // Private member functions 65b278d183SNicolas Bonnefon size_t processINotifyEvent( const struct inotify_event* event, 66b278d183SNicolas Bonnefon ObservedFileList* list, 67b278d183SNicolas Bonnefon std::mutex* list_mutex, 68b278d183SNicolas Bonnefon std::vector<ObservedFile*>* files_to_notify ); 69c540156cSNicolas Bonnefon }; 70c540156cSNicolas Bonnefon 71c540156cSNicolas Bonnefon #endif 72