summaryrefslogtreecommitdiff
path: root/src/lib/eio/efl_io_manager.c
blob: 0b7fb3def9502becf49d828fe97cdcfed28537e7 (plain) (blame)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/* EIO - EFL data type library
 * Copyright (C) 2016 Enlightenment Developers:
 *           Felipe Magno de Almeida <felipe@expertisesolutions.com.br>
 *           Lauro Moura <lauromoura@expertisesolutions.com.br>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

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

#include <Eo.h>
#include <Ecore.h>
#include "Eio.h"

#include "eio_private.h"

typedef struct _Efl_Io_Manager_Data Efl_Io_Manager_Data;

struct _Efl_Io_Manager_Data
{
   Eo *object;
   Eina_List *operations;
};

typedef Eio_File* (*Efl_Io_Manager_Direct_Ls_Func)(const char *path, Eio_Filter_Direct_Cb, Eio_Main_Direct_Cb, Eio_Done_Cb, Eio_Error_Cb, const void *data);

typedef struct _Job_Closure Job_Closure;
struct _Job_Closure
{
   Eo *object;
   Efl_Io_Manager_Data *pdata;
   Efl_Promise *promise;
   Eio_File *file;
   Eina_Bool delete_me;
   void *delayed_arg;
   Efl_Io_Manager_Direct_Ls_Func direct_func;  // Used when dispatching direct ls funcs.
};

/* Helper functions */

static void
_no_future(void *data, const Efl_Event *ev EINA_UNUSED)
{
   Eio_File *h = data;

   eio_file_cancel(h);
}

static void
_forced_shutdown(void *data, const Efl_Event *ev EINA_UNUSED)
{
   Eio_File *h = data;

   eio_file_cancel(h);
   // FIXME: handle memory lock here !
   // Acceptable strategy will be to unlock all thread as
   // if we were freeing some memory
   // FIXME: Handle long to finish thread
   ecore_thread_wait(h->thread, 1.0);
}

static void
_progress(void *data EINA_UNUSED, const Efl_Event *ev)
{
   efl_key_data_set(ev->object, "_eio.progress", (void*) EINA_TRUE);
}

EFL_CALLBACKS_ARRAY_DEFINE(promise_progress_handling,
                           { EFL_PROMISE_EVENT_FUTURE_PROGRESS_SET, _progress },
                           { EFL_PROMISE_EVENT_FUTURE_NONE, _no_future },
                           { EFL_EVENT_DEL, _forced_shutdown });

EFL_CALLBACKS_ARRAY_DEFINE(promise_handling,
                           { EFL_PROMISE_EVENT_FUTURE_NONE, _no_future },
                           { EFL_EVENT_DEL, _forced_shutdown });

static void
_file_done_data_cb(void *data, Eio_File *handler, const char *attr_data, unsigned int size)
{
   Efl_Promise *p = data;
   Eina_Binbuf *buf;

   efl_event_callback_array_del(p, promise_handling(), handler);

   buf = eina_binbuf_new();
   eina_binbuf_append_length(buf, (const unsigned char*) attr_data, size);

   efl_promise_value_set(p, buf, EINA_FREE_CB(eina_binbuf_free));

   efl_del(p);
}

static void
_file_error_cb(void *data, Eio_File *handler, int error)
{
   Efl_Promise *p = data;

   efl_event_callback_array_del(p, promise_handling(), handler);

   efl_promise_failed_set(p, error);

   efl_del(p);
}

static void
_file_done_cb(void *data, Eio_File *handler)
{
   Efl_Promise *p = data;
   uint64_t *v = calloc(1, sizeof (uint64_t));

   efl_event_callback_array_del(p, promise_handling(), handler);

   if (!v)
     {
        efl_promise_failed_set(p, ENOMEM);
        goto end;
     }

   *v = handler->length;
   efl_promise_value_set(p, v, free);

 end:
   efl_del(p);
}

/* Basic listing callbacks */
static void
_cleanup_string_progress(void *data)
{
   Eina_Array *existing = data;
   Eina_Stringshare *s;
   Eina_Array_Iterator it;
   unsigned int i;

   EINA_ARRAY_ITER_NEXT(existing, i, s, it)
     eina_stringshare_del(s);
   eina_array_free(existing);
}

static void
_cleanup_info_progress(void *data)
{
   Eina_Array *existing = data;
   Eio_File_Direct_Info *d; // This is a trick because we use the head of the structure
   Eina_Array_Iterator it;
   unsigned int i;

   EINA_ARRAY_ITER_NEXT(existing, i, d, it)
     eio_direct_info_free(d);
   eina_array_free(existing);
}

static void
_file_string_cb(void *data, Eio_File *handler, Eina_Array *gather)
{
   Efl_Promise *p = data;
   Eina_Array *existing = efl_key_data_get(p, "_eio.stored");
   void **tmp;

   // If a future is set, but without progress, we should assume
   // that we should discard all future progress. [[FIXME]]
   if (existing)
     {
        tmp = realloc(existing->data, sizeof (void*) * (existing->count + gather->count));
        if (!tmp)
          {
             eina_array_free(gather);
             eina_array_free(existing);
             efl_key_data_set(p, "_eio.stored", NULL);
             handler->error = ENOMEM;
             eio_file_cancel(handler);
             return ;
          }

        existing->data = tmp;
        memcpy(existing->data + existing->count, gather->data, gather->count * sizeof (void*));
        existing->count += gather->count;
        existing->total = existing->count;
        eina_array_free(gather);
     }
   else
     {
        existing = gather;
     }

   if (!efl_key_data_get(p, "_eio.progress"))
     {
        efl_key_data_set(p, "_eio.stored", existing);
        return ;
     }

   efl_promise_progress_set(p, existing);
   efl_key_data_set(p, "_eio.stored", NULL);
   _cleanup_string_progress(existing);
}

/* Direct listing callbacks */
static void
_file_info_cb(void *data, Eio_File *handler, Eina_Array *gather)
{
   Efl_Promise *p = data;
   Eina_Array *existing = efl_key_data_get(p, "_eio.stored");
   void **tmp;

   // If a future is set, but without progress, we should assume
   // that we should discard all future progress. [[FIXME]]
   if (existing)
     {
        tmp = realloc(existing->data, sizeof (void*) * (existing->count + gather->count));
        if (!tmp)
          {
             eina_array_free(gather);
             eina_array_free(existing);
             efl_key_data_set(p, "_eio.stored", NULL);
             handler->error = ENOMEM;
             eio_file_cancel(handler);
             return ;
          }
        existing->data = tmp;
        memcpy(existing->data + existing->count, gather->data, gather->count * sizeof (void*));
        existing->count += gather->count;
        existing->total = existing->count;
        eina_array_free(gather);
     }
   else
     {
        existing = gather;
     }
   if (!efl_key_data_get(p, "_eio.progress"))
     {
        efl_key_data_set(p, "_eio.stored", existing);
        return ;
     }
   efl_promise_progress_set(p, existing);
   efl_key_data_set(p, "_eio.stored", NULL);
   _cleanup_info_progress(existing);
}

/* Method implementations */
static Efl_Future *
_efl_io_manager_direct_ls(Eo *obj,
                          Efl_Io_Manager_Data *pd EINA_UNUSED,
                          const char *path,
                          Eina_Bool recursive)
{
   Efl_Promise *p;
   Eio_File *h;

   Eo *loop = efl_loop_get(obj);
   p = efl_add(EFL_PROMISE_CLASS, loop);
   if (!p) return NULL;

   if (!recursive)
     {
        h = _eio_file_direct_ls(path,
                                _file_info_cb,
                                _file_done_cb,
                                _file_error_cb,
                                p);
     }
   else
     {
        h = _eio_dir_direct_ls(path,
                               _file_info_cb,
                               _file_done_cb,
                               _file_error_cb,
                               p);
     }

   if (!h) goto end;

   efl_event_callback_array_add(p, promise_progress_handling(), h);
   return efl_promise_future_get(p);

 end:
   efl_del(p);
   return NULL;
}

static Efl_Future *
_efl_io_manager_stat_ls(Eo *obj,
                        Efl_Io_Manager_Data *pd EINA_UNUSED,
                        const char *path,
                        Eina_Bool recursive)
{
   Efl_Promise *p;
   Eio_File *h;

   Eo *loop = efl_loop_get(obj);
   p = efl_add(EFL_PROMISE_CLASS, loop);
   if (!p) return NULL;

   if (!recursive)
     {
        h = _eio_file_stat_ls(path,
                              _file_info_cb,
                              _file_done_cb,
                              _file_error_cb,
                              p);
     }
   else
     {
        h = _eio_dir_stat_ls(path,
                             _file_info_cb,
                             _file_done_cb,
                             _file_error_cb,
                             p);
     }

   if (!h) goto end;

   efl_event_callback_array_add(p, promise_progress_handling(), h);
   return efl_promise_future_get(p);

 end:
   efl_del(p);
   return NULL;
}

static Efl_Future *
_efl_io_manager_ls(Eo *obj,
                   Efl_Io_Manager_Data *pd EINA_UNUSED,
                   const char *path)
{
   Efl_Promise *p;
   Eio_File *h;

   Eo *loop = efl_loop_get(obj);
   p = efl_add(EFL_PROMISE_CLASS, loop);
   if (!p) return NULL;

   h = _eio_file_ls(path,
                    _file_string_cb,
                    _file_done_cb,
                    _file_error_cb,
                    p);
   if (!h) goto end;

   efl_event_callback_array_add(p, promise_progress_handling(), h);
   return efl_promise_future_get(p);

 end:
   efl_del(p);
   return NULL;
}

/* Stat function */
static void
_file_stat_done_cb(void *data, Eio_File *handle EINA_UNUSED, const Eina_Stat *stat)
{
   Efl_Promise *p = data;
   Eina_Stat *c;

   c = calloc(1, sizeof (Eina_Stat));
   if (!c)
     {
        efl_promise_failed_set(p, ENOMEM);
        goto end;
     }

   memcpy(c, stat, sizeof (Eina_Stat));
   efl_promise_value_set(p, c, free);

 end:
   efl_del(p);
}

static Efl_Future *
_efl_io_manager_stat(Eo *obj,
                     Efl_Io_Manager_Data *pd EINA_UNUSED,
                     const char *path)
{
   Efl_Promise *p;
   Eio_File *h;

   Eo *loop = efl_loop_get(obj);
   p = efl_add(EFL_PROMISE_CLASS, loop);
   if (!p) return NULL;

   h = eio_file_direct_stat(path,
                            _file_stat_done_cb,
                            _file_error_cb,
                            p);
   if (!h) goto end;

   efl_event_callback_array_add(p, promise_handling(), h);
   return efl_promise_future_get(p);

 end:
   efl_del(p);
   return NULL;
}

/* eXtended attribute manipulation */

static Efl_Future *
_efl_io_manager_xattr_ls(Eo *obj,
                         Efl_Io_Manager_Data *pd EINA_UNUSED,
                         const char *path)
{
   Efl_Promise *p;
   Eio_File *h;

   Eo *loop = efl_loop_get(obj);
   p = efl_add(EFL_PROMISE_CLASS, loop);
   if (!p) return NULL;

   h = _eio_file_xattr(path,
                       _file_string_cb,
                       _file_done_cb,
                       _file_error_cb,
                       p);
   if (!h) goto end;

   efl_event_callback_array_add(p, promise_progress_handling(), h);
   return efl_promise_future_get(p);

 end:
   efl_del(p);
   return NULL;
}

static void
_future_file_done_cb(void *data, Eio_File *handler)
{
   Eina_Promise *p = data;

   eina_promise_resolve(p, eina_value_uint64_init(handler->length));
}

static void
_future_file_error_cb(void *data,
                      Eio_File *handler EINA_UNUSED,
                      int error)
{
   Eina_Promise *p = data;

   eina_promise_reject(p, error);
}

static void
_efl_io_manager_future_cancel(void *data, const Eina_Promise *dead_ptr EINA_UNUSED)
{
   eio_file_cancel(data);
}

static Eina_Future *
_efl_io_manager_xattr_set(Eo *obj,
                          Efl_Io_Manager_Data *pd EINA_UNUSED,
                          const char *path,
                          const char *attribute,
                          Eina_Binbuf *data,
                          Eina_Xattr_Flags flags)
{
   Eina_Promise *p;
   Eina_Future *future;
   Eio_File *h;

   p = eina_promise_new(efl_loop_future_scheduler_get(obj),
                        _efl_io_manager_future_cancel, NULL);
   if (!p) return NULL;
   future = eina_future_new(p);

   h = eio_file_xattr_set(path, attribute,
                          (const char *) eina_binbuf_string_get(data),
                          eina_binbuf_length_get(data),
                          flags,
                          _future_file_done_cb,
                          _future_file_error_cb,
                          p);
   if (!h) goto end;
   eina_promise_data_set(p, h);

   return efl_future_Eina_FutureXXX_then(obj, future);

 end:
   return future;
}

static Efl_Future *
_efl_io_manager_xattr_get(Eo *obj,
                          Efl_Io_Manager_Data *pd EINA_UNUSED,
                          const char *path,
                          const char *attribute)
{
   Efl_Promise *p;
   Eio_File *h;

   Eo *loop = efl_loop_get(obj);
   p = efl_add(EFL_PROMISE_CLASS, loop);
   if (!p) return NULL;

   h = eio_file_xattr_get(path, attribute,
                          _file_done_data_cb,
                          _file_error_cb,
                          p);
   if (!h) goto end;

   efl_event_callback_array_add(p, promise_handling(), h);
   return efl_promise_future_get(p);

 end:
   efl_del(p);
   return NULL;
}

static void
_future_file_open_cb(void *data, Eio_File *handler EINA_UNUSED, Eina_File *file)
{
   Eina_Promise *p = data;
   Eina_Value v = EINA_VALUE_EMPTY;

   eina_value_setup(&v, EINA_VALUE_TYPE_FILE);
   eina_value_set(&v, file);
   eina_promise_resolve(p, v);
   eina_value_flush(&v);
}

static Eina_Future *
_efl_io_manager_open(Eo *obj,
                     Efl_Io_Manager_Data *pd EINA_UNUSED,
                     const char *path,
                     Eina_Bool shared)
{
   Eina_Promise *p;
   Eina_Future *future;
   Eio_File *h;

   p = eina_promise_new(efl_loop_future_scheduler_get(obj),
                        _efl_io_manager_future_cancel, NULL);
   if (!p) return NULL;
   future = eina_future_new(p);

   h = eio_file_open(path, shared,
                     _future_file_open_cb,
                     _future_file_error_cb,
                     p);
   if (!h) goto end;
   eina_promise_data_set(p, h);

   return efl_future_Eina_FutureXXX_then(obj, future);

 end:
   return future;
}

static Eina_Future *
_efl_io_manager_close(Eo *obj,
                      Efl_Io_Manager_Data *pd EINA_UNUSED,
                      Eina_File *file)
{
   Eina_Promise *p;
   Eina_Future *future;
   Eio_File *h;

   p = eina_promise_new(efl_loop_future_scheduler_get(obj),
                        _efl_io_manager_future_cancel, NULL);
   if (!p) return NULL;
   future = eina_future_new(p);

   h = eio_file_close(file,
                      _future_file_done_cb,
                      _future_file_error_cb,
                      p);
   if (!h) goto end;
   eina_promise_data_set(p, h);

   return efl_future_Eina_FutureXXX_then(obj, future);

 end:
   return future;
}

#include "efl_io_manager.eo.c"