/
fsevents_monitor.cpp
255 lines (212 loc) · 9.04 KB
/
fsevents_monitor.cpp
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
/*
* Copyright (c) 2014-2022 Enrico M. Crisostomo
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libfswatch/libfswatch_config.h>
#include <memory>
#include <unistd.h> // isatty()
#include <cstdio> // fileno()
#include <thread>
#include "fsevents_monitor.hpp"
#include "libfswatch/gettext_defs.h"
#include "libfswatch_exception.hpp"
#include "libfswatch/c/libfswatch_log.h"
#include <mutex>
namespace fsw
{
using std::vector;
using std::string;
using FSEventFlagType =
struct FSEventFlagType
{
FSEventStreamEventFlags flag;
fsw_event_flag type;
};
static vector<FSEventFlagType> create_flag_type_vector()
{
vector<FSEventFlagType> flags;
#ifdef HAVE_MACOS_GE_10_5
flags.push_back({kFSEventStreamEventFlagNone, fsw_event_flag::PlatformSpecific});
flags.push_back({kFSEventStreamEventFlagMustScanSubDirs, fsw_event_flag::PlatformSpecific});
flags.push_back({kFSEventStreamEventFlagUserDropped, fsw_event_flag::PlatformSpecific});
flags.push_back({kFSEventStreamEventFlagKernelDropped, fsw_event_flag::PlatformSpecific});
flags.push_back({kFSEventStreamEventFlagEventIdsWrapped, fsw_event_flag::PlatformSpecific});
flags.push_back({kFSEventStreamEventFlagHistoryDone, fsw_event_flag::PlatformSpecific});
flags.push_back({kFSEventStreamEventFlagRootChanged, fsw_event_flag::PlatformSpecific});
flags.push_back({kFSEventStreamEventFlagMount, fsw_event_flag::PlatformSpecific});
flags.push_back({kFSEventStreamEventFlagUnmount, fsw_event_flag::PlatformSpecific});
#endif
#ifdef HAVE_MACOS_GE_10_7
flags.push_back({kFSEventStreamEventFlagItemChangeOwner, fsw_event_flag::OwnerModified});
flags.push_back({kFSEventStreamEventFlagItemCreated, fsw_event_flag::Created});
flags.push_back({kFSEventStreamEventFlagItemFinderInfoMod, fsw_event_flag::PlatformSpecific});
flags.push_back({kFSEventStreamEventFlagItemFinderInfoMod, fsw_event_flag::AttributeModified});
flags.push_back({kFSEventStreamEventFlagItemInodeMetaMod, fsw_event_flag::AttributeModified});
flags.push_back({kFSEventStreamEventFlagItemIsDir, fsw_event_flag::IsDir});
flags.push_back({kFSEventStreamEventFlagItemIsFile, fsw_event_flag::IsFile});
flags.push_back({kFSEventStreamEventFlagItemIsSymlink, fsw_event_flag::IsSymLink});
flags.push_back({kFSEventStreamEventFlagItemModified, fsw_event_flag::Updated});
flags.push_back({kFSEventStreamEventFlagItemRemoved, fsw_event_flag::Removed});
flags.push_back({kFSEventStreamEventFlagItemRenamed, fsw_event_flag::Renamed});
flags.push_back({kFSEventStreamEventFlagItemXattrMod, fsw_event_flag::AttributeModified});
#endif
#ifdef HAVE_MACOS_GE_10_9
flags.push_back({kFSEventStreamEventFlagOwnEvent, fsw_event_flag::AttributeModified});
#endif
#ifdef HAVE_MACOS_GE_10_10
flags.push_back({kFSEventStreamEventFlagItemIsHardlink, fsw_event_flag::Link});
flags.push_back({kFSEventStreamEventFlagItemIsLastHardlink, fsw_event_flag::Link});
flags.push_back({kFSEventStreamEventFlagItemIsLastHardlink, fsw_event_flag::PlatformSpecific});
#endif
#ifdef HAVE_MACOS_GE_10_13
flags.push_back({kFSEventStreamEventFlagItemCloned, fsw_event_flag::PlatformSpecific});
#endif
return flags;
}
static const vector<FSEventFlagType> event_flag_type = create_flag_type_vector();
fsevents_monitor::fsevents_monitor(vector<string> paths_to_monitor,
FSW_EVENT_CALLBACK *callback,
void *context) :
monitor(std::move(paths_to_monitor), callback, context)
{
}
void fsevents_monitor::run()
{
std::unique_lock<std::mutex> run_loop_lock(run_mutex);
if (stream) return;
// parsing paths
vector<CFStringRef> dirs;
for (const string& path : paths)
{
dirs.push_back(CFStringCreateWithCString(nullptr,
path.c_str(),
kCFStringEncodingUTF8));
}
if (dirs.empty()) return;
CFArrayRef pathsToWatch =
CFArrayCreate(nullptr,
reinterpret_cast<const void **> (&dirs[0]),
dirs.size(),
&kCFTypeArrayCallBacks);
create_stream(pathsToWatch);
if (!stream)
throw libfsw_exception(_("Event stream could not be created."));
// Creating dispatch queue
fsevents_queue = dispatch_queue_create("fswatch_event_queue", nullptr);
FSEventStreamSetDispatchQueue(stream, fsevents_queue);
FSW_ELOG(_("Starting event stream...\n"));
FSEventStreamStart(stream);
run_loop_lock.unlock();
for(;;)
{
run_loop_lock.lock();
if (should_stop) break;
run_loop_lock.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds((long long) (latency * 1000)));
}
// Deinitialization part
FSW_ELOG(_("Stopping event stream...\n"));
FSEventStreamStop(stream);
FSW_ELOG(_("Invalidating event stream...\n"));
FSEventStreamInvalidate(stream);
FSW_ELOG(_("Releasing event stream...\n"));
FSEventStreamRelease(stream);
dispatch_release(fsevents_queue);
stream = nullptr;
}
static vector<fsw_event_flag> decode_flags(FSEventStreamEventFlags flag)
{
vector<fsw_event_flag> evt_flags;
for (const FSEventFlagType& type : event_flag_type)
{
if (flag & type.flag)
{
evt_flags.push_back(type.type);
}
}
return evt_flags;
}
void fsevents_monitor::fsevents_callback(ConstFSEventStreamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId *)
{
const auto *fse_monitor = static_cast<fsevents_monitor *> (clientCallBackInfo);
if (!fse_monitor)
{
throw libfsw_exception(_("The callback info cannot be cast to fsevents_monitor."));
}
// Build the notification objects.
vector<event> events;
time_t curr_time;
time(&curr_time);
for (size_t i = 0; i < numEvents; ++i)
{
#if defined(HAVE_MACOS_GE_10_13)
auto path_info_dict = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex((CFArrayRef) eventPaths,
i));
auto path = static_cast<CFStringRef>(CFDictionaryGetValue(path_info_dict,
kFSEventStreamEventExtendedDataPathKey));
auto cf_inode = static_cast<CFNumberRef>(CFDictionaryGetValue(path_info_dict,
kFSEventStreamEventExtendedFileIDKey));
unsigned long inode;
CFNumberGetValue(cf_inode, kCFNumberLongType, &inode);
events.emplace_back(std::string(CFStringGetCStringPtr(path, kCFStringEncodingUTF8)),
curr_time,
decode_flags(eventFlags[i]),
inode);
#else
events.emplace_back(((char **) eventPaths)[i],
curr_time,
decode_flags(eventFlags[i]));
#endif
}
if (!events.empty())
{
fse_monitor->notify_events(events);
}
}
bool fsevents_monitor::no_defer()
{
string no_defer = get_property(DARWIN_EVENTSTREAM_NO_DEFER);
if (no_defer.empty())
return (!isatty(fileno(stdin)));
return (no_defer == "true");
}
void fsevents_monitor::create_stream(CFArrayRef pathsToWatch)
{
std::unique_ptr<FSEventStreamContext> context(new FSEventStreamContext());
context->version = 0;
context->info = this;
context->retain = nullptr;
context->release = nullptr;
context->copyDescription = nullptr;
FSEventStreamCreateFlags streamFlags = kFSEventStreamCreateFlagFileEvents;
if (this->no_defer()) streamFlags |= kFSEventStreamCreateFlagNoDefer;
#if defined (HAVE_MACOS_GE_10_13)
streamFlags |= kFSEventStreamCreateFlagUseExtendedData;
streamFlags |= kFSEventStreamCreateFlagUseCFTypes;
#endif
FSW_ELOG(_("Creating FSEvent stream...\n"));
stream = FSEventStreamCreate(nullptr,
&fsevents_monitor::fsevents_callback,
context.get(),
pathsToWatch,
kFSEventStreamEventIdSinceNow,
latency,
streamFlags);
}
}