summaryrefslogtreecommitdiff
path: root/legacy/ecore/src/lib/ecore_file/ecore_file_monitor_poll.c
blob: 6573539dc1b29a0e04e2fbe70bc2a723d838ae15 (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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <string.h>

#include "ecore_file_private.h"

#ifdef HAVE_POLL

/*
 * TODO:
 * - Implement recursive as an option!
 * - Keep whole path or just name of file? (Memory or CPU...)
 * - Remove requests without files?
 * - Change poll time
 */

typedef struct _Ecore_File_Monitor_Poll Ecore_File_Monitor_Poll;
typedef struct _Ecore_File              Ecore_File;

#define ECORE_FILE_MONITOR_POLL(x) ((Ecore_File_Monitor_Poll *)(x))

struct _Ecore_File_Monitor_Poll
{
   Ecore_File_Monitor  monitor;
   int                 mtime;
   unsigned char       deleted;
};

struct _Ecore_File
{
   Ecore_List2  __list_data;
   char          *name;
   int            mtime;
   unsigned char  is_dir;
};

#define ECORE_FILE_INTERVAL_MIN  1.0
#define ECORE_FILE_INTERVAL_STEP 0.5
#define ECORE_FILE_INTERVAL_MAX  5.0

static double         _interval = ECORE_FILE_INTERVAL_MIN;
static Ecore_Timer   *_timer = NULL;
static Ecore_List2 *_monitors = NULL;
static int          _lock = 0;

static int         _ecore_file_monitor_poll_handler(void *data);
static void        _ecore_file_monitor_poll_check(Ecore_File_Monitor *em);
static int         _ecore_file_monitor_poll_checking(Ecore_File_Monitor *em, char *name);

int
ecore_file_monitor_poll_init(void)
{
   return 1;
}

int
ecore_file_monitor_poll_shutdown(void)
{
   Ecore_List2 *l;

   for (l = _monitors; l;)
     {
	Ecore_File_Monitor *em;

	em = ECORE_FILE_MONITOR(l);
	l = l->next;
	ecore_file_monitor_poll_del(em);
     }

   if (_timer)
     {
       	ecore_timer_del(_timer);
	_timer = NULL;
     }
   return 1;
}

Ecore_File_Monitor *
ecore_file_monitor_poll_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;
   int len;

   if (!path) return NULL;
   if (!func) return NULL;

   em = calloc(1, sizeof(Ecore_File_Monitor_Poll));
   if (!em) return NULL;
   
   if (!_timer)
     _timer = ecore_timer_add(_interval, _ecore_file_monitor_poll_handler, NULL);
   else
     ecore_timer_interval_set(_timer, ECORE_FILE_INTERVAL_MIN);

   em->path = strdup(path);
   len = strlen(em->path);
   if (em->path[len - 1] == '/' && strcmp(em->path, "/"))
     em->path[len - 1] = 0;

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

   ECORE_FILE_MONITOR_POLL(em)->mtime = ecore_file_mod_time(em->path);
   if (ecore_file_exists(em->path))
     {
	if (ecore_file_is_dir(em->path))
	  {
	     /* Check for subdirs */
	     Ecore_List *files;
	     char *file;

	     files = ecore_file_ls(em->path);
	     if (files)
	       {
		  while ((file = ecore_list_next(files)))
		    {
		       Ecore_File *f;
		       char buf[PATH_MAX];

		       f = calloc(1, sizeof(Ecore_File));
		       if (!f)
			 continue;

		       snprintf(buf, sizeof(buf), "%s/%s", em->path, file);
		       f->name = strdup(file);
		       f->mtime = ecore_file_mod_time(buf);
		       f->is_dir = ecore_file_is_dir(buf);
		       em->files = _ecore_list2_append(em->files, f);
		    }
		  ecore_list_destroy(files);
	       }
	  }
     }
   else
     {
	ecore_file_monitor_poll_del(em);
	return NULL;
     }

   _monitors = _ecore_list2_append(_monitors, em);

   return em;
}

void
ecore_file_monitor_poll_del(Ecore_File_Monitor *em)
{
   Ecore_List2 *l;

   if (_lock)
     {
	ECORE_FILE_MONITOR_POLL(em)->deleted = 1;
	return;
     }

   /* Remove files */
   /*It's possible there weren't any files to monitor, so check if the list is init*/
   if (em->files)
     {
	for (l = em->files; l;)
	  {
	     Ecore_File *file;
	     
	     file = (Ecore_File *)l;
	     l = l->next;
	     free(file->name);
	     free(file);
	  }
     }
   
   _monitors = _ecore_list2_remove(_monitors, em);
   
   free(em->path);
   free(em);
   
   if (_timer)
     {
	if (!_monitors)
	  {
	     ecore_timer_del(_timer);
	     _timer = NULL;
	  }
	else
	  ecore_timer_interval_set(_timer, ECORE_FILE_INTERVAL_MIN);
     }
}

static int
_ecore_file_monitor_poll_handler(void *data __UNUSED__)
{
   Ecore_List2 *l;

   _interval += ECORE_FILE_INTERVAL_STEP;

   _lock = 1;
   for (l = _monitors; l; l = l->next)
     {
	Ecore_File_Monitor *em;

	em = ECORE_FILE_MONITOR(l);
	_ecore_file_monitor_poll_check(em);
     }
   _lock = 0;

   if (_interval > ECORE_FILE_INTERVAL_MAX)
     _interval = ECORE_FILE_INTERVAL_MAX;
   ecore_timer_interval_set(_timer, _interval);

   for (l = _monitors; l;)
     {
	Ecore_File_Monitor *em;

	em = ECORE_FILE_MONITOR(l);
	l = l->next;
	if (ECORE_FILE_MONITOR_POLL(em)->deleted)
	  ecore_file_monitor_del(em);
     }
   return 1;
}

static void
_ecore_file_monitor_poll_check(Ecore_File_Monitor *em)
{
   int mtime;
   int is_dir;

   mtime = ecore_file_mod_time(em->path);
   is_dir = ecore_file_is_dir(em->path);
   if (mtime < ECORE_FILE_MONITOR_POLL(em)->mtime)
     {
	Ecore_List2 *l;
	Ecore_File_Event event;
	
	/* Notify all files deleted */
	for (l = em->files; l;)
	  {
	     Ecore_File *f;
	     char buf[PATH_MAX];
	     
	     f = (Ecore_File *)l;
	     l = l->next;

	     snprintf(buf, sizeof(buf), "%s/%s", em->path, f->name);
	     if (f->is_dir)
	       event = ECORE_FILE_EVENT_DELETED_DIRECTORY;
	     else
	       event = ECORE_FILE_EVENT_DELETED_FILE;
	     em->func(em->data, em, event, buf);
	     free(f->name);
	     free(f);
	  }
	em->files = NULL;
	em->func(em->data, em, ECORE_FILE_EVENT_DELETED_SELF, em->path);
	_interval = ECORE_FILE_INTERVAL_MIN;
     }
   else
     {
	Ecore_List2 *l;

	/* Check for changed files */
	for (l = em->files; l;)
	  {
	     Ecore_File *f;
	     char buf[PATH_MAX];
	     int mtime;
	     Ecore_File_Event event;

	     f = (Ecore_File *)l;
	     l = l->next;

	     snprintf(buf, sizeof(buf), "%s/%s", em->path, f->name);
	     mtime = ecore_file_mod_time(buf);
	     if (mtime < f->mtime)
	       {
		  if (f->is_dir)
		    event = ECORE_FILE_EVENT_DELETED_DIRECTORY;
		  else
		    event = ECORE_FILE_EVENT_DELETED_FILE;

		  em->func(em->data, em, event, buf);
		  em->files = _ecore_list2_remove(em->files, f);
		  free(f->name);
		  free(f);
		  _interval = ECORE_FILE_INTERVAL_MIN;
	       }
	     else if ((mtime > f->mtime) && !(f->is_dir))
	       {
		  em->func(em->data, em, ECORE_FILE_EVENT_MODIFIED, buf);
		  _interval = ECORE_FILE_INTERVAL_MIN;
		  f->mtime = mtime;
	       }
	     else
	       f->mtime = mtime;
	  }

	/* Check for new files */
	if (ECORE_FILE_MONITOR_POLL(em)->mtime < mtime)
	  {
	     Ecore_List *files;
	     char *file;

	     /* Files have been added or removed */
	     files = ecore_file_ls(em->path);
	     if (files)
	       { 
		  /* Are we a directory? We should check first, rather than rely on null here*/
		  while ((file = ecore_list_next(files)))
		    {
		       Ecore_File *f;
		       char buf[PATH_MAX];
		       Ecore_File_Event event;
		       
		       if (_ecore_file_monitor_poll_checking(em, file))
			 continue;
		       
		       snprintf(buf, sizeof(buf), "%s/%s", em->path, file);
		       f = calloc(1, sizeof(Ecore_File));
		       if (!f)
			 continue;
		       
		       f->name = strdup(file);
		       f->mtime = ecore_file_mod_time(buf);
		       f->is_dir = ecore_file_is_dir(buf);
		       if (f->is_dir)
			 event = ECORE_FILE_EVENT_CREATED_DIRECTORY;
		       else
			 event = ECORE_FILE_EVENT_CREATED_FILE;
		       em->func(em->data, em, event, buf);
		       em->files = _ecore_list2_append(em->files, f);
		    }
		  ecore_list_destroy(files);
	       }
	     
	     if (!ecore_file_is_dir(em->path))
	       em->func(em->data, em, ECORE_FILE_EVENT_MODIFIED, em->path);
	     _interval = ECORE_FILE_INTERVAL_MIN;
	  }
     }
   ECORE_FILE_MONITOR_POLL(em)->mtime = mtime;
}

static int
_ecore_file_monitor_poll_checking(Ecore_File_Monitor *em, char *name)
{
   Ecore_List2 *l;

   for (l = em->files; l; l = l->next)
     {
	Ecore_File *f;

	f = (Ecore_File *)l;
	if (!strcmp(f->name, name))
	  return 1;
     }
   return 0;
}
#endif