Rev 17 | Rev 23 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | luk | 1 | |
2 | /// inotify C++ interface implementation |
||
3 | /** |
||
4 | * \file inotify-cxx.cpp |
||
5 | * |
||
6 | * inotify C++ interface |
||
7 | * |
||
8 | * Copyright (C) 2006 Lukas Jelinek <lukas@aiken.cz> |
||
9 | * |
||
10 | * This program is free software; you can redistribute it and/or |
||
11 | * modify it under the terms of one of the following licenses: |
||
12 | * |
||
13 | * \li 1. X11-style license (see LICENSE-X11) |
||
14 | * \li 2. GNU Lesser General Public License, version 2.1 (see LICENSE-LGPL) |
||
15 | * \li 3. GNU General Public License, version 2 (see LICENSE-GPL) |
||
16 | * |
||
17 | * If you want to help with choosing the best license for you, |
||
18 | * please visit http://www.gnu.org/licenses/license-list.html. |
||
19 | * |
||
20 | */ |
||
21 | |||
22 | |||
23 | #include <errno.h> |
||
24 | #include <unistd.h> |
||
13 | luk | 25 | #include <fcntl.h> |
3 | luk | 26 | |
27 | #include "inotify-cxx.h" |
||
28 | |||
13 | luk | 29 | /// dump separator (between particular entries) |
3 | luk | 30 | #define DUMP_SEP \ |
31 | ({ \ |
||
32 | if (!rStr.empty()) { \ |
||
11 | luk | 33 | rStr.append(","); \ |
3 | luk | 34 | } \ |
35 | }) |
||
36 | |||
13 | luk | 37 | |
38 | int32_t InotifyEvent::GetDescriptor() const |
||
39 | { |
||
40 | return m_pWatch != NULL // if watch exists |
||
41 | ? m_pWatch->GetDescriptor() // return its descriptor |
||
42 | : -1; // else return -1 |
||
43 | } |
||
44 | |||
11 | luk | 45 | uint32_t InotifyEvent::GetMaskByName(const std::string& rName) |
46 | { |
||
47 | if (rName == "IN_ACCESS") |
||
48 | return IN_ACCESS; |
||
49 | else if (rName == "IN_MODIFY") |
||
50 | return IN_MODIFY; |
||
51 | else if (rName == "IN_ATTRIB") |
||
52 | return IN_ATTRIB; |
||
53 | else if (rName == "IN_CLOSE_WRITE") |
||
54 | return IN_CLOSE_WRITE; |
||
55 | else if (rName == "IN_CLOSE_NOWRITE") |
||
56 | return IN_CLOSE_NOWRITE; |
||
57 | else if (rName == "IN_MOVED_FROM") |
||
58 | return IN_MOVED_FROM; |
||
59 | else if (rName == "IN_MOVED_TO") |
||
60 | return IN_MOVED_TO; |
||
61 | else if (rName == "IN_CREATE") |
||
62 | return IN_CREATE; |
||
63 | else if (rName == "IN_DELETE") |
||
64 | return IN_DELETE; |
||
65 | else if (rName == "IN_DELETE_SELF") |
||
66 | return IN_DELETE_SELF; |
||
67 | else if (rName == "IN_UNMOUNT") |
||
68 | return IN_UNMOUNT; |
||
69 | else if (rName == "IN_Q_OVERFLOW") |
||
70 | return IN_Q_OVERFLOW; |
||
71 | else if (rName == "IN_IGNORED") |
||
72 | return IN_IGNORED; |
||
73 | else if (rName == "IN_CLOSE") |
||
74 | return IN_CLOSE; |
||
75 | else if (rName == "IN_MOVE") |
||
76 | return IN_MOVE; |
||
77 | else if (rName == "IN_ISDIR") |
||
78 | return IN_ISDIR; |
||
79 | else if (rName == "IN_ONESHOT") |
||
80 | return IN_ONESHOT; |
||
81 | else if (rName == "IN_ALL_EVENTS") |
||
82 | return IN_ALL_EVENTS; |
||
83 | |||
17 | luk | 84 | #ifdef IN_DONT_FOLLOW |
85 | else if (rName == "IN_DONT_FOLLOW") |
||
86 | return IN_DONT_FOLLOW; |
||
87 | #endif // IN_DONT_FOLLOW |
||
88 | |||
89 | #ifdef IN_ONLYDIR |
||
90 | else if (rName == "IN_ONLYDIR") |
||
91 | return IN_ONLYDIR; |
||
92 | #endif // IN_ONLYDIR |
||
93 | |||
11 | luk | 94 | return (uint32_t) 0; |
95 | } |
||
3 | luk | 96 | |
11 | luk | 97 | void InotifyEvent::DumpTypes(uint32_t uValue, std::string& rStr) |
3 | luk | 98 | { |
99 | rStr = ""; |
||
100 | |||
11 | luk | 101 | if (IsType(uValue, IN_ALL_EVENTS)) { |
102 | rStr.append("IN_ALL_EVENTS"); |
||
3 | luk | 103 | } |
11 | luk | 104 | else { |
105 | if (IsType(uValue, IN_ACCESS)) { |
||
106 | DUMP_SEP; |
||
107 | rStr.append("IN_ACCESS"); |
||
108 | } |
||
109 | if (IsType(uValue, IN_MODIFY)) { |
||
110 | DUMP_SEP; |
||
111 | rStr.append("IN_MODIFY"); |
||
112 | } |
||
113 | if (IsType(uValue, IN_ATTRIB)) { |
||
114 | DUMP_SEP; |
||
115 | rStr.append("IN_ATTRIB"); |
||
116 | } |
||
117 | if (IsType(uValue, IN_CREATE)) { |
||
118 | DUMP_SEP; |
||
119 | rStr.append("IN_CREATE"); |
||
120 | } |
||
121 | if (IsType(uValue, IN_DELETE)) { |
||
122 | DUMP_SEP; |
||
123 | rStr.append("IN_DELETE"); |
||
124 | } |
||
125 | if (IsType(uValue, IN_DELETE_SELF)) { |
||
126 | DUMP_SEP; |
||
127 | rStr.append("IN_DELETE_SELF"); |
||
128 | } |
||
129 | if (IsType(uValue, IN_OPEN)) { |
||
130 | DUMP_SEP; |
||
131 | rStr.append("IN_OPEN"); |
||
132 | } |
||
133 | if (IsType(uValue, IN_CLOSE)) { |
||
134 | DUMP_SEP; |
||
135 | rStr.append("IN_CLOSE"); |
||
136 | } |
||
137 | else { |
||
138 | if (IsType(uValue, IN_CLOSE_WRITE)) { |
||
139 | DUMP_SEP; |
||
140 | rStr.append("IN_CLOSE_WRITE"); |
||
141 | } |
||
142 | if (IsType(uValue, IN_CLOSE_NOWRITE)) { |
||
143 | DUMP_SEP; |
||
144 | rStr.append("IN_CLOSE_NOWRITE"); |
||
145 | } |
||
146 | } |
||
147 | if (IsType(uValue, IN_MOVE)) { |
||
148 | DUMP_SEP; |
||
149 | rStr.append("IN_MOVE"); |
||
150 | } |
||
151 | else { |
||
152 | if (IsType(uValue, IN_MOVED_FROM)) { |
||
153 | DUMP_SEP; |
||
154 | rStr.append("IN_MOVED_FROM"); |
||
155 | } |
||
156 | if (IsType(uValue, IN_MOVED_TO)) { |
||
157 | DUMP_SEP; |
||
158 | rStr.append("IN_MOVED_TO"); |
||
159 | } |
||
160 | } |
||
3 | luk | 161 | } |
11 | luk | 162 | if (IsType(uValue, IN_UNMOUNT)) { |
3 | luk | 163 | DUMP_SEP; |
164 | rStr.append("IN_UNMOUNT"); |
||
165 | } |
||
11 | luk | 166 | if (IsType(uValue, IN_Q_OVERFLOW)) { |
3 | luk | 167 | DUMP_SEP; |
168 | rStr.append("IN_Q_OVERFLOW"); |
||
169 | } |
||
11 | luk | 170 | if (IsType(uValue, IN_IGNORED)) { |
3 | luk | 171 | DUMP_SEP; |
172 | rStr.append("IN_IGNORED"); |
||
173 | } |
||
11 | luk | 174 | if (IsType(uValue, IN_ISDIR)) { |
3 | luk | 175 | DUMP_SEP; |
176 | rStr.append("IN_ISDIR"); |
||
177 | } |
||
11 | luk | 178 | if (IsType(uValue, IN_ONESHOT)) { |
3 | luk | 179 | DUMP_SEP; |
180 | rStr.append("IN_ONESHOT"); |
||
181 | } |
||
17 | luk | 182 | |
183 | #ifdef IN_DONT_FOLLOW |
||
184 | if (IsType(uValue, IN_DONT_FOLLOW)) { |
||
185 | DUMP_SEP; |
||
186 | rStr.append("IN_DONT_FOLLOW"); |
||
187 | } |
||
188 | #endif // IN_DONT_FOLLOW |
||
189 | |||
190 | #ifdef IN_ONLYDIR |
||
191 | if (IsType(uValue, IN_ONLYDIR)) { |
||
192 | DUMP_SEP; |
||
193 | rStr.append("IN_ONLYDIR"); |
||
194 | } |
||
195 | #endif // IN_ONLYDIR |
||
3 | luk | 196 | } |
197 | |||
11 | luk | 198 | void InotifyEvent::DumpTypes(std::string& rStr) const |
199 | { |
||
13 | luk | 200 | DumpTypes(m_uMask, rStr); |
11 | luk | 201 | } |
3 | luk | 202 | |
11 | luk | 203 | |
17 | luk | 204 | void InotifyWatch::SetMask(uint32_t uMask) throw (InotifyException) |
205 | { |
||
21 | luk | 206 | IN_WRITE_BEGIN |
207 | |||
17 | luk | 208 | if (m_wd != -1) { |
209 | int wd = inotify_add_watch(m_pInotify->GetDescriptor(), m_path.c_str(), uMask); |
||
21 | luk | 210 | if (wd != m_wd) { |
211 | IN_WRITE_END_NOTHROW |
||
17 | luk | 212 | throw InotifyException(IN_EXC_MSG("changing mask failed"), wd == -1 ? errno : EINVAL, this); |
21 | luk | 213 | } |
17 | luk | 214 | } |
215 | |||
216 | m_uMask = uMask; |
||
21 | luk | 217 | |
218 | IN_WRITE_END |
||
17 | luk | 219 | } |
220 | |||
221 | void InotifyWatch::SetEnabled(bool fEnabled) throw (InotifyException) |
||
222 | { |
||
21 | luk | 223 | IN_WRITE_BEGIN |
224 | |||
225 | if (fEnabled == m_fEnabled) { |
||
226 | IN_WRITE_END_NOTHROW |
||
17 | luk | 227 | return; |
21 | luk | 228 | } |
17 | luk | 229 | |
230 | if (m_pInotify != NULL) { |
||
231 | if (fEnabled) { |
||
232 | m_wd = inotify_add_watch(m_pInotify->GetDescriptor(), m_path.c_str(), m_uMask); |
||
21 | luk | 233 | if (m_wd == -1) { |
234 | IN_WRITE_END_NOTHROW |
||
17 | luk | 235 | throw InotifyException(IN_EXC_MSG("enabling watch failed"), errno, this); |
21 | luk | 236 | } |
17 | luk | 237 | m_pInotify->m_watches.insert(IN_WATCH_MAP::value_type(m_wd, this)); |
238 | } |
||
239 | else { |
||
21 | luk | 240 | if (inotify_rm_watch(m_pInotify->GetDescriptor(), m_wd) != 0) { |
241 | IN_WRITE_END_NOTHROW |
||
17 | luk | 242 | throw InotifyException(IN_EXC_MSG("disabling watch failed"), errno, this); |
21 | luk | 243 | } |
17 | luk | 244 | m_pInotify->m_watches.erase(m_wd); |
245 | m_wd = -1; |
||
246 | } |
||
247 | } |
||
248 | |||
249 | m_fEnabled = fEnabled; |
||
21 | luk | 250 | |
251 | IN_WRITE_END |
||
17 | luk | 252 | } |
253 | |||
254 | |||
13 | luk | 255 | Inotify::Inotify() throw (InotifyException) |
3 | luk | 256 | { |
21 | luk | 257 | IN_LOCK_INIT |
258 | |||
13 | luk | 259 | m_fd = inotify_init(); |
21 | luk | 260 | if (m_fd == -1) { |
261 | IN_LOCK_DONE |
||
17 | luk | 262 | throw InotifyException(IN_EXC_MSG("inotify init failed"), errno, NULL); |
21 | luk | 263 | } |
3 | luk | 264 | } |
265 | |||
266 | Inotify::~Inotify() |
||
267 | { |
||
268 | Close(); |
||
21 | luk | 269 | |
270 | IN_LOCK_DONE |
||
3 | luk | 271 | } |
272 | |||
273 | void Inotify::Close() |
||
274 | { |
||
21 | luk | 275 | IN_WRITE_BEGIN |
276 | |||
3 | luk | 277 | if (m_fd != -1) { |
278 | RemoveAll(); |
||
279 | close(m_fd); |
||
280 | m_fd = -1; |
||
281 | } |
||
21 | luk | 282 | |
283 | IN_WRITE_END |
||
3 | luk | 284 | } |
285 | |||
13 | luk | 286 | void Inotify::Add(InotifyWatch* pWatch) throw (InotifyException) |
3 | luk | 287 | { |
21 | luk | 288 | IN_WRITE_BEGIN |
289 | |||
17 | luk | 290 | // invalid descriptor - this case shouldn't occur - go away |
21 | luk | 291 | if (m_fd == -1) { |
292 | IN_WRITE_END_NOTHROW |
||
13 | luk | 293 | throw InotifyException(IN_EXC_MSG("invalid file descriptor"), EBUSY, this); |
21 | luk | 294 | } |
17 | luk | 295 | |
296 | // this path already watched - go away |
||
21 | luk | 297 | if (FindWatch(pWatch->GetPath()) != NULL) { |
298 | IN_WRITE_END_NOTHROW |
||
17 | luk | 299 | throw InotifyException(IN_EXC_MSG("path already watched"), EBUSY, this); |
21 | luk | 300 | } |
17 | luk | 301 | |
302 | // for enabled watch |
||
303 | if (pWatch->IsEnabled()) { |
||
3 | luk | 304 | |
17 | luk | 305 | // try to add watch to kernel |
306 | int wd = inotify_add_watch(m_fd, pWatch->GetPath().c_str(), pWatch->GetMask()); |
||
307 | |||
308 | // adding failed - go away |
||
21 | luk | 309 | if (wd == -1) { |
310 | IN_WRITE_END_NOTHROW |
||
17 | luk | 311 | throw InotifyException(IN_EXC_MSG("adding watch failed"), errno, this); |
21 | luk | 312 | } |
17 | luk | 313 | |
314 | // this path already watched (but defined another way) |
||
315 | InotifyWatch* pW = FindWatch(wd); |
||
316 | if (pW != NULL) { |
||
317 | |||
318 | // try to recover old watch because it may be modified - then go away |
||
319 | if (inotify_add_watch(m_fd, pW->GetPath().c_str(), pW->GetMask()) < 0) { |
||
21 | luk | 320 | IN_WRITE_END_NOTHROW |
17 | luk | 321 | throw InotifyException(IN_EXC_MSG("watch collision detected and recovery failed"), errno, this); |
322 | } |
||
323 | else { |
||
324 | // recovery failed - go away |
||
21 | luk | 325 | IN_WRITE_END_NOTHROW |
17 | luk | 326 | throw InotifyException(IN_EXC_MSG("path already watched (but defined another way)"), EBUSY, this); |
327 | } |
||
328 | } |
||
329 | |||
330 | pWatch->m_wd = wd; |
||
331 | m_watches.insert(IN_WATCH_MAP::value_type(pWatch->m_wd, pWatch)); |
||
332 | } |
||
333 | |||
334 | m_paths.insert(IN_WP_MAP::value_type(pWatch->m_path, pWatch)); |
||
13 | luk | 335 | pWatch->m_pInotify = this; |
21 | luk | 336 | |
337 | IN_WRITE_END |
||
3 | luk | 338 | } |
339 | |||
13 | luk | 340 | void Inotify::Remove(InotifyWatch* pWatch) throw (InotifyException) |
3 | luk | 341 | { |
21 | luk | 342 | IN_WRITE_BEGIN |
343 | |||
17 | luk | 344 | // invalid descriptor - this case shouldn't occur - go away |
21 | luk | 345 | if (m_fd == -1) { |
346 | IN_WRITE_END_NOTHROW |
||
13 | luk | 347 | throw InotifyException(IN_EXC_MSG("invalid file descriptor"), EBUSY, this); |
21 | luk | 348 | } |
17 | luk | 349 | |
350 | // for enabled watch |
||
351 | if (pWatch->m_wd != -1) { |
||
3 | luk | 352 | |
17 | luk | 353 | // removing watch failed - go away |
21 | luk | 354 | if (inotify_rm_watch(m_fd, pWatch->m_wd) == -1) { |
355 | IN_WRITE_END_NOTHROW |
||
17 | luk | 356 | throw InotifyException(IN_EXC_MSG("removing watch failed"), errno, this); |
21 | luk | 357 | } |
17 | luk | 358 | m_watches.erase(pWatch->m_wd); |
359 | pWatch->m_wd = -1; |
||
360 | } |
||
361 | |||
362 | m_paths.erase(pWatch->m_path); |
||
13 | luk | 363 | pWatch->m_pInotify = NULL; |
21 | luk | 364 | |
365 | IN_WRITE_END |
||
3 | luk | 366 | } |
367 | |||
368 | void Inotify::RemoveAll() |
||
369 | { |
||
21 | luk | 370 | IN_WRITE_BEGIN |
371 | |||
17 | luk | 372 | IN_WP_MAP::iterator it = m_paths.begin(); |
373 | while (it != m_paths.end()) { |
||
11 | luk | 374 | InotifyWatch* pW = (*it).second; |
17 | luk | 375 | if (pW->m_wd != -1) { |
376 | inotify_rm_watch(m_fd, pW->m_wd); |
||
377 | pW->m_wd = -1; |
||
378 | } |
||
11 | luk | 379 | pW->m_pInotify = NULL; |
3 | luk | 380 | it++; |
381 | } |
||
382 | |||
383 | m_watches.clear(); |
||
17 | luk | 384 | m_paths.clear(); |
21 | luk | 385 | |
386 | IN_WRITE_END |
||
3 | luk | 387 | } |
388 | |||
13 | luk | 389 | void Inotify::WaitForEvents(bool fNoIntr) throw (InotifyException) |
3 | luk | 390 | { |
391 | ssize_t len = 0; |
||
392 | |||
393 | do { |
||
394 | len = read(m_fd, m_buf, INOTIFY_BUFLEN); |
||
395 | } while (fNoIntr && len == -1 && errno == EINTR); |
||
396 | |||
15 | luk | 397 | if (errno == EWOULDBLOCK) |
398 | return; |
||
399 | |||
13 | luk | 400 | if (len < 0) |
401 | throw InotifyException(IN_EXC_MSG("reading events failed"), errno, this); |
||
3 | luk | 402 | |
21 | luk | 403 | IN_WRITE_BEGIN |
404 | |||
3 | luk | 405 | ssize_t i = 0; |
406 | while (i < len) { |
||
13 | luk | 407 | struct inotify_event* pEvt = (struct inotify_event*) &m_buf[i]; |
408 | InotifyWatch* pW = FindWatch(pEvt->wd); |
||
17 | luk | 409 | if (pW != NULL) { |
13 | luk | 410 | InotifyEvent evt(pEvt, pW); |
411 | m_events.push_back(evt); |
||
412 | } |
||
413 | i += INOTIFY_EVENT_SIZE + (ssize_t) pEvt->len; |
||
3 | luk | 414 | } |
415 | |||
21 | luk | 416 | IN_WRITE_END |
3 | luk | 417 | } |
418 | |||
13 | luk | 419 | bool Inotify::GetEvent(InotifyEvent* pEvt) throw (InotifyException) |
3 | luk | 420 | { |
21 | luk | 421 | if (pEvt == NULL) |
422 | throw InotifyException(IN_EXC_MSG("null pointer to event"), EINVAL, this); |
||
13 | luk | 423 | |
21 | luk | 424 | IN_WRITE_BEGIN |
425 | |||
426 | bool b = !m_events.empty(); |
||
427 | if (b) { |
||
428 | *pEvt = m_events.front(); |
||
3 | luk | 429 | m_events.pop_front(); |
21 | luk | 430 | } |
431 | |||
432 | IN_WRITE_END |
||
13 | luk | 433 | |
3 | luk | 434 | return b; |
435 | } |
||
436 | |||
13 | luk | 437 | bool Inotify::PeekEvent(InotifyEvent* pEvt) throw (InotifyException) |
3 | luk | 438 | { |
13 | luk | 439 | if (pEvt == NULL) |
440 | throw InotifyException(IN_EXC_MSG("null pointer to event"), EINVAL, this); |
||
3 | luk | 441 | |
21 | luk | 442 | IN_READ_BEGIN |
443 | |||
444 | bool b = !m_events.empty(); |
||
445 | if (b) { |
||
13 | luk | 446 | *pEvt = m_events.front(); |
447 | } |
||
448 | |||
21 | luk | 449 | IN_READ_END |
450 | |||
451 | return b; |
||
3 | luk | 452 | } |
453 | |||
454 | InotifyWatch* Inotify::FindWatch(int iDescriptor) |
||
455 | { |
||
21 | luk | 456 | IN_READ_BEGIN |
457 | |||
3 | luk | 458 | IN_WATCH_MAP::iterator it = m_watches.find(iDescriptor); |
21 | luk | 459 | InotifyWatch* pW = it == m_watches.end() ? NULL : (*it).second; |
460 | |||
461 | IN_READ_END |
||
462 | |||
463 | return pW; |
||
3 | luk | 464 | } |
17 | luk | 465 | |
466 | InotifyWatch* Inotify::FindWatch(const std::string& rPath) |
||
467 | { |
||
21 | luk | 468 | IN_READ_BEGIN |
469 | |||
17 | luk | 470 | IN_WP_MAP::iterator it = m_paths.find(rPath); |
21 | luk | 471 | InotifyWatch* pW = it == m_paths.end() ? NULL : (*it).second; |
472 | |||
473 | IN_READ_END |
||
17 | luk | 474 | |
21 | luk | 475 | return pW; |
17 | luk | 476 | } |
3 | luk | 477 | |
13 | luk | 478 | void Inotify::SetNonBlock(bool fNonBlock) throw (InotifyException) |
479 | { |
||
21 | luk | 480 | IN_WRITE_BEGIN |
481 | |||
482 | if (m_fd == -1) { |
||
483 | IN_WRITE_END_NOTHROW |
||
13 | luk | 484 | throw InotifyException(IN_EXC_MSG("invalid file descriptor"), EBUSY, this); |
21 | luk | 485 | } |
13 | luk | 486 | |
487 | int res = fcntl(m_fd, F_GETFL); |
||
21 | luk | 488 | if (res == -1) { |
489 | IN_WRITE_END_NOTHROW |
||
13 | luk | 490 | throw InotifyException(IN_EXC_MSG("cannot get inotify flags"), errno, this); |
21 | luk | 491 | } |
13 | luk | 492 | |
493 | if (fNonBlock) { |
||
494 | res |= O_NONBLOCK; |
||
495 | } |
||
496 | else { |
||
497 | res &= ~O_NONBLOCK; |
||
498 | } |
||
499 | |||
21 | luk | 500 | if (fcntl(m_fd, F_SETFL, res) == -1) { |
501 | IN_WRITE_END_NOTHROW |
||
13 | luk | 502 | throw InotifyException(IN_EXC_MSG("cannot set inotify flags"), errno, this); |
21 | luk | 503 | } |
504 | |||
505 | IN_WRITE_END |
||
13 | luk | 506 | } |
507 |