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