Subversion Repositories public

Rev

Rev 49 | Rev 55 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
45 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>
47 luk 25
#include <fcntl.h>
45 luk 26
 
27
#include "inotify-cxx.h"
28
 
47 luk 29
/// dump separator (between particular entries)
45 luk 30
#define DUMP_SEP \
31
  ({ \
32
    if (!rStr.empty()) { \
33
      rStr.append(","); \
34
    } \
35
  })
36
 
47 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
 
45 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
 
49 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
 
45 luk 94
  return (uint32_t) 0;
95
}
96
 
97
void InotifyEvent::DumpTypes(uint32_t uValue, std::string& rStr)
98
{
99
  rStr = "";
100
 
101
  if (IsType(uValue, IN_ALL_EVENTS)) {
102
    rStr.append("IN_ALL_EVENTS");
103
  }
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
    }
161
  }
162
  if (IsType(uValue, IN_UNMOUNT)) {
163
    DUMP_SEP;
164
    rStr.append("IN_UNMOUNT");
165
  }
166
  if (IsType(uValue, IN_Q_OVERFLOW)) {
167
    DUMP_SEP;
168
    rStr.append("IN_Q_OVERFLOW");
169
  }
170
  if (IsType(uValue, IN_IGNORED)) {
171
    DUMP_SEP;
172
    rStr.append("IN_IGNORED");
173
  }
174
  if (IsType(uValue, IN_ISDIR)) {
175
    DUMP_SEP;
176
    rStr.append("IN_ISDIR");
177
  }
178
  if (IsType(uValue, IN_ONESHOT)) {
179
    DUMP_SEP;
180
    rStr.append("IN_ONESHOT");
181
  }
49 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
45 luk 196
}
197
 
198
void InotifyEvent::DumpTypes(std::string& rStr) const
199
{
47 luk 200
  DumpTypes(m_uMask, rStr);
45 luk 201
}
202
 
203
 
49 luk 204
void InotifyWatch::SetMask(uint32_t uMask) throw (InotifyException)
205
{
51 luk 206
  IN_WRITE_BEGIN
207
 
49 luk 208
  if (m_wd != -1) {
209
    int wd = inotify_add_watch(m_pInotify->GetDescriptor(), m_path.c_str(), uMask);
51 luk 210
    if (wd != m_wd) {
211
      IN_WRITE_END_NOTHROW
49 luk 212
      throw InotifyException(IN_EXC_MSG("changing mask failed"), wd == -1 ? errno : EINVAL, this);
51 luk 213
    }
49 luk 214
  }
215
 
216
  m_uMask = uMask;
51 luk 217
 
218
  IN_WRITE_END
49 luk 219
}
220
 
221
void InotifyWatch::SetEnabled(bool fEnabled) throw (InotifyException)
222
{
51 luk 223
  IN_WRITE_BEGIN
224
 
225
  if (fEnabled == m_fEnabled) {
226
    IN_WRITE_END_NOTHROW
49 luk 227
    return;
51 luk 228
  }
49 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);
51 luk 233
      if (m_wd == -1) {
234
        IN_WRITE_END_NOTHROW
49 luk 235
        throw InotifyException(IN_EXC_MSG("enabling watch failed"), errno, this);
51 luk 236
      }
49 luk 237
      m_pInotify->m_watches.insert(IN_WATCH_MAP::value_type(m_wd, this));
238
    }
239
    else {
51 luk 240
      if (inotify_rm_watch(m_pInotify->GetDescriptor(), m_wd) != 0) {
241
        IN_WRITE_END_NOTHROW
49 luk 242
        throw InotifyException(IN_EXC_MSG("disabling watch failed"), errno, this);
51 luk 243
      }
49 luk 244
      m_pInotify->m_watches.erase(m_wd);
245
      m_wd = -1;
246
    }
247
  }
248
 
249
  m_fEnabled = fEnabled;
51 luk 250
 
251
  IN_WRITE_END
49 luk 252
}
253
 
254
 
47 luk 255
Inotify::Inotify() throw (InotifyException)
45 luk 256
{
51 luk 257
  IN_LOCK_INIT
258
 
47 luk 259
  m_fd = inotify_init();
51 luk 260
  if (m_fd == -1) {
261
    IN_LOCK_DONE
49 luk 262
    throw InotifyException(IN_EXC_MSG("inotify init failed"), errno, NULL);
51 luk 263
  }
45 luk 264
}
265
 
266
Inotify::~Inotify()
267
{
268
  Close();
51 luk 269
 
270
  IN_LOCK_DONE
45 luk 271
}
272
 
273
void Inotify::Close()
274
{
51 luk 275
  IN_WRITE_BEGIN
276
 
45 luk 277
  if (m_fd != -1) {
278
    RemoveAll();
279
    close(m_fd);
280
    m_fd = -1;
281
  }
51 luk 282
 
283
  IN_WRITE_END
45 luk 284
}
285
 
47 luk 286
void Inotify::Add(InotifyWatch* pWatch) throw (InotifyException)
45 luk 287
{
51 luk 288
  IN_WRITE_BEGIN
289
 
49 luk 290
  // invalid descriptor - this case shouldn't occur - go away
51 luk 291
  if (m_fd == -1) {
292
    IN_WRITE_END_NOTHROW
47 luk 293
    throw InotifyException(IN_EXC_MSG("invalid file descriptor"), EBUSY, this);
51 luk 294
  }
49 luk 295
 
296
  // this path already watched - go away  
51 luk 297
  if (FindWatch(pWatch->GetPath()) != NULL) {
298
    IN_WRITE_END_NOTHROW
49 luk 299
    throw InotifyException(IN_EXC_MSG("path already watched"), EBUSY, this);
51 luk 300
  }
49 luk 301
 
302
  // for enabled watch
303
  if (pWatch->IsEnabled()) {
45 luk 304
 
49 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
51 luk 309
    if (wd == -1) {
310
      IN_WRITE_END_NOTHROW
49 luk 311
      throw InotifyException(IN_EXC_MSG("adding watch failed"), errno, this);
51 luk 312
    }
49 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) {
51 luk 320
        IN_WRITE_END_NOTHROW
49 luk 321
        throw InotifyException(IN_EXC_MSG("watch collision detected and recovery failed"), errno, this);
322
      }
323
      else {
324
        // recovery failed - go away
51 luk 325
        IN_WRITE_END_NOTHROW
49 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));
47 luk 335
  pWatch->m_pInotify = this;
51 luk 336
 
337
  IN_WRITE_END
45 luk 338
}
339
 
47 luk 340
void Inotify::Remove(InotifyWatch* pWatch) throw (InotifyException)
45 luk 341
{
51 luk 342
  IN_WRITE_BEGIN
343
 
49 luk 344
  // invalid descriptor - this case shouldn't occur - go away
51 luk 345
  if (m_fd == -1) {
346
    IN_WRITE_END_NOTHROW
47 luk 347
    throw InotifyException(IN_EXC_MSG("invalid file descriptor"), EBUSY, this);
51 luk 348
  }
49 luk 349
 
350
  // for enabled watch
351
  if (pWatch->m_wd != -1) {  
45 luk 352
 
49 luk 353
    // removing watch failed - go away
51 luk 354
    if (inotify_rm_watch(m_fd, pWatch->m_wd) == -1) {
355
      IN_WRITE_END_NOTHROW
49 luk 356
      throw InotifyException(IN_EXC_MSG("removing watch failed"), errno, this);
51 luk 357
    }
49 luk 358
    m_watches.erase(pWatch->m_wd);
359
    pWatch->m_wd = -1;
360
  }
361
 
362
  m_paths.erase(pWatch->m_path);
47 luk 363
  pWatch->m_pInotify = NULL;
51 luk 364
 
365
  IN_WRITE_END
45 luk 366
}
367
 
368
void Inotify::RemoveAll()
369
{
51 luk 370
  IN_WRITE_BEGIN
371
 
49 luk 372
  IN_WP_MAP::iterator it = m_paths.begin();
373
  while (it != m_paths.end()) {
45 luk 374
    InotifyWatch* pW = (*it).second;
49 luk 375
    if (pW->m_wd != -1) {
376
      inotify_rm_watch(m_fd, pW->m_wd);
377
      pW->m_wd = -1;
378
    }
45 luk 379
    pW->m_pInotify = NULL;
380
    it++;
381
  }
382
 
383
  m_watches.clear();
49 luk 384
  m_paths.clear();
51 luk 385
 
386
  IN_WRITE_END
45 luk 387
}
388
 
47 luk 389
void Inotify::WaitForEvents(bool fNoIntr) throw (InotifyException)
45 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
 
47 luk 397
  if (errno == EWOULDBLOCK)
398
    return;
45 luk 399
 
47 luk 400
  if (len < 0)
401
    throw InotifyException(IN_EXC_MSG("reading events failed"), errno, this);
402
 
51 luk 403
  IN_WRITE_BEGIN
404
 
45 luk 405
  ssize_t i = 0;
406
  while (i < len) {
47 luk 407
    struct inotify_event* pEvt = (struct inotify_event*) &m_buf[i];
408
    InotifyWatch* pW = FindWatch(pEvt->wd);
49 luk 409
    if (pW != NULL) {
47 luk 410
      InotifyEvent evt(pEvt, pW);
411
      m_events.push_back(evt);
412
    }
413
    i += INOTIFY_EVENT_SIZE + (ssize_t) pEvt->len;
45 luk 414
  }
415
 
51 luk 416
  IN_WRITE_END
45 luk 417
}
418
 
47 luk 419
bool Inotify::GetEvent(InotifyEvent* pEvt) throw (InotifyException)
45 luk 420
{
51 luk 421
  if (pEvt == NULL)
422
    throw InotifyException(IN_EXC_MSG("null pointer to event"), EINVAL, this);
47 luk 423
 
51 luk 424
  IN_WRITE_BEGIN
425
 
426
  bool b = !m_events.empty();
427
  if (b) {
428
    *pEvt = m_events.front();
45 luk 429
    m_events.pop_front();
51 luk 430
  }
431
 
432
  IN_WRITE_END
47 luk 433
 
45 luk 434
  return b;
435
}
436
 
47 luk 437
bool Inotify::PeekEvent(InotifyEvent* pEvt) throw (InotifyException)
45 luk 438
{
47 luk 439
  if (pEvt == NULL)
440
    throw InotifyException(IN_EXC_MSG("null pointer to event"), EINVAL, this);
45 luk 441
 
51 luk 442
  IN_READ_BEGIN
443
 
444
  bool b = !m_events.empty();
445
  if (b) {
47 luk 446
    *pEvt = m_events.front();
447
  }
448
 
51 luk 449
  IN_READ_END
450
 
451
  return b;
45 luk 452
}
453
 
454
InotifyWatch* Inotify::FindWatch(int iDescriptor)
455
{
51 luk 456
  IN_READ_BEGIN
457
 
45 luk 458
  IN_WATCH_MAP::iterator it = m_watches.find(iDescriptor);
51 luk 459
  InotifyWatch* pW = it == m_watches.end() ? NULL : (*it).second;
460
 
461
  IN_READ_END
462
 
463
  return pW;
45 luk 464
}
49 luk 465
 
466
InotifyWatch* Inotify::FindWatch(const std::string& rPath)
467
{
51 luk 468
  IN_READ_BEGIN
469
 
49 luk 470
  IN_WP_MAP::iterator it = m_paths.find(rPath);
51 luk 471
  InotifyWatch* pW = it == m_paths.end() ? NULL : (*it).second;
472
 
473
  IN_READ_END
49 luk 474
 
51 luk 475
  return pW;
49 luk 476
}
45 luk 477
 
47 luk 478
void Inotify::SetNonBlock(bool fNonBlock) throw (InotifyException)
479
{
51 luk 480
  IN_WRITE_BEGIN
481
 
482
  if (m_fd == -1) {
483
    IN_WRITE_END_NOTHROW
47 luk 484
    throw InotifyException(IN_EXC_MSG("invalid file descriptor"), EBUSY, this);
51 luk 485
  }
47 luk 486
 
487
  int res = fcntl(m_fd, F_GETFL);
51 luk 488
  if (res == -1) {
489
    IN_WRITE_END_NOTHROW
47 luk 490
    throw InotifyException(IN_EXC_MSG("cannot get inotify flags"), errno, this);
51 luk 491
  }
47 luk 492
 
493
  if (fNonBlock) {
494
    res |= O_NONBLOCK;
495
  }
496
  else {
497
    res &= ~O_NONBLOCK;
498
  }
499
 
51 luk 500
  if (fcntl(m_fd, F_SETFL, res) == -1) {
501
    IN_WRITE_END_NOTHROW
47 luk 502
    throw InotifyException(IN_EXC_MSG("cannot set inotify flags"), errno, this);
51 luk 503
  }
504
 
505
  IN_WRITE_END
47 luk 506
}  
507