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