fs_watcher.c
10 KB
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
#include <sys/select.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <scot/inotify.h>
#include <scot/fs_watcher.h>
#include <scot/event_listener.h>
#include <scot/exception.h>
#include <scot/stream.h>
#include <scot/event.h>
#include <scot/memory.h>
#include <scot/thread.h>
#include <scot/scot_types.h>
#define GEN_LOCAL
# include <scot/list.h>
#undef GEN_LOCAL
GEN_LIST_FUNC_PROTO (scot_fsw_info);
GEN_LIST_IMPL (scot_fsw_info);
static
void
scot_fsw_info_free (scot_fsw_info * e)
{
if (e != NULL)
{
if (e->path != NULL)
SCOT_MEM_FREE (e->path);
if (e->old_name != NULL)
SCOT_MEM_FREE (e->old_name);
SCOT_MEM_FREE (e);
}
}
static
int
scot_fsw_info_cmp_path (const scot_fsw_info * a, const scot_fsw_info * b)
{
return strcmp (a->path, b->path);
}
static
int
scot_fsw_info_cmp_watch_d (const scot_fsw_info * a, const scot_fsw_info * b)
{
return (a->watch_d == b->watch_d)?0:(a->watch_d < b->watch_d)?-1:1;
}
static
void
thread_fini (void * arg)
{
exc_end ();
}
static
unsigned short
default_cb (struct scot_event * _e)
{
SSIZE_T n;
char puffer[1024];
struct scot_fs_watcher_event * e = (struct scot_fs_watcher_event *) _e;
switch (_e->event)
{
case (SCOT_EVENT_FS_WATCHER_CREATE):
printf ("%s/%s created\n", e->path, e->name);
break;
case (SCOT_EVENT_FS_WATCHER_DELETE):
printf ("%s/%s deleted\n", e->path, e->name);
break;
case (SCOT_EVENT_FS_WATCHER_RENAME):
printf ("%s/%s renamed to %s/%s\n",
e->path, e->oldname,
e->path, e->name);
break;
/* more to come .... */
default: break;
}
fflush (stdout);
return SCOT_EVENT_END;
}
struct scot_fs_watcher *
scot_fs_watcher_new (void)
{
struct scot_fs_watcher * fsw;
fsw = SCOT_MEM_GET (sizeof (struct scot_fs_watcher));
SCOT_MEM_ZERO (fsw, sizeof (struct scot_fs_watcher));
fsw->w_list = list_scot_fsw_info_new (fsw->w_list);
list_scot_fsw_info_set_elem_free (scot_fsw_info_free);
SCOT_MEM_ZERO (&fsw->notify_d, sizeof (struct scot_stream));
fsw->notify_d.handle.file = inotify_init ();
if (fsw->notify_d.handle.file == -1)
THROW (EXC (EXC_ERROR, errno, strerror (errno)));
fsw->notify_d.s_type = SCOT_STREAM_TYPE_INOTIFY;
scot_stream_set_nonblock (&fsw->notify_d);
FD_SET (fsw->notify_d.handle.file, &fsw->rfds);
scot_event_listener_init (
(struct scot_event_listener *) fsw,
SCOT_EG_FS_WATCHER,
(scot_thread_entry_fptr) scot_fs_watcher_main_loop);
scot_event_listener_register_cb (
(struct scot_event_listener *) fsw,
SCOT_EVENT_FS_WATCHER_CREATE,
default_cb, NULL);
scot_event_listener_register_cb (
(struct scot_event_listener *) fsw,
SCOT_EVENT_FS_WATCHER_DELETE,
default_cb, NULL);
scot_event_listener_register_cb (
(struct scot_event_listener *) fsw,
SCOT_EVENT_FS_WATCHER_RENAME,
default_cb, NULL);
NEW_THREAD_MUTEX (&fsw->mutex);
return fsw;
}
void
scot_fs_watcher_free (struct scot_fs_watcher * fsw)
{
scot_event_listener_fini ((struct scot_event_listener *) fsw);
list_scot_fsw_info_free (fsw->w_list);
FREE_THREAD_MUTEX (&fsw->mutex);
SCOT_MEM_FREE (fsw);
}
void
scot_fs_watcher_add (
struct scot_fs_watcher * fsw,
const char * path,
uint32_t mask)
{
struct scot_event_listener * l = (struct scot_event_listener *) fsw;
struct scot_fsw_info * fswi;
/*
* soweit ich das in erinnerung habe sind meine listen nicht per se
* thread save. Daher sollte hier unbedingt noch ein mutex her,
* damit nicht gleichzeitig in die liste geschriebe (hier)
* und gelöscht wird (in remove)
*/
LOCK_THREAD_MUTEX (&fsw->mutex);
fswi = SCOT_MEM_GET (sizeof (struct scot_fsw_info));
SCOT_MEM_ZERO (fswi, sizeof (struct scot_fsw_info));
fswi->path = SCOT_MEM_GET (SCOT_STR_LENGTH (path) + 1);
SCOT_STR_COPY (fswi->path, path);
if ((mask & SCOT_EVENT_FS_WATCHER_CREATE) == SCOT_EVENT_FS_WATCHER_CREATE)
fswi->mask |= IN_CREATE;
if ((mask & SCOT_EVENT_FS_WATCHER_DELETE) == SCOT_EVENT_FS_WATCHER_DELETE)
fswi->mask |= IN_DELETE;
if ((mask & SCOT_EVENT_FS_WATCHER_RENAME) == SCOT_EVENT_FS_WATCHER_RENAME)
fswi->mask |= IN_MOVED_FROM | IN_MOVED_TO;
fswi->watch_d = inotify_add_watch (fsw->notify_d.handle.file, path, fswi->mask);
if (fswi->watch_d == -1)
{
SCOT_MEM_FREE (fswi->path);
SCOT_MEM_FREE (fswi);
THROW (EXC (EXC_ERROR, errno, strerror (errno)));
}
list_scot_fsw_info_set_cmp (scot_fsw_info_cmp_path);
if (list_scot_fsw_info_find (fsw->w_list, fswi) == NULL)
list_scot_fsw_info_insert (fsw->w_list, fswi);
list_scot_fsw_info_set_cmp (list_scot_fsw_info_default_cmp);
/* restart thread if running. */
if (scot_event_listener_is_running (l) != 0 && l->thread_handle != THREAD_ID)
{
scot_event_listener_stop (l);
scot_event_listener_start (l);
}
UNLOCK_THREAD_MUTEX (&fsw->mutex);
}
void
scot_fs_watcher_remove (
struct scot_fs_watcher * fsw,
const char * path,
uint32_t mask)
{
struct scot_event_listener * l = (struct scot_event_listener *) fsw;
int was_running = scot_event_listener_is_running (l);
list_scot_fsw_info_node_t * node;
struct scot_fsw_info search_fswi = {0, (char *)path, 0, 0, NULL};
struct scot_fsw_info * fswi;
LOCK_THREAD_MUTEX (&fsw->mutex);
if (was_running != 0 && l->thread_handle != THREAD_ID)
scot_event_listener_stop (l);
list_scot_fsw_info_set_cmp (scot_fsw_info_cmp_path);
node = list_scot_fsw_info_find (fsw->w_list, &search_fswi);
fswi = list_scot_fsw_info_retrive (node);
list_scot_fsw_info_set_cmp (list_scot_fsw_info_default_cmp);
if ((mask & SCOT_EVENT_FS_WATCHER_CREATE) == SCOT_EVENT_FS_WATCHER_CREATE)
fswi->mask &= ~IN_CREATE;
if ((mask & SCOT_EVENT_FS_WATCHER_DELETE) == SCOT_EVENT_FS_WATCHER_DELETE)
fswi->mask &= ~IN_DELETE;
if ((mask & SCOT_EVENT_FS_WATCHER_RENAME) == SCOT_EVENT_FS_WATCHER_RENAME)
fswi->mask &= ~IN_MOVED_FROM & ~IN_MOVED_TO;
if (fswi->mask == 0)
{
inotify_rm_watch (fsw->notify_d.handle.file, fswi->watch_d);
node = list_scot_fsw_info_delete (node);
}
else
{
inotify_add_watch (fsw->notify_d.handle.file, fswi->path, fswi->mask);
node = list_scot_fsw_info_next (node);
}
if (was_running != 0 && l->thread_handle != THREAD_ID)
scot_event_listener_start (l);
UNLOCK_THREAD_MUTEX (&fsw->mutex);
}
static
struct inotify_event *
scot_fs_watcher_get_event (struct scot_fs_watcher * fsw)
{
static int size = 0;
static int got = 0;
static char * buffer = NULL;
struct inotify_event * e = NULL;
if (size == 0)
{
got = 0;
size = 16;
size = sizeof (struct inotify_event);
buffer = SCOT_MEM_GET (size);
}
got += scot_stream_read (&fsw->notify_d, buffer+got, size-got);
if (size == got)
{
if (size == sizeof (struct inotify_event))
{
char * tmp_buf = buffer;
size += ((struct inotify_event *)buffer)->len;
buffer = SCOT_MEM_GET (size);
SCOT_MEM_COPY (buffer, tmp_buf, sizeof (struct inotify_event));
SCOT_MEM_FREE (tmp_buf);
got += scot_stream_read (&fsw->notify_d, buffer+got, size-got);
}
if (size == got)
{
e = SCOT_MEM_GET (size);
SCOT_MEM_COPY (e, buffer, size);
SCOT_MEM_FREE (buffer);
size = 0;
}
}
return e;
}
/*
* entry point für einen stream-pool event-source thread.
*/
void
scot_fs_watcher_main_loop (struct scot_fs_watcher * fsw)
{
fd_set run_rfds;
excenv_t * ee;
THREAD_CANCEL_ENABLE;
THREAD_CANCEL_DEFER;
TRY
{
THREAD_ATEXIT_BEGIN (thread_fini, NULL);
while (1)
{
struct inotify_event * ie = NULL;
run_rfds = fsw->rfds;
/* evtl. sollte man hier noch timeout support einbauen, sprich
* das letzte NULL mit einer time austauschen. */
if (select (fsw->notify_d.handle.file+1, &run_rfds, NULL, NULL, NULL) == -1)
THROW (EXC (EXC_ERROR, errno, strerror (errno)));
while ((ie = scot_fs_watcher_get_event (fsw)) != NULL)
{
struct scot_fsw_info sfswi = {ie->wd, NULL, 0, 0, NULL};
struct scot_fsw_info * fswi;
struct scot_fs_watcher_event * e = NULL;
list_scot_fsw_info_node_t * w_node;
SCOT_MEM_ZERO (&e, sizeof (struct scot_fs_watcher_event));
list_scot_fsw_info_set_cmp (scot_fsw_info_cmp_watch_d);
w_node = list_scot_fsw_info_find (fsw->w_list, &sfswi);
fswi = list_scot_fsw_info_retrive (w_node);
list_scot_fsw_info_set_cmp (list_scot_fsw_info_default_cmp);
/*
* Ok, jetzt muß code folgen, der die got_events maske
* analysiert und je nachdem wie diese gesetzt ist
* ein scot event erzeugt oder nicht...soll heißen einen
* callback aufruft oder nicht.
*/
if (ie->mask == IN_MOVED_FROM)
{
struct inotify_event * _ie = scot_fs_watcher_get_event (fsw);
if (_ie != NULL &&
_ie->mask == IN_MOVED_TO &&
ie->wd == _ie->wd)
{
e = scot_fs_watcher_event_new (
e, SCOT_EVENT_FS_WATCHER_RENAME, NULL, 0,
fswi->path, _ie->name, ie->name);
if (_ie != NULL)
SCOT_MEM_FREE (_ie);
}
else
{
e = scot_fs_watcher_event_new (
e, SCOT_EVENT_FS_WATCHER_DELETE, NULL, 0,
fswi->path, ie->name, NULL);
scot_event_listener_call_cb (
(struct scot_event_listener *) fsw,
(struct scot_event *) e);
scot_event_free ((struct scot_event *) e);
e = NULL;
SCOT_MEM_FREE (ie);
ie = _ie;
}
}
if (ie != NULL && (ie->mask & (IN_MOVED_TO | IN_CREATE)) != 0)
e = scot_fs_watcher_event_new (
e, SCOT_EVENT_FS_WATCHER_CREATE, NULL, 0,
fswi->path, ie->name, NULL);
if (ie != NULL && ie->mask ==IN_DELETE)
e = scot_fs_watcher_event_new (
e, SCOT_EVENT_FS_WATCHER_DELETE, NULL, 0,
fswi->path, ie->name, NULL);
if (e != NULL)
{
/* call callback... */
scot_event_listener_call_cb (
(struct scot_event_listener *) fsw,
(struct scot_event *) e);
/* free e after callback */
scot_event_free ((struct scot_event *) e);
e = NULL;
}
if (ie != NULL)
SCOT_MEM_FREE (ie);
}
}
THREAD_ATEXIT_END (1);
}
CATCH (ee)
{
print_all_exceptions (ee);
exit (EXIT_FAILURE);
}
}