-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathInotifyBackend.cc
230 lines (190 loc) · 6.68 KB
/
InotifyBackend.cc
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
#include <memory>
#include <poll.h>
#include <unistd.h>
#include "InotifyBackend.hh"
#define INOTIFY_MASK \
IN_ATTRIB | IN_CREATE | IN_DELETE | \
IN_DELETE_SELF | IN_MODIFY | IN_MOVE_SELF | IN_MOVED_FROM | \
IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR | IN_EXCL_UNLINK
#define BUFFER_SIZE 8192
#define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec)
void InotifyBackend::start() {
// Create a pipe that we will write to when we want to end the thread.
int err = pipe2(mPipe, O_CLOEXEC | O_NONBLOCK);
if (err == -1) {
throw std::runtime_error(std::string("Unable to open pipe: ") + strerror(errno));
}
// Init inotify file descriptor.
mInotify = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (mInotify == -1) {
throw std::runtime_error(std::string("Unable to initialize inotify: ") + strerror(errno));
}
pollfd pollfds[2];
pollfds[0].fd = mPipe[0];
pollfds[0].events = POLLIN;
pollfds[0].revents = 0;
pollfds[1].fd = mInotify;
pollfds[1].events = POLLIN;
pollfds[1].revents = 0;
notifyStarted();
// Loop until we get an event from the pipe.
while (true) {
int result = poll(pollfds, 2, 500);
if (result < 0) {
throw std::runtime_error(std::string("Unable to poll: ") + strerror(errno));
}
if (pollfds[0].revents) {
break;
}
if (pollfds[1].revents) {
handleEvents();
}
}
close(mPipe[0]);
close(mPipe[1]);
close(mInotify);
mEndedSignal.notify();
}
InotifyBackend::~InotifyBackend() {
write(mPipe[1], "X", 1);
mEndedSignal.wait();
}
// This function is called by Backend::watch which takes a lock on mMutex
void InotifyBackend::subscribe(Watcher &watcher) {
// Build a full directory tree recursively, and watch each directory.
std::shared_ptr<DirTree> tree = getTree(watcher);
for (auto it = tree->entries.begin(); it != tree->entries.end(); it++) {
if (it->second.isDir) {
bool success = watchDir(watcher, it->second.path, tree);
if (!success) {
throw WatcherError(std::string("inotify_add_watch on '") + it->second.path + std::string("' failed: ") + strerror(errno), &watcher);
}
}
}
}
bool InotifyBackend::watchDir(Watcher &watcher, std::string path, std::shared_ptr<DirTree> tree) {
int wd = inotify_add_watch(mInotify, path.c_str(), INOTIFY_MASK);
if (wd == -1) {
return false;
}
std::shared_ptr<InotifySubscription> sub = std::make_shared<InotifySubscription>();
sub->tree = tree;
sub->path = path;
sub->watcher = &watcher;
mSubscriptions.emplace(wd, sub);
return true;
}
void InotifyBackend::handleEvents() {
char buf[BUFFER_SIZE] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
struct inotify_event *event;
// Track all of the watchers that are touched so we can notify them at the end of the events.
std::unordered_set<Watcher *> watchers;
while (true) {
int n = read(mInotify, &buf, BUFFER_SIZE);
if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
break;
}
throw std::runtime_error(std::string("Error reading from inotify: ") + strerror(errno));
}
if (n == 0) {
break;
}
for (char *ptr = buf; ptr < buf + n; ptr += sizeof(*event) + event->len) {
event = (struct inotify_event *)ptr;
if ((event->mask & IN_Q_OVERFLOW) == IN_Q_OVERFLOW) {
// overflow
continue;
}
handleEvent(event, watchers);
}
}
for (auto it = watchers.begin(); it != watchers.end(); it++) {
(*it)->notify();
}
}
void InotifyBackend::handleEvent(struct inotify_event *event, std::unordered_set<Watcher *> &watchers) {
std::unique_lock<std::mutex> lock(mMutex);
// Find the subscriptions for this watch descriptor
auto range = mSubscriptions.equal_range(event->wd);
std::unordered_set<std::shared_ptr<InotifySubscription>> set;
for (auto it = range.first; it != range.second; it++) {
set.insert(it->second);
}
for (auto it = set.begin(); it != set.end(); it++) {
if (handleSubscription(event, *it)) {
watchers.insert((*it)->watcher);
}
}
}
bool InotifyBackend::handleSubscription(struct inotify_event *event, std::shared_ptr<InotifySubscription> sub) {
// Build full path and check if its in our ignore list.
Watcher *watcher = sub->watcher;
std::string path = std::string(sub->path);
bool isDir = event->mask & IN_ISDIR;
if (event->len > 0) {
path += "/" + std::string(event->name);
}
if (watcher->mIgnore.count(path) > 0) {
return false;
}
// If this is a create, check if it's a directory and start watching if it is.
// In any case, keep the directory tree up to date.
if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
watcher->mEvents.create(path);
struct stat st;
// Use lstat to avoid resolving symbolic links that we cannot watch anyway
// https://github.com/parcel-bundler/watcher/issues/76
lstat(path.c_str(), &st);
DirEntry *entry = sub->tree->add(path, CONVERT_TIME(st.st_mtim), S_ISDIR(st.st_mode));
if (entry->isDir) {
bool success = watchDir(*watcher, path, sub->tree);
if (!success) {
sub->tree->remove(path);
return false;
}
}
} else if (event->mask & (IN_MODIFY | IN_ATTRIB)) {
watcher->mEvents.update(path);
struct stat st;
stat(path.c_str(), &st);
sub->tree->update(path, CONVERT_TIME(st.st_mtim));
} else if (event->mask & (IN_DELETE | IN_DELETE_SELF | IN_MOVED_FROM | IN_MOVE_SELF)) {
bool isSelfEvent = (event->mask & (IN_DELETE_SELF | IN_MOVE_SELF));
// Ignore delete/move self events unless this is the recursive watch root
if (isSelfEvent && path != watcher->mDir) {
return false;
}
// If the entry being deleted/moved is a directory, remove it from the list of subscriptions
// XXX: self events don't have the IN_ISDIR mask
if (isSelfEvent || isDir) {
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
if (it->second->path == path) {
it = mSubscriptions.erase(it);
} else {
++it;
}
}
}
watcher->mEvents.remove(path);
sub->tree->remove(path);
}
return true;
}
// This function is called by Backend::unwatch which takes a lock on mMutex
void InotifyBackend::unsubscribe(Watcher &watcher) {
// Find any subscriptions pointing to this watcher, and remove them.
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
if (it->second->watcher == &watcher) {
if (mSubscriptions.count(it->first) == 1) {
int err = inotify_rm_watch(mInotify, it->first);
if (err == -1) {
throw WatcherError(std::string("Unable to remove watcher: ") + strerror(errno), &watcher);
}
}
it = mSubscriptions.erase(it);
} else {
it++;
}
}
}