summaryrefslogtreecommitdiff
path: root/src/lib/ecore_file/ecore_file_monitor_inotify.c
diff options
context:
space:
mode:
authorVincent Torri <vincent.torri@gmail.com>2012-12-02 22:35:45 +0000
committerVincent Torri <vincent.torri@gmail.com>2012-12-02 22:35:45 +0000
commit7d6010b12c47a20e492da808e3192c3f87dab619 (patch)
tree26c6fd189e046a76560c0bc740b85f4d767ae399 /src/lib/ecore_file/ecore_file_monitor_inotify.c
parent53fc441d5475155965d92da89502fe4634a561b2 (diff)
merge: add escape ecore, fix several bugs
SVN revision: 79995
Diffstat (limited to 'src/lib/ecore_file/ecore_file_monitor_inotify.c')
-rw-r--r--src/lib/ecore_file/ecore_file_monitor_inotify.c331
1 files changed, 331 insertions, 0 deletions
diff --git a/src/lib/ecore_file/ecore_file_monitor_inotify.c b/src/lib/ecore_file/ecore_file_monitor_inotify.c
new file mode 100644
index 0000000000..1b682fc4a0
--- /dev/null
+++ b/src/lib/ecore_file/ecore_file_monitor_inotify.c
@@ -0,0 +1,331 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <stdlib.h>
6#include <stdio.h>
7#include <string.h>
8#include <sys/types.h>
9#include <unistd.h>
10
11#include "ecore_file_private.h"
12
13/*
14 * TODO:
15 *
16 * - Listen to these events:
17 * IN_ACCESS, IN_ATTRIB, IN_CLOSE_WRITE, IN_CLOSE_NOWRITE, IN_OPEN
18 * - Read all events first, then call the callbacks. This will prevent several
19 * callbacks with the typic save cycle (delete file, new file)
20 * - Listen to IN_IGNORED, emitted when the watch is removed
21 */
22
23#ifdef HAVE_INOTIFY
24
25#include <sys/inotify.h>
26
27
28typedef struct _Ecore_File_Monitor_Inotify Ecore_File_Monitor_Inotify;
29
30#define ECORE_FILE_MONITOR_INOTIFY(x) ((Ecore_File_Monitor_Inotify *)(x))
31
32struct _Ecore_File_Monitor_Inotify
33{
34 Ecore_File_Monitor monitor;
35 int wd;
36};
37
38static Ecore_Fd_Handler *_fdh = NULL;
39static Ecore_File_Monitor *_monitors = NULL;
40static pid_t _inotify_fd_pid = -1;
41
42static Eina_Bool _ecore_file_monitor_inotify_handler(void *data, Ecore_Fd_Handler *fdh);
43static Ecore_File_Monitor *_ecore_file_monitor_inotify_monitor_find(int wd);
44static void _ecore_file_monitor_inotify_events(Ecore_File_Monitor *em, char *file, int mask);
45static int _ecore_file_monitor_inotify_monitor(Ecore_File_Monitor *em, const char *path);
46#if 0
47static void _ecore_file_monitor_inotify_print(char *file, int mask);
48#endif
49
50int
51ecore_file_monitor_inotify_init(void)
52{
53 int fd;
54
55 fd = inotify_init();
56 if (fd < 0)
57 return 0;
58
59 _fdh = ecore_main_fd_handler_add(fd, ECORE_FD_READ, _ecore_file_monitor_inotify_handler,
60 NULL, NULL, NULL);
61 if (!_fdh)
62 {
63 close(fd);
64 return 0;
65 }
66
67 _inotify_fd_pid = getpid();
68 return 1;
69}
70
71int
72ecore_file_monitor_inotify_shutdown(void)
73{
74 int fd;
75
76 while(_monitors)
77 ecore_file_monitor_inotify_del(_monitors);
78
79 if (_fdh)
80 {
81 fd = ecore_main_fd_handler_fd_get(_fdh);
82 ecore_main_fd_handler_del(_fdh);
83 close(fd);
84 }
85 _inotify_fd_pid = -1;
86 return 1;
87}
88
89Ecore_File_Monitor *
90ecore_file_monitor_inotify_add(const char *path,
91 void (*func) (void *data, Ecore_File_Monitor *em,
92 Ecore_File_Event event,
93 const char *path),
94 void *data)
95{
96 Ecore_File_Monitor *em;
97 int len;
98
99 if (_inotify_fd_pid == -1) return NULL;
100
101 if (_inotify_fd_pid != getpid())
102 {
103 ecore_file_monitor_inotify_shutdown();
104 ecore_file_monitor_inotify_init();
105 }
106
107 em = calloc(1, sizeof(Ecore_File_Monitor_Inotify));
108 if (!em) return NULL;
109
110 em->func = func;
111 em->data = data;
112
113 em->path = strdup(path);
114 len = strlen(em->path);
115 if (em->path[len - 1] == '/' && strcmp(em->path, "/"))
116 em->path[len - 1] = 0;
117
118 _monitors = ECORE_FILE_MONITOR(eina_inlist_append(EINA_INLIST_GET(_monitors), EINA_INLIST_GET(em)));
119
120 if (!_ecore_file_monitor_inotify_monitor(em, em->path))
121 return NULL;
122
123 return em;
124}
125
126void
127ecore_file_monitor_inotify_del(Ecore_File_Monitor *em)
128{
129 int fd;
130
131 if (_monitors)
132 _monitors = ECORE_FILE_MONITOR(eina_inlist_remove(EINA_INLIST_GET(_monitors), EINA_INLIST_GET(em)));
133
134 fd = ecore_main_fd_handler_fd_get(_fdh);
135 if (ECORE_FILE_MONITOR_INOTIFY(em)->wd)
136 inotify_rm_watch(fd, ECORE_FILE_MONITOR_INOTIFY(em)->wd);
137 free(em->path);
138 free(em);
139}
140
141static Eina_Bool
142_ecore_file_monitor_inotify_handler(void *data EINA_UNUSED, Ecore_Fd_Handler *fdh)
143{
144 Ecore_File_Monitor *em;
145 char buffer[16384];
146 struct inotify_event *event;
147 int i = 0;
148 int event_size;
149 ssize_t size;
150
151 size = read(ecore_main_fd_handler_fd_get(fdh), buffer, sizeof(buffer));
152 while (i < size)
153 {
154 event = (struct inotify_event *)&buffer[i];
155 event_size = sizeof(struct inotify_event) + event->len;
156 i += event_size;
157
158 em = _ecore_file_monitor_inotify_monitor_find(event->wd);
159 if (!em) continue;
160
161 _ecore_file_monitor_inotify_events(em, (event->len ? event->name : NULL), event->mask);
162 }
163
164 return ECORE_CALLBACK_RENEW;
165}
166
167static Ecore_File_Monitor *
168_ecore_file_monitor_inotify_monitor_find(int wd)
169{
170 Ecore_File_Monitor *l;
171
172 EINA_INLIST_FOREACH(_monitors, l)
173 {
174 if (ECORE_FILE_MONITOR_INOTIFY(l)->wd == wd)
175 return l;
176 }
177 return NULL;
178}
179
180static void
181_ecore_file_monitor_inotify_events(Ecore_File_Monitor *em, char *file, int mask)
182{
183 char buf[PATH_MAX];
184 int isdir;
185
186 if ((file) && (file[0]))
187 snprintf(buf, sizeof(buf), "%s/%s", em->path, file);
188 else
189 strcpy(buf, em->path);
190 isdir = mask & IN_ISDIR;
191
192#if 0
193 _ecore_file_monitor_inotify_print(buf, mask);
194#endif
195
196 if (mask & IN_ATTRIB)
197 {
198 em->func(em->data, em, ECORE_FILE_EVENT_MODIFIED, buf);
199 }
200 if (mask & IN_CLOSE_WRITE)
201 {
202 if (!isdir)
203 em->func(em->data, em, ECORE_FILE_EVENT_CLOSED, buf);
204 }
205 if (mask & IN_MODIFY)
206 {
207 if (!isdir)
208 em->func(em->data, em, ECORE_FILE_EVENT_MODIFIED, buf);
209 }
210 if (mask & IN_MOVED_FROM)
211 {
212 if (isdir)
213 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_DIRECTORY, buf);
214 else
215 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_FILE, buf);
216 }
217 if (mask & IN_MOVED_TO)
218 {
219 if (isdir)
220 em->func(em->data, em, ECORE_FILE_EVENT_CREATED_DIRECTORY, buf);
221 else
222 em->func(em->data, em, ECORE_FILE_EVENT_CREATED_FILE, buf);
223 }
224 if (mask & IN_DELETE)
225 {
226 if (isdir)
227 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_DIRECTORY, buf);
228 else
229 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_FILE, buf);
230 }
231 if (mask & IN_CREATE)
232 {
233 if (isdir)
234 em->func(em->data, em, ECORE_FILE_EVENT_CREATED_DIRECTORY, buf);
235 else
236 em->func(em->data, em, ECORE_FILE_EVENT_CREATED_FILE, buf);
237 }
238 if (mask & IN_DELETE_SELF)
239 {
240 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
241 }
242 if (mask & IN_MOVE_SELF)
243 {
244 /* We just call delete. The dir is gone... */
245 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
246 }
247 if (mask & IN_UNMOUNT)
248 {
249 /* We just call delete. The dir is gone... */
250 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
251 }
252 if (mask & IN_IGNORED)
253 {
254 /* The watch is removed. If the file name still exists monitor the new one,
255 * else delete it */
256 if (ecore_file_exists(em->path))
257 {
258 if (_ecore_file_monitor_inotify_monitor(em, em->path))
259 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
260 }
261 else
262 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
263 }
264}
265
266static int
267_ecore_file_monitor_inotify_monitor(Ecore_File_Monitor *em, const char *path)
268{
269 int mask =
270 IN_ATTRIB |
271 IN_CLOSE_WRITE |
272 IN_MOVED_FROM |
273 IN_MOVED_TO |
274 IN_DELETE |
275 IN_CREATE |
276 IN_MODIFY |
277 IN_DELETE_SELF |
278 IN_MOVE_SELF |
279 IN_UNMOUNT;
280
281 ECORE_FILE_MONITOR_INOTIFY(em)->wd =
282 inotify_add_watch(ecore_main_fd_handler_fd_get(_fdh), path, mask);
283 if (ECORE_FILE_MONITOR_INOTIFY(em)->wd < 0)
284 {
285 INF("inotify_add_watch failed, file was deleted");
286 ecore_file_monitor_inotify_del(em);
287 return 0;
288 }
289 return 1;
290}
291
292#if 0
293static void
294_ecore_file_monitor_inotify_print(char *file, int mask)
295{
296 const char *type;
297
298 if (mask & IN_ISDIR)
299 type = "dir";
300 else
301 type = "file";
302
303 if (mask & IN_ACCESS)
304 INF("Inotify accessed %s: %s", type, file);
305 if (mask & IN_MODIFY)
306 INF("Inotify modified %s: %s", type, file);
307 if (mask & IN_ATTRIB)
308 INF("Inotify attributes %s: %s", type, file);
309 if (mask & IN_CLOSE_WRITE)
310 INF("Inotify close write %s: %s", type, file);
311 if (mask & IN_CLOSE_NOWRITE)
312 INF("Inotify close write %s: %s", type, file);
313 if (mask & IN_OPEN)
314 INF("Inotify open %s: %s", type, file);
315 if (mask & IN_MOVED_FROM)
316 INF("Inotify moved from %s: %s", type, file);
317 if (mask & IN_MOVED_TO)
318 INF("Inotify moved to %s: %s", type, file);
319 if (mask & IN_DELETE)
320 INF("Inotify delete %s: %s", type, file);
321 if (mask & IN_CREATE)
322 INF("Inotify create %s: %s", type, file);
323 if (mask & IN_DELETE_SELF)
324 INF("Inotify delete self %s: %s", type, file);
325 if (mask & IN_MOVE_SELF)
326 INF("Inotify move self %s: %s", type, file);
327 if (mask & IN_UNMOUNT)
328 INF("Inotify unmount %s: %s", type, file);
329}
330#endif
331#endif /* HAVE_INOTIFY */