summaryrefslogtreecommitdiff
path: root/src/lib/ecore_file/ecore_file_monitor_inotify.c
blob: 988893de4f0eb3a4d907c3c8a74d88c6a6d77f39 (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

#include "ecore_file_private.h"

/*
 * TODO:
 *
 * - Listen to these events:
 *   IN_ACCESS, IN_ATTRIB, IN_CLOSE_WRITE, IN_CLOSE_NOWRITE, IN_OPEN
 * - Read all events first, then call the callbacks. This will prevent several
 *   callbacks with the typic save cycle (delete file, new file)
 * - Listen to IN_IGNORED, emitted when the watch is removed
 */

#include <sys/inotify.h>


typedef struct _Ecore_File_Monitor_Inotify Ecore_File_Monitor_Inotify;

#define ECORE_FILE_MONITOR_INOTIFY(x) ((Ecore_File_Monitor_Inotify *)(x))

struct _Ecore_File_Monitor_Inotify
{
   Ecore_File_Monitor  monitor;
   int                 wd;
};

static Ecore_Fd_Handler *_fdh = NULL;
static Ecore_File_Monitor    *_monitors = NULL;
static pid_t             _inotify_fd_pid = -1;

static Eina_Bool           _ecore_file_monitor_inotify_handler(void *data, Ecore_Fd_Handler *fdh);
static Eina_List *_ecore_file_monitor_inotify_monitor_find(int wd);
static void                _ecore_file_monitor_inotify_events(Ecore_File_Monitor *em, char *file, int mask);
static int                 _ecore_file_monitor_inotify_monitor(Ecore_File_Monitor *em, const char *path);
#if 0
static void                _ecore_file_monitor_inotify_print(char *file, int mask);
#endif

static Eina_Bool reseting;
static Eina_Hash *monitor_hash;

static void
_ecore_file_monitor_inotify_reset()
{
   Eina_Iterator *it;
   Ecore_File_Monitor *em;
   Eina_Hash *h = monitor_hash;
   monitor_hash = NULL;
   reseting = 1;
   ecore_file_monitor_backend_shutdown();
   ecore_file_monitor_backend_init();
   it = eina_hash_iterator_data_new(h);
   EINA_ITERATOR_FOREACH(it, em)
     _ecore_file_monitor_inotify_monitor(em, em->path);
   eina_iterator_free(it);
   eina_hash_free(h);
   reseting = 0;
}

int
ecore_file_monitor_backend_init(void)
{
   int fd;

   fd = inotify_init();
   if (fd < 0)
     return 0;

   eina_file_close_on_exec(fd, EINA_TRUE);

   _fdh = ecore_main_fd_handler_add(fd, ECORE_FD_READ, _ecore_file_monitor_inotify_handler,
                                    NULL, NULL, NULL);
   if (!_fdh)
     {
        close(fd);
        return 0;
     }

   if (!reseting)
     ecore_fork_reset_callback_add(_ecore_file_monitor_inotify_reset, NULL);
   _inotify_fd_pid = getpid();
   monitor_hash = eina_hash_int32_new(NULL);
   return 1;
}

int
ecore_file_monitor_backend_shutdown(void)
{
   int fd;

   while(_monitors)
        ecore_file_monitor_backend_del(_monitors);

   if (_fdh)
     {
        fd = ecore_main_fd_handler_fd_get(_fdh);
        ecore_main_fd_handler_del(_fdh);
        if (fd > -1)
          close(fd);
        _fdh = NULL;
     }
   eina_hash_free(monitor_hash);
   monitor_hash = NULL;
   _inotify_fd_pid = -1;
   if (!reseting)
     ecore_fork_reset_callback_del(_ecore_file_monitor_inotify_reset, NULL);
   return 1;
}

Ecore_File_Monitor *
ecore_file_monitor_backend_add(const char *path,
                               void (*func) (void *data, Ecore_File_Monitor *em,
                                             Ecore_File_Event event,
                                             const char *path),
                               void *data)
{
   Ecore_File_Monitor *em;
   char *path2;
   size_t len;

   if (_inotify_fd_pid == -1) return NULL;

   if (_inotify_fd_pid != getpid())
     _ecore_file_monitor_inotify_reset();

   em = (Ecore_File_Monitor *)calloc(1, sizeof(Ecore_File_Monitor_Inotify));
   if (!em) return NULL;

   em->func = func;
   em->data = data;

   len = strlen(path);
   path2 = alloca(len + 1);
   strcpy(path2, path);
   if (path2[len - 1] == '/' && strcmp(path2, "/")) path2[len - 1] = 0;
   em->path = eina_stringshare_add(path2);

   _monitors = ECORE_FILE_MONITOR(eina_inlist_append(EINA_INLIST_GET(_monitors), EINA_INLIST_GET(em)));

   if (!_ecore_file_monitor_inotify_monitor(em, em->path))
     return NULL;

   return em;
}

void
ecore_file_monitor_backend_del(Ecore_File_Monitor *em)
{
   int fd;

   if (_monitors)
     _monitors = ECORE_FILE_MONITOR(eina_inlist_remove(EINA_INLIST_GET(_monitors), EINA_INLIST_GET(em)));
   if (ECORE_FILE_MONITOR_INOTIFY(em)->wd >= 0)
     eina_hash_list_remove(monitor_hash, &ECORE_FILE_MONITOR_INOTIFY(em)->wd, em);

   fd = ecore_main_fd_handler_fd_get(_fdh);
   if (ECORE_FILE_MONITOR_INOTIFY(em)->wd >= 0)
     inotify_rm_watch(fd, ECORE_FILE_MONITOR_INOTIFY(em)->wd);
   eina_stringshare_del(em->path);
   free(em);
}

static Eina_Bool
_ecore_file_monitor_inotify_handler(void *data EINA_UNUSED, Ecore_Fd_Handler *fdh)
{
   Eina_List *l, *ll, *ll2;
   Ecore_File_Monitor *em;
   char buffer[16384];
   struct inotify_event *event;
   int i = 0, fd;
   int event_size;
   ssize_t size;

   fd = ecore_main_fd_handler_fd_get(fdh);
   if (fd < 0) return ECORE_CALLBACK_RENEW;

   size = read(fd, buffer, sizeof(buffer));
   while ((i + (int) sizeof(struct inotify_event)) <= (int) size)
     {
        event = (struct inotify_event *)&buffer[i];
        event_size = sizeof(struct inotify_event) + event->len;
        if ((event_size + i) > size) break ;
        i += event_size;

        l = _ecore_file_monitor_inotify_monitor_find(event->wd);
        EINA_LIST_FOREACH_SAFE(l, ll, ll2, em)
          _ecore_file_monitor_inotify_events(em, (event->len ? event->name : NULL), event->mask);
     }

   return ECORE_CALLBACK_RENEW;
}

static Eina_List *
_ecore_file_monitor_inotify_monitor_find(int wd)
{
   return eina_hash_find(monitor_hash, &wd);
}

static void
_ecore_file_monitor_inotify_events(Ecore_File_Monitor *em, char *file, int mask)
{
   char buf[PATH_MAX];
   int isdir;

   if ((file) && (file[0]))
     snprintf(buf, sizeof(buf), "%s/%s", em->path, file);
   else
     {
        strncpy(buf, em->path, sizeof(buf));
        buf[PATH_MAX - 1] = 0;
     }
   isdir = mask & IN_ISDIR;

#if 0
   _ecore_file_monitor_inotify_print(buf, mask);
#endif

   if (mask & IN_ATTRIB)
     {
        em->func(em->data, em, ECORE_FILE_EVENT_MODIFIED, buf);
     }
   if (mask & IN_CLOSE_WRITE)
     {
        if (!isdir)
          em->func(em->data, em, ECORE_FILE_EVENT_CLOSED, buf);
     }
   if (mask & IN_MODIFY)
     {
        if (!isdir)
          em->func(em->data, em, ECORE_FILE_EVENT_MODIFIED, buf);
     }
   if (mask & IN_MOVED_FROM)
     {
        if (isdir)
          em->func(em->data, em, ECORE_FILE_EVENT_DELETED_DIRECTORY, buf);
        else
          em->func(em->data, em, ECORE_FILE_EVENT_DELETED_FILE, buf);
     }
   if (mask & IN_MOVED_TO)
     {
        if (isdir)
          em->func(em->data, em, ECORE_FILE_EVENT_CREATED_DIRECTORY, buf);
        else
          em->func(em->data, em, ECORE_FILE_EVENT_CREATED_FILE, buf);
     }
   if (mask & IN_DELETE)
     {
        if (isdir)
          em->func(em->data, em, ECORE_FILE_EVENT_DELETED_DIRECTORY, buf);
        else
          em->func(em->data, em, ECORE_FILE_EVENT_DELETED_FILE, buf);
     }
   if (mask & IN_CREATE)
     {
        if (isdir)
          em->func(em->data, em, ECORE_FILE_EVENT_CREATED_DIRECTORY, buf);
        else
          em->func(em->data, em, ECORE_FILE_EVENT_CREATED_FILE, buf);
     }
   if (mask & IN_DELETE_SELF)
     {
        em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
     }
   if (mask & IN_MOVE_SELF)
     {
        /* We just call delete. The dir is gone... */
        em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
     }
   if (mask & IN_UNMOUNT)
     {
        /* We just call delete. The dir is gone... */
        em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
     }
   if (mask & IN_IGNORED)
     /* The watch is removed. If the file name still exists monitor the new one,
      * else delete it */
     _ecore_file_monitor_inotify_monitor(em, em->path);
}

static int
_ecore_file_monitor_inotify_monitor(Ecore_File_Monitor *em, const char *path)
{
   int mask = IN_ATTRIB | IN_CLOSE_WRITE | IN_MOVED_FROM | IN_MOVED_TO |
     IN_DELETE | IN_CREATE | IN_MODIFY | IN_DELETE_SELF | IN_MOVE_SELF |
     IN_UNMOUNT;

   ECORE_FILE_MONITOR_INOTIFY(em)->wd =
      inotify_add_watch(ecore_main_fd_handler_fd_get(_fdh), path, mask);
   if (ECORE_FILE_MONITOR_INOTIFY(em)->wd < 0)
     {
        INF("inotify_add_watch failed, %s", strerror(errno));
        ecore_file_monitor_backend_del(em);
        return 0;
     }
   eina_hash_list_append(monitor_hash, &ECORE_FILE_MONITOR_INOTIFY(em)->wd, em);
   return 1;
}

#if 0
static void
_ecore_file_monitor_inotify_print(char *file, int mask)
{
   const char *type;

   if (mask & IN_ISDIR)
     type = "dir";
   else
     type = "file";

   if (mask & IN_ACCESS)
     INF("Inotify accessed %s: %s", type, file);
   if (mask & IN_MODIFY)
     INF("Inotify modified %s: %s", type, file);
   if (mask & IN_ATTRIB)
     INF("Inotify attributes %s: %s", type, file);
   if (mask & IN_CLOSE_WRITE)
     INF("Inotify close write %s: %s", type, file);
   if (mask & IN_CLOSE_NOWRITE)
     INF("Inotify close write %s: %s", type, file);
   if (mask & IN_OPEN)
     INF("Inotify open %s: %s", type, file);
   if (mask & IN_MOVED_FROM)
     INF("Inotify moved from %s: %s", type, file);
   if (mask & IN_MOVED_TO)
     INF("Inotify moved to %s: %s", type, file);
   if (mask & IN_DELETE)
     INF("Inotify delete %s: %s", type, file);
   if (mask & IN_CREATE)
     INF("Inotify create %s: %s", type, file);
   if (mask & IN_DELETE_SELF)
     INF("Inotify delete self %s: %s", type, file);
   if (mask & IN_MOVE_SELF)
     INF("Inotify move self %s: %s", type, file);
   if (mask & IN_UNMOUNT)
     INF("Inotify unmount %s: %s", type, file);
}
#endif