summaryrefslogtreecommitdiff
path: root/src/lib/eio/eio_job.c
blob: e9fb2f795f29ba5b468715fd0e14d233b28cb6e8 (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
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
/* EIO - EFL data type library
 * Copyright (C) 2016 Enlightenment Developers:
 *           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 "Eio.h"
#include "eio_job_private.h"

typedef Eio_File* (*Eio_Job_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;
   Eio_Job_Data *pdata;
   Eina_Promise_Owner *promise;
   Eio_File *file;
   Eina_Bool delete_me;
   void *delayed_arg;
   Eio_Job_Direct_Ls_Func direct_func;  // Used when dispatching direct ls funcs.
};

/* Helper functions */

static Job_Closure *
_job_closure_create(Eo *obj, Eio_Job_Data *pdata, Eina_Promise_Owner *owner)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(obj, NULL);
   EINA_SAFETY_ON_NULL_RETURN_VAL(pdata, NULL);
   EINA_SAFETY_ON_NULL_RETURN_VAL(owner, NULL);

   Job_Closure *closure = malloc(sizeof(Job_Closure));

   if (!closure)
     {
        EINA_LOG_CRIT("Failed to allocate memory.");
        return 0;
     }

   closure->object = eo_ref(obj);
   closure->pdata = pdata;
   closure->promise = owner;
   closure->file = NULL; // Will be set once the Eio operation is under way
   closure->delete_me = EINA_FALSE;
   closure->delayed_arg = NULL;
   closure->direct_func = NULL;

   pdata->operations = eina_list_prepend(pdata->operations, closure);

   return closure;
}

static void
_job_closure_del(Job_Closure *closure)
{
   EINA_SAFETY_ON_NULL_RETURN(closure);
   Eio_Job_Data *pdata = closure->pdata;
   if (pdata)
     pdata->operations = eina_list_remove(pdata->operations, closure);

   eo_unref(closure->object);

   if (closure->delayed_arg)
     free(closure->delayed_arg);

   free(closure);
}

static void
_file_error_cb(void *data, Eio_File *handler EINA_UNUSED, int error)
{
   Job_Closure *operation = data;

   EINA_SAFETY_ON_NULL_RETURN(operation);
   EINA_SAFETY_ON_NULL_RETURN(operation->promise);

   eina_promise_owner_error_set(operation->promise, error);

   _job_closure_del(operation);
}

/* Basic listing callbacks */

static Eina_Bool
_file_ls_filter_cb_helper(const Eo_Event_Description *event, void *data, const char *file)
{
   Job_Closure *operation = data;

   EINA_SAFETY_ON_NULL_RETURN_VAL(operation, EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(operation->pdata, EINA_FALSE);

   Eio_Filter_Name_Data* event_info = malloc(sizeof(Eio_Filter_Name_Data));

   EINA_SAFETY_ON_NULL_RETURN_VAL(event_info, EINA_FALSE);

   event_info->file = file;
   event_info->filter = EINA_FALSE;

   eo_event_callback_call(operation->pdata->object, event, event_info);

   Eina_Bool filter = event_info->filter;

   free(event_info);

   return filter;
}

static Eina_Bool
_file_ls_filter_xattr_cb(void *data, Eio_File *handler EINA_UNUSED, const char *file)
{
   return _file_ls_filter_cb_helper(EIO_JOB_EVENT_XATTR, data, file);
}

static Eina_Bool
_file_ls_filter_named_cb(void *data, Eio_File *handler EINA_UNUSED, const char *file)
{
   return _file_ls_filter_cb_helper(EIO_JOB_EVENT_FILTER_NAME, data, file);
}

static void
_file_ls_main_cb(void *data, Eio_File *handler EINA_UNUSED, const char *file)
{
   Job_Closure *operation = data;
   EINA_SAFETY_ON_NULL_RETURN(operation);
   EINA_SAFETY_ON_NULL_RETURN(operation->promise);

   eina_promise_owner_progress(operation->promise, (void*)file);
}

static void
_file_done_cb(void *data, Eio_File *handler EINA_UNUSED)
{
   Job_Closure *operation = data;

   EINA_SAFETY_ON_NULL_RETURN(operation);
   EINA_SAFETY_ON_NULL_RETURN(operation->promise);

   // Placeholder value. We just want the callback to be called.
   Eina_Bool result = EINA_TRUE;
   eina_promise_owner_value_set(operation->promise, &result, NULL);

   _job_closure_del(operation);
}

static void
_free_xattr_data(Eio_Xattr_Data *value)
{
    EINA_SAFETY_ON_NULL_RETURN(value);
    if (value->data)
      free((void*)value->data);
}

static void
_file_done_data_cb(void *data, Eio_File *handler EINA_UNUSED, const char *attr_data, unsigned int size)
{
   Job_Closure *operation = data;
   Eio_Xattr_Data *ret_data = NULL;

   EINA_SAFETY_ON_NULL_RETURN(operation);
   EINA_SAFETY_ON_NULL_RETURN(operation->promise);

   ret_data = malloc(sizeof(Eio_Xattr_Data));

   if (!ret_data)
     {
        EINA_LOG_CRIT("Failed to create promise result data.");
        return;
     }


   ret_data->data = calloc(sizeof(char), size + 1);
   strcpy((char*)ret_data->data, attr_data);
   ret_data->size = size;

   eina_promise_owner_value_set(operation->promise, ret_data, (Eina_Promise_Free_Cb)&_free_xattr_data);

   free(ret_data);

   _job_closure_del(operation);
}

/* Direct listing callbacks */

static Eina_Bool
_file_direct_ls_filter_cb(void *data, Eio_File *handle EINA_UNUSED, const Eina_File_Direct_Info *info)
{
   Job_Closure *operation = data;

   EINA_SAFETY_ON_NULL_RETURN_VAL(operation, EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(operation->pdata, EINA_FALSE);

   Eio_Filter_Direct_Data* event_info = malloc(sizeof(Eio_Filter_Direct_Data));

   EINA_SAFETY_ON_NULL_RETURN_VAL(event_info, EINA_FALSE);

   event_info->info = info;
   event_info->filter = EINA_FALSE;

   eo_event_callback_call(operation->pdata->object, EIO_JOB_EVENT_FILTER_DIRECT, event_info);

   Eina_Bool filter = event_info->filter;

   free(event_info);

   return filter;
}

static void
_file_direct_ls_main_cb(void *data, Eio_File *handler EINA_UNUSED, const Eina_File_Direct_Info *info)
{
   Job_Closure *operation = data;
   EINA_SAFETY_ON_NULL_RETURN(operation);
   EINA_SAFETY_ON_NULL_RETURN(operation->promise);

   eina_promise_owner_progress(operation->promise, (void*)info);
}

static void
_ls_direct_notify_start(void* data, Eina_Promise_Owner *promise)
{
   Job_Closure *operation_data = (Job_Closure*)data;
   char* path = operation_data->delayed_arg;

   Eio_File *handle = operation_data->direct_func(path,
         _file_direct_ls_filter_cb,
         _file_direct_ls_main_cb,
         _file_done_cb,
         _file_error_cb,
         operation_data);
   operation_data->file = handle;

   promise->progress_notify = NULL;
}

static void
_free_notify_start_data(void *data)
{
   Job_Closure *operation_data = (Job_Closure*)data;
   if (!operation_data->delayed_arg)
     return;
   free(operation_data->delayed_arg);
   operation_data->delayed_arg = NULL;
}

static void
_ls_notify_start(void *data, Eina_Promise_Owner* promise)
{
   Job_Closure *operation_data = (Job_Closure*)data;
   char* path = operation_data->delayed_arg;

   Eio_File *handle = eio_file_ls(path,
         _file_ls_filter_named_cb,
         _file_ls_main_cb,
         _file_done_cb,
         _file_error_cb,
         operation_data);
   operation_data->file = handle;

   promise->progress_notify = NULL; // Don't ever think about calling me again...
}

static void
_xattr_notify_start(void *data, Eina_Promise_Owner *promise)
{
   Job_Closure *operation_data = (Job_Closure*)data;
   char* path = operation_data->delayed_arg;

   Eio_File *handle = eio_file_xattr(path,
         _file_ls_filter_xattr_cb,
         _file_ls_main_cb,
         _file_done_cb,
         _file_error_cb,
         operation_data);
   operation_data->file = handle;

   promise->progress_notify = NULL; // Don't ever think about calling me again...
}

static void
_job_direct_ls_helper(Eio_Job_Direct_Ls_Func ls_func,
      Eo* obj,
      Eio_Job_Data *pd,
      const char *path,
      Eina_Promise_Owner *promise)
{
   Job_Closure *operation_data = _job_closure_create(obj, pd, promise);

   if (!operation_data)
     {
        EINA_LOG_CRIT("Failed to create eio job operation data.");
        return;
     }

   operation_data->delayed_arg = (char*)calloc(sizeof(char), strlen(path) + 1);
   strcpy(operation_data->delayed_arg, path);

   operation_data->direct_func = ls_func;

   eina_promise_owner_progress_notify(promise,
         _ls_direct_notify_start,
         operation_data,
         _free_notify_start_data);
}

/* Method implementations */

void
_eio_job_file_direct_ls(Eo *obj,
      Eio_Job_Data *pd,
      const char *path,
      Eina_Promise_Owner *promise)
{
   _job_direct_ls_helper(&eio_file_direct_ls, obj, pd, path, promise);
}

void
_eio_job_file_stat_ls(Eo *obj,
      Eio_Job_Data *pd,
      const char *path,
      Eina_Promise_Owner *promise)
{
   _job_direct_ls_helper(&eio_file_stat_ls, obj, pd, path, promise);
}

void
_eio_job_dir_stat_ls(Eo *obj,
      Eio_Job_Data *pd,
      const char *path,
      Eina_Promise_Owner *promise)
{
   _job_direct_ls_helper(&eio_dir_stat_ls, obj, pd, path, promise);
}

void
_eio_job_dir_direct_ls(Eo *obj EINA_UNUSED,
                       Eio_Job_Data *pd EINA_UNUSED,
                       const char *path,
                       Eina_Promise_Owner *promise EINA_UNUSED)
{
   // Had to add the cast as dir_direct differs in the filter callback constness of one of
   // its arguments.
   _job_direct_ls_helper((Eio_Job_Direct_Ls_Func)&eio_dir_direct_ls, obj, pd, path, promise);
}

void
_eio_job_file_ls(Eo *obj,
      Eio_Job_Data *pd,
      const char *path,
      Eina_Promise_Owner *promise)
{
   Job_Closure *operation_data = _job_closure_create(obj, pd, promise);

   if (!operation_data)
     {
        EINA_LOG_CRIT("Failed to create eio job operation data.");
        return;
     }

   operation_data->delayed_arg = (char*)calloc(sizeof(char), strlen(path) + 1);
   strcpy(operation_data->delayed_arg, path);

   eina_promise_owner_progress_notify(promise,
      _ls_notify_start,
      operation_data,
      _free_notify_start_data);
}

/* Stat function */

static void
_file_stat_done_cb(void *data, Eio_File *handle EINA_UNUSED, const Eina_Stat *stat)
{
   Job_Closure *operation = data;

   EINA_SAFETY_ON_NULL_RETURN(operation);
   EINA_SAFETY_ON_NULL_RETURN(operation->promise);

   // Placeholder value. We just want the callback to be called.
   eina_promise_owner_value_set(operation->promise, &stat, NULL);

   _job_closure_del(operation);
}

void
_eio_job_file_direct_stat(Eo *obj,
                          Eio_Job_Data *pd,
                          const char *path,
                          Eina_Promise_Owner *promise)
{
   Job_Closure *operation_data = _job_closure_create(obj, pd, promise);

   if (!operation_data)
     {
        EINA_LOG_CRIT("Failed to create eio job operation data.");
        return;
     }

   Eio_File *handle = eio_file_direct_stat(path,
         _file_stat_done_cb,
         _file_error_cb,
         operation_data);
   operation_data->file = handle;
}

/* eXtended attribute manipulation */

void
_eio_job_file_xattr(Eo *obj,
                    Eio_Job_Data *pd,
                    const char *path,
                    Eina_Promise_Owner *promise)
{
   Job_Closure *operation_data = _job_closure_create(obj, pd, promise);

   if (!operation_data)
     {
        EINA_LOG_CRIT("Failed to create eio job operation data.");
        return;
     }

   operation_data->delayed_arg = (char*)calloc(sizeof(char), strlen(path) + 1);
   strcpy(operation_data->delayed_arg, path);

   eina_promise_owner_progress_notify(promise,
      _xattr_notify_start,
      operation_data,
      _free_notify_start_data);
}

void
_eio_job_file_xattr_set(Eo *obj,
                        Eio_Job_Data *pd,
                        const char *path,
                        const char *attribute,
                        const char *xattr_data,
                        unsigned int xattr_size,
                        Eina_Xattr_Flags flags,
                        Eina_Promise_Owner *promise)
{
   Job_Closure *operation_data = _job_closure_create(obj, pd, promise);

   if (!operation_data)
     {
        EINA_LOG_CRIT("Failed to create eio job operation data.");
        return;
     }

   Eio_File *handle = eio_file_xattr_set(path,
         attribute,
         xattr_data,
         xattr_size,
         flags,
         _file_done_cb,
         _file_error_cb,
         operation_data);
   operation_data->file = handle;
}

void
_eio_job_file_xattr_get(Eo *obj,
                        Eio_Job_Data *pd,
                        const char *path,
                        const char *attribute,
                        Eina_Promise_Owner *promise)
{
   Job_Closure *operation_data = _job_closure_create(obj, pd, promise);

   if (!operation_data)
     {
        EINA_LOG_CRIT("Failed to create eio job operation data.");
        return;
     }

   Eio_File *handle = eio_file_xattr_get(path,
         attribute,
         _file_done_data_cb,
         _file_error_cb,
         operation_data);
   operation_data->file = handle;
}

/* Eina_File mapping and handling. */

static void
_file_open_open_cb(void *data, Eio_File *handler EINA_UNUSED, Eina_File *file)
{
   Job_Closure *operation = data;
   EINA_SAFETY_ON_NULL_RETURN(operation);
   EINA_SAFETY_ON_NULL_RETURN(operation->promise);
   // FIXME On promise composition, a successfully open file would leak open
   //       another promise in the composition fails as there is no free/close
   //       function. Calling eina_file_close blocks on a lock_take call on a
   //       field of the Eina_File file.
   eina_promise_owner_value_set(operation->promise, &file, NULL);

   _job_closure_del(operation);
}

void
_eio_job_file_open(Eo *obj,
                   Eio_Job_Data *pd,
                   const char *path,
                   Eina_Bool shared,
                   Eina_Promise_Owner *promise)
{
   Job_Closure *operation_data = _job_closure_create(obj, pd, promise);

   if (!operation_data)
     {
        EINA_LOG_CRIT("Failed to create eio job operation data.");
        return;
     }

   Eio_File *handle = eio_file_open(path, shared, _file_open_open_cb, _file_error_cb, operation_data);
   operation_data->file = handle;
}


static
void _file_close_done_cb(void *data, Eio_File *handler EINA_UNUSED)
{
   EINA_SAFETY_ON_NULL_RETURN(data);
   Job_Closure *operation = data;
   Eina_Bool result = EINA_TRUE;
   eina_promise_owner_value_set(operation->promise, &result, NULL);

   _job_closure_del(operation);
}
void _eio_job_file_close(Eo *obj,
                         Eio_Job_Data *pd,
                         Eina_File *file,
                         Eina_Promise_Owner *promise)
{
   Job_Closure *operation_data = _job_closure_create(obj, pd, promise);

   if (!operation_data)
     {
        EINA_LOG_CRIT("Failed to create eio job operation data.");
        return;
     }

   Eio_File *handle = eio_file_close(file, _file_close_done_cb, _file_error_cb, operation_data);
   operation_data->file = handle;
}

void _eio_job_cancel_all(Eo *obj EINA_UNUSED, Eio_Job_Data *pd EINA_UNUSED)
{
   printf("%s called.\n", __FUNCTION__);
}

Eo_Base * _eio_job_eo_base_constructor(Eo *obj, Eio_Job_Data *pd EINA_UNUSED)
{
   obj = eo_constructor(eo_super(obj, EIO_JOB_CLASS));

   pd->object = obj;
   pd->operations = NULL;

   return obj;
}

void _eio_job_eo_base_destructor(Eo *obj, Eio_Job_Data *pd EINA_UNUSED)
{
   eo_destructor(eo_super(obj, EIO_JOB_CLASS));
}

#include "eio_job.eo.c"