1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "gfileenumerator.h"
25 #include "gioscheduler.h"
26 #include "gasynchelper.h"
27 #include "gsimpleasyncresult.h"
33 * SECTION:gfileenumerator
34 * @short_description: Enumerated Files Routines
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).
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.
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().
55 G_DEFINE_TYPE (GFileEnumerator, g_file_enumerator, G_TYPE_OBJECT);
57 struct _GFileEnumeratorPrivate {
58 /* TODO: Should be public for subclasses? */
62 GAsyncReadyCallback outstanding_callback;
63 GError *outstanding_error;
71 static void g_file_enumerator_real_next_files_async (GFileEnumerator *enumerator,
74 GCancellable *cancellable,
75 GAsyncReadyCallback callback,
77 static GList * g_file_enumerator_real_next_files_finish (GFileEnumerator *enumerator,
80 static void g_file_enumerator_real_close_async (GFileEnumerator *enumerator,
82 GCancellable *cancellable,
83 GAsyncReadyCallback callback,
85 static gboolean g_file_enumerator_real_close_finish (GFileEnumerator *enumerator,
90 g_file_enumerator_set_property (GObject *object,
95 GFileEnumerator *enumerator;
97 enumerator = G_FILE_ENUMERATOR (object);
99 switch (property_id) {
101 enumerator->priv->container = g_value_dup_object (value);
104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
110 g_file_enumerator_dispose (GObject *object)
112 GFileEnumerator *enumerator;
114 enumerator = G_FILE_ENUMERATOR (object);
116 if (enumerator->priv->container) {
117 g_object_unref (enumerator->priv->container);
118 enumerator->priv->container = NULL;
121 if (G_OBJECT_CLASS (g_file_enumerator_parent_class)->dispose)
122 (*G_OBJECT_CLASS (g_file_enumerator_parent_class)->dispose) (object);
126 g_file_enumerator_finalize (GObject *object)
128 GFileEnumerator *enumerator;
130 enumerator = G_FILE_ENUMERATOR (object);
132 if (!enumerator->priv->closed)
133 g_file_enumerator_close (enumerator, NULL, NULL);
135 if (G_OBJECT_CLASS (g_file_enumerator_parent_class)->finalize)
136 (*G_OBJECT_CLASS (g_file_enumerator_parent_class)->finalize) (object);
140 g_file_enumerator_class_init (GFileEnumeratorClass *klass)
142 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
144 g_type_class_add_private (klass, sizeof (GFileEnumeratorPrivate));
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;
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;
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"),
161 G_PARAM_CONSTRUCT_ONLY |
162 G_PARAM_STATIC_STRINGS));
166 g_file_enumerator_init (GFileEnumerator *enumerator)
168 enumerator->priv = G_TYPE_INSTANCE_GET_PRIVATE (enumerator,
169 G_TYPE_FILE_ENUMERATOR,
170 GFileEnumeratorPrivate);
174 * g_file_enumerator_next_file:
175 * @enumerator: a #GFileEnumerator.
176 * @cancellable: optional #GCancellable object, %NULL to ignore.
177 * @error: location to store the error occuring, or %NULL to ignore
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.
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
188 * Return value: A #GFileInfo or %NULL on error or end of enumerator
191 g_file_enumerator_next_file (GFileEnumerator *enumerator,
192 GCancellable *cancellable,
195 GFileEnumeratorClass *class;
198 g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
199 g_return_val_if_fail (enumerator != NULL, NULL);
201 if (enumerator->priv->closed)
203 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
204 _("Enumerator is closed"));
208 if (enumerator->priv->pending)
210 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
211 _("File enumerator has outstanding operation"));
215 if (enumerator->priv->outstanding_error)
217 g_propagate_error (error, enumerator->priv->outstanding_error);
218 enumerator->priv->outstanding_error = NULL;
222 class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
225 g_cancellable_push_current (cancellable);
227 enumerator->priv->pending = TRUE;
228 info = (* class->next_file) (enumerator, cancellable, error);
229 enumerator->priv->pending = FALSE;
232 g_cancellable_pop_current (cancellable);
238 * g_file_enumerator_close:
239 * @enumerator: a #GFileEnumerator.
240 * @cancellable: optional #GCancellable object, %NULL to ignore.
241 * @error: location to store the error occuring, or %NULL to ignore
243 * Releases all resources used by this enumerator, making the
244 * enumerator return %G_IO_ERROR_CLOSED on all calls.
246 * This will be automatically called when the last reference
247 * is dropped, but you might want to call this function to make
248 * sure resources are released as early as possible.
250 * Return value: #TRUE on success or #FALSE on error.
253 g_file_enumerator_close (GFileEnumerator *enumerator,
254 GCancellable *cancellable,
257 GFileEnumeratorClass *class;
259 g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), FALSE);
260 g_return_val_if_fail (enumerator != NULL, FALSE);
262 class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
264 if (enumerator->priv->closed)
267 if (enumerator->priv->pending)
269 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
270 _("File enumerator has outstanding operation"));
275 g_cancellable_push_current (cancellable);
277 enumerator->priv->pending = TRUE;
278 (* class->close_fn) (enumerator, cancellable, error);
279 enumerator->priv->pending = FALSE;
280 enumerator->priv->closed = TRUE;
283 g_cancellable_pop_current (cancellable);
289 next_async_callback_wrapper (GObject *source_object,
293 GFileEnumerator *enumerator = G_FILE_ENUMERATOR (source_object);
295 enumerator->priv->pending = FALSE;
296 if (enumerator->priv->outstanding_callback)
297 (*enumerator->priv->outstanding_callback) (source_object, res, user_data);
298 g_object_unref (enumerator);
302 * g_file_enumerator_next_files_async:
303 * @enumerator: a #GFileEnumerator.
304 * @num_files: the number of file info objects to request
305 * @io_priority: the <link linkend="gioscheduler">io priority</link>
307 * @cancellable: optional #GCancellable object, %NULL to ignore.
308 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
309 * @user_data: the data to pass to callback function
311 * Request information for a number of files from the enumerator asynchronously.
312 * When all i/o for the operation is finished the @callback will be called with
313 * the requested information.
315 * The callback can be called with less than @num_files files in case of error
316 * or at the end of the enumerator. In case of a partial error the callback will
317 * be called with any succeeding items and no error, and on the next request the
318 * error will be reported. If a request is cancelled the callback will be called
319 * with %G_IO_ERROR_CANCELLED.
321 * During an async request no other sync and async calls are allowed, and will
322 * result in %G_IO_ERROR_PENDING errors.
324 * Any outstanding i/o request with higher priority (lower numerical value) will
325 * be executed before an outstanding request with lower priority. Default
326 * priority is %G_PRIORITY_DEFAULT.
329 g_file_enumerator_next_files_async (GFileEnumerator *enumerator,
332 GCancellable *cancellable,
333 GAsyncReadyCallback callback,
336 GFileEnumeratorClass *class;
337 GSimpleAsyncResult *simple;
339 g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
340 g_return_if_fail (enumerator != NULL);
341 g_return_if_fail (num_files >= 0);
345 simple = g_simple_async_result_new (G_OBJECT (enumerator),
348 g_file_enumerator_next_files_async);
349 g_simple_async_result_complete_in_idle (simple);
350 g_object_unref (simple);
354 if (enumerator->priv->closed)
356 g_simple_async_report_error_in_idle (G_OBJECT (enumerator),
359 G_IO_ERROR, G_IO_ERROR_CLOSED,
360 _("File enumerator is already closed"));
364 if (enumerator->priv->pending)
366 g_simple_async_report_error_in_idle (G_OBJECT (enumerator),
369 G_IO_ERROR, G_IO_ERROR_PENDING,
370 _("File enumerator has outstanding operation"));
374 class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
376 enumerator->priv->pending = TRUE;
377 enumerator->priv->outstanding_callback = callback;
378 g_object_ref (enumerator);
379 (* class->next_files_async) (enumerator, num_files, io_priority, cancellable,
380 next_async_callback_wrapper, user_data);
384 * g_file_enumerator_next_files_finish:
385 * @enumerator: a #GFileEnumerator.
386 * @result: a #GAsyncResult.
387 * @error: a #GError location to store the error occuring, or %NULL to
390 * Finishes the asynchronous operation started with g_file_enumerator_next_files_async().
392 * Returns: a #GList of #GFileInfo<!---->s. You must free the list with
393 * g_list_free() and unref the infos with g_object_unref when you're
397 g_file_enumerator_next_files_finish (GFileEnumerator *enumerator,
398 GAsyncResult *result,
401 GFileEnumeratorClass *class;
402 GSimpleAsyncResult *simple;
404 g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
405 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
407 if (G_IS_SIMPLE_ASYNC_RESULT (result))
409 simple = G_SIMPLE_ASYNC_RESULT (result);
410 if (g_simple_async_result_propagate_error (simple, error))
413 /* Special case read of 0 files */
414 if (g_simple_async_result_get_source_tag (simple) == g_file_enumerator_next_files_async)
418 class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
419 return class->next_files_finish (enumerator, result, error);
423 close_async_callback_wrapper (GObject *source_object,
427 GFileEnumerator *enumerator = G_FILE_ENUMERATOR (source_object);
429 enumerator->priv->pending = FALSE;
430 enumerator->priv->closed = TRUE;
431 if (enumerator->priv->outstanding_callback)
432 (*enumerator->priv->outstanding_callback) (source_object, res, user_data);
433 g_object_unref (enumerator);
437 * g_file_enumerator_close_async:
438 * @enumerator: a #GFileEnumerator.
439 * @io_priority: the <link linkend="io-priority">I/O priority</link>
441 * @cancellable: optional #GCancellable object, %NULL to ignore.
442 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
443 * @user_data: the data to pass to callback function
445 * Asynchronously closes the file enumerator.
447 * If @cancellable is not %NULL, then the operation can be cancelled by
448 * triggering the cancellable object from another thread. If the operation
449 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned in
450 * g_file_enumerator_close_finish().
453 g_file_enumerator_close_async (GFileEnumerator *enumerator,
455 GCancellable *cancellable,
456 GAsyncReadyCallback callback,
459 GFileEnumeratorClass *class;
461 g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
463 if (enumerator->priv->closed)
465 g_simple_async_report_error_in_idle (G_OBJECT (enumerator),
468 G_IO_ERROR, G_IO_ERROR_CLOSED,
469 _("File enumerator is already closed"));
473 if (enumerator->priv->pending)
475 g_simple_async_report_error_in_idle (G_OBJECT (enumerator),
478 G_IO_ERROR, G_IO_ERROR_PENDING,
479 _("File enumerator has outstanding operation"));
483 class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
485 enumerator->priv->pending = TRUE;
486 enumerator->priv->outstanding_callback = callback;
487 g_object_ref (enumerator);
488 (* class->close_async) (enumerator, io_priority, cancellable,
489 close_async_callback_wrapper, user_data);
493 * g_file_enumerator_close_finish:
494 * @enumerator: a #GFileEnumerator.
495 * @result: a #GAsyncResult.
496 * @error: a #GError location to store the error occuring, or %NULL to
499 * Finishes closing a file enumerator, started from g_file_enumerator_close_async().
501 * If the file enumerator was already closed when g_file_enumerator_close_async()
502 * was called, then this function will report %G_IO_ERROR_CLOSED in @error, and
503 * return %FALSE. If the file enumerator had pending operation when the close
504 * operation was started, then this function will report %G_IO_ERROR_PENDING, and
505 * return %FALSE. If @cancellable was not %NULL, then the operation may have been
506 * cancelled by triggering the cancellable object from another thread. If the operation
507 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %FALSE will be
510 * Returns: %TRUE if the close operation has finished successfully.
513 g_file_enumerator_close_finish (GFileEnumerator *enumerator,
514 GAsyncResult *result,
517 GSimpleAsyncResult *simple;
518 GFileEnumeratorClass *class;
520 g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), FALSE);
521 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
523 if (G_IS_SIMPLE_ASYNC_RESULT (result))
525 simple = G_SIMPLE_ASYNC_RESULT (result);
526 if (g_simple_async_result_propagate_error (simple, error))
530 class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
531 return class->close_finish (enumerator, result, error);
535 * g_file_enumerator_is_closed:
536 * @enumerator: a #GFileEnumerator.
538 * Checks if the file enumerator has been closed.
540 * Returns: %TRUE if the @enumerator is closed.
543 g_file_enumerator_is_closed (GFileEnumerator *enumerator)
545 g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), TRUE);
547 return enumerator->priv->closed;
551 * g_file_enumerator_has_pending:
552 * @enumerator: a #GFileEnumerator.
554 * Checks if the file enumerator has pending operations.
556 * Returns: %TRUE if the @enumerator has pending operations.
559 g_file_enumerator_has_pending (GFileEnumerator *enumerator)
561 g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), TRUE);
563 return enumerator->priv->pending;
567 * g_file_enumerator_set_pending:
568 * @enumerator: a #GFileEnumerator.
569 * @pending: a boolean value.
571 * Sets the file enumerator as having pending operations.
574 g_file_enumerator_set_pending (GFileEnumerator *enumerator,
577 g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
579 enumerator->priv->pending = pending;
583 * g_file_enumerator_get_container:
584 * @enumerator: a #GFileEnumerator
586 * Get the #GFile container which is being enumerated.
588 * Returns: the #GFile which is being enumerated.
593 g_file_enumerator_get_container (GFileEnumerator *enumerator)
595 g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
597 return enumerator->priv->container;
606 next_async_op_free (NextAsyncOp *op)
608 /* Free the list, if finish wasn't called */
609 g_list_foreach (op->files, (GFunc)g_object_unref, NULL);
610 g_list_free (op->files);
618 next_files_thread (GSimpleAsyncResult *res,
620 GCancellable *cancellable)
623 GFileEnumeratorClass *class;
624 GError *error = NULL;
626 GFileEnumerator *enumerator;
629 enumerator = G_FILE_ENUMERATOR (object);
630 op = g_simple_async_result_get_op_res_gpointer (res);
632 class = G_FILE_ENUMERATOR_GET_CLASS (object);
634 for (i = 0; i < op->num_files; i++)
636 if (g_cancellable_set_error_if_cancelled (cancellable, &error))
639 info = class->next_file (enumerator, cancellable, &error);
643 /* If we get an error after first file, return that on next operation */
644 if (error != NULL && i > 0)
646 if (error->domain == G_IO_ERROR &&
647 error->code == G_IO_ERROR_CANCELLED)
648 g_error_free (error); /* Never propagate cancel errors to other call */
650 enumerator->priv->outstanding_error = error;
657 op->files = g_list_prepend (op->files, info);
662 g_file_enumerator_real_next_files_async (GFileEnumerator *enumerator,
665 GCancellable *cancellable,
666 GAsyncReadyCallback callback,
669 GSimpleAsyncResult *res;
672 op = g_new0 (NextAsyncOp, 1);
674 op->num_files = num_files;
677 res = g_simple_async_result_new (G_OBJECT (enumerator), callback, user_data, g_file_enumerator_real_next_files_async);
678 g_simple_async_result_set_op_res_gpointer (res, op, (GDestroyNotify) next_async_op_free);
680 g_simple_async_result_run_in_thread (res, next_files_thread, io_priority, cancellable);
681 g_object_unref (res);
685 g_file_enumerator_real_next_files_finish (GFileEnumerator *enumerator,
686 GAsyncResult *result,
689 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
693 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
694 g_file_enumerator_real_next_files_async);
696 op = g_simple_async_result_get_op_res_gpointer (simple);
704 close_async_thread (GSimpleAsyncResult *res,
706 GCancellable *cancellable)
708 GFileEnumeratorClass *class;
709 GError *error = NULL;
712 /* Auto handling of cancelation disabled, and ignore
713 cancellation, since we want to close things anyway, although
714 possibly in a quick-n-dirty way. At least we never want to leak
717 class = G_FILE_ENUMERATOR_GET_CLASS (object);
718 result = class->close_fn (G_FILE_ENUMERATOR (object), cancellable, &error);
721 g_simple_async_result_set_from_error (res, error);
722 g_error_free (error);
728 g_file_enumerator_real_close_async (GFileEnumerator *enumerator,
730 GCancellable *cancellable,
731 GAsyncReadyCallback callback,
734 GSimpleAsyncResult *res;
736 res = g_simple_async_result_new (G_OBJECT (enumerator),
739 g_file_enumerator_real_close_async);
741 g_simple_async_result_set_handle_cancellation (res, FALSE);
743 g_simple_async_result_run_in_thread (res,
747 g_object_unref (res);
751 g_file_enumerator_real_close_finish (GFileEnumerator *enumerator,
752 GAsyncResult *result,
755 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
756 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
757 g_file_enumerator_real_close_async);
761 #define __G_FILE_ENUMERATOR_C__
762 #include "gioaliasdef.c"