xref: /glogg/src/watchtower.h (revision 8b11848fd9995077713535870cee0df00a8eeea0) !
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 
423104b268SNicolas Bonnefon     // Number of watched directories (for tests)
433104b268SNicolas Bonnefon     unsigned int numberWatchedDirectories() const;
443104b268SNicolas 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
6491f7c705SNicolas Bonnefon     std::tuple<typename Driver::FileId, typename Driver::SymlinkId>
6591f7c705SNicolas Bonnefon         addFileToDriver( const std::string& );
66c540156cSNicolas Bonnefon     static void removeNotification( WatchTower* watch_tower,
67c540156cSNicolas Bonnefon             std::shared_ptr<void> notification );
68c540156cSNicolas Bonnefon     void run();
6987e05652SNicolas Bonnefon };
7084b2179eSNicolas Bonnefon 
7196bde7d5SNicolas Bonnefon // Class template implementation
7296bde7d5SNicolas Bonnefon 
7396bde7d5SNicolas Bonnefon #include <algorithm>
7496bde7d5SNicolas Bonnefon 
7596bde7d5SNicolas Bonnefon #include <sys/types.h>
7696bde7d5SNicolas Bonnefon #include <sys/stat.h>
7796bde7d5SNicolas Bonnefon #include <unistd.h>
7896bde7d5SNicolas Bonnefon 
7996bde7d5SNicolas Bonnefon #include "log.h"
8096bde7d5SNicolas Bonnefon 
8196bde7d5SNicolas Bonnefon namespace {
8296bde7d5SNicolas Bonnefon     bool isSymLink( const std::string& file_name );
8396bde7d5SNicolas Bonnefon     std::string directory_path( const std::string& path );
8496bde7d5SNicolas Bonnefon };
8596bde7d5SNicolas Bonnefon 
8696bde7d5SNicolas Bonnefon template <typename Driver>
8796bde7d5SNicolas Bonnefon WatchTower<Driver>::WatchTower()
883104b268SNicolas Bonnefon     : driver_(), thread_(),
8996bde7d5SNicolas Bonnefon     heartBeat_(std::shared_ptr<void>((void*) 0xDEADC0DE, [] (void*) {}))
9096bde7d5SNicolas Bonnefon {
9196bde7d5SNicolas Bonnefon     running_ = true;
9296bde7d5SNicolas Bonnefon     thread_ = std::thread( &WatchTower::run, this );
9396bde7d5SNicolas Bonnefon }
9496bde7d5SNicolas Bonnefon 
9596bde7d5SNicolas Bonnefon template <typename Driver>
9696bde7d5SNicolas Bonnefon WatchTower<Driver>::~WatchTower()
9796bde7d5SNicolas Bonnefon {
9896bde7d5SNicolas Bonnefon     running_ = false;
99b0345991SNicolas Bonnefon     driver_.interruptWait();
10096bde7d5SNicolas Bonnefon     thread_.join();
10196bde7d5SNicolas Bonnefon }
10296bde7d5SNicolas Bonnefon 
10396bde7d5SNicolas Bonnefon template <typename Driver>
10496bde7d5SNicolas Bonnefon Registration WatchTower<Driver>::addFile(
10596bde7d5SNicolas Bonnefon         const std::string& file_name,
10696bde7d5SNicolas Bonnefon         std::function<void()> notification )
10796bde7d5SNicolas Bonnefon {
108f09fa651SNicolas Bonnefon     // LOG(logDEBUG) << "WatchTower::addFile " << file_name;
10996bde7d5SNicolas Bonnefon 
1103104b268SNicolas Bonnefon     std::weak_ptr<void> weakHeartBeat(heartBeat_);
1113104b268SNicolas Bonnefon 
11296bde7d5SNicolas Bonnefon     std::lock_guard<std::mutex> lock( observers_mutex_ );
11396bde7d5SNicolas Bonnefon 
114f09fa651SNicolas Bonnefon     auto existing_observed_file =
11596bde7d5SNicolas Bonnefon         observed_file_list_.searchByName( file_name );
11696bde7d5SNicolas Bonnefon 
11796bde7d5SNicolas Bonnefon     std::shared_ptr<std::function<void()>> ptr( new std::function<void()>(std::move( notification )) );
11896bde7d5SNicolas Bonnefon 
11996bde7d5SNicolas Bonnefon     if ( ! existing_observed_file )
12096bde7d5SNicolas Bonnefon     {
12191f7c705SNicolas Bonnefon         typename Driver::FileId file_id;
122f09fa651SNicolas Bonnefon         typename Driver::SymlinkId symlink_id;
12396bde7d5SNicolas Bonnefon 
12491f7c705SNicolas Bonnefon         std::tie( file_id, symlink_id ) = addFileToDriver( file_name );
12596bde7d5SNicolas Bonnefon         auto new_file = observed_file_list_.addNewObservedFile(
126f09fa651SNicolas Bonnefon                 ObservedFile<Driver>( file_name, ptr, file_id, symlink_id ) );
12796bde7d5SNicolas Bonnefon 
12896bde7d5SNicolas Bonnefon         auto dir = observed_file_list_.watchedDirectoryForFile( file_name );
129*8b11848fSNicolas Bonnefon         if ( ! dir ) {
130f09fa651SNicolas Bonnefon             LOG(logDEBUG) << "WatchTower::addFile dir for " << file_name
13196bde7d5SNicolas Bonnefon                 << " not watched, adding...";
132*8b11848fSNicolas Bonnefon 
1333104b268SNicolas Bonnefon             dir = observed_file_list_.addWatchedDirectoryForFile( file_name,
1343104b268SNicolas Bonnefon                     [this, weakHeartBeat] (ObservedDir<Driver>* dir) {
1353104b268SNicolas Bonnefon                         if ( auto heart_beat = weakHeartBeat.lock() ) {
1363104b268SNicolas Bonnefon                             driver_.removeDir( dir->dir_id_ );
1373104b268SNicolas Bonnefon                         } } );
13896bde7d5SNicolas Bonnefon 
13996bde7d5SNicolas Bonnefon             dir->dir_id_ = driver_.addDir( dir->path );
140*8b11848fSNicolas Bonnefon 
141*8b11848fSNicolas Bonnefon             if ( ! dir->dir_id_.valid() ) {
142*8b11848fSNicolas Bonnefon                 LOG(logWARNING) << "WatchTower::addFile driver failed to add dir";
143*8b11848fSNicolas Bonnefon                 dir = nullptr;
144*8b11848fSNicolas Bonnefon             }
145*8b11848fSNicolas Bonnefon         }
146*8b11848fSNicolas Bonnefon         else {
147*8b11848fSNicolas Bonnefon             LOG(logDEBUG) << "WatchTower::addFile Found exisiting watch for dir " << file_name;
14896bde7d5SNicolas Bonnefon         }
14996bde7d5SNicolas Bonnefon 
150f09fa651SNicolas Bonnefon         // Associate the dir to the file
151*8b11848fSNicolas Bonnefon         if ( dir )
15296bde7d5SNicolas Bonnefon             new_file->dir_ = dir;
15396bde7d5SNicolas Bonnefon     }
15496bde7d5SNicolas Bonnefon     else
15596bde7d5SNicolas Bonnefon     {
15696bde7d5SNicolas Bonnefon         existing_observed_file->addCallback( ptr );
15796bde7d5SNicolas Bonnefon     }
15896bde7d5SNicolas Bonnefon 
15996bde7d5SNicolas Bonnefon     // Returns a shared pointer that removes its own entry
16096bde7d5SNicolas Bonnefon     // from the list of watched stuff when it goes out of scope!
16196bde7d5SNicolas Bonnefon     // Uses a custom deleter to do the work.
16296bde7d5SNicolas Bonnefon     return std::shared_ptr<void>( 0x0, [this, ptr, weakHeartBeat] (void*) {
16396bde7d5SNicolas Bonnefon             if ( auto heart_beat = weakHeartBeat.lock() )
164dc7f5916SNicolas Bonnefon                 WatchTower<Driver>::removeNotification( this, ptr );
16596bde7d5SNicolas Bonnefon             } );
16696bde7d5SNicolas Bonnefon }
16796bde7d5SNicolas Bonnefon 
1683104b268SNicolas Bonnefon template <typename Driver>
1693104b268SNicolas Bonnefon unsigned int WatchTower<Driver>::numberWatchedDirectories() const
1703104b268SNicolas Bonnefon {
1713104b268SNicolas Bonnefon     return observed_file_list_.numberWatchedDirectories();
1723104b268SNicolas Bonnefon }
1733104b268SNicolas Bonnefon 
17496bde7d5SNicolas Bonnefon //
17596bde7d5SNicolas Bonnefon // Private functions
17696bde7d5SNicolas Bonnefon //
17796bde7d5SNicolas Bonnefon 
17891f7c705SNicolas Bonnefon // Add the passed file name to the driver, returning the file and symlink id
17991f7c705SNicolas Bonnefon template <typename Driver>
18091f7c705SNicolas Bonnefon std::tuple<typename Driver::FileId, typename Driver::SymlinkId>
18191f7c705SNicolas Bonnefon WatchTower<Driver>::addFileToDriver( const std::string& file_name )
18291f7c705SNicolas Bonnefon {
18391f7c705SNicolas Bonnefon     typename Driver::SymlinkId symlink_id;
18491f7c705SNicolas Bonnefon     auto file_id = driver_.addFile( file_name );
18591f7c705SNicolas Bonnefon 
18691f7c705SNicolas Bonnefon     if ( isSymLink( file_name ) )
18791f7c705SNicolas Bonnefon     {
18891f7c705SNicolas Bonnefon         // We want to follow the name (as opposed to the inode)
18991f7c705SNicolas Bonnefon         // so we watch the symlink as well.
19091f7c705SNicolas Bonnefon         symlink_id = driver_.addSymlink( file_name );
19191f7c705SNicolas Bonnefon     }
19291f7c705SNicolas Bonnefon 
19391f7c705SNicolas Bonnefon     return std::make_tuple( file_id, symlink_id );
19491f7c705SNicolas Bonnefon }
19591f7c705SNicolas Bonnefon 
19696bde7d5SNicolas Bonnefon // Called by the dtor for a registration object
19796bde7d5SNicolas Bonnefon template <typename Driver>
19896bde7d5SNicolas Bonnefon void WatchTower<Driver>::removeNotification(
19996bde7d5SNicolas Bonnefon         WatchTower* watch_tower, std::shared_ptr<void> notification )
20096bde7d5SNicolas Bonnefon {
20196bde7d5SNicolas Bonnefon     LOG(logDEBUG) << "WatchTower::removeNotification";
20296bde7d5SNicolas Bonnefon 
20396bde7d5SNicolas Bonnefon     std::lock_guard<std::mutex> lock( watch_tower->observers_mutex_ );
20496bde7d5SNicolas Bonnefon 
20596bde7d5SNicolas Bonnefon     auto file =
20696bde7d5SNicolas Bonnefon         watch_tower->observed_file_list_.removeCallback( notification );
20796bde7d5SNicolas Bonnefon 
20896bde7d5SNicolas Bonnefon     if ( file )
20996bde7d5SNicolas Bonnefon     {
21096bde7d5SNicolas Bonnefon         watch_tower->driver_.removeFile( file->file_id_ );
21196bde7d5SNicolas Bonnefon         watch_tower->driver_.removeSymlink( file->symlink_id_ );
21296bde7d5SNicolas Bonnefon     }
21396bde7d5SNicolas Bonnefon }
21496bde7d5SNicolas Bonnefon 
21596bde7d5SNicolas Bonnefon // Run in its own thread
21696bde7d5SNicolas Bonnefon template <typename Driver>
21796bde7d5SNicolas Bonnefon void WatchTower<Driver>::run()
21896bde7d5SNicolas Bonnefon {
21996bde7d5SNicolas Bonnefon     while ( running_ ) {
2203104b268SNicolas Bonnefon         std::unique_lock<std::mutex> lock( observers_mutex_ );
2213104b268SNicolas Bonnefon 
22291f7c705SNicolas Bonnefon         std::vector<ObservedFile<Driver>*> files_needing_readding;
22391f7c705SNicolas Bonnefon 
22496bde7d5SNicolas Bonnefon         auto files = driver_.waitAndProcessEvents(
22591f7c705SNicolas Bonnefon                 &observed_file_list_, &lock, &files_needing_readding );
2263104b268SNicolas Bonnefon         LOG(logDEBUG) << "WatchTower::run: waitAndProcessEvents returned "
22791f7c705SNicolas Bonnefon             << files.size() << " files, " << files_needing_readding.size()
22891f7c705SNicolas Bonnefon             << " needing re-adding";
22991f7c705SNicolas Bonnefon 
23091f7c705SNicolas Bonnefon         for ( auto file: files_needing_readding ) {
23191f7c705SNicolas Bonnefon             // A file 'needing readding' has the same name,
23291f7c705SNicolas Bonnefon             // but probably a different inode, so it needs
23391f7c705SNicolas Bonnefon             // to be readded for some drivers that rely on the
23491f7c705SNicolas Bonnefon             // inode (e.g. inotify)
23591f7c705SNicolas Bonnefon             driver_.removeFile( file->file_id_ );
23691f7c705SNicolas Bonnefon             driver_.removeSymlink( file->symlink_id_ );
23791f7c705SNicolas Bonnefon 
23891f7c705SNicolas Bonnefon             std::tie( file->file_id_, file->symlink_id_ ) =
23991f7c705SNicolas Bonnefon                 addFileToDriver( file->file_name_ );
24091f7c705SNicolas Bonnefon         }
24196bde7d5SNicolas Bonnefon 
24296bde7d5SNicolas Bonnefon         for ( auto file: files ) {
24396bde7d5SNicolas Bonnefon             for ( auto observer: file->callbacks ) {
2443104b268SNicolas Bonnefon                 LOG(logDEBUG) << "WatchTower::run: notifying the client!";
24596bde7d5SNicolas Bonnefon                 // Here we have to cast our generic pointer back to
24696bde7d5SNicolas Bonnefon                 // the function pointer in order to perform the call
24796bde7d5SNicolas Bonnefon                 const std::shared_ptr<std::function<void()>> fptr =
24896bde7d5SNicolas Bonnefon                     std::static_pointer_cast<std::function<void()>>( observer );
24996bde7d5SNicolas Bonnefon                 // The observer is called with the mutex held,
25096bde7d5SNicolas Bonnefon                 // Let's hope it doesn't do anything too funky.
25196bde7d5SNicolas Bonnefon                 (*fptr)();
25296bde7d5SNicolas Bonnefon             }
25396bde7d5SNicolas Bonnefon         }
25496bde7d5SNicolas Bonnefon     }
25596bde7d5SNicolas Bonnefon }
25696bde7d5SNicolas Bonnefon 
25796bde7d5SNicolas Bonnefon namespace {
25896bde7d5SNicolas Bonnefon     bool isSymLink( const std::string& file_name )
25996bde7d5SNicolas Bonnefon     {
260f09fa651SNicolas Bonnefon #ifdef HAVE_SYMLINK
26196bde7d5SNicolas Bonnefon         struct stat buf;
26296bde7d5SNicolas Bonnefon 
26396bde7d5SNicolas Bonnefon         lstat( file_name.c_str(), &buf );
26496bde7d5SNicolas Bonnefon         return ( S_ISLNK(buf.st_mode) );
265f09fa651SNicolas Bonnefon #else
266f09fa651SNicolas Bonnefon         return false;
267f09fa651SNicolas Bonnefon #endif
26896bde7d5SNicolas Bonnefon     }
26996bde7d5SNicolas Bonnefon };
27096bde7d5SNicolas Bonnefon 
27184b2179eSNicolas Bonnefon #endif
272