Line |
Branch |
Exec |
Source |
1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System. |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
#ifndef CVMFS_FILE_WATCHER_H_ |
6 |
|
|
#define CVMFS_FILE_WATCHER_H_ |
7 |
|
|
|
8 |
|
|
#include <pthread.h> |
9 |
|
|
|
10 |
|
|
#include <map> |
11 |
|
|
#include <string> |
12 |
|
|
|
13 |
|
|
namespace file_watcher { |
14 |
|
|
|
15 |
|
|
enum Event { |
16 |
|
|
kModified, |
17 |
|
|
kRenamed, |
18 |
|
|
kAttributes, |
19 |
|
|
kHardlinked, |
20 |
|
|
kDeleted, |
21 |
|
|
kIgnored, |
22 |
|
|
kInvalid |
23 |
|
|
}; |
24 |
|
|
|
25 |
|
|
class EventHandler { |
26 |
|
|
public: |
27 |
|
|
EventHandler(); |
28 |
|
|
virtual ~EventHandler(); |
29 |
|
|
|
30 |
|
|
/** |
31 |
|
|
* Handle function called per event |
32 |
|
|
* |
33 |
|
|
* @param file_path - the path of the file this event corresponds to |
34 |
|
|
* @param event - the type of event |
35 |
|
|
* @param clear_handler - (output) set this to true to have the event |
36 |
|
|
* handler remove from the loop after this call |
37 |
|
|
* |
38 |
|
|
* Should return false in case of failure. |
39 |
|
|
* |
40 |
|
|
* Setting the clear_handler parameter to false means that the FileWatcher |
41 |
|
|
* object will attempt to re-register the handler for the same file name |
42 |
|
|
* in the case a file was delete - useful for continuously watching a file |
43 |
|
|
* which may be deleted and recreated |
44 |
|
|
*/ |
45 |
|
|
virtual bool Handle(const std::string& file_path, |
46 |
|
|
Event event, |
47 |
|
|
bool* clear_handler) = 0; |
48 |
|
|
}; |
49 |
|
|
|
50 |
|
|
struct WatchRecord { |
51 |
|
4 |
WatchRecord() : file_path_(), handler_(NULL) {} |
52 |
|
|
|
53 |
|
4 |
WatchRecord(const std::string& path, |
54 |
|
|
file_watcher::EventHandler* h) |
55 |
|
4 |
: file_path_(path), |
56 |
|
4 |
handler_(h) {} |
57 |
|
|
|
58 |
|
|
std::string file_path_; |
59 |
|
|
file_watcher::EventHandler* handler_; |
60 |
|
|
}; |
61 |
|
|
|
62 |
|
|
class FileWatcher { |
63 |
|
|
public: |
64 |
|
|
typedef std::map<std::string, EventHandler*> HandlerMap; |
65 |
|
|
|
66 |
|
|
FileWatcher(); |
67 |
|
|
virtual ~FileWatcher(); |
68 |
|
|
static FileWatcher *Create(); |
69 |
|
|
|
70 |
|
|
void RegisterHandler(const std::string& file_path, |
71 |
|
|
EventHandler* handler); |
72 |
|
|
|
73 |
|
|
bool Spawn(); |
74 |
|
|
|
75 |
|
|
void Stop(); |
76 |
|
|
|
77 |
|
|
protected: |
78 |
|
|
// Delays controlling the backoff throttle when registering new watches |
79 |
|
|
static const unsigned kInitialDelay; |
80 |
|
|
static const unsigned kMaxDelay; |
81 |
|
|
static const unsigned kResetDelay; |
82 |
|
|
|
83 |
|
|
void RegisterFilter(const std::string& file_path, |
84 |
|
|
EventHandler* handler); |
85 |
|
|
|
86 |
|
|
virtual bool RunEventLoop(const HandlerMap& handler_map, |
87 |
|
|
int read_pipe, int write_pipe) = 0; |
88 |
|
|
|
89 |
|
|
virtual int TryRegisterFilter(const std::string& file_path) = 0; |
90 |
|
|
|
91 |
|
|
std::map<int, WatchRecord> watch_records_; |
92 |
|
|
|
93 |
|
|
private: |
94 |
|
|
static void* BackgroundThread(void* d); |
95 |
|
|
|
96 |
|
|
HandlerMap handler_map_; |
97 |
|
|
|
98 |
|
|
int control_pipe_to_back_[2]; |
99 |
|
|
int control_pipe_to_front_[2]; |
100 |
|
|
|
101 |
|
|
pthread_t thread_; |
102 |
|
|
|
103 |
|
|
bool started_; |
104 |
|
|
}; |
105 |
|
|
|
106 |
|
|
} // namespace file_watcher |
107 |
|
|
|
108 |
|
|
#endif // CVMFS_FILE_WATCHER_H_ |
109 |
|
|
|