11 #include "ecore_file_private.h"
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
25 #ifdef HAVE_SYS_INOTIFY
26 # include <sys/inotify.h>
28 # include <asm/unistd.h>
29 # include <linux/inotify.h>
32 #ifndef HAVE_SYS_INOTIFY
33 static inline int inotify_init(void);
34 static inline int inotify_add_watch(int fd, const char *name, __u32 mask);
35 static inline int inotify_rm_watch(int fd, __u32 wd);
39 typedef struct _Ecore_File_Monitor_Inotify Ecore_File_Monitor_Inotify;
41 #define ECORE_FILE_MONITOR_INOTIFY(x) ((Ecore_File_Monitor_Inotify *)(x))
43 struct _Ecore_File_Monitor_Inotify
45 Ecore_File_Monitor monitor;
49 static Ecore_Fd_Handler *_fdh = NULL;
50 static Ecore_File_Monitor *_monitors = NULL;
51 static pid_t _inotify_fd_pid = -1;
53 static Eina_Bool _ecore_file_monitor_inotify_handler(void *data, Ecore_Fd_Handler *fdh);
54 static Ecore_File_Monitor *_ecore_file_monitor_inotify_monitor_find(int wd);
55 static void _ecore_file_monitor_inotify_events(Ecore_File_Monitor *em, char *file, int mask);
56 static int _ecore_file_monitor_inotify_monitor(Ecore_File_Monitor *em, const char *path);
58 static void _ecore_file_monitor_inotify_print(char *file, int mask);
62 ecore_file_monitor_inotify_init(void)
70 _fdh = ecore_main_fd_handler_add(fd, ECORE_FD_READ, _ecore_file_monitor_inotify_handler,
78 _inotify_fd_pid = getpid();
83 ecore_file_monitor_inotify_shutdown(void)
88 ecore_file_monitor_inotify_del(_monitors);
92 fd = ecore_main_fd_handler_fd_get(_fdh);
93 ecore_main_fd_handler_del(_fdh);
101 ecore_file_monitor_inotify_add(const char *path,
102 void (*func) (void *data, Ecore_File_Monitor *em,
103 Ecore_File_Event event,
107 Ecore_File_Monitor *em;
110 if (_inotify_fd_pid == -1) return NULL;
112 if (_inotify_fd_pid != getpid())
114 ecore_file_monitor_inotify_shutdown();
115 ecore_file_monitor_inotify_init();
118 em = calloc(1, sizeof(Ecore_File_Monitor_Inotify));
119 if (!em) return NULL;
124 em->path = strdup(path);
125 len = strlen(em->path);
126 if (em->path[len - 1] == '/' && strcmp(em->path, "/"))
127 em->path[len - 1] = 0;
129 _monitors = ECORE_FILE_MONITOR(eina_inlist_append(EINA_INLIST_GET(_monitors), EINA_INLIST_GET(em)));
131 if (ecore_file_exists(em->path))
133 if (!_ecore_file_monitor_inotify_monitor(em, em->path))
138 ecore_file_monitor_inotify_del(em);
146 ecore_file_monitor_inotify_del(Ecore_File_Monitor *em)
151 _monitors = ECORE_FILE_MONITOR(eina_inlist_remove(EINA_INLIST_GET(_monitors), EINA_INLIST_GET(em)));
153 fd = ecore_main_fd_handler_fd_get(_fdh);
154 if (ECORE_FILE_MONITOR_INOTIFY(em)->wd)
155 inotify_rm_watch(fd, ECORE_FILE_MONITOR_INOTIFY(em)->wd);
161 _ecore_file_monitor_inotify_handler(void *data __UNUSED__, Ecore_Fd_Handler *fdh)
163 Ecore_File_Monitor *em;
165 struct inotify_event *event;
170 size = read(ecore_main_fd_handler_fd_get(fdh), buffer, sizeof(buffer));
173 event = (struct inotify_event *)&buffer[i];
174 event_size = sizeof(struct inotify_event) + event->len;
177 em = _ecore_file_monitor_inotify_monitor_find(event->wd);
180 _ecore_file_monitor_inotify_events(em, (event->len ? event->name : NULL), event->mask);
183 return ECORE_CALLBACK_RENEW;
186 static Ecore_File_Monitor *
187 _ecore_file_monitor_inotify_monitor_find(int wd)
189 Ecore_File_Monitor *l;
191 EINA_INLIST_FOREACH(_monitors, l)
193 if (ECORE_FILE_MONITOR_INOTIFY(l)->wd == wd)
200 _ecore_file_monitor_inotify_events(Ecore_File_Monitor *em, char *file, int mask)
205 if ((file) && (file[0]))
206 snprintf(buf, sizeof(buf), "%s/%s", em->path, file);
208 strcpy(buf, em->path);
209 isdir = mask & IN_ISDIR;
212 _ecore_file_monitor_inotify_print(buf, mask);
215 if (mask & IN_ATTRIB)
217 em->func(em->data, em, ECORE_FILE_EVENT_MODIFIED, buf);
219 if (mask & IN_CLOSE_WRITE)
222 em->func(em->data, em, ECORE_FILE_EVENT_CLOSED, buf);
224 if (mask & IN_MODIFY)
227 em->func(em->data, em, ECORE_FILE_EVENT_MODIFIED, buf);
229 if (mask & IN_MOVED_FROM)
232 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_DIRECTORY, buf);
234 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_FILE, buf);
236 if (mask & IN_MOVED_TO)
239 em->func(em->data, em, ECORE_FILE_EVENT_CREATED_DIRECTORY, buf);
241 em->func(em->data, em, ECORE_FILE_EVENT_CREATED_FILE, buf);
243 if (mask & IN_DELETE)
246 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_DIRECTORY, buf);
248 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_FILE, buf);
250 if (mask & IN_CREATE)
253 em->func(em->data, em, ECORE_FILE_EVENT_CREATED_DIRECTORY, buf);
255 em->func(em->data, em, ECORE_FILE_EVENT_CREATED_FILE, buf);
257 if (mask & IN_DELETE_SELF)
259 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
261 if (mask & IN_MOVE_SELF)
263 /* We just call delete. The dir is gone... */
264 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
266 if (mask & IN_UNMOUNT)
268 /* We just call delete. The dir is gone... */
269 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
271 if (mask & IN_IGNORED)
273 /* The watch is removed. If the file name still exists monitor the new one,
275 if (ecore_file_exists(em->path))
277 if (_ecore_file_monitor_inotify_monitor(em, em->path))
278 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
281 em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
286 _ecore_file_monitor_inotify_monitor(Ecore_File_Monitor *em, const char *path)
300 ECORE_FILE_MONITOR_INOTIFY(em)->wd =
301 inotify_add_watch(ecore_main_fd_handler_fd_get(_fdh), path, mask);
302 if (ECORE_FILE_MONITOR_INOTIFY(em)->wd < 0)
304 INF("inotify_add_watch failed, file was deleted");
305 ecore_file_monitor_inotify_del(em);
311 #ifndef HAVE_SYS_INOTIFY
315 return syscall(__NR_inotify_init);
319 inotify_add_watch(int fd, const char *name, __u32 mask)
321 return syscall(__NR_inotify_add_watch, fd, name, mask);
325 inotify_rm_watch(int fd, __u32 wd)
327 return syscall(__NR_inotify_rm_watch, fd, wd);
333 _ecore_file_monitor_inotify_print(char *file, int mask)
342 if (mask & IN_ACCESS)
343 INF("Inotify accessed %s: %s", type, file);
344 if (mask & IN_MODIFY)
345 INF("Inotify modified %s: %s", type, file);
346 if (mask & IN_ATTRIB)
347 INF("Inotify attributes %s: %s", type, file);
348 if (mask & IN_CLOSE_WRITE)
349 INF("Inotify close write %s: %s", type, file);
350 if (mask & IN_CLOSE_NOWRITE)
351 INF("Inotify close write %s: %s", type, file);
353 INF("Inotify open %s: %s", type, file);
354 if (mask & IN_MOVED_FROM)
355 INF("Inotify moved from %s: %s", type, file);
356 if (mask & IN_MOVED_TO)
357 INF("Inotify moved to %s: %s", type, file);
358 if (mask & IN_DELETE)
359 INF("Inotify delete %s: %s", type, file);
360 if (mask & IN_CREATE)
361 INF("Inotify create %s: %s", type, file);
362 if (mask & IN_DELETE_SELF)
363 INF("Inotify delete self %s: %s", type, file);
364 if (mask & IN_MOVE_SELF)
365 INF("Inotify move self %s: %s", type, file);
366 if (mask & IN_UNMOUNT)
367 INF("Inotify unmount %s: %s", type, file);
370 #endif /* HAVE_INOTIFY */