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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
|
#include <config.h>
#include "gfileenumerator.h"
#include <glib/gi18n-lib.h>
#include "gioscheduler.h"
#include "gasynchelper.h"
G_DEFINE_TYPE (GFileEnumerator, g_file_enumerator, G_TYPE_OBJECT);
struct _GFileEnumeratorPrivate {
/* TODO: Should be public for subclasses? */
guint stopped : 1;
guint pending : 1;
guint cancelled : 1;
GMainContext *context;
gint io_job_id;
gpointer outstanding_callback;
GError *outstanding_error;
};
static void g_file_enumerator_real_next_files_async (GFileEnumerator *enumerator,
int num_files,
int io_priority,
GAsyncNextFilesCallback callback,
gpointer data,
GDestroyNotify notify);
static void g_file_enumerator_real_stop_async (GFileEnumerator *enumerator,
int io_priority,
GAsyncStopEnumeratingCallback callback,
gpointer data,
GDestroyNotify notify);
static void g_file_enumerator_real_cancel (GFileEnumerator *enumerator);
static void
g_file_enumerator_finalize (GObject *object)
{
GFileEnumerator *enumerator;
enumerator = G_FILE_ENUMERATOR (object);
if (!enumerator->priv->stopped)
g_file_enumerator_stop (enumerator, NULL);
if (enumerator->priv->context)
{
g_main_context_unref (enumerator->priv->context);
enumerator->priv->context = NULL;
}
if (G_OBJECT_CLASS (g_file_enumerator_parent_class)->finalize)
(*G_OBJECT_CLASS (g_file_enumerator_parent_class)->finalize) (object);
}
static void
g_file_enumerator_class_init (GFileEnumeratorClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (GFileEnumeratorPrivate));
gobject_class->finalize = g_file_enumerator_finalize;
klass->next_files_async = g_file_enumerator_real_next_files_async;
klass->stop_async = g_file_enumerator_real_stop_async;
klass->cancel = g_file_enumerator_real_cancel;
}
static void
g_file_enumerator_init (GFileEnumerator *enumerator)
{
enumerator->priv = G_TYPE_INSTANCE_GET_PRIVATE (enumerator,
G_TYPE_FILE_ENUMERATOR,
GFileEnumeratorPrivate);
}
/**
* g_file_enumerator_next_file:
* @enumerator: a #GFileEnumerator.
* @error: location to store the error occuring, or %NULL to ignore
*
* Returns information for the next file in the enumerated object.
* Will block until the information is availible.
*
* On error, returns %NULL and sets @error to the error. If the
* enumerator is at the end, %NULL will be returned and @error will
* be unset.
*
* Return value: A GFileInfo or %NULL on error or end of enumerator
**/
GFileInfo *
g_file_enumerator_next_file (GFileEnumerator *enumerator,
GError **error)
{
GFileEnumeratorClass *class;
GFileInfo *info;
g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
g_return_val_if_fail (enumerator != NULL, NULL);
if (enumerator->priv->stopped)
{
g_set_error (error, G_VFS_ERROR, G_VFS_ERROR_CLOSED,
_("Enumerator is stopped"));
return NULL;
}
if (enumerator->priv->pending)
{
g_set_error (error, G_VFS_ERROR, G_VFS_ERROR_PENDING,
_("File enumerator has outstanding operation"));
return NULL;
}
if (enumerator->priv->outstanding_error)
{
g_propagate_error (error, enumerator->priv->outstanding_error);
enumerator->priv->outstanding_error = NULL;
return NULL;
}
class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
enumerator->priv->pending = TRUE;
info = (* class->next_file) (enumerator, error);
enumerator->priv->pending = FALSE;
return info;
}
/**
* g_file_enumerator_stop:
* @enumerator: a #GFileEnumerator.
*
* Releases all resources used by this enumerator, making the
* enumerator return %G_VFS_ERROR_CLOSED on all calls.
*
* This will be automatically called when the last reference
* is dropped, but you might want to call make sure resources
* are released as early as possible.
**/
gboolean
g_file_enumerator_stop (GFileEnumerator *enumerator,
GError **error)
{
GFileEnumeratorClass *class;
g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), FALSE);
g_return_val_if_fail (enumerator != NULL, FALSE);
class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
if (enumerator->priv->stopped)
return TRUE;
if (enumerator->priv->pending)
{
g_set_error (error, G_VFS_ERROR, G_VFS_ERROR_PENDING,
_("File enumerator has outstanding operation"));
return FALSE;
}
enumerator->priv->pending = TRUE;
(* class->stop) (enumerator, error);
enumerator->priv->pending = FALSE;
enumerator->priv->stopped = TRUE;
return TRUE;
}
/**
* g_file_enumerator_set_async_context:
* @enumerator: A #GFileEnumerator.
* @context: a #GMainContext (if %NULL, the default context will be used)
*
* Set the mainloop @context to be used for asynchronous i/o.
* If not set, or if set to %NULL the default context will be used.
**/
void
g_file_enumerator_set_async_context (GFileEnumerator *enumerator,
GMainContext *context)
{
g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
g_return_if_fail (enumerator != NULL);
if (enumerator->priv->context)
g_main_context_unref (enumerator->priv->context);
enumerator->priv->context = context;
if (context)
g_main_context_ref (context);
}
/**
* g_file_enumerator_get_async_context:
* @enumerator: A #GFileEnumerator.
*
* Returns the mainloop used for async operation on this enumerator.
* If you implement a enumerator you have to look at this to know what
* context to use for async i/o.
*
* The context is set by the user by calling g_file_enumerator_set_async_context().
*
* Return value: A #GMainContext
**/
GMainContext *
g_file_enumerator_get_async_context (GFileEnumerator *enumerator)
{
g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
g_return_val_if_fail (enumerator != NULL, NULL);
if (enumerator->priv->context == NULL)
{
enumerator->priv->context = g_main_context_default ();
g_main_context_ref (enumerator->priv->context);
}
return enumerator->priv->context;
}
typedef struct {
GAsyncResult generic;
int num_files;
GAsyncNextFilesCallback callback;
} NextAsyncResult;
static gboolean
call_next_async_result (gpointer data)
{
NextAsyncResult *res = data;
if (res->callback)
res->callback (res->generic.async_object,
NULL,
res->num_files,
res->generic.data,
res->generic.error);
return FALSE;
}
static void
queue_next_async_result (GFileEnumerator *enumerator,
int num_files,
GError *error,
GAsyncNextFilesCallback callback,
gpointer data,
GDestroyNotify notify)
{
NextAsyncResult *res;
res = g_new0 (NextAsyncResult, 1);
res->num_files = num_files;
res->callback = callback;
_g_queue_async_result ((GAsyncResult *)res, enumerator,
error, data, notify,
g_file_enumerator_get_async_context (enumerator),
call_next_async_result);
}
static void
next_async_callback_wrapper (GFileEnumerator *enumerator,
GList *files,
int num_files,
gpointer data,
GError *error)
{
GAsyncNextFilesCallback real_callback = enumerator->priv->outstanding_callback;
enumerator->priv->pending = FALSE;
(*real_callback) (enumerator, files, num_files, data, error);
g_object_unref (enumerator);
}
/**
* g_file_enumerator_next_files_async:
* @enumerator: a #GFileEnumerator.
* @num_file: the number of file info objects to request
* @io_priority: the io priority of the request
* @callback: callback to call when the request is satisfied
* @data: the data to pass to callback function
* @notify: a function to call when @data is no longer in use, or %NULL.
*
* Request information for a number of files from the enumerator asynchronously.
* When all i/o for the operation is finished the @callback will be called with
* the requested information.
*
* The callback can be called with less than @num_files files in case of error
* or at the end of the enumerator. In case of a partial error the callback will
* be called with any succeeding items and no error, and on the next request the
* error will be reported. If a request is cancelled the callback will be called
* with %G_VFS_ERROR_CANCELLED.
*
* During an async request no other sync and async calls are allowed, and will
* result in %G_VFS_ERROR_PENDING errors.
*
* Any outstanding i/o request with higher priority (lower numerical value) will
* be executed before an outstanding request with lower priority. Default
* priority is %G_PRIORITY_DEFAULT.
**/
void
g_file_enumerator_next_files_async (GFileEnumerator *enumerator,
int num_files,
int io_priority,
GAsyncNextFilesCallback callback,
gpointer data,
GDestroyNotify notify)
{
GFileEnumeratorClass *class;
GError *error;
g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
g_return_if_fail (enumerator != NULL);
g_return_if_fail (num_files < 0);
enumerator->priv->cancelled = FALSE;
if (num_files == 0)
{
queue_next_async_result (enumerator, 0, NULL, callback, data, notify);
return;
}
if (enumerator->priv->stopped)
{
error = NULL;
g_set_error (&error, G_VFS_ERROR, G_VFS_ERROR_CLOSED,
_("File enumerator is already closed"));
queue_next_async_result (enumerator, -1, error,
callback, data, notify);
return;
}
if (enumerator->priv->pending)
{
error = NULL;
g_set_error (&error, G_VFS_ERROR, G_VFS_ERROR_PENDING,
_("File enumerator has outstanding operation"));
queue_next_async_result (enumerator, -1, error,
callback, data, notify);
return;
}
class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
enumerator->priv->pending = TRUE;
enumerator->priv->outstanding_callback = callback;
g_object_ref (enumerator);
(* class->next_files_async) (enumerator, num_files, io_priority,
next_async_callback_wrapper, data, notify);
}
typedef struct {
GAsyncResult generic;
gboolean result;
GAsyncStopEnumeratingCallback callback;
} StopAsyncResult;
static gboolean
call_stop_async_result (gpointer data)
{
StopAsyncResult *res = data;
if (res->callback)
res->callback (res->generic.async_object,
res->result,
res->generic.data,
res->generic.error);
return FALSE;
}
static void
queue_stop_async_result (GFileEnumerator *enumerator,
gboolean result,
GError *error,
GAsyncStopEnumeratingCallback callback,
gpointer data,
GDestroyNotify notify)
{
StopAsyncResult *res;
res = g_new0 (StopAsyncResult, 1);
res->result = result;
res->callback = callback;
_g_queue_async_result ((GAsyncResult *)res, enumerator,
error, data, notify,
g_file_enumerator_get_async_context (enumerator),
call_stop_async_result);
}
static void
stop_async_callback_wrapper (GFileEnumerator *enumerator,
gboolean result,
gpointer data,
GError *error)
{
GAsyncStopEnumeratingCallback real_callback = enumerator->priv->outstanding_callback;
enumerator->priv->pending = FALSE;
enumerator->priv->stopped = TRUE;
(*real_callback) (enumerator, result, data, error);
g_object_unref (enumerator);
}
void
g_file_enumerator_stop_async (GFileEnumerator *enumerator,
int io_priority,
GAsyncStopEnumeratingCallback callback,
gpointer data,
GDestroyNotify notify)
{
GFileEnumeratorClass *class;
GError *error;
g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
enumerator->priv->cancelled = FALSE;
if (enumerator->priv->stopped)
{
error = NULL;
g_set_error (&error, G_VFS_ERROR, G_VFS_ERROR_CLOSED,
_("File enumerator is already stopped"));
queue_stop_async_result (enumerator, FALSE, error,
callback, data, notify);
return;
}
if (enumerator->priv->pending)
{
error = NULL;
g_set_error (&error, G_VFS_ERROR, G_VFS_ERROR_PENDING,
_("File enumerator has outstanding operation"));
queue_stop_async_result (enumerator, FALSE, error,
callback, data, notify);
return;
}
class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
enumerator->priv->pending = TRUE;
enumerator->priv->outstanding_callback = callback;
g_object_ref (enumerator);
(* class->stop_async) (enumerator, io_priority,
stop_async_callback_wrapper, data, notify);
}
/**
* g_file_enumerator_cancel:
* @enumerator: a #GFileEnumerator.
* @tag: a value returned from an async request
*
* Tries to cancel an outstanding request for file information. If it
* succeeds the outstanding request callback will be called with
* %G_VFS_ERROR_CANCELLED.
*
* Generally if a request is cancelled before its callback has been
* called the cancellation will succeed and the callback will only
* be called with %G_VFS_ERROR_CANCELLED. However, this cannot be guaranteed,
* especially if multiple threads are in use, so you might get a succeeding
* callback and no %G_VFS_ERROR_CANCELLED callback even if you call cancel.
**/
void
g_file_enumerator_cancel (GFileEnumerator *enumerator)
{
GFileEnumeratorClass *class;
g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
g_return_if_fail (enumerator != NULL);
enumerator->priv->cancelled = TRUE;
class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
(* class->cancel) (enumerator);
}
gboolean
g_file_enumerator_is_cancelled (GFileEnumerator *enumerator)
{
g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), TRUE);
g_return_val_if_fail (enumerator != NULL, TRUE);
return enumerator->priv->cancelled;
}
gboolean
g_file_enumerator_is_stopped (GFileEnumerator *enumerator)
{
g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), TRUE);
g_return_val_if_fail (enumerator != NULL, TRUE);
return enumerator->priv->stopped;
}
gboolean
g_file_enumerator_has_pending (GFileEnumerator *enumerator)
{
g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), TRUE);
g_return_val_if_fail (enumerator != NULL, TRUE);
return enumerator->priv->pending;
}
void
g_file_enumerator_set_pending (GFileEnumerator *enumerator,
gboolean pending)
{
g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
g_return_if_fail (enumerator != NULL);
enumerator->priv->pending = pending;
}
typedef struct {
GFileEnumerator *enumerator;
GError *error;
gpointer data;
GDestroyNotify notify;
} FileEnumeratorOp;
static void
file_enumerator_op_free (gpointer data)
{
FileEnumeratorOp *op = data;
if (op->notify)
op->notify (op->data);
if (op->error)
g_error_free (op->error);
g_free (op);
}
static void
file_enumerator_op_cancel (gpointer data)
{
FileEnumeratorOp *op = data;
GFileEnumeratorClass *class;
class = G_FILE_ENUMERATOR_GET_CLASS (op->enumerator);
if (class->cancel_sync)
class->cancel_sync (op->enumerator);
}
typedef struct {
FileEnumeratorOp op;
int num_files;
GList *files;
GAsyncNextFilesCallback callback;
} NextAsyncOp;
static void
next_op_report (gpointer data)
{
NextAsyncOp *op = data;
op->callback (op->op.enumerator,
op->files,
op->num_files,
op->op.data,
op->op.error);
g_list_foreach (op->files, (GFunc)g_object_unref, NULL);
g_list_free (op->files);
op->files = NULL;
}
static void
next_op_func (GIOJob *job,
gpointer data)
{
NextAsyncOp *op = data;
GFileEnumeratorClass *class;
GFileInfo *info;
int i;
class = G_FILE_ENUMERATOR_GET_CLASS (op->op.enumerator);
for (i = 0; i < op->num_files; i++)
{
if (g_io_job_is_cancelled (job))
{
info = NULL;
g_set_error (&op->op.error,
G_VFS_ERROR,
G_VFS_ERROR_CANCELLED,
_("Operation was cancelled"));
}
else
info = class->next_file (op->op.enumerator, &op->op.error);
if (info == NULL)
{
/* If we get an error after first file, return that on next operation */
if (op->op.error != NULL && i > 0)
{
op->op.enumerator->priv->outstanding_error = op->op.error;
op->op.error = NULL;
}
break;
}
}
if (op->op.error != NULL)
op->num_files = -1;
else
op->num_files = i;
g_io_job_mark_done (job);
g_io_job_send_to_mainloop (job, next_op_report,
op, file_enumerator_op_free,
FALSE);
}
static void
g_file_enumerator_real_next_files_async (GFileEnumerator *enumerator,
int num_files,
int io_priority,
GAsyncNextFilesCallback callback,
gpointer data,
GDestroyNotify notify)
{
NextAsyncOp *op;
op = g_new0 (NextAsyncOp, 1);
op->op.enumerator = enumerator;
op->num_files = num_files;
op->files = NULL;
op->callback = callback;
op->op.data = data;
op->op.notify = notify;
enumerator->priv->io_job_id =
g_schedule_io_job (next_op_func,
file_enumerator_op_cancel,
op,
NULL,
io_priority,
g_file_enumerator_get_async_context (enumerator));
}
typedef struct {
FileEnumeratorOp op;
gboolean result;
GAsyncStopEnumeratingCallback callback;
} StopAsyncOp;
static void
stop_op_report (gpointer data)
{
StopAsyncOp *op = data;
op->callback (op->op.enumerator,
op->result,
op->op.data,
op->op.error);
}
static void
stop_op_func (GIOJob *job,
gpointer data)
{
StopAsyncOp *op = data;
GFileEnumeratorClass *class;
if (g_io_job_is_cancelled (job))
{
op->result = FALSE;
g_set_error (&op->op.error,
G_VFS_ERROR,
G_VFS_ERROR_CANCELLED,
_("Operation was cancelled"));
}
else
{
class = G_FILE_ENUMERATOR_GET_CLASS (op->op.enumerator);
op->result = class->stop (op->op.enumerator, &op->op.error);
}
g_io_job_mark_done (job);
g_io_job_send_to_mainloop (job, stop_op_report,
op, file_enumerator_op_free,
FALSE);
}
static void
g_file_enumerator_real_stop_async (GFileEnumerator *enumerator,
int io_priority,
GAsyncStopEnumeratingCallback callback,
gpointer data,
GDestroyNotify notify)
{
StopAsyncOp *op;
op = g_new0 (StopAsyncOp, 1);
op->op.enumerator = enumerator;
op->callback = callback;
op->op.data = data;
op->op.notify = notify;
enumerator->priv->io_job_id =
g_schedule_io_job (stop_op_func,
file_enumerator_op_cancel,
op,
NULL,
io_priority,
g_file_enumerator_get_async_context (enumerator));
}
static void
g_file_enumerator_real_cancel (GFileEnumerator *enumerator)
{
g_cancel_io_job (enumerator->priv->io_job_id);
}
|