xref: /glogg/src/watchtower.h (revision 91f7c70525aef93239c2facfe440e65d1672a468)
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
64*91f7c705SNicolas Bonnefon     std::tuple<typename Driver::FileId, typename Driver::SymlinkId>
65*91f7c705SNicolas 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     {
121*91f7c705SNicolas Bonnefon         typename Driver::FileId file_id;
122f09fa651SNicolas Bonnefon         typename Driver::SymlinkId symlink_id;
12396bde7d5SNicolas Bonnefon 
124*91f7c705SNicolas 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 );
12996bde7d5SNicolas Bonnefon         if ( ! dir )
13096bde7d5SNicolas Bonnefon         {
131f09fa651SNicolas Bonnefon             LOG(logDEBUG) << "WatchTower::addFile dir for " << file_name
13296bde7d5SNicolas Bonnefon                 << " not watched, adding...";
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 );
14096bde7d5SNicolas Bonnefon         }
14196bde7d5SNicolas Bonnefon 
142f09fa651SNicolas Bonnefon         // Associate the dir to the file
14396bde7d5SNicolas Bonnefon         new_file->dir_ = dir;
144f09fa651SNicolas Bonnefon 
145f09fa651SNicolas Bonnefon         LOG(logDEBUG) << "dir ref count is " << dir.use_count();
14696bde7d5SNicolas Bonnefon     }
14796bde7d5SNicolas Bonnefon     else
14896bde7d5SNicolas Bonnefon     {
14996bde7d5SNicolas Bonnefon         existing_observed_file->addCallback( ptr );
15096bde7d5SNicolas Bonnefon     }
15196bde7d5SNicolas Bonnefon 
15296bde7d5SNicolas Bonnefon     // Returns a shared pointer that removes its own entry
15396bde7d5SNicolas Bonnefon     // from the list of watched stuff when it goes out of scope!
15496bde7d5SNicolas Bonnefon     // Uses a custom deleter to do the work.
15596bde7d5SNicolas Bonnefon     return std::shared_ptr<void>( 0x0, [this, ptr, weakHeartBeat] (void*) {
15696bde7d5SNicolas Bonnefon             if ( auto heart_beat = weakHeartBeat.lock() )
157dc7f5916SNicolas Bonnefon                 WatchTower<Driver>::removeNotification( this, ptr );
15896bde7d5SNicolas Bonnefon             } );
15996bde7d5SNicolas Bonnefon }
16096bde7d5SNicolas Bonnefon 
1613104b268SNicolas Bonnefon template <typename Driver>
1623104b268SNicolas Bonnefon unsigned int WatchTower<Driver>::numberWatchedDirectories() const
1633104b268SNicolas Bonnefon {
1643104b268SNicolas Bonnefon     return observed_file_list_.numberWatchedDirectories();
1653104b268SNicolas Bonnefon }
1663104b268SNicolas Bonnefon 
16796bde7d5SNicolas Bonnefon //
16896bde7d5SNicolas Bonnefon // Private functions
16996bde7d5SNicolas Bonnefon //
17096bde7d5SNicolas Bonnefon 
171*91f7c705SNicolas Bonnefon // Add the passed file name to the driver, returning the file and symlink id
172*91f7c705SNicolas Bonnefon template <typename Driver>
173*91f7c705SNicolas Bonnefon std::tuple<typename Driver::FileId, typename Driver::SymlinkId>
174*91f7c705SNicolas Bonnefon WatchTower<Driver>::addFileToDriver( const std::string& file_name )
175*91f7c705SNicolas Bonnefon {
176*91f7c705SNicolas Bonnefon     typename Driver::SymlinkId symlink_id;
177*91f7c705SNicolas Bonnefon     auto file_id = driver_.addFile( file_name );
178*91f7c705SNicolas Bonnefon 
179*91f7c705SNicolas Bonnefon     if ( isSymLink( file_name ) )
180*91f7c705SNicolas Bonnefon     {
181*91f7c705SNicolas Bonnefon         // We want to follow the name (as opposed to the inode)
182*91f7c705SNicolas Bonnefon         // so we watch the symlink as well.
183*91f7c705SNicolas Bonnefon         symlink_id = driver_.addSymlink( file_name );
184*91f7c705SNicolas Bonnefon     }
185*91f7c705SNicolas Bonnefon 
186*91f7c705SNicolas Bonnefon     return std::make_tuple( file_id, symlink_id );
187*91f7c705SNicolas Bonnefon }
188*91f7c705SNicolas Bonnefon 
18996bde7d5SNicolas Bonnefon // Called by the dtor for a registration object
19096bde7d5SNicolas Bonnefon template <typename Driver>
19196bde7d5SNicolas Bonnefon void WatchTower<Driver>::removeNotification(
19296bde7d5SNicolas Bonnefon         WatchTower* watch_tower, std::shared_ptr<void> notification )
19396bde7d5SNicolas Bonnefon {
19496bde7d5SNicolas Bonnefon     LOG(logDEBUG) << "WatchTower::removeNotification";
19596bde7d5SNicolas Bonnefon 
19696bde7d5SNicolas Bonnefon     std::lock_guard<std::mutex> lock( watch_tower->observers_mutex_ );
19796bde7d5SNicolas Bonnefon 
19896bde7d5SNicolas Bonnefon     auto file =
19996bde7d5SNicolas Bonnefon         watch_tower->observed_file_list_.removeCallback( notification );
20096bde7d5SNicolas Bonnefon 
20196bde7d5SNicolas Bonnefon     if ( file )
20296bde7d5SNicolas Bonnefon     {
20396bde7d5SNicolas Bonnefon         watch_tower->driver_.removeFile( file->file_id_ );
20496bde7d5SNicolas Bonnefon         watch_tower->driver_.removeSymlink( file->symlink_id_ );
20596bde7d5SNicolas Bonnefon     }
20696bde7d5SNicolas Bonnefon }
20796bde7d5SNicolas Bonnefon 
20896bde7d5SNicolas Bonnefon // Run in its own thread
20996bde7d5SNicolas Bonnefon template <typename Driver>
21096bde7d5SNicolas Bonnefon void WatchTower<Driver>::run()
21196bde7d5SNicolas Bonnefon {
21296bde7d5SNicolas Bonnefon     while ( running_ ) {
2133104b268SNicolas Bonnefon         std::unique_lock<std::mutex> lock( observers_mutex_ );
2143104b268SNicolas Bonnefon 
215*91f7c705SNicolas Bonnefon         std::vector<ObservedFile<Driver>*> files_needing_readding;
216*91f7c705SNicolas Bonnefon 
21796bde7d5SNicolas Bonnefon         auto files = driver_.waitAndProcessEvents(
218*91f7c705SNicolas Bonnefon                 &observed_file_list_, &lock, &files_needing_readding );
2193104b268SNicolas Bonnefon         LOG(logDEBUG) << "WatchTower::run: waitAndProcessEvents returned "
220*91f7c705SNicolas Bonnefon             << files.size() << " files, " << files_needing_readding.size()
221*91f7c705SNicolas Bonnefon             << " needing re-adding";
222*91f7c705SNicolas Bonnefon 
223*91f7c705SNicolas Bonnefon         for ( auto file: files_needing_readding ) {
224*91f7c705SNicolas Bonnefon             // A file 'needing readding' has the same name,
225*91f7c705SNicolas Bonnefon             // but probably a different inode, so it needs
226*91f7c705SNicolas Bonnefon             // to be readded for some drivers that rely on the
227*91f7c705SNicolas Bonnefon             // inode (e.g. inotify)
228*91f7c705SNicolas Bonnefon             driver_.removeFile( file->file_id_ );
229*91f7c705SNicolas Bonnefon             driver_.removeSymlink( file->symlink_id_ );
230*91f7c705SNicolas Bonnefon 
231*91f7c705SNicolas Bonnefon             std::tie( file->file_id_, file->symlink_id_ ) =
232*91f7c705SNicolas Bonnefon                 addFileToDriver( file->file_name_ );
233*91f7c705SNicolas Bonnefon         }
23496bde7d5SNicolas Bonnefon 
23596bde7d5SNicolas Bonnefon         for ( auto file: files ) {
23696bde7d5SNicolas Bonnefon             for ( auto observer: file->callbacks ) {
2373104b268SNicolas Bonnefon                 LOG(logDEBUG) << "WatchTower::run: notifying the client!";
23896bde7d5SNicolas Bonnefon                 // Here we have to cast our generic pointer back to
23996bde7d5SNicolas Bonnefon                 // the function pointer in order to perform the call
24096bde7d5SNicolas Bonnefon                 const std::shared_ptr<std::function<void()>> fptr =
24196bde7d5SNicolas Bonnefon                     std::static_pointer_cast<std::function<void()>>( observer );
24296bde7d5SNicolas Bonnefon                 // The observer is called with the mutex held,
24396bde7d5SNicolas Bonnefon                 // Let's hope it doesn't do anything too funky.
24496bde7d5SNicolas Bonnefon                 (*fptr)();
24596bde7d5SNicolas Bonnefon             }
24696bde7d5SNicolas Bonnefon         }
24796bde7d5SNicolas Bonnefon     }
24896bde7d5SNicolas Bonnefon }
24996bde7d5SNicolas Bonnefon 
25096bde7d5SNicolas Bonnefon namespace {
25196bde7d5SNicolas Bonnefon     bool isSymLink( const std::string& file_name )
25296bde7d5SNicolas Bonnefon     {
253f09fa651SNicolas Bonnefon #ifdef HAVE_SYMLINK
25496bde7d5SNicolas Bonnefon         struct stat buf;
25596bde7d5SNicolas Bonnefon 
25696bde7d5SNicolas Bonnefon         lstat( file_name.c_str(), &buf );
25796bde7d5SNicolas Bonnefon         return ( S_ISLNK(buf.st_mode) );
258f09fa651SNicolas Bonnefon #else
259f09fa651SNicolas Bonnefon         return false;
260f09fa651SNicolas Bonnefon #endif
26196bde7d5SNicolas Bonnefon     }
26296bde7d5SNicolas Bonnefon };
26396bde7d5SNicolas Bonnefon 
26484b2179eSNicolas Bonnefon #endif
265