summaryrefslogtreecommitdiff
path: root/server/src/inotify.cc
blob: cba781a90f6bae2bed0b5e4487b0b517dd42718a (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
 *            inotify.cc
 *
 *  Wed Jan  6 09:58:47 CET 2010
 *  Copyright 2010 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Pracro.
 *
 *  Pracro 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  Pracro 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 Pracro; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "inotify.h"

#include "debug.h"

#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>

#define TEST(x, m) ((x & m) == m)

#define STEST(x, m) (TEST(x, m)?#m" ":"")

static std::string mask2asc(uint32_t mask)
{
  std::string str;

  str += STEST(mask, IN_ACCESS);
  str += STEST(mask, IN_ATTRIB);
  str += STEST(mask, IN_CLOSE_WRITE);
  str += STEST(mask, IN_CLOSE_NOWRITE);
  str += STEST(mask, IN_CREATE);
  str += STEST(mask, IN_DELETE);
  str += STEST(mask, IN_DELETE_SELF);
  str += STEST(mask, IN_MODIFY);
  str += STEST(mask, IN_MOVE_SELF);
  str += STEST(mask, IN_MOVED_FROM);
  str += STEST(mask, IN_MOVED_TO);
  str += STEST(mask, IN_OPEN);

  str += STEST(mask, IN_ALL_EVENTS);
  str += STEST(mask, IN_CLOSE);
  str += STEST(mask, IN_MOVE);

  str += STEST(mask, IN_DONT_FOLLOW);
  str += STEST(mask, IN_MASK_ADD);
  str += STEST(mask, IN_ONESHOT);
  str += STEST(mask, IN_ONLYDIR);

  str += STEST(mask, IN_IGNORED);
  str += STEST(mask, IN_ISDIR);
  str += STEST(mask, IN_Q_OVERFLOW);
  str += STEST(mask, IN_UNMOUNT);

  return str;
}

static inline bool isdir(const char *name)
{
  struct stat s;
  stat(name, &s);
  return S_ISDIR(s.st_mode);
}

INotify::Event::Event(struct inotify_event *event, std::string name)
{
  this->_name = name;
  if(event) {
    this->_mask = event->mask;
    this->_file = event->name;
  } else {
    this->_mask = 0;
    this->_file = "";
  }

  DEBUG(inotify, "Event [%s] %s (%s)\n",
        mask2asc(_mask).c_str(),
        _name.c_str(),
        _file.c_str());
}

bool INotify::Event::isAttributeChangeEvent()
{
  return TEST(_mask, IN_ATTRIB);
}

bool INotify::Event::isCloseEvent()
{
  return isCloseWriteEvent() || isCloseNoWriteEvent();
}

bool INotify::Event::isCloseWriteEvent()
{
  return TEST(_mask, IN_CLOSE_WRITE);
}

bool INotify::Event::isCloseNoWriteEvent()
{
  return TEST(_mask, IN_CLOSE_NOWRITE);
}

bool INotify::Event::isCreateEvent()
{
  return TEST(_mask, IN_CREATE);
}

bool INotify::Event::isOpenEvent()
{
  return TEST(_mask, IN_OPEN);
}

bool INotify::Event::isModifyEvent()
{
  return TEST(_mask, IN_MODIFY);
}

bool INotify::Event::isAccessEvent()
{
  return TEST(_mask, IN_ACCESS);
}

bool INotify::Event::isDeleteEvent()
{
  return TEST(_mask, IN_DELETE);
}

bool INotify::Event::isDeleteSelfEvent()
{
  return TEST(_mask, IN_DELETE_SELF);
}

bool INotify::Event::isIgnoredEvent()
{
  return TEST(_mask, IN_IGNORED);
}

bool INotify::Event::isMoveSelfEvent()
{
  return TEST(_mask, IN_MOVE_SELF);
}

bool INotify::Event::isMovedFromEvent()
{
  return TEST(_mask, IN_MOVED_FROM);
}

bool INotify::Event::isMovedToEvent()
{
  return TEST(_mask, IN_MOVED_TO);
}

bool INotify::Event::isDir()
{
  return TEST(_mask, IN_ISDIR);
}

std::string INotify::Event::name()
{
  return _name;
}

std::string INotify::Event::file()
{
  return _file;
}

uint32_t INotify::Event::mask()
{
  return _mask;
}

std::string INotify::Event::maskstr()
{
  return mask2asc(_mask);
}

INotify::INotify()
{
  ifd = inotify_init1(O_NONBLOCK); 
  if(ifd == -1) {
    perror("Inotify init failed.\n");
    return;
  }
}

INotify::~INotify()
{
  if(ifd != -1) close(ifd);
}

void INotify::addFile(std::string name, uint32_t mask)
{
  // Extra mask bitflags:
  //IN_DONT_FOLLOW (since Linux 2.6.15)
  // Don't dereference pathname if it is a symbolic link.
  //IN_MASK_ADD
  // Add (OR) events to watch mask for this pathname if it already
  // exists (instead of replacing mask).
  //IN_ONESHOT
  // Monitor pathname for one event, then remove from watch list.
  //IN_ONLYDIR (since Linux 2.6.15)
  // Only watch pathname if it is a directory.

  int wd = inotify_add_watch(ifd, name.c_str(), mask);
  if(wd == -1) {
    perror("INotify: Add watch failed!");
    return;
  }

  Watch w;
  w.mask = mask;
  w.name = name;
  w.depth = WATCH_SINGLE;
  dirmap[wd] = w;
}

static inline bool isdir(std::string name)
{
  struct stat s;
  stat(name.c_str(), &s);
  return S_ISDIR(s.st_mode);
}

void INotify::addDirectory(std::string name, depth_t depth, uint32_t mask)
{
  DEBUG(inotify, "Adding dir: %s\n", name.c_str());

  int depth_mask = 0;
  if(depth == WATCH_DEEP || depth == WATCH_DEEP_FOLLOW) {
    depth_mask = IN_CREATE; // We need to watch for create in order to catch
    // creation of new subdirs.

    DIR *dir = opendir(name.c_str());
    if(!dir) {
      ERR(inotify, "Could not open directory: %s - %s\n",
          name.c_str(), strerror(errno));
      return;
    }

    struct dirent *dirent;
    while( (dirent = readdir(dir)) != NULL ) {

      if(std::string(dirent->d_name) == "." ||
         std::string(dirent->d_name) == "..")
        continue;

      if(isdir(name+"/"+dirent->d_name))
        addDirectory(name+"/"+dirent->d_name, depth, mask);
    }

    closedir(dir);
  }

  int wd = inotify_add_watch(ifd, name.c_str(), mask | IN_ONLYDIR | depth_mask);
  if(wd == -1) {
    ERR(inotify, "INotify: Add watch failed on %s\n", name.c_str());
    return;
  }

  Watch w;
  w.mask = mask;
  w.name = name;
  w.depth = depth;
  dirmap[wd] = w;
}

void INotify::remove(int wd)
{
  if(inotify_rm_watch(ifd, wd) == -1) {
    perror("inotify_rm_watch failed");
    return;
  }
  dirmap.erase(wd);
}

void INotify::remove(std::string name)
{
  std::map<int, Watch>::iterator i = dirmap.begin();
  while(i != dirmap.end()) {
    Watch w = i->second;
    if(w.name == name) this->remove(i->first);
    i++;
  }
}

void INotify::readEvents()
{
  size_t size = 64;
  char *buf = (char*)malloc(size);

  ssize_t r;
  while( ((r = read(ifd, buf, size)) == -1 && errno == EINVAL) || r == 0 ) {
    //    fprintf(stderr, "Doubling buffer size: %d\n", size);
    size *= 2;
    buf = (char*)realloc(buf, size);
  }

  int p = 0;
  while(p < r) {
    struct inotify_event *event = (struct inotify_event*)(buf + p);
    p += sizeof(struct inotify_event) + event->len;

    /*
    switch(event.mask) {
    case IN_IGNORED:
      //Watch was removed explicitly (inotify_rm_watch(2)) or automatically 
      // (file was deleted, or file system was unmounted).
    case IN_ISDIR:
      //Subject of this event is a directory.
    case IN_Q_OVERFLOW:
      //Event queue overflowed (wd is -1 for this event).
    case IN_UNMOUNT:
      //File system containing watched object was unmounted.
      break;
    default:
      break;
    }
    */

    // TODO: We need to figure out what the new filename/destination is...
    if(TEST(event->mask, IN_MOVE_SELF)) dirmap[event->wd].name = "????????";

    if(dirmap[event->wd].depth == WATCH_DEEP_FOLLOW && 
       TEST(event->mask, IN_CREATE) && 
       TEST(event->mask, IN_ISDIR))
      addDirectory(dirmap[event->wd].name + "/" + event->name,
                   dirmap[event->wd].depth, dirmap[event->wd].mask);

    if(TEST(event->mask, IN_CREATE) &&
       !TEST(dirmap[event->wd].mask, IN_CREATE)) {
      // Ignore this event, it was not requested by the user.
    } else {
      eventlist.push_back(INotify::Event(event, dirmap[event->wd].name));
    }

    if(TEST(event->mask, IN_IGNORED)) dirmap.erase(event->wd);
  }

  free(buf);
}

bool INotify::hasEvents()
{
  readEvents();
  return eventlist.size() > 0;
}

INotify::Event INotify::getNextEvent()
{
  readEvents();
  if(eventlist.size() == 0) return Event(NULL, "");
  Event e = eventlist.front();
  eventlist.pop_front();
  return e;
}

void INotify::clear()
{
  if(ifd != -1) close(ifd);
  ifd = inotify_init1(O_NONBLOCK); 
  if(ifd == -1) {
    perror("Inotify init failed.\n");
  }
}

#ifdef TEST_INOTIFY
//deps: debug.cc log.cc
//cflags: -I..
//libs:
#include "test.h"

#include <stdio.h>

#define _BASEDIR "/tmp"
#define _DIR _BASEDIR"/inotify_test_dir"
#define _FILE _BASEDIR"/inotify_test"

TEST_BEGIN;

debug_parse("+all");

INotify inotify;

// Create file
FILE *fp = fopen(_FILE, "w");
if(!fp) TEST_FATAL("Unable to write to the file");
fprintf(fp, "something");
fclose(fp);

inotify.addFile(_FILE);

TEST_MSG("Positive tests on file watch.");

// Append to file
fp = fopen(_FILE, "r");
if(!fp) TEST_FATAL("Unable to read from the file");
TEST_TRUE(inotify.hasEvents(), "Test if the open event was triggered.");
TEST_TRUE(inotify.getNextEvent().isOpenEvent(), "Test if the event was an open event.");

char buf[32];
fread(buf, sizeof(buf), 1, fp);
TEST_TRUE(inotify.hasEvents(), "Test if the read event was triggered.");
TEST_TRUE(inotify.getNextEvent().isAccessEvent(), "Test if the event was a access event.");

fclose(fp);
TEST_TRUE(inotify.hasEvents(), "Test if the close event was triggered.");
TEST_TRUE(inotify.getNextEvent().isCloseNoWriteEvent(), "Test if the event was a close-nowrite event.");


// Append to file
fp = fopen(_FILE, "a");
if(!fp) TEST_FATAL("Unable to write to the file");
TEST_TRUE(inotify.hasEvents(), "Test if the open event was triggered.");
TEST_TRUE(inotify.getNextEvent().isOpenEvent(), "Test if the event was an open event.");

fprintf(fp, "else"); fflush(fp);
TEST_TRUE(inotify.hasEvents(), "Test if the append event was triggered.");
TEST_TRUE(inotify.getNextEvent().isModifyEvent(), "Test if the event was a modified event.");

fclose(fp);
TEST_TRUE(inotify.hasEvents(), "Test if the close event was triggered.");
TEST_TRUE(inotify.getNextEvent().isCloseWriteEvent(), "Test if the event was a close event.");


// Overwrite file
fp = fopen(_FILE, "w");
if(!fp) TEST_FATAL("Unable to write to the file");

// Open for write initially empties the file content, thus provoking a changed event.
TEST_TRUE(inotify.hasEvents(), "Test if the modified event was triggered.");
TEST_TRUE(inotify.getNextEvent().isModifyEvent(), "Test if the event was a modified event.");

TEST_TRUE(inotify.hasEvents(), "Test if the open event was triggered.");
TEST_TRUE(inotify.getNextEvent().isOpenEvent(), "Test if the event was an open event.");

fprintf(fp, "else"); fflush(fp);
TEST_TRUE(inotify.hasEvents(), "Test if the write event was triggered.");
TEST_TRUE(inotify.getNextEvent().isModifyEvent(), "Test if the event was a modified event.");

fclose(fp);
TEST_TRUE(inotify.hasEvents(), "Test if the close event was triggered.");
TEST_TRUE(inotify.getNextEvent().isCloseWriteEvent(), "Test if the event was a close event.");

// Rename file
rename(_FILE, _FILE"2");
TEST_TRUE(inotify.hasEvents(), "Test if the rename event was triggered.");
TEST_TRUE(inotify.getNextEvent().isMoveSelfEvent(), "Test if the event was a move self event.");

// Delete file
unlink(_FILE"2");

// Unlink initially counts down the link counter, which provokes an attributes changed event.
TEST_TRUE(inotify.hasEvents(), "Test if the delete event was triggered.");
TEST_TRUE(inotify.getNextEvent().isAttributeChangeEvent(), "Test if the event was an attribute change event.");

// Since the linkcount should now be zero, the file should be deleted.
TEST_TRUE(inotify.hasEvents(), "Test if the delete event was triggered.");
TEST_TRUE(inotify.getNextEvent().isDeleteSelfEvent(), "Test if the event was a delete self event.");

// Watch is removed upon delete.
//inotify.remove(_FILE);

TEST_TRUE(inotify.hasEvents(), "Test if the delete event triggered an ignored event.");
TEST_TRUE(inotify.getNextEvent().isIgnoredEvent(), "Test if the event was an ignored event.");

// Create file again
fp = fopen(_FILE, "w");
if(!fp) TEST_FATAL("Unable to write to the file");
fprintf(fp, "something");
fclose(fp);

inotify.addFile(_FILE);
inotify.remove(_FILE);

TEST_TRUE(inotify.hasEvents(), "Test if the call to remove triggered an ignored event.");
TEST_TRUE(inotify.getNextEvent().isIgnoredEvent(), "Test if the event was an ignored event.");

// Delete file
unlink(_FILE);
inotify.getNextEvent();
TEST_FALSE(inotify.hasEvents(), "Test if the delete event was ignored.");

TEST_FALSE(inotify.hasEvents(), "Test if the event queue is now empty.");

TEST_MSG("Positive tests on directory watch.");

if(mkdir(_DIR, 0777) == -1) TEST_FATAL("Could not create test dir.");
inotify.addDirectory(_DIR, WATCH_DEEP_FOLLOW);

// Create file again
fp = fopen(_DIR"/file1", "w");
if(!fp) TEST_FATAL("Unable to write to the file");
fprintf(fp, "something");
fclose(fp);

TEST_TRUE(inotify.hasEvents(), "Test if the file creation triggered events.");
TEST_TRUE(inotify.getNextEvent().isCreateEvent(), "...");
TEST_TRUE(inotify.getNextEvent().isOpenEvent(), "...");
TEST_TRUE(inotify.getNextEvent().isModifyEvent(), "...");
TEST_TRUE(inotify.getNextEvent().isCloseWriteEvent(), "...");

rename(_DIR"/file1", _DIR"/file2");
TEST_TRUE(inotify.hasEvents(), "Test if the file renaming triggered events.");
TEST_TRUE(inotify.getNextEvent().isMovedFromEvent(), "...");
TEST_TRUE(inotify.getNextEvent().isMovedToEvent(), "...");

unlink(_DIR"/file2");

if(mkdir(_DIR"/dir", 0777) == -1) TEST_FATAL("Could not create test dir.");

if(mkdir(_DIR"/dir/anotherdir", 0777) == -1) TEST_FATAL("Could not create test dir.");

while(inotify.hasEvents()) inotify.getNextEvent();

rmdir(_DIR"/dir/anotherdir");

rmdir(_DIR"/dir");

rmdir(_DIR);

while(inotify.hasEvents()) inotify.getNextEvent();

TEST_END;

#endif/*TEST_INOTIFY*/