summaryrefslogtreecommitdiff
path: root/src/modules/ecordova/linux/ecordova_entry.c
blob: 0d2e31c7eecb5c672790e862ba857a65d5b7f25b (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Eo.h>
#include <Ecordova.h>

#include "ecordova_entry_private.h"
#include "ecordova_directoryentry_private.h"

#ifdef EAPI
# undef EAPI
#endif

#ifdef _WIN32
# ifdef DLL_EXPORT
#  define EAPI __declspec(dllexport)
# else
#  define EAPI
# endif /* ! DLL_EXPORT */
#else
# ifdef __GNUC__
#  if __GNUC__ >= 4
#   define EAPI __attribute__ ((visibility("default")))
#  else
#   define EAPI
#  endif
# else
#  define EAPI
# endif
#endif /* ! _WIN32 */

/* logging support */
extern int _ecordova_log_dom;

#define CRI(...) EINA_LOG_DOM_CRIT(_ecordova_log_dom, __VA_ARGS__)
#define ERR(...) EINA_LOG_DOM_ERR(_ecordova_log_dom, __VA_ARGS__)
#define WRN(...) EINA_LOG_DOM_WARN(_ecordova_log_dom, __VA_ARGS__)
#define INF(...) EINA_LOG_DOM_INFO(_ecordova_log_dom, __VA_ARGS__)
#define DBG(...) EINA_LOG_DOM_DBG(_ecordova_log_dom, __VA_ARGS__)

#define MY_CLASS ECORDOVA_ENTRY_CLASS
#define MY_CLASS_NAME "Ecordova_Entry"

static void _eio_stat_cb(void *, Eio_File *, const Eina_Stat *);
static void _eio_moved_cb(void *, Eio_File *);
static void _eio_copied_cb(void *, Eio_File *);
static void _metadata_notify(Eo *, void *);
static void _move_notify(Eo *, void *);
static void _copy_notify(Eo *, void *);
static void _remove_notify(Eo *, void *);
static Eina_Bool _eio_remove_non_recursively_filter_cb(void *, Eio_File *, const Eina_File_Direct_Info *);
static void _parent_get_cb(void *, Eio_File *, const Eina_Stat *);
static void _parent_get_notify(Eo *, void *);

static Eo_Base *
_ecordova_entry_eo_base_constructor(Eo *obj, Ecordova_Entry_Data *pd)
{
   DBG("(%p)", obj);

   pd->obj = obj;
   pd->name = NULL;
   pd->path = NULL;
   pd->file_system = NULL;
   pd->native = NULL;
   pd->pending = NULL;

   return eo_do_super_ret(obj, MY_CLASS, obj, eo_constructor());
}

static void
_ecordova_entry_constructor(Eo *obj,
                            Ecordova_Entry_Data *pd,
                            Eina_Bool file_is,
                            Eina_Bool directory_is,
                            const char *name,
                            const char *path,
                            Ecordova_FileSystem *file_system,
                            const char *url)
{
   EINA_SAFETY_ON_NULL_RETURN(name);
   EINA_SAFETY_ON_NULL_RETURN(path);
   //EINA_SAFETY_ON_NULL_RETURN(file_system);
   EINA_SAFETY_ON_NULL_RETURN(url);
   DBG("(%p) name=%s, path=%s, url=%s", obj, name, path, url);

   pd->is_file = file_is;
   pd->is_directory = directory_is;
   pd->name = strdup(name);
   pd->path = strdup(path);
   pd->file_system = eo_ref(file_system);
   pd->native = strdup(url);
}

static void
_ecordova_entry_eo_base_destructor(Eo *obj, Ecordova_Entry_Data *pd)
{
   DBG("(%p)", obj);

   free(pd->name);
   free(pd->path);
   eo_unref(pd->file_system);
   free(pd->native);

   Eio_Operation_Data *data;
   EINA_LIST_FREE(pd->pending, data)
     {
        eio_file_cancel(data->file);
        _data_free(data);
     }

   eo_do_super(obj, MY_CLASS, eo_destructor());
}

static void
_ecordova_entry_metadata_get(Eo *obj EINA_UNUSED, Ecordova_Entry_Data *pd)
{
   DBG("(%p)", obj);
   _metadata_get(pd, _metadata_notify, _error_notify);
}

static void
_ecordova_entry_metadata_set(Eo *obj EINA_UNUSED,
                             Ecordova_Entry_Data *pd EINA_UNUSED,
                             Ecordova_Metadata *metadata EINA_UNUSED)
{
   ERR("Not implemented.");
}

static void
_ecordova_entry_move(Eo *obj EINA_UNUSED,
                     Ecordova_Entry_Data *pd,
                     Ecordova_DirectoryEntry *parent,
                     const char *new_name)
{
   DBG("(%p)", obj);
   EINA_SAFETY_ON_NULL_RETURN(parent);
   if (!new_name) new_name = pd->name;

   Eio_Operation_Data *data = _data_new(pd, _move_notify, _error_notify);

   Ecordova_Entry_Data *dest_dir = eo_data_scope_get(parent, ECORDOVA_ENTRY_CLASS);

   data->name = strdup(new_name);
   data->path = strdup(dest_dir->native);

   // TODO: file_system?

   size_t len = strlen(data->path) + 1 + strlen(data->name) + 1;
   data->native = malloc(len);
   EINA_SAFETY_ON_NULL_GOTO(data->native, on_error);
   snprintf(data->native, len, "%s/%s", data->path, new_name);

   if (pd->is_file)
     {
        DBG("Moving file from %s to %s", pd->native, data->native);
        data->file = eio_file_move(pd->native,
                                   data->native,
                                   _eio_progress_cb,
                                   _eio_moved_cb,
                                   _eio_error_cb,
                                   data);
     }
   else
     {
        DBG("Moving directory from %s to %s", pd->native, data->native);
        data->file = eio_dir_move(pd->native,
                                  data->native,
                                  _eio_filter_cb,
                                  _eio_progress_cb,
                                  _eio_moved_cb,
                                  _eio_error_cb,
                                  data);
     }

   pd->pending = eina_list_append(pd->pending, data);
   return;

on_error:
   _data_free(data);
}

static void
_ecordova_entry_copy(Eo *obj EINA_UNUSED,
                     Ecordova_Entry_Data *pd,
                     Ecordova_DirectoryEntry *parent,
                     const char *new_name)
{
   DBG("(%p)", obj);
   EINA_SAFETY_ON_NULL_RETURN(parent);
   if (!new_name) new_name = pd->name;

   Eio_Operation_Data *data = _data_new(pd, _copy_notify, _error_notify);

   Ecordova_Entry_Data *dest_dir = eo_data_scope_get(parent, ECORDOVA_ENTRY_CLASS);

   data->name = strdup(new_name);
   data->path = strdup(dest_dir->native);

   // TODO: file_system?

   size_t len = strlen(data->path) + 1 + strlen(data->name) + 1;
   data->native = malloc(len);
   EINA_SAFETY_ON_NULL_GOTO(data->native, on_error);
   snprintf(data->native, len, "%s/%s", data->path, new_name);

   if (pd->is_file)
     data->file = eio_file_copy(pd->native,
                                data->native,
                                _eio_progress_cb,
                                _eio_copied_cb,
                                _eio_error_cb,
                                data);
   else
     data->file = eio_dir_copy(pd->native,
                               data->native,
                               _eio_filter_cb,
                               _eio_progress_cb,
                               _eio_copied_cb,
                               _eio_error_cb,
                               data);

   pd->pending = eina_list_append(pd->pending, data);
   return;

on_error:
   _data_free(data);
}

static void
_ecordova_entry_remove(Eo *obj EINA_UNUSED, Ecordova_Entry_Data *pd)
{
   DBG("(%p)", obj);
   _entry_remove(pd, _remove_notify, _error_notify, false);
}

void
_entry_remove(Ecordova_Entry_Data *pd,
              Ecordova_Entry_Success_Callback success_cb,
              Ecordova_Entry_Error_Callback error_cb,
              bool recursively)
{
   DBG("(%p)", pd->obj);

   Eio_Operation_Data *data = _data_new(pd, success_cb, error_cb);

   // TODO: file_system?

   if (pd->is_file)
     data->file = eio_file_unlink(pd->native,
                                  _eio_removed_cb,
                                  _eio_error_cb,
                                  data);
   else
     {
        Eio_Filter_Direct_Cb filter_cb =
          recursively ? _eio_filter_cb
                      : _eio_remove_non_recursively_filter_cb;

        data->file = eio_dir_unlink(pd->native,
                                    filter_cb,
                                    _eio_progress_cb,
                                    _eio_removed_cb,
                                    _eio_error_cb,
                                    data);
     }

   pd->pending = eina_list_append(pd->pending, data);
}

static void
_ecordova_entry_parent_get(Eo *obj, Ecordova_Entry_Data *pd)
{
   DBG("(%p)", obj);

   Eio_Operation_Data *data = _data_new(pd, _parent_get_notify, _error_notify);

   data->file = eio_file_direct_stat(pd->native,
                                     _parent_get_cb,
                                     _eio_error_cb,
                                     data);

   pd->pending = eina_list_append(pd->pending, data);
 }

static void
_parent_get_cb(void *user_data,
               Eio_File *handler EINA_UNUSED,
               const Eina_Stat *stat EINA_UNUSED)
{
   Eio_Operation_Data *data = user_data;
   data->success_cb(data->pd->obj, data->pd);
   _data_free(data);
}

static void
_parent_get_notify(Eo *obj, void *data)
{
   Ecordova_Entry_Data *pd = data;

   char *name, *path, *url;
   split_path(NULL, pd->path, &path, &name, &url);

   Ecordova_DirectoryEntry *parent =
     eo_add(ECORDOVA_DIRECTORYENTRY_CLASS,
            NULL,
            ecordova_directoryentry_constructor(name,
                                                path,
                                                NULL, // TODO: filesystem ?
                                                url));
   free(name);
   free(path);
   free(url);

   eo_do(obj, eo_event_callback_call(ECORDOVA_ENTRY_EVENT_PARENT_GET, parent));
   eo_unref(parent);
}

static Eina_Bool
_ecordova_entry_file_is_get(Eo *obj EINA_UNUSED, Ecordova_Entry_Data *pd)
{
   return pd->is_file;
}

static Eina_Bool
_ecordova_entry_directory_is_get(Eo *obj EINA_UNUSED, Ecordova_Entry_Data *pd)
{
   return pd->is_directory;
}

static const char *
_ecordova_entry_name_get(Eo *obj EINA_UNUSED, Ecordova_Entry_Data *pd)
{
   return pd->name;
}

static const char *
_ecordova_entry_path_get(Eo *obj EINA_UNUSED, Ecordova_Entry_Data *pd)
{
   return pd->path;
}

void
_metadata_get(Ecordova_Entry_Data *pd,
              Ecordova_Entry_Success_Callback success_cb,
              Ecordova_Entry_Error_Callback error_cb)
{
   DBG("(%p)", pd->obj);

   Eio_Operation_Data *data = _data_new(pd, success_cb, error_cb);

   data->file = eio_file_direct_stat(pd->native,
                                     _eio_stat_cb,
                                     _eio_error_cb,
                                     data);

   pd->pending = eina_list_append(pd->pending, data);
}

static void
_eio_stat_cb(void *user_data,
             Eio_File *handler EINA_UNUSED,
             const Eina_Stat *stat)
{
   Eio_Operation_Data *data = user_data;
   Ecordova_Metadata metadata = {
     .modification_date = (time_t)stat->mtime,
     .size = stat->size
   };

   data->success_cb(data->pd->obj, &metadata);
   _data_free(data);
}

static void
_metadata_notify(Eo *obj, void *data)
{
   eo_do(obj, eo_event_callback_call(ECORDOVA_ENTRY_EVENT_METADATA_GET, data));
}

void
_eio_error_cb(void *user_data,
              Eio_File *handler EINA_UNUSED,
              int error)
{
   Eio_Operation_Data *data = user_data;
   DBG("(%p)", data->pd->obj);
   data->error_cb(data->pd->obj, error);
   _data_free(data);
}

void
_eio_progress_cb(void *data EINA_UNUSED,
                 Eio_File *handler EINA_UNUSED,
                 const Eio_Progress *info EINA_UNUSED)
{
}

static void
_eio_moved_cb(void *user_data, Eio_File *handler EINA_UNUSED)
{
   Eio_Operation_Data *data = user_data;
   free(data->pd->path);
   free(data->pd->name);
   free(data->pd->native);
   data->pd->path = data->path;
   data->pd->name = data->name;
   data->pd->native = data->native;
   data->path = NULL;
   data->name = NULL;
   data->native = NULL;

   data->success_cb(data->pd->obj, data->pd->obj);
   _data_free(data);
}

static void
_move_notify(Eo *obj, void *data)
{
   eo_do(obj, eo_event_callback_call(ECORDOVA_ENTRY_EVENT_MOVE_SUCCESS, data));
}

static void
_eio_copied_cb(void *user_data, Eio_File *handler EINA_UNUSED)
{
   Eio_Operation_Data *data = user_data;
   Ecordova_Entry *entry = NULL;
   if (data->pd->is_file)
     entry = eo_add(ECORDOVA_FILEENTRY_CLASS,
                    NULL,
                    ecordova_fileentry_constructor(data->name,
                                                   data->path,
                                                   data->pd->file_system,
                                                   data->native));
   else
     entry = eo_add(ECORDOVA_DIRECTORYENTRY_CLASS,
                    NULL,
                    ecordova_directoryentry_constructor(data->name,
                                                        data->path,
                                                        data->pd->file_system,
                                                        data->native));

   data->success_cb(data->pd->obj, entry);

   eo_unref(entry);
   _data_free(data);
}

static void
_copy_notify(Eo *obj, void *data)
{
   eo_do(obj, eo_event_callback_call(ECORDOVA_ENTRY_EVENT_COPY_SUCCESS, data));
}

void
_eio_removed_cb(void *user_data, Eio_File *handler EINA_UNUSED)
{
   Eio_Operation_Data *data = user_data;
   data->success_cb(data->pd->obj, NULL);
   _data_free(data);
}

static void
_remove_notify(Eo *obj, void *data)
{
   eo_do(obj, eo_event_callback_call(ECORDOVA_ENTRY_EVENT_REMOVE_SUCCESS, data));
}

Eina_Bool
_eio_filter_cb(void *data EINA_UNUSED,
               Eio_File *handler EINA_UNUSED,
               const Eina_File_Direct_Info *info EINA_UNUSED)
{
   return EINA_TRUE;
}

static Eina_Bool
_eio_remove_non_recursively_filter_cb(void *user_data,
                                      Eio_File *handler EINA_UNUSED,
                                      const Eina_File_Direct_Info *info EINA_UNUSED)
{
   Eio_Operation_Data *data = user_data;
   DBG("filter_cb: %s", info->path);
   if (++data->count != 1)
     {
        eio_file_cancel(handler);
        return EINA_FALSE;
     }

   return EINA_TRUE;
}

Eio_Operation_Data *
_data_new(Ecordova_Entry_Data *pd,
          Ecordova_Entry_Success_Callback success_cb,
          Ecordova_Entry_Error_Callback error_cb)
{
   Eio_Operation_Data *data = calloc(1, sizeof(Eio_Operation_Data));
   data->pd = pd;
   data->success_cb = success_cb;
   data->error_cb = error_cb;
   return data;
}

void
_data_free(Eio_Operation_Data *data)
{
   EINA_SAFETY_ON_NULL_RETURN(data);

   data->pd->pending = eina_list_remove(data->pd->pending, data);
   free(data->path);
   free(data->name);
   free(data->native);
   free(data);
}

void
_error_notify(Eo *obj, int error)
{
   Ecordova_FileError file_error = _translate_errno(error);
   eo_do(obj, eo_event_callback_call(ECORDOVA_ENTRY_EVENT_ERROR, &file_error));
}

Ecordova_FileError
_translate_errno(int error)
{
   // TODO: translate other errors
   switch (error)
     {
      case EPERM:
      case EACCES:
        return ECORDOVA_FILEERROR_SECURITY_ERR;
      case ENOENT:
        return ECORDOVA_FILEERROR_NOT_FOUND_ERR;
     }

   return -1;
}

#include "undefs.h"

#define ecordova_entry_class_get ecordova_entry_impl_class_get

#include "ecordova_entry.eo.c"