184b2179eSNicolas Bonnefon #ifndef WATCHTOWER_H 284b2179eSNicolas Bonnefon #define WATCHTOWER_H 384b2179eSNicolas Bonnefon 4f09fa651SNicolas Bonnefon #include "config.h" 5f09fa651SNicolas Bonnefon 687e05652SNicolas Bonnefon #include <memory> 7c540156cSNicolas Bonnefon #include <atomic> 8c540156cSNicolas Bonnefon #include <thread> 9c540156cSNicolas Bonnefon #include <mutex> 1087e05652SNicolas Bonnefon 11c540156cSNicolas Bonnefon #include "watchtowerlist.h" 12b278d183SNicolas Bonnefon 13c540156cSNicolas Bonnefon // Allow the client to register for notification on an arbitrary number 14c540156cSNicolas Bonnefon // of files. It will be notified to any change (creation/deletion/modification) 15c540156cSNicolas Bonnefon // on those files. 16c540156cSNicolas Bonnefon // It is passed a platform specific driver. 1796bde7d5SNicolas Bonnefon 1896bde7d5SNicolas Bonnefon // FIXME: Where to put that so it is not dependant 1987e05652SNicolas Bonnefon // Registration object to implement RAII 2058f443c7SNicolas Bonnefon #ifdef HAS_TEMPLATE_ALIASES 2187e05652SNicolas Bonnefon using Registration = std::shared_ptr<void>; 2258f443c7SNicolas Bonnefon #else 2358f443c7SNicolas Bonnefon typedef std::shared_ptr<void> Registration; 24dc7f5916SNicolas Bonnefon #endif 2587e05652SNicolas Bonnefon 2696bde7d5SNicolas Bonnefon template<typename Driver> 2796bde7d5SNicolas Bonnefon class WatchTower { 2896bde7d5SNicolas Bonnefon public: 2987e05652SNicolas Bonnefon // Create an empty watchtower 30b278d183SNicolas Bonnefon WatchTower(); 3187e05652SNicolas Bonnefon // Destroy the object 32c540156cSNicolas Bonnefon ~WatchTower(); 3387e05652SNicolas Bonnefon 3487e05652SNicolas Bonnefon // Add a file to the notification list. notification will be called when 3587e05652SNicolas Bonnefon // the file is modified, moved or deleted. 3687e05652SNicolas Bonnefon // Lifetime of the notification is tied to the Registration object returned. 3787e05652SNicolas Bonnefon // Note the notification function is called with a mutex held and in a 3887e05652SNicolas Bonnefon // third party thread, beware of races! 39c540156cSNicolas Bonnefon Registration addFile( const std::string& file_name, 40c540156cSNicolas Bonnefon std::function<void()> notification ); 41a0936e1eSNicolas Bonnefon 42*3104b268SNicolas Bonnefon // Number of watched directories (for tests) 43*3104b268SNicolas Bonnefon unsigned int numberWatchedDirectories() const; 44*3104b268SNicolas Bonnefon 45a0936e1eSNicolas Bonnefon private: 46b278d183SNicolas Bonnefon // The driver (parametrised) 4796bde7d5SNicolas Bonnefon Driver driver_; 48a0936e1eSNicolas Bonnefon 49c540156cSNicolas Bonnefon // List of files/dirs observed 50f09fa651SNicolas Bonnefon ObservedFileList<Driver> observed_file_list_; 51a0936e1eSNicolas Bonnefon 52c540156cSNicolas Bonnefon // Protects the observed_file_list_ 53c540156cSNicolas Bonnefon std::mutex observers_mutex_; 54c540156cSNicolas Bonnefon 55c540156cSNicolas Bonnefon // Thread 56c540156cSNicolas Bonnefon std::atomic_bool running_; 57c540156cSNicolas Bonnefon std::thread thread_; 58c540156cSNicolas Bonnefon 59c540156cSNicolas Bonnefon // Exist as long as the onject exists, to ensure observers won't try to 60c540156cSNicolas Bonnefon // call us if we are dead. 61c540156cSNicolas Bonnefon std::shared_ptr<void> heartBeat_; 62c540156cSNicolas Bonnefon 63c540156cSNicolas Bonnefon // Private member functions 64c540156cSNicolas Bonnefon static void removeNotification( WatchTower* watch_tower, 65c540156cSNicolas Bonnefon std::shared_ptr<void> notification ); 66c540156cSNicolas Bonnefon void run(); 6787e05652SNicolas Bonnefon }; 6884b2179eSNicolas Bonnefon 6996bde7d5SNicolas Bonnefon // Class template implementation 7096bde7d5SNicolas Bonnefon 7196bde7d5SNicolas Bonnefon #include <algorithm> 7296bde7d5SNicolas Bonnefon 7396bde7d5SNicolas Bonnefon #include <sys/types.h> 7496bde7d5SNicolas Bonnefon #include <sys/stat.h> 7596bde7d5SNicolas Bonnefon #include <unistd.h> 7696bde7d5SNicolas Bonnefon 7796bde7d5SNicolas Bonnefon #include "log.h" 7896bde7d5SNicolas Bonnefon 7996bde7d5SNicolas Bonnefon namespace { 8096bde7d5SNicolas Bonnefon bool isSymLink( const std::string& file_name ); 8196bde7d5SNicolas Bonnefon std::string directory_path( const std::string& path ); 8296bde7d5SNicolas Bonnefon }; 8396bde7d5SNicolas Bonnefon 8496bde7d5SNicolas Bonnefon template <typename Driver> 8596bde7d5SNicolas Bonnefon WatchTower<Driver>::WatchTower() 86*3104b268SNicolas Bonnefon : driver_(), thread_(), 8796bde7d5SNicolas Bonnefon heartBeat_(std::shared_ptr<void>((void*) 0xDEADC0DE, [] (void*) {})) 8896bde7d5SNicolas Bonnefon { 8996bde7d5SNicolas Bonnefon running_ = true; 9096bde7d5SNicolas Bonnefon thread_ = std::thread( &WatchTower::run, this ); 9196bde7d5SNicolas Bonnefon } 9296bde7d5SNicolas Bonnefon 9396bde7d5SNicolas Bonnefon template <typename Driver> 9496bde7d5SNicolas Bonnefon WatchTower<Driver>::~WatchTower() 9596bde7d5SNicolas Bonnefon { 9696bde7d5SNicolas Bonnefon running_ = false; 97b0345991SNicolas Bonnefon driver_.interruptWait(); 9896bde7d5SNicolas Bonnefon thread_.join(); 9996bde7d5SNicolas Bonnefon } 10096bde7d5SNicolas Bonnefon 10196bde7d5SNicolas Bonnefon template <typename Driver> 10296bde7d5SNicolas Bonnefon Registration WatchTower<Driver>::addFile( 10396bde7d5SNicolas Bonnefon const std::string& file_name, 10496bde7d5SNicolas Bonnefon std::function<void()> notification ) 10596bde7d5SNicolas Bonnefon { 106f09fa651SNicolas Bonnefon // LOG(logDEBUG) << "WatchTower::addFile " << file_name; 10796bde7d5SNicolas Bonnefon 108*3104b268SNicolas Bonnefon std::weak_ptr<void> weakHeartBeat(heartBeat_); 109*3104b268SNicolas Bonnefon 11096bde7d5SNicolas Bonnefon std::lock_guard<std::mutex> lock( observers_mutex_ ); 11196bde7d5SNicolas Bonnefon 112f09fa651SNicolas Bonnefon auto existing_observed_file = 11396bde7d5SNicolas Bonnefon observed_file_list_.searchByName( file_name ); 11496bde7d5SNicolas Bonnefon 11596bde7d5SNicolas Bonnefon std::shared_ptr<std::function<void()>> ptr( new std::function<void()>(std::move( notification )) ); 11696bde7d5SNicolas Bonnefon 11796bde7d5SNicolas Bonnefon if ( ! existing_observed_file ) 11896bde7d5SNicolas Bonnefon { 119f09fa651SNicolas Bonnefon typename Driver::SymlinkId symlink_id; 12096bde7d5SNicolas Bonnefon 12196bde7d5SNicolas Bonnefon auto file_id = driver_.addFile( file_name ); 12296bde7d5SNicolas Bonnefon 12396bde7d5SNicolas Bonnefon if ( isSymLink( file_name ) ) 12496bde7d5SNicolas Bonnefon { 12596bde7d5SNicolas Bonnefon // We want to follow the name (as opposed to the inode) 12696bde7d5SNicolas Bonnefon // so we watch the symlink as well. 12796bde7d5SNicolas Bonnefon symlink_id = driver_.addSymlink( file_name ); 12896bde7d5SNicolas Bonnefon } 12996bde7d5SNicolas Bonnefon 13096bde7d5SNicolas Bonnefon auto new_file = observed_file_list_.addNewObservedFile( 131f09fa651SNicolas Bonnefon ObservedFile<Driver>( file_name, ptr, file_id, symlink_id ) ); 13296bde7d5SNicolas Bonnefon 13396bde7d5SNicolas Bonnefon auto dir = observed_file_list_.watchedDirectoryForFile( file_name ); 13496bde7d5SNicolas Bonnefon if ( ! dir ) 13596bde7d5SNicolas Bonnefon { 136f09fa651SNicolas Bonnefon LOG(logDEBUG) << "WatchTower::addFile dir for " << file_name 13796bde7d5SNicolas Bonnefon << " not watched, adding..."; 138*3104b268SNicolas Bonnefon dir = observed_file_list_.addWatchedDirectoryForFile( file_name, 139*3104b268SNicolas Bonnefon [this, weakHeartBeat] (ObservedDir<Driver>* dir) { 140*3104b268SNicolas Bonnefon if ( auto heart_beat = weakHeartBeat.lock() ) { 141*3104b268SNicolas Bonnefon driver_.removeDir( dir->dir_id_ ); 142*3104b268SNicolas Bonnefon } } ); 14396bde7d5SNicolas Bonnefon 14496bde7d5SNicolas Bonnefon dir->dir_id_ = driver_.addDir( dir->path ); 14596bde7d5SNicolas Bonnefon } 14696bde7d5SNicolas Bonnefon 147f09fa651SNicolas Bonnefon // Associate the dir to the file 14896bde7d5SNicolas Bonnefon new_file->dir_ = dir; 149f09fa651SNicolas Bonnefon 150f09fa651SNicolas Bonnefon LOG(logDEBUG) << "dir ref count is " << dir.use_count(); 15196bde7d5SNicolas Bonnefon } 15296bde7d5SNicolas Bonnefon else 15396bde7d5SNicolas Bonnefon { 15496bde7d5SNicolas Bonnefon existing_observed_file->addCallback( ptr ); 15596bde7d5SNicolas Bonnefon } 15696bde7d5SNicolas Bonnefon 15796bde7d5SNicolas Bonnefon // Returns a shared pointer that removes its own entry 15896bde7d5SNicolas Bonnefon // from the list of watched stuff when it goes out of scope! 15996bde7d5SNicolas Bonnefon // Uses a custom deleter to do the work. 16096bde7d5SNicolas Bonnefon return std::shared_ptr<void>( 0x0, [this, ptr, weakHeartBeat] (void*) { 16196bde7d5SNicolas Bonnefon if ( auto heart_beat = weakHeartBeat.lock() ) 162dc7f5916SNicolas Bonnefon WatchTower<Driver>::removeNotification( this, ptr ); 16396bde7d5SNicolas Bonnefon } ); 16496bde7d5SNicolas Bonnefon } 16596bde7d5SNicolas Bonnefon 166*3104b268SNicolas Bonnefon template <typename Driver> 167*3104b268SNicolas Bonnefon unsigned int WatchTower<Driver>::numberWatchedDirectories() const 168*3104b268SNicolas Bonnefon { 169*3104b268SNicolas Bonnefon return observed_file_list_.numberWatchedDirectories(); 170*3104b268SNicolas Bonnefon } 171*3104b268SNicolas Bonnefon 17296bde7d5SNicolas Bonnefon // 17396bde7d5SNicolas Bonnefon // Private functions 17496bde7d5SNicolas Bonnefon // 17596bde7d5SNicolas Bonnefon 17696bde7d5SNicolas Bonnefon // Called by the dtor for a registration object 17796bde7d5SNicolas Bonnefon template <typename Driver> 17896bde7d5SNicolas Bonnefon void WatchTower<Driver>::removeNotification( 17996bde7d5SNicolas Bonnefon WatchTower* watch_tower, std::shared_ptr<void> notification ) 18096bde7d5SNicolas Bonnefon { 18196bde7d5SNicolas Bonnefon LOG(logDEBUG) << "WatchTower::removeNotification"; 18296bde7d5SNicolas Bonnefon 18396bde7d5SNicolas Bonnefon std::lock_guard<std::mutex> lock( watch_tower->observers_mutex_ ); 18496bde7d5SNicolas Bonnefon 18596bde7d5SNicolas Bonnefon auto file = 18696bde7d5SNicolas Bonnefon watch_tower->observed_file_list_.removeCallback( notification ); 18796bde7d5SNicolas Bonnefon 18896bde7d5SNicolas Bonnefon if ( file ) 18996bde7d5SNicolas Bonnefon { 19096bde7d5SNicolas Bonnefon watch_tower->driver_.removeFile( file->file_id_ ); 19196bde7d5SNicolas Bonnefon watch_tower->driver_.removeSymlink( file->symlink_id_ ); 19296bde7d5SNicolas Bonnefon } 19396bde7d5SNicolas Bonnefon } 19496bde7d5SNicolas Bonnefon 19596bde7d5SNicolas Bonnefon // Run in its own thread 19696bde7d5SNicolas Bonnefon template <typename Driver> 19796bde7d5SNicolas Bonnefon void WatchTower<Driver>::run() 19896bde7d5SNicolas Bonnefon { 19996bde7d5SNicolas Bonnefon while ( running_ ) { 200*3104b268SNicolas Bonnefon std::unique_lock<std::mutex> lock( observers_mutex_ ); 201*3104b268SNicolas Bonnefon 20296bde7d5SNicolas Bonnefon auto files = driver_.waitAndProcessEvents( 203*3104b268SNicolas Bonnefon &observed_file_list_, &lock ); 204*3104b268SNicolas Bonnefon LOG(logDEBUG) << "WatchTower::run: waitAndProcessEvents returned " 205*3104b268SNicolas Bonnefon << files.size() << " files."; 20696bde7d5SNicolas Bonnefon 20796bde7d5SNicolas Bonnefon for ( auto file: files ) { 20896bde7d5SNicolas Bonnefon for ( auto observer: file->callbacks ) { 209*3104b268SNicolas Bonnefon LOG(logDEBUG) << "WatchTower::run: notifying the client!"; 21096bde7d5SNicolas Bonnefon // Here we have to cast our generic pointer back to 21196bde7d5SNicolas Bonnefon // the function pointer in order to perform the call 21296bde7d5SNicolas Bonnefon const std::shared_ptr<std::function<void()>> fptr = 21396bde7d5SNicolas Bonnefon std::static_pointer_cast<std::function<void()>>( observer ); 21496bde7d5SNicolas Bonnefon // The observer is called with the mutex held, 21596bde7d5SNicolas Bonnefon // Let's hope it doesn't do anything too funky. 21696bde7d5SNicolas Bonnefon (*fptr)(); 21796bde7d5SNicolas Bonnefon } 21896bde7d5SNicolas Bonnefon } 21996bde7d5SNicolas Bonnefon } 22096bde7d5SNicolas Bonnefon } 22196bde7d5SNicolas Bonnefon 22296bde7d5SNicolas Bonnefon namespace { 22396bde7d5SNicolas Bonnefon bool isSymLink( const std::string& file_name ) 22496bde7d5SNicolas Bonnefon { 225f09fa651SNicolas Bonnefon #ifdef HAVE_SYMLINK 22696bde7d5SNicolas Bonnefon struct stat buf; 22796bde7d5SNicolas Bonnefon 22896bde7d5SNicolas Bonnefon lstat( file_name.c_str(), &buf ); 22996bde7d5SNicolas Bonnefon return ( S_ISLNK(buf.st_mode) ); 230f09fa651SNicolas Bonnefon #else 231f09fa651SNicolas Bonnefon return false; 232f09fa651SNicolas Bonnefon #endif 23396bde7d5SNicolas Bonnefon } 23496bde7d5SNicolas Bonnefon }; 23596bde7d5SNicolas Bonnefon 23684b2179eSNicolas Bonnefon #endif 237