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

#include "ecordova_filewriter_private.h"
#include "ecordova_entry_private.h"

#include <stdbool.h>
#include <unistd.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_FILEWRITER_CLASS
#define MY_CLASS_NAME "Ecordova_FileWriter"

static void _write_cb(void *, Ecore_Thread *);
static void _truncate_cb(void *, Ecore_Thread *);
static bool _stream_init(Ecordova_FileWriter_Data *);
static bool _offset_set(Ecordova_FileWriter_Data *);
static void _write_end_cb(void *, Ecore_Thread *);
static void _write_abort_cb(void *, Ecore_Thread *);
static void _write_progress_cb(void *, Ecore_Thread *, void *);
static void _progress_notify(size_t, size_t, Eo *, Ecore_Thread *);

static Eo_Base *
_ecordova_filewriter_eo_base_constructor(Eo *obj,
                                         Ecordova_FileWriter_Data *pd)
{
   DBG("(%p)", obj);

   pd->obj = obj;
   pd->error = 0;
   pd->error = ECORDOVA_FILEWRITER_STATE_INIT;
   pd->url = NULL;
   pd->offset = 0;
   pd->length = 0;
   pd->stream = NULL;
   pd->data = NULL;
   pd->data_size = 0;
   pd->truncate_size = 0;

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

static void
_ecordova_filewriter_constructor(Eo *obj,
                                 Ecordova_FileWriter_Data *pd,
                                 Ecordova_File *file)
{
   DBG("(%p)", obj);
   EINA_SAFETY_ON_NULL_RETURN(file);

   eo_do(file,
         pd->url = strdup(ecordova_file_url_get()),
         pd->offset = ecordova_file_start_get(),
         pd->length = ecordova_file_end_get() - pd->offset);
}

static void
_ecordova_filewriter_eo_base_destructor(Eo *obj,
                                        Ecordova_FileWriter_Data *pd)
{
   DBG("(%p)", obj);

   if (pd->thread) ERR("%s", "Destructing without aborting first");

   free(pd->url);
   if (pd->stream)
     fclose(pd->stream);
   if (pd->data)
     free(pd->data);

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

static Ecordova_FileError
_ecordova_filewriter_abort(Eo *obj, Ecordova_FileWriter_Data *pd)
{
   DBG("(%p)", obj);

   if (ECORDOVA_FILEWRITER_STATE_DONE == pd->state ||
       ECORDOVA_FILEWRITER_STATE_INIT == pd->state)
     return ECORDOVA_FILEERROR_INVALID_STATE_ERR;
   pd->state = ECORDOVA_FILEWRITER_STATE_DONE;
   pd->error = ECORDOVA_FILEERROR_ABORT_ERR;

   if (pd->thread)
     ecore_thread_cancel(pd->thread);

   return 0;
}

static Ecordova_FileError
_ecordova_filewriter_write(Eo *obj,
                           Ecordova_FileWriter_Data *pd,
                           const char *data,
                           long size)
{
   DBG("(%p)", obj);

   if (ECORDOVA_FILEWRITER_STATE_WRITING == pd->state)
     return ECORDOVA_FILEERROR_INVALID_STATE_ERR;
   pd->state = ECORDOVA_FILEWRITER_STATE_WRITING;
   pd->error = 0;

   Ecordova_ProgressEvent writestart = {.type = "writestart", .target = obj};
   eo_do(obj,
         eo_event_callback_call(ECORDOVA_FILEWRITER_EVENT_ON_WRITE_START,
                                &writestart));

   pd->data_size = size ? size : (long)strlen(data);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(pd->data_size > 0, ECORDOVA_FILEERROR_SYNTAX_ERR);

   pd->data = pd->data ? realloc(pd->data, pd->data_size) : malloc(pd->data_size);
   EINA_SAFETY_ON_NULL_RETURN_VAL(pd->data, ECORDOVA_FILEERROR_SYNTAX_ERR);

   memcpy(pd->data, data, pd->data_size);

   pd->thread = ecore_thread_feedback_run(_write_cb,
                                          _write_progress_cb,
                                          _write_end_cb,
                                          _write_abort_cb,
                                          pd,
                                          EINA_FALSE);
   return 0;
}

static Ecordova_FileError
_ecordova_filewriter_seek(Eo *obj, Ecordova_FileWriter_Data *pd, long offset)
{
   DBG("(%p)", obj);
   if (ECORDOVA_FILEWRITER_STATE_WRITING == pd->state)
     return ECORDOVA_FILEERROR_INVALID_STATE_ERR;

   if (offset < 0)
     pd->offset = MAX(offset + pd->length, 0);
   else if (offset > pd->length)
     pd->offset = pd->length;
   else
     pd->offset = offset;

   return 0;
}

static Ecordova_FileError
_ecordova_filewriter_truncate(Eo *obj, Ecordova_FileWriter_Data *pd, long size)
{
   DBG("(%p)", obj);

   if (ECORDOVA_FILEWRITER_STATE_WRITING == pd->state)
     return ECORDOVA_FILEERROR_INVALID_STATE_ERR;
   pd->state = ECORDOVA_FILEWRITER_STATE_WRITING;
   pd->error = 0;
   pd->truncate_size = size;

   pd->thread = ecore_thread_run(_truncate_cb,
                                 _write_end_cb,
                                 _write_abort_cb,
                                 pd);
   return 0;
}

static Ecordova_FileError
_ecordova_filewriter_error_get(Eo *obj, Ecordova_FileWriter_Data *pd)
{
   DBG("(%p)", obj);
   return pd->error;
}

static long
_ecordova_filewriter_position_get(Eo *obj, Ecordova_FileWriter_Data *pd)
{
   DBG("(%p)", obj);
   return pd->offset;
}

static Ecordova_FileWriter_State
_ecordova_filewriter_state_get(Eo *obj, Ecordova_FileWriter_Data *pd)
{
   DBG("(%p)", obj);
   return pd->state;
}

static void
_write_cb(void *data, Ecore_Thread *thread)
{
   Ecordova_FileWriter_Data *pd = data;
   DBG("(%p)", pd->obj);

   if (ecore_thread_check(thread))
     return;

   if (!pd->stream && !_stream_init(pd))
     return;

   if (!_offset_set(pd))
     return;

   _progress_notify(0, pd->data_size, pd->obj, thread);
   if (ecore_thread_check(thread))
     return;

   size_t total = pd->data_size;
   char *buffer = pd->data;
   do
     {
        size_t written = fwrite(buffer, sizeof(char), total, pd->stream);
        if (!written)
          {
             pd->error = _translate_errno(errno);
             return;
          }

        buffer += written;
        total -= written;
        pd->offset += written;

        _progress_notify(pd->data_size - total, pd->data_size, pd->obj, thread);
        if (ecore_thread_check(thread))
          return;
     }
   while (total > 0);

   pd->length = MAX(pd->offset, pd->length);
}

static void
_truncate_cb(void *data, Ecore_Thread *thread EINA_UNUSED)
{
   Ecordova_FileWriter_Data *pd = data;
   DBG("(%p)", pd->obj);

   if (ecore_thread_check(thread))
     return;

   if (!pd->stream && !_stream_init(pd))
     return;

   if (!_offset_set(pd))
     return;

   if (ftruncate(fileno(pd->stream), pd->truncate_size) != 0)
     {
        pd->error = _translate_errno(errno);
        return;
     }

   pd->length = pd->truncate_size;
   pd->offset = MIN(pd->offset, pd->truncate_size);
}

static bool
_stream_init(Ecordova_FileWriter_Data *pd)
{
   pd->stream = fopen(pd->url, "rb+");
   if (!pd->stream)
     {
        pd->error = _translate_errno(errno);
        return false;
     }

  return true;
}

static bool
_offset_set(Ecordova_FileWriter_Data *pd)
{
  int error = fseek(pd->stream, pd->offset, SEEK_SET);
  if (error)
    {
       pd->error = _translate_errno(errno);
       return false;
    }

  return true;
}

static void
_progress_notify(size_t written, size_t total, Eo *obj, Ecore_Thread *thread)
{
   Ecordova_ProgressEvent *progress = malloc(sizeof(Ecordova_ProgressEvent));
   *progress = (Ecordova_ProgressEvent)
     {
        .type = "progress",
        .length_computable = EINA_TRUE,
        .loaded = written,
        .total = total,
        .target = obj
     };
   if (!ecore_thread_feedback(thread, progress))
     free(progress);
}

static void
_write_progress_cb(void *data,
                   Ecore_Thread *thread EINA_UNUSED,
                   void *msg_data)
{
   Ecordova_FileWriter_Data *pd = data;
   DBG("(%p)", pd->obj);
   Ecordova_ProgressEvent *event = msg_data;
   eo_do(pd->obj,
         eo_event_callback_call(ECORDOVA_FILEWRITER_EVENT_ON_PROGRESS, &event));
   free(event);
}

static void
_write_end_cb(void *data, Ecore_Thread *thread EINA_UNUSED)
{
   Ecordova_FileWriter_Data *pd = data;
   DBG("(%p)", pd->obj);

   // If DONE (cancelled), then don't do anything
   if (ECORDOVA_FILEWRITER_STATE_DONE == pd->state)
     return;
   pd->thread = NULL;
   pd->state = ECORDOVA_FILEWRITER_STATE_DONE;

   if (pd->error)
     {
        Ecordova_ProgressEvent error = {.type = "error", .target = pd->obj};
        eo_do(pd->obj,
              eo_event_callback_call(ECORDOVA_FILEWRITER_EVENT_ON_ERROR,
                                     &error));
     }
   else
     {
        Ecordova_ProgressEvent write = {.type = "write", .target = pd->obj};
        eo_do(pd->obj,
              eo_event_callback_call(ECORDOVA_FILEWRITER_EVENT_ON_WRITE,
                                     &write));
     }

   Ecordova_ProgressEvent writeend = {.type = "writeend", .target = pd->obj};
   eo_do(pd->obj,
         eo_event_callback_call(ECORDOVA_FILEWRITER_EVENT_ON_WRITE_END,
                                &writeend));
}

static void
_write_abort_cb(void *data, Ecore_Thread *thread EINA_UNUSED)
{
   Ecordova_FileWriter_Data *pd = data;
   DBG("(%p)", pd->obj);

   pd->thread = NULL;

   Ecordova_ProgressEvent on_abort = {.type = "abort", .target = pd->obj};
   eo_do(pd->obj,
         eo_event_callback_call(ECORDOVA_FILEWRITER_EVENT_ON_ABORT,
                                &on_abort));

   Ecordova_ProgressEvent writeend = {.type = "writeend", .target = pd->obj};
   eo_do(pd->obj,
         eo_event_callback_call(ECORDOVA_FILEWRITER_EVENT_ON_WRITE_END,
                                &writeend));
}

#undef EOAPI
#define EOAPI EAPI

#include "undefs.h"

#define ecordova_filewriter_class_get ecordova_filewriter_impl_class_get

#include "ecordova_filewriter.eo.c"