b4f7424635cb539e7dc7ad224a2e333866671a2c
[platform/upstream/glib.git] / gio / gfileenumerator.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24 #include "gfileenumerator.h"
25 #include "gfile.h"
26 #include "gioscheduler.h"
27 #include "gasyncresult.h"
28 #include "gasynchelper.h"
29 #include "gsimpleasyncresult.h"
30 #include "gioerror.h"
31 #include "glibintl.h"
32
33
34 /**
35  * SECTION:gfileenumerator
36  * @short_description: Enumerated Files Routines
37  * @include: gio/gio.h
38  * 
39  * #GFileEnumerator allows you to operate on a set of #GFile<!-- -->s, 
40  * returning a #GFileInfo structure for each file enumerated (e.g. 
41  * g_file_enumerate_children() will return a #GFileEnumerator for each 
42  * of the children within a directory).
43  *
44  * To get the next file's information from a #GFileEnumerator, use 
45  * g_file_enumerator_next_file() or its asynchronous version, 
46  * g_file_enumerator_next_files_async(). Note that the asynchronous 
47  * version will return a list of #GFileInfo<!---->s, whereas the 
48  * synchronous will only return the next file in the enumerator.
49  *
50  * To close a #GFileEnumerator, use g_file_enumerator_close(), or 
51  * its asynchronous version, g_file_enumerator_close_async(). Once 
52  * a #GFileEnumerator is closed, no further actions may be performed 
53  * on it, and it should be freed with g_object_unref().
54  * 
55  **/ 
56
57 G_DEFINE_TYPE (GFileEnumerator, g_file_enumerator, G_TYPE_OBJECT);
58
59 struct _GFileEnumeratorPrivate {
60   /* TODO: Should be public for subclasses? */
61   GFile *container;
62   guint closed : 1;
63   guint pending : 1;
64   GAsyncReadyCallback outstanding_callback;
65   GError *outstanding_error;
66 };
67
68 enum {
69   PROP_0,
70   PROP_CONTAINER
71 };
72
73 static void     g_file_enumerator_real_next_files_async  (GFileEnumerator      *enumerator,
74                                                           int                   num_files,
75                                                           int                   io_priority,
76                                                           GCancellable         *cancellable,
77                                                           GAsyncReadyCallback   callback,
78                                                           gpointer              user_data);
79 static GList *  g_file_enumerator_real_next_files_finish (GFileEnumerator      *enumerator,
80                                                           GAsyncResult         *res,
81                                                           GError              **error);
82 static void     g_file_enumerator_real_close_async       (GFileEnumerator      *enumerator,
83                                                           int                   io_priority,
84                                                           GCancellable         *cancellable,
85                                                           GAsyncReadyCallback   callback,
86                                                           gpointer              user_data);
87 static gboolean g_file_enumerator_real_close_finish      (GFileEnumerator      *enumerator,
88                                                           GAsyncResult         *res,
89                                                           GError              **error);
90
91 static void
92 g_file_enumerator_set_property (GObject      *object,
93                                 guint         property_id,
94                                 const GValue *value,
95                                 GParamSpec   *pspec)
96 {
97   GFileEnumerator *enumerator;
98   
99   enumerator = G_FILE_ENUMERATOR (object);
100   
101   switch (property_id) {
102   case PROP_CONTAINER:
103     enumerator->priv->container = g_value_dup_object (value);
104     break;
105   default:
106     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
107     break;
108   }
109 }
110
111 static void
112 g_file_enumerator_dispose (GObject *object)
113 {
114   GFileEnumerator *enumerator;
115
116   enumerator = G_FILE_ENUMERATOR (object);
117   
118   if (enumerator->priv->container) {
119     g_object_unref (enumerator->priv->container);
120     enumerator->priv->container = NULL;
121   }
122
123   G_OBJECT_CLASS (g_file_enumerator_parent_class)->dispose (object);
124 }
125
126 static void
127 g_file_enumerator_finalize (GObject *object)
128 {
129   GFileEnumerator *enumerator;
130
131   enumerator = G_FILE_ENUMERATOR (object);
132   
133   if (!enumerator->priv->closed)
134     g_file_enumerator_close (enumerator, NULL, NULL);
135
136   G_OBJECT_CLASS (g_file_enumerator_parent_class)->finalize (object);
137 }
138
139 static void
140 g_file_enumerator_class_init (GFileEnumeratorClass *klass)
141 {
142   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
143   
144   g_type_class_add_private (klass, sizeof (GFileEnumeratorPrivate));
145   
146   gobject_class->set_property = g_file_enumerator_set_property;
147   gobject_class->dispose = g_file_enumerator_dispose;
148   gobject_class->finalize = g_file_enumerator_finalize;
149
150   klass->next_files_async = g_file_enumerator_real_next_files_async;
151   klass->next_files_finish = g_file_enumerator_real_next_files_finish;
152   klass->close_async = g_file_enumerator_real_close_async;
153   klass->close_finish = g_file_enumerator_real_close_finish;
154
155   g_object_class_install_property
156     (gobject_class, PROP_CONTAINER,
157      g_param_spec_object ("container", P_("Container"),
158                           P_("The container that is being enumerated"),
159                           G_TYPE_FILE,
160                           G_PARAM_WRITABLE |
161                           G_PARAM_CONSTRUCT_ONLY |
162                           G_PARAM_STATIC_STRINGS));
163 }
164
165 static void
166 g_file_enumerator_init (GFileEnumerator *enumerator)
167 {
168   enumerator->priv = G_TYPE_INSTANCE_GET_PRIVATE (enumerator,
169                                                   G_TYPE_FILE_ENUMERATOR,
170                                                   GFileEnumeratorPrivate);
171 }
172
173 /**
174  * g_file_enumerator_next_file:
175  * @enumerator: a #GFileEnumerator.
176  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
177  * @error: location to store the error occurring, or %NULL to ignore
178  *
179  * Returns information for the next file in the enumerated object.
180  * Will block until the information is available. The #GFileInfo 
181  * returned from this function will contain attributes that match the 
182  * attribute string that was passed when the #GFileEnumerator was created.
183  *
184  * On error, returns %NULL and sets @error to the error. If the
185  * enumerator is at the end, %NULL will be returned and @error will
186  * be unset.
187  *
188  * Return value: (transfer full): A #GFileInfo or %NULL on error or end of enumerator.
189  *    Free the returned object with g_object_unref() when no longer needed.
190  **/
191 GFileInfo *
192 g_file_enumerator_next_file (GFileEnumerator *enumerator,
193                              GCancellable *cancellable,
194                              GError **error)
195 {
196   GFileEnumeratorClass *class;
197   GFileInfo *info;
198   
199   g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
200   g_return_val_if_fail (enumerator != NULL, NULL);
201   
202   if (enumerator->priv->closed)
203     {
204       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
205                            _("Enumerator is closed"));
206       return NULL;
207     }
208
209   if (enumerator->priv->pending)
210     {
211       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
212                            _("File enumerator has outstanding operation"));
213       return NULL;
214     }
215
216   if (enumerator->priv->outstanding_error)
217     {
218       g_propagate_error (error, enumerator->priv->outstanding_error);
219       enumerator->priv->outstanding_error = NULL;
220       return NULL;
221     }
222   
223   class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
224
225   if (cancellable)
226     g_cancellable_push_current (cancellable);
227   
228   enumerator->priv->pending = TRUE;
229   info = (* class->next_file) (enumerator, cancellable, error);
230   enumerator->priv->pending = FALSE;
231
232   if (cancellable)
233     g_cancellable_pop_current (cancellable);
234   
235   return info;
236 }
237   
238 /**
239  * g_file_enumerator_close:
240  * @enumerator: a #GFileEnumerator.
241  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. 
242  * @error: location to store the error occurring, or %NULL to ignore
243  *
244  * Releases all resources used by this enumerator, making the
245  * enumerator return %G_IO_ERROR_CLOSED on all calls.
246  *
247  * This will be automatically called when the last reference
248  * is dropped, but you might want to call this function to make 
249  * sure resources are released as early as possible.
250  *
251  * Return value: #TRUE on success or #FALSE on error.
252  **/
253 gboolean
254 g_file_enumerator_close (GFileEnumerator  *enumerator,
255                          GCancellable     *cancellable,
256                          GError          **error)
257 {
258   GFileEnumeratorClass *class;
259
260   g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), FALSE);
261   g_return_val_if_fail (enumerator != NULL, FALSE);
262   
263   class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
264
265   if (enumerator->priv->closed)
266     return TRUE;
267   
268   if (enumerator->priv->pending)
269     {
270       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
271                            _("File enumerator has outstanding operation"));
272       return FALSE;
273     }
274
275   if (cancellable)
276     g_cancellable_push_current (cancellable);
277   
278   enumerator->priv->pending = TRUE;
279   (* class->close_fn) (enumerator, cancellable, error);
280   enumerator->priv->pending = FALSE;
281   enumerator->priv->closed = TRUE;
282
283   if (cancellable)
284     g_cancellable_pop_current (cancellable);
285   
286   return TRUE;
287 }
288
289 static void
290 next_async_callback_wrapper (GObject      *source_object,
291                              GAsyncResult *res,
292                              gpointer      user_data)
293 {
294   GFileEnumerator *enumerator = G_FILE_ENUMERATOR (source_object);
295
296   enumerator->priv->pending = FALSE;
297   if (enumerator->priv->outstanding_callback)
298     (*enumerator->priv->outstanding_callback) (source_object, res, user_data);
299   g_object_unref (enumerator);
300 }
301
302 /**
303  * g_file_enumerator_next_files_async:
304  * @enumerator: a #GFileEnumerator.
305  * @num_files: the number of file info objects to request
306  * @io_priority: the <link linkend="gioscheduler">io priority</link> 
307  *     of the request. 
308  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
309  * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
310  * @user_data: (closure): the data to pass to callback function
311  *
312  * Request information for a number of files from the enumerator asynchronously.
313  * When all i/o for the operation is finished the @callback will be called with
314  * the requested information. 
315  *
316  * The callback can be called with less than @num_files files in case of error
317  * or at the end of the enumerator. In case of a partial error the callback will
318  * be called with any succeeding items and no error, and on the next request the
319  * error will be reported. If a request is cancelled the callback will be called
320  * with %G_IO_ERROR_CANCELLED.
321  *
322  * During an async request no other sync and async calls are allowed, and will
323  * result in %G_IO_ERROR_PENDING errors. 
324  *
325  * Any outstanding i/o request with higher priority (lower numerical value) will
326  * be executed before an outstanding request with lower priority. Default
327  * priority is %G_PRIORITY_DEFAULT.
328  **/
329 void
330 g_file_enumerator_next_files_async (GFileEnumerator     *enumerator,
331                                     int                  num_files,
332                                     int                  io_priority,
333                                     GCancellable        *cancellable,
334                                     GAsyncReadyCallback  callback,
335                                     gpointer             user_data)
336 {
337   GFileEnumeratorClass *class;
338   GSimpleAsyncResult *simple;
339
340   g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
341   g_return_if_fail (enumerator != NULL);
342   g_return_if_fail (num_files >= 0);
343
344   if (num_files == 0)
345     {
346       simple = g_simple_async_result_new (G_OBJECT (enumerator),
347                                           callback,
348                                           user_data,
349                                           g_file_enumerator_next_files_async);
350       g_simple_async_result_complete_in_idle (simple);
351       g_object_unref (simple);
352       return;
353     }
354   
355   if (enumerator->priv->closed)
356     {
357       g_simple_async_report_error_in_idle (G_OBJECT (enumerator),
358                                            callback,
359                                            user_data,
360                                            G_IO_ERROR, G_IO_ERROR_CLOSED,
361                                            _("File enumerator is already closed"));
362       return;
363     }
364   
365   if (enumerator->priv->pending)
366     {
367       g_simple_async_report_error_in_idle (G_OBJECT (enumerator),
368                                            callback,
369                                            user_data,
370                                            G_IO_ERROR, G_IO_ERROR_PENDING,
371                                            _("File enumerator has outstanding operation"));
372       return;
373     }
374
375   class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
376   
377   enumerator->priv->pending = TRUE;
378   enumerator->priv->outstanding_callback = callback;
379   g_object_ref (enumerator);
380   (* class->next_files_async) (enumerator, num_files, io_priority, cancellable, 
381                                next_async_callback_wrapper, user_data);
382 }
383
384 /**
385  * g_file_enumerator_next_files_finish:
386  * @enumerator: a #GFileEnumerator.
387  * @result: a #GAsyncResult.
388  * @error: a #GError location to store the error occurring, or %NULL to 
389  * ignore.
390  * 
391  * Finishes the asynchronous operation started with g_file_enumerator_next_files_async().
392  * 
393  * Returns: (transfer full) (element-type Gio.FileInfo): a #GList of #GFileInfo<!---->s. You must free the list with 
394  *     g_list_free() and unref the infos with g_object_unref() when you're 
395  *     done with them.
396  **/
397 GList *
398 g_file_enumerator_next_files_finish (GFileEnumerator  *enumerator,
399                                      GAsyncResult     *result,
400                                      GError          **error)
401 {
402   GFileEnumeratorClass *class;
403   GSimpleAsyncResult *simple;
404   
405   g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
406   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
407   
408   if (g_async_result_legacy_propagate_error (result, error))
409     return NULL;
410
411   if (G_IS_SIMPLE_ASYNC_RESULT (result))
412     {
413       simple = G_SIMPLE_ASYNC_RESULT (result);
414       
415       /* Special case read of 0 files */
416       if (g_simple_async_result_get_source_tag (simple) == g_file_enumerator_next_files_async)
417         return NULL;
418     }
419   
420   class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
421   return class->next_files_finish (enumerator, result, error);
422 }
423
424 static void
425 close_async_callback_wrapper (GObject      *source_object,
426                               GAsyncResult *res,
427                               gpointer      user_data)
428 {
429   GFileEnumerator *enumerator = G_FILE_ENUMERATOR (source_object);
430   
431   enumerator->priv->pending = FALSE;
432   enumerator->priv->closed = TRUE;
433   if (enumerator->priv->outstanding_callback)
434     (*enumerator->priv->outstanding_callback) (source_object, res, user_data);
435   g_object_unref (enumerator);
436 }
437
438 /**
439  * g_file_enumerator_close_async:
440  * @enumerator: a #GFileEnumerator.
441  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
442  *     of the request.
443  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. 
444  * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
445  * @user_data: (closure): the data to pass to callback function
446  *
447  * Asynchronously closes the file enumerator. 
448  *
449  * If @cancellable is not %NULL, then the operation can be cancelled by
450  * triggering the cancellable object from another thread. If the operation
451  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned in 
452  * g_file_enumerator_close_finish(). 
453  **/
454 void
455 g_file_enumerator_close_async (GFileEnumerator     *enumerator,
456                                int                  io_priority,
457                                GCancellable        *cancellable,
458                                GAsyncReadyCallback  callback,
459                                gpointer             user_data)
460 {
461   GFileEnumeratorClass *class;
462
463   g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
464
465   if (enumerator->priv->closed)
466     {
467       g_simple_async_report_error_in_idle (G_OBJECT (enumerator),
468                                            callback,
469                                            user_data,
470                                            G_IO_ERROR, G_IO_ERROR_CLOSED,
471                                            _("File enumerator is already closed"));
472       return;
473     }
474   
475   if (enumerator->priv->pending)
476     {
477       g_simple_async_report_error_in_idle (G_OBJECT (enumerator),
478                                            callback,
479                                            user_data,
480                                            G_IO_ERROR, G_IO_ERROR_PENDING,
481                                            _("File enumerator has outstanding operation"));
482       return;
483     }
484
485   class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
486   
487   enumerator->priv->pending = TRUE;
488   enumerator->priv->outstanding_callback = callback;
489   g_object_ref (enumerator);
490   (* class->close_async) (enumerator, io_priority, cancellable,
491                           close_async_callback_wrapper, user_data);
492 }
493
494 /**
495  * g_file_enumerator_close_finish:
496  * @enumerator: a #GFileEnumerator.
497  * @result: a #GAsyncResult.
498  * @error: a #GError location to store the error occurring, or %NULL to 
499  * ignore.
500  * 
501  * Finishes closing a file enumerator, started from g_file_enumerator_close_async().
502  * 
503  * If the file enumerator was already closed when g_file_enumerator_close_async() 
504  * was called, then this function will report %G_IO_ERROR_CLOSED in @error, and 
505  * return %FALSE. If the file enumerator had pending operation when the close 
506  * operation was started, then this function will report %G_IO_ERROR_PENDING, and
507  * return %FALSE.  If @cancellable was not %NULL, then the operation may have been 
508  * cancelled by triggering the cancellable object from another thread. If the operation
509  * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %FALSE will be 
510  * returned. 
511  * 
512  * Returns: %TRUE if the close operation has finished successfully.
513  **/
514 gboolean
515 g_file_enumerator_close_finish (GFileEnumerator  *enumerator,
516                                 GAsyncResult     *result,
517                                 GError          **error)
518 {
519   GFileEnumeratorClass *class;
520
521   g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), FALSE);
522   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
523   
524   if (g_async_result_legacy_propagate_error (result, error))
525     return FALSE;
526
527   class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
528   return class->close_finish (enumerator, result, error);
529 }
530
531 /**
532  * g_file_enumerator_is_closed:
533  * @enumerator: a #GFileEnumerator.
534  *
535  * Checks if the file enumerator has been closed.
536  * 
537  * Returns: %TRUE if the @enumerator is closed.
538  **/
539 gboolean
540 g_file_enumerator_is_closed (GFileEnumerator *enumerator)
541 {
542   g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), TRUE);
543   
544   return enumerator->priv->closed;
545 }
546
547 /**
548  * g_file_enumerator_has_pending:
549  * @enumerator: a #GFileEnumerator.
550  * 
551  * Checks if the file enumerator has pending operations.
552  *
553  * Returns: %TRUE if the @enumerator has pending operations.
554  **/
555 gboolean
556 g_file_enumerator_has_pending (GFileEnumerator *enumerator)
557 {
558   g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), TRUE);
559   
560   return enumerator->priv->pending;
561 }
562
563 /**
564  * g_file_enumerator_set_pending:
565  * @enumerator: a #GFileEnumerator.
566  * @pending: a boolean value.
567  * 
568  * Sets the file enumerator as having pending operations.
569  **/
570 void
571 g_file_enumerator_set_pending (GFileEnumerator *enumerator,
572                                gboolean         pending)
573 {
574   g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
575   
576   enumerator->priv->pending = pending;
577 }
578
579 /**
580  * g_file_enumerator_get_container:
581  * @enumerator: a #GFileEnumerator
582  *
583  * Get the #GFile container which is being enumerated.
584  *
585  * Returns: (transfer none): the #GFile which is being enumerated.
586  *
587  * Since: 2.18
588  */
589 GFile *
590 g_file_enumerator_get_container (GFileEnumerator *enumerator)
591 {
592   g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
593
594   return enumerator->priv->container;
595 }
596
597 typedef struct {
598   int                num_files;
599   GList             *files;
600 } NextAsyncOp;
601
602 static void
603 next_async_op_free (NextAsyncOp *op)
604 {
605   /* Free the list, if finish wasn't called */
606   g_list_free_full (op->files, g_object_unref);
607   
608   g_free (op);
609 }
610                     
611
612
613 static void
614 next_files_thread (GSimpleAsyncResult *res,
615                    GObject            *object,
616                    GCancellable       *cancellable)
617 {
618   NextAsyncOp *op;
619   GFileEnumeratorClass *class;
620   GError *error = NULL;
621   GFileInfo *info;
622   GFileEnumerator *enumerator;
623   int i;
624
625   enumerator = G_FILE_ENUMERATOR (object);
626   op = g_simple_async_result_get_op_res_gpointer (res);
627
628   class = G_FILE_ENUMERATOR_GET_CLASS (object);
629
630   for (i = 0; i < op->num_files; i++)
631     {
632       if (g_cancellable_set_error_if_cancelled (cancellable, &error))
633         info = NULL;
634       else
635         info = class->next_file (enumerator, cancellable, &error);
636       
637       if (info == NULL)
638         {
639           /* If we get an error after first file, return that on next operation */
640           if (error != NULL && i > 0)
641             {
642               if (error->domain == G_IO_ERROR &&
643                   error->code == G_IO_ERROR_CANCELLED)
644                 g_error_free (error); /* Never propagate cancel errors to other call */
645               else
646                 enumerator->priv->outstanding_error = error;
647               error = NULL;
648             }
649               
650           break;
651         }
652       else
653         op->files = g_list_prepend (op->files, info);
654     }
655 }
656
657 static void
658 g_file_enumerator_real_next_files_async (GFileEnumerator     *enumerator,
659                                          int                  num_files,
660                                          int                  io_priority,
661                                          GCancellable        *cancellable,
662                                          GAsyncReadyCallback  callback,
663                                          gpointer             user_data)
664 {
665   GSimpleAsyncResult *res;
666   NextAsyncOp *op;
667
668   op = g_new0 (NextAsyncOp, 1);
669
670   op->num_files = num_files;
671   op->files = NULL;
672
673   res = g_simple_async_result_new (G_OBJECT (enumerator), callback, user_data, g_file_enumerator_real_next_files_async);
674   g_simple_async_result_set_op_res_gpointer (res, op, (GDestroyNotify) next_async_op_free);
675   
676   g_simple_async_result_run_in_thread (res, next_files_thread, io_priority, cancellable);
677   g_object_unref (res);
678 }
679
680 static GList *
681 g_file_enumerator_real_next_files_finish (GFileEnumerator                *enumerator,
682                                           GAsyncResult                   *result,
683                                           GError                        **error)
684 {
685   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
686   NextAsyncOp *op;
687   GList *res;
688
689   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == 
690             g_file_enumerator_real_next_files_async);
691
692   if (g_simple_async_result_propagate_error (simple, error))
693     return NULL;
694
695   op = g_simple_async_result_get_op_res_gpointer (simple);
696
697   res = op->files;
698   op->files = NULL;
699   return res;
700 }
701
702 static void
703 close_async_thread (GSimpleAsyncResult *res,
704                     GObject            *object,
705                     GCancellable       *cancellable)
706 {
707   GFileEnumeratorClass *class;
708   GError *error = NULL;
709   gboolean result;
710
711   /* Auto handling of cancelation disabled, and ignore
712      cancellation, since we want to close things anyway, although
713      possibly in a quick-n-dirty way. At least we never want to leak
714      open handles */
715   
716   class = G_FILE_ENUMERATOR_GET_CLASS (object);
717   result = class->close_fn (G_FILE_ENUMERATOR (object), cancellable, &error);
718   if (!result)
719     g_simple_async_result_take_error (res, error);
720 }
721
722
723 static void
724 g_file_enumerator_real_close_async (GFileEnumerator     *enumerator,
725                                     int                  io_priority,
726                                     GCancellable        *cancellable,
727                                     GAsyncReadyCallback  callback,
728                                     gpointer             user_data)
729 {
730   GSimpleAsyncResult *res;
731   
732   res = g_simple_async_result_new (G_OBJECT (enumerator),
733                                    callback,
734                                    user_data,
735                                    g_file_enumerator_real_close_async);
736
737   g_simple_async_result_set_handle_cancellation (res, FALSE);
738   
739   g_simple_async_result_run_in_thread (res,
740                                        close_async_thread,
741                                        io_priority,
742                                        cancellable);
743   g_object_unref (res);
744 }
745
746 static gboolean
747 g_file_enumerator_real_close_finish (GFileEnumerator  *enumerator,
748                                      GAsyncResult     *result,
749                                      GError          **error)
750 {
751   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
752
753   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == 
754             g_file_enumerator_real_close_async);
755
756   if (g_simple_async_result_propagate_error (simple, error))
757     return FALSE;
758
759   return TRUE;
760 }