-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpath_monitor.hpp
268 lines (234 loc) · 7.57 KB
/
path_monitor.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#pragma once
#include "fly/types/string/formatters.hpp"
#include <cstdint>
#include <filesystem>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
namespace fly::task {
class SequencedTaskRunner;
} // namespace fly::task
namespace fly::path {
class PathConfig;
class PathMonitorTask;
/**
* Enumerated list of path events.
*/
enum class PathEvent : std::uint8_t
{
None,
Created,
Deleted,
Changed
};
/**
* Virtual interface to monitor a local path. Provides monitoring of either all files or
* user-specified files under a path for addition, deletion, or change. This interface is platform
* independent - OS dependent implementations should inherit from this class.
*
* @author Timothy Flynn ([email protected])
* @version May 14, 2017
*/
class PathMonitor : public std::enable_shared_from_this<PathMonitor>
{
public:
/**
* Callback definition for function to be triggered on a path change.
*/
using PathEventCallback = std::function<void(std::filesystem::path, PathEvent)>;
/**
* Create and start a path monitor.
*
* @param task_runner Task runner for posting path-related tasks onto.
* @param config Reference to path configuration.
*
* @return The created path monitor.
*/
static std::shared_ptr<PathMonitor> create(
std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
std::shared_ptr<PathConfig> config);
/**
* Destructor. Remove all paths from the path monitor.
*/
virtual ~PathMonitor();
/**
* Monitor for changes to all files under a directory. Callbacks registered with AddFile take
* precendence over callbacks registered with AddPath.
*
* @param path Path to the directory to start monitoring.
* @param callback Callback to trigger when a file changes.
*
* @return True if the directory could be added.
*/
bool add_path(std::filesystem::path const &path, PathEventCallback callback);
/**
* Stop monitoring for changes to all files under a directory.
*
* @param path Path to the directory to stop monitoring.
*
* @return True if the directory was removed.
*/
bool remove_path(std::filesystem::path const &path);
/**
* Stop monitoring all paths.
*/
void remove_all_paths();
/**
* Monitor for changes to a single file. Callbacks registered with AddFile take precendence over
* callbacks registered with AddPath.
*
* @param file Path to the file to start monitoring.
* @param callback Callback to trigger when the file changes.
*
* @return True if the file could be added.
*/
bool add_file(std::filesystem::path const &file, PathEventCallback callback);
/**
* Stop monitoring for changes to a single file. If there are no more files monitored in the
* file's directory, and there is no callback registered for that directory, the directory
* itself is removed from the monitor.
*
* @param file Path to the file to stop monitoring.
*
* @return True if the file was removed.
*/
bool remove_file(std::filesystem::path const &file);
protected:
/**
* Struct to store information about a monitored path. OS dependent implementations of
* PathMonitor should also have a concrete defintion of this struct.
*/
struct PathInfo
{
/**
* Destructor.
*/
virtual ~PathInfo() = default;
/**
* Check if the monitored path is in a good state.
*
* @return True if the monitored path is healthy.
*/
virtual bool is_valid() const = 0;
PathEventCallback m_path_handler;
std::map<std::filesystem::path, PathEventCallback> m_file_handlers;
};
/**
* Map of monitored paths to their path information.
*/
using PathInfoMap = std::map<std::filesystem::path, std::unique_ptr<PathInfo>>;
/**
* Constructor.
*
* @param task_runner Task runner for posting path-related tasks onto.
* @param config Reference to path configuration.
*/
PathMonitor(
std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
std::shared_ptr<PathConfig> config) noexcept;
/**
* Create an instance of the OS dependent PathInfo struct.
*
* @param path The path to be monitored.
*
* @return Up-casted pointer to the PathInfo struct.
*/
virtual std::unique_ptr<PathInfo> create_path_info(std::filesystem::path const &path) const = 0;
/**
* Check if the path monitor implementation is valid.
*
* @return True if the implementation is valid.
*/
virtual bool is_valid() const = 0;
/**
* Check the path monitor implementation for any changes to the monitored paths.
*
* @param timeout Max time allow for an event to be occur.
*/
virtual void poll(std::chrono::milliseconds timeout) = 0;
mutable std::mutex m_mutex;
PathInfoMap m_path_info;
private:
/**
* Queue a task to poll monitored paths.
*
* @return True if the path monitor is in a valid state.
*/
bool start();
/**
* Search for a path to be monitored in the PathInfo map. If the map does not contain the path,
* create an entry.
*
* @param path The path to be monitored.
*
* @return Shared pointer to the PathInfo struct.
*/
PathInfo *get_or_create_path_info(std::filesystem::path const &path);
/**
* Queue a task to poll monitored paths. When the task is completed, it re-arms itself (if the
* path monitor is still in a valid state).
*
* @return True if task was able to be queued.
*/
bool poll_paths_later();
std::shared_ptr<fly::task::SequencedTaskRunner> m_task_runner;
std::shared_ptr<PathConfig> m_config;
};
} // namespace fly::path
//==================================================================================================
template <>
struct fly::string::Formatter<fly::path::PathEvent>
{
/**
* Format a path event.
*
* @tparam FormatContext The type of the formatting context.
*
* @param event The path event to format.
* @param context The context holding the formatting state.
*/
template <typename FormatContext>
void format(fly::path::PathEvent event, FormatContext &context)
{
auto append = [&context](std::string_view value) {
for (auto ch : value)
{
context.out()++ = ch;
}
};
switch (event)
{
case fly::path::PathEvent::None:
append("None");
break;
case fly::path::PathEvent::Created:
append("Created");
break;
case fly::path::PathEvent::Deleted:
append("Deleted");
break;
case fly::path::PathEvent::Changed:
append("Changed");
break;
}
}
};
//==================================================================================================
template <>
struct fly::string::Formatter<std::filesystem::path> : public fly::string::Formatter<std::string>
{
/**
* Format a filesystem path.
*
* @tparam FormatContext The type of the formatting context.
*
* @param path The filesystem path to format.
* @param context The context holding the formatting state.
*/
template <typename FormatContext>
void format(std::filesystem::path const &path, FormatContext &context)
{
fly::string::Formatter<std::string>::format(path.string(), context);
}
};