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