summaryrefslogtreecommitdiff
path: root/server/src/inotify.cc
blob: e1d47d158e7de3cc1fbefa4bdb67b9e05d8e85af (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
/* -*- 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 <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <fcntl.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(std::string name, uint32_t mask)
{
  this->_name = name;
  this->_mask = mask;

  printf("[%s]\n", mask2asc(_mask).c_str());
}
bool INotify::Event::isAttributeChangeEvent()
{
  return TEST(_mask, IN_ATTRIB);
}

bool INotify::Event::isCloseEvent()
{
  return TEST(_mask, IN_CLOSE_WRITE) || TEST(_mask, IN_CLOSE_NOWRITE);
}

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

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

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

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

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

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

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

void INotify::add(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 _fd = inotify_add_watch(fd, name.c_str(), mask);
  if(_fd == -1) {
    perror("INotify: Add watch failed!");
    return;
  }
  //  dirmap[fd] = name;
}

void INotify::addRecursive(std::string name, uint32_t mask)
{
  /*
  int fd = this->add(name, mask);
  return fd;
  */
}

bool INotify::hasEvents()
{
  struct timeval tv;
  tv.tv_sec = 0;
  tv.tv_usec = 0;

  fd_set fset;
  int ret;
  FD_ZERO(&fset);
  FD_SET(fd, &fset);
  ret = select (fd+1, &fset, NULL, NULL, &tv);
  switch(ret) {
  case -1:
    if(errno == EINTR) {
      // Interrupt
    }
    break;

  case 0:
    // Timeout
    break;

  default:
    if(FD_ISSET(fd, &fset)) {
      // Ready read
      return true;
    }
  }

  return false;
}

INotify::Event INotify::getNextEvent()
{
  char buf[1024];
  ssize_t r = read(fd, buf, sizeof(struct inotify_event));
  if(r == sizeof(struct inotify_event)) {
    struct inotify_event *event = (struct inotify_event *)buf;
    if(event->len) read(fd, buf + 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;
    }

    return Event(event->name, event->mask);
  }
  return Event("", 0);
}

void INotify::remove(int fd)
{
  inotify_rm_watch(this->fd, fd);
}

void INotify::remove(std::string name)
{
  //  this->remove(rlookup(map, name))
}

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

#include <stdio.h>

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

TEST_BEGIN;

INotify inotify;

// Create file
FILE *fp = fopen(_FILE, "w");
TEST_NOTEQUAL(fp, NULL, "Testing if able to write file");
fprintf(fp, "something");
fclose(fp);

inotify.add(_FILE);


// Append to file
fp = fopen(_FILE, "a");
TEST_NOTEQUAL(fp, NULL, "Testing if able to write 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().isCloseEvent(), "Test if the event was a close event.");


// Overwrite file
fp = fopen(_FILE, "w");
TEST_NOTEQUAL(fp, NULL, "Testing if able to write 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().isCloseEvent(), "Test if the event was a close event.");


// Delete file
unlink(_FILE);

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

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

TEST_END;

#endif/*TEST_INOTIFY*/