Blame gio/gfileenumerator.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "gfileenumerator.h"
Packit ae235b
#include "gfile.h"
Packit ae235b
#include "gioscheduler.h"
Packit ae235b
#include "gasyncresult.h"
Packit ae235b
#include "gasynchelper.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
struct _GFileEnumeratorPrivate {
Packit ae235b
  /* TODO: Should be public for subclasses? */
Packit ae235b
  GFile *container;
Packit ae235b
  guint closed : 1;
Packit ae235b
  guint pending : 1;
Packit ae235b
  GAsyncReadyCallback outstanding_callback;
Packit ae235b
  GError *outstanding_error;
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gfileenumerator
Packit ae235b
 * @short_description: Enumerated Files Routines
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * 
Packit ae235b
 * #GFileEnumerator allows you to operate on a set of #GFiles, 
Packit ae235b
 * returning a #GFileInfo structure for each file enumerated (e.g. 
Packit ae235b
 * g_file_enumerate_children() will return a #GFileEnumerator for each 
Packit ae235b
 * of the children within a directory).
Packit ae235b
 *
Packit ae235b
 * To get the next file's information from a #GFileEnumerator, use 
Packit ae235b
 * g_file_enumerator_next_file() or its asynchronous version, 
Packit ae235b
 * g_file_enumerator_next_files_async(). Note that the asynchronous 
Packit ae235b
 * version will return a list of #GFileInfos, whereas the 
Packit ae235b
 * synchronous will only return the next file in the enumerator.
Packit ae235b
 *
Packit ae235b
 * The ordering of returned files is unspecified for non-Unix
Packit ae235b
 * platforms; for more information, see g_dir_read_name().  On Unix,
Packit ae235b
 * when operating on local files, returned files will be sorted by
Packit ae235b
 * inode number.  Effectively you can assume that the ordering of
Packit ae235b
 * returned files will be stable between successive calls (and
Packit ae235b
 * applications) assuming the directory is unchanged.
Packit ae235b
 *
Packit ae235b
 * If your application needs a specific ordering, such as by name or
Packit ae235b
 * modification time, you will have to implement that in your
Packit ae235b
 * application code.
Packit ae235b
 *
Packit ae235b
 * To close a #GFileEnumerator, use g_file_enumerator_close(), or 
Packit ae235b
 * its asynchronous version, g_file_enumerator_close_async(). Once 
Packit ae235b
 * a #GFileEnumerator is closed, no further actions may be performed 
Packit ae235b
 * on it, and it should be freed with g_object_unref().
Packit ae235b
 * 
Packit ae235b
 **/ 
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_PRIVATE (GFileEnumerator, g_file_enumerator, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
enum {
Packit ae235b
  PROP_0,
Packit ae235b
  PROP_CONTAINER
Packit ae235b
};
Packit ae235b
Packit ae235b
static void     g_file_enumerator_real_next_files_async  (GFileEnumerator      *enumerator,
Packit ae235b
							  int                   num_files,
Packit ae235b
							  int                   io_priority,
Packit ae235b
							  GCancellable         *cancellable,
Packit ae235b
							  GAsyncReadyCallback   callback,
Packit ae235b
							  gpointer              user_data);
Packit ae235b
static GList *  g_file_enumerator_real_next_files_finish (GFileEnumerator      *enumerator,
Packit ae235b
							  GAsyncResult         *res,
Packit ae235b
							  GError              **error);
Packit ae235b
static void     g_file_enumerator_real_close_async       (GFileEnumerator      *enumerator,
Packit ae235b
							  int                   io_priority,
Packit ae235b
							  GCancellable         *cancellable,
Packit ae235b
							  GAsyncReadyCallback   callback,
Packit ae235b
							  gpointer              user_data);
Packit ae235b
static gboolean g_file_enumerator_real_close_finish      (GFileEnumerator      *enumerator,
Packit ae235b
							  GAsyncResult         *res,
Packit ae235b
							  GError              **error);
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_enumerator_set_property (GObject      *object,
Packit ae235b
                                guint         property_id,
Packit ae235b
                                const GValue *value,
Packit ae235b
                                GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  GFileEnumerator *enumerator;
Packit ae235b
  
Packit ae235b
  enumerator = G_FILE_ENUMERATOR (object);
Packit ae235b
  
Packit ae235b
  switch (property_id) {
Packit ae235b
  case PROP_CONTAINER:
Packit ae235b
    enumerator->priv->container = g_value_dup_object (value);
Packit ae235b
    break;
Packit ae235b
  default:
Packit ae235b
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
Packit ae235b
    break;
Packit ae235b
  }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_enumerator_dispose (GObject *object)
Packit ae235b
{
Packit ae235b
  GFileEnumerator *enumerator;
Packit ae235b
Packit ae235b
  enumerator = G_FILE_ENUMERATOR (object);
Packit ae235b
  
Packit ae235b
  if (enumerator->priv->container) {
Packit ae235b
    g_object_unref (enumerator->priv->container);
Packit ae235b
    enumerator->priv->container = NULL;
Packit ae235b
  }
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_file_enumerator_parent_class)->dispose (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_enumerator_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GFileEnumerator *enumerator;
Packit ae235b
Packit ae235b
  enumerator = G_FILE_ENUMERATOR (object);
Packit ae235b
  
Packit ae235b
  if (!enumerator->priv->closed)
Packit ae235b
    g_file_enumerator_close (enumerator, NULL, NULL);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_file_enumerator_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_enumerator_class_init (GFileEnumeratorClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->set_property = g_file_enumerator_set_property;
Packit ae235b
  gobject_class->dispose = g_file_enumerator_dispose;
Packit ae235b
  gobject_class->finalize = g_file_enumerator_finalize;
Packit ae235b
Packit ae235b
  klass->next_files_async = g_file_enumerator_real_next_files_async;
Packit ae235b
  klass->next_files_finish = g_file_enumerator_real_next_files_finish;
Packit ae235b
  klass->close_async = g_file_enumerator_real_close_async;
Packit ae235b
  klass->close_finish = g_file_enumerator_real_close_finish;
Packit ae235b
Packit ae235b
  g_object_class_install_property
Packit ae235b
    (gobject_class, PROP_CONTAINER,
Packit ae235b
     g_param_spec_object ("container", P_("Container"),
Packit ae235b
                          P_("The container that is being enumerated"),
Packit ae235b
                          G_TYPE_FILE,
Packit ae235b
                          G_PARAM_WRITABLE |
Packit ae235b
                          G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                          G_PARAM_STATIC_STRINGS));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_enumerator_init (GFileEnumerator *enumerator)
Packit ae235b
{
Packit ae235b
  enumerator->priv = g_file_enumerator_get_instance_private (enumerator);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_enumerator_next_file:
Packit ae235b
 * @enumerator: a #GFileEnumerator.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore
Packit ae235b
 *
Packit ae235b
 * Returns information for the next file in the enumerated object.
Packit ae235b
 * Will block until the information is available. The #GFileInfo 
Packit ae235b
 * returned from this function will contain attributes that match the 
Packit ae235b
 * attribute string that was passed when the #GFileEnumerator was created.
Packit ae235b
 *
Packit ae235b
 * See the documentation of #GFileEnumerator for information about the
Packit ae235b
 * order of returned files.
Packit ae235b
 *
Packit ae235b
 * On error, returns %NULL and sets @error to the error. If the
Packit ae235b
 * enumerator is at the end, %NULL will be returned and @error will
Packit ae235b
 * be unset.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable) (transfer full): A #GFileInfo or %NULL on error
Packit ae235b
 *    or end of enumerator.  Free the returned object with
Packit ae235b
 *    g_object_unref() when no longer needed.
Packit ae235b
 **/
Packit ae235b
GFileInfo *
Packit ae235b
g_file_enumerator_next_file (GFileEnumerator *enumerator,
Packit ae235b
			     GCancellable *cancellable,
Packit ae235b
			     GError **error)
Packit ae235b
{
Packit ae235b
  GFileEnumeratorClass *class;
Packit ae235b
  GFileInfo *info;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
Packit ae235b
  g_return_val_if_fail (enumerator != NULL, NULL);
Packit ae235b
  
Packit ae235b
  if (enumerator->priv->closed)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
Packit ae235b
                           _("Enumerator is closed"));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (enumerator->priv->pending)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
Packit ae235b
                           _("File enumerator has outstanding operation"));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (enumerator->priv->outstanding_error)
Packit ae235b
    {
Packit ae235b
      g_propagate_error (error, enumerator->priv->outstanding_error);
Packit ae235b
      enumerator->priv->outstanding_error = NULL;
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_push_current (cancellable);
Packit ae235b
  
Packit ae235b
  enumerator->priv->pending = TRUE;
Packit ae235b
  info = (* class->next_file) (enumerator, cancellable, error);
Packit ae235b
  enumerator->priv->pending = FALSE;
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_pop_current (cancellable);
Packit ae235b
  
Packit ae235b
  return info;
Packit ae235b
}
Packit ae235b
  
Packit ae235b
/**
Packit ae235b
 * g_file_enumerator_close:
Packit ae235b
 * @enumerator: a #GFileEnumerator.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore
Packit ae235b
 *
Packit ae235b
 * Releases all resources used by this enumerator, making the
Packit ae235b
 * enumerator return %G_IO_ERROR_CLOSED on all calls.
Packit ae235b
 *
Packit ae235b
 * This will be automatically called when the last reference
Packit ae235b
 * is dropped, but you might want to call this function to make 
Packit ae235b
 * sure resources are released as early as possible.
Packit ae235b
 *
Packit ae235b
 * Returns: #TRUE on success or #FALSE on error.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_file_enumerator_close (GFileEnumerator  *enumerator,
Packit ae235b
			 GCancellable     *cancellable,
Packit ae235b
			 GError          **error)
Packit ae235b
{
Packit ae235b
  GFileEnumeratorClass *class;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), FALSE);
Packit ae235b
  g_return_val_if_fail (enumerator != NULL, FALSE);
Packit ae235b
  
Packit ae235b
  class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
Packit ae235b
Packit ae235b
  if (enumerator->priv->closed)
Packit ae235b
    return TRUE;
Packit ae235b
  
Packit ae235b
  if (enumerator->priv->pending)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
Packit ae235b
                           _("File enumerator has outstanding operation"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_push_current (cancellable);
Packit ae235b
  
Packit ae235b
  enumerator->priv->pending = TRUE;
Packit ae235b
  (* class->close_fn) (enumerator, cancellable, error);
Packit ae235b
  enumerator->priv->pending = FALSE;
Packit ae235b
  enumerator->priv->closed = TRUE;
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_pop_current (cancellable);
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
next_async_callback_wrapper (GObject      *source_object,
Packit ae235b
			     GAsyncResult *res,
Packit ae235b
			     gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GFileEnumerator *enumerator = G_FILE_ENUMERATOR (source_object);
Packit ae235b
Packit ae235b
  enumerator->priv->pending = FALSE;
Packit ae235b
  if (enumerator->priv->outstanding_callback)
Packit ae235b
    (*enumerator->priv->outstanding_callback) (source_object, res, user_data);
Packit ae235b
  g_object_unref (enumerator);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_enumerator_next_files_async:
Packit ae235b
 * @enumerator: a #GFileEnumerator.
Packit ae235b
 * @num_files: the number of file info objects to request
Packit ae235b
 * @io_priority: the [I/O priority][io-priority] of the request
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
Packit ae235b
 * @user_data: (closure): the data to pass to callback function
Packit ae235b
 *
Packit ae235b
 * Request information for a number of files from the enumerator asynchronously.
Packit ae235b
 * When all i/o for the operation is finished the @callback will be called with
Packit ae235b
 * the requested information. 
Packit ae235b
 *
Packit ae235b
 * See the documentation of #GFileEnumerator for information about the
Packit ae235b
 * order of returned files.
Packit ae235b
 *
Packit ae235b
 * The callback can be called with less than @num_files files in case of error
Packit ae235b
 * or at the end of the enumerator. In case of a partial error the callback will
Packit ae235b
 * be called with any succeeding items and no error, and on the next request the
Packit ae235b
 * error will be reported. If a request is cancelled the callback will be called
Packit ae235b
 * with %G_IO_ERROR_CANCELLED.
Packit ae235b
 *
Packit ae235b
 * During an async request no other sync and async calls are allowed, and will
Packit ae235b
 * result in %G_IO_ERROR_PENDING errors. 
Packit ae235b
 *
Packit ae235b
 * Any outstanding i/o request with higher priority (lower numerical value) will
Packit ae235b
 * be executed before an outstanding request with lower priority. Default
Packit ae235b
 * priority is %G_PRIORITY_DEFAULT.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_file_enumerator_next_files_async (GFileEnumerator     *enumerator,
Packit ae235b
				    int                  num_files,
Packit ae235b
				    int                  io_priority,
Packit ae235b
				    GCancellable        *cancellable,
Packit ae235b
				    GAsyncReadyCallback  callback,
Packit ae235b
				    gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GFileEnumeratorClass *class;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
Packit ae235b
  g_return_if_fail (enumerator != NULL);
Packit ae235b
  g_return_if_fail (num_files >= 0);
Packit ae235b
Packit ae235b
  if (num_files == 0)
Packit ae235b
    {
Packit ae235b
      GTask *task;
Packit ae235b
Packit ae235b
      task = g_task_new (enumerator, cancellable, callback, user_data);
Packit ae235b
      g_task_set_source_tag (task, g_file_enumerator_next_files_async);
Packit ae235b
      g_task_return_pointer (task, NULL, NULL);
Packit ae235b
      g_object_unref (task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (enumerator->priv->closed)
Packit ae235b
    {
Packit ae235b
      g_task_report_new_error (enumerator, callback, user_data,
Packit ae235b
                               g_file_enumerator_next_files_async,
Packit ae235b
                               G_IO_ERROR, G_IO_ERROR_CLOSED,
Packit ae235b
                               _("File enumerator is already closed"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (enumerator->priv->pending)
Packit ae235b
    {
Packit ae235b
      g_task_report_new_error (enumerator, callback, user_data,
Packit ae235b
                               g_file_enumerator_next_files_async,
Packit ae235b
                               G_IO_ERROR, G_IO_ERROR_PENDING,
Packit ae235b
                               _("File enumerator has outstanding operation"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
Packit ae235b
  
Packit ae235b
  enumerator->priv->pending = TRUE;
Packit ae235b
  enumerator->priv->outstanding_callback = callback;
Packit ae235b
  g_object_ref (enumerator);
Packit ae235b
  (* class->next_files_async) (enumerator, num_files, io_priority, cancellable, 
Packit ae235b
			       next_async_callback_wrapper, user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_enumerator_next_files_finish:
Packit ae235b
 * @enumerator: a #GFileEnumerator.
Packit ae235b
 * @result: a #GAsyncResult.
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to 
Packit ae235b
 * ignore.
Packit ae235b
 * 
Packit ae235b
 * Finishes the asynchronous operation started with g_file_enumerator_next_files_async().
Packit ae235b
 * 
Packit ae235b
 * Returns: (transfer full) (element-type Gio.FileInfo): a #GList of #GFileInfos. You must free the list with 
Packit ae235b
 *     g_list_free() and unref the infos with g_object_unref() when you're 
Packit ae235b
 *     done with them.
Packit ae235b
 **/
Packit ae235b
GList *
Packit ae235b
g_file_enumerator_next_files_finish (GFileEnumerator  *enumerator,
Packit ae235b
				     GAsyncResult     *result,
Packit ae235b
				     GError          **error)
Packit ae235b
{
Packit ae235b
  GFileEnumeratorClass *class;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
Packit ae235b
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
Packit ae235b
  
Packit ae235b
  if (g_async_result_legacy_propagate_error (result, error))
Packit ae235b
    return NULL;
Packit ae235b
  else if (g_async_result_is_tagged (result, g_file_enumerator_next_files_async))
Packit ae235b
    return g_task_propagate_pointer (G_TASK (result), error);
Packit ae235b
  
Packit ae235b
  class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
Packit ae235b
  return class->next_files_finish (enumerator, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
close_async_callback_wrapper (GObject      *source_object,
Packit ae235b
			      GAsyncResult *res,
Packit ae235b
			      gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GFileEnumerator *enumerator = G_FILE_ENUMERATOR (source_object);
Packit ae235b
  
Packit ae235b
  enumerator->priv->pending = FALSE;
Packit ae235b
  enumerator->priv->closed = TRUE;
Packit ae235b
  if (enumerator->priv->outstanding_callback)
Packit ae235b
    (*enumerator->priv->outstanding_callback) (source_object, res, user_data);
Packit ae235b
  g_object_unref (enumerator);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_enumerator_close_async:
Packit ae235b
 * @enumerator: a #GFileEnumerator.
Packit ae235b
 * @io_priority: the [I/O priority][io-priority] of the request
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
Packit ae235b
 * @user_data: (closure): the data to pass to callback function
Packit ae235b
 *
Packit ae235b
 * Asynchronously closes the file enumerator. 
Packit ae235b
 *
Packit ae235b
 * If @cancellable is not %NULL, then the operation can be cancelled by
Packit ae235b
 * triggering the cancellable object from another thread. If the operation
Packit ae235b
 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned in 
Packit ae235b
 * g_file_enumerator_close_finish(). 
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_file_enumerator_close_async (GFileEnumerator     *enumerator,
Packit ae235b
			       int                  io_priority,
Packit ae235b
			       GCancellable        *cancellable,
Packit ae235b
			       GAsyncReadyCallback  callback,
Packit ae235b
			       gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GFileEnumeratorClass *class;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
Packit ae235b
Packit ae235b
  if (enumerator->priv->closed)
Packit ae235b
    {
Packit ae235b
      g_task_report_new_error (enumerator, callback, user_data,
Packit ae235b
                               g_file_enumerator_close_async,
Packit ae235b
                               G_IO_ERROR, G_IO_ERROR_CLOSED,
Packit ae235b
                               _("File enumerator is already closed"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (enumerator->priv->pending)
Packit ae235b
    {
Packit ae235b
      g_task_report_new_error (enumerator, callback, user_data,
Packit ae235b
                               g_file_enumerator_close_async,
Packit ae235b
                               G_IO_ERROR, G_IO_ERROR_PENDING,
Packit ae235b
                               _("File enumerator has outstanding operation"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
Packit ae235b
  
Packit ae235b
  enumerator->priv->pending = TRUE;
Packit ae235b
  enumerator->priv->outstanding_callback = callback;
Packit ae235b
  g_object_ref (enumerator);
Packit ae235b
  (* class->close_async) (enumerator, io_priority, cancellable,
Packit ae235b
			  close_async_callback_wrapper, user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_enumerator_close_finish:
Packit ae235b
 * @enumerator: a #GFileEnumerator.
Packit ae235b
 * @result: a #GAsyncResult.
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to 
Packit ae235b
 * ignore.
Packit ae235b
 * 
Packit ae235b
 * Finishes closing a file enumerator, started from g_file_enumerator_close_async().
Packit ae235b
 * 
Packit ae235b
 * If the file enumerator was already closed when g_file_enumerator_close_async() 
Packit ae235b
 * was called, then this function will report %G_IO_ERROR_CLOSED in @error, and 
Packit ae235b
 * return %FALSE. If the file enumerator had pending operation when the close 
Packit ae235b
 * operation was started, then this function will report %G_IO_ERROR_PENDING, and
Packit ae235b
 * return %FALSE.  If @cancellable was not %NULL, then the operation may have been 
Packit ae235b
 * cancelled by triggering the cancellable object from another thread. If the operation
Packit ae235b
 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %FALSE will be 
Packit ae235b
 * returned. 
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE if the close operation has finished successfully.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_file_enumerator_close_finish (GFileEnumerator  *enumerator,
Packit ae235b
				GAsyncResult     *result,
Packit ae235b
				GError          **error)
Packit ae235b
{
Packit ae235b
  GFileEnumeratorClass *class;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), FALSE);
Packit ae235b
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
Packit ae235b
  
Packit ae235b
  if (g_async_result_legacy_propagate_error (result, error))
Packit ae235b
    return FALSE;
Packit ae235b
  else if (g_async_result_is_tagged (result, g_file_enumerator_close_async))
Packit ae235b
    return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
Packit ae235b
  class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
Packit ae235b
  return class->close_finish (enumerator, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_enumerator_is_closed:
Packit ae235b
 * @enumerator: a #GFileEnumerator.
Packit ae235b
 *
Packit ae235b
 * Checks if the file enumerator has been closed.
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE if the @enumerator is closed.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_file_enumerator_is_closed (GFileEnumerator *enumerator)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), TRUE);
Packit ae235b
  
Packit ae235b
  return enumerator->priv->closed;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_enumerator_has_pending:
Packit ae235b
 * @enumerator: a #GFileEnumerator.
Packit ae235b
 * 
Packit ae235b
 * Checks if the file enumerator has pending operations.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the @enumerator has pending operations.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_file_enumerator_has_pending (GFileEnumerator *enumerator)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), TRUE);
Packit ae235b
  
Packit ae235b
  return enumerator->priv->pending;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_enumerator_set_pending:
Packit ae235b
 * @enumerator: a #GFileEnumerator.
Packit ae235b
 * @pending: a boolean value.
Packit ae235b
 * 
Packit ae235b
 * Sets the file enumerator as having pending operations.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_file_enumerator_set_pending (GFileEnumerator *enumerator,
Packit ae235b
			       gboolean         pending)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));
Packit ae235b
  
Packit ae235b
  enumerator->priv->pending = pending;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_enumerator_iterate:
Packit ae235b
 * @direnum: an open #GFileEnumerator
Packit ae235b
 * @out_info: (out) (transfer none) (optional): Output location for the next #GFileInfo, or %NULL
Packit ae235b
 * @out_child: (out) (transfer none) (optional): Output location for the next #GFile, or %NULL
Packit ae235b
 * @cancellable: a #GCancellable
Packit ae235b
 * @error: a #GError
Packit ae235b
 *
Packit ae235b
 * This is a version of g_file_enumerator_next_file() that's easier to
Packit ae235b
 * use correctly from C programs.  With g_file_enumerator_next_file(),
Packit ae235b
 * the gboolean return value signifies "end of iteration or error", which
Packit ae235b
 * requires allocation of a temporary #GError.
Packit ae235b
 *
Packit ae235b
 * In contrast, with this function, a %FALSE return from
Packit ae235b
 * g_file_enumerator_iterate() *always* means
Packit ae235b
 * "error".  End of iteration is signaled by @out_info or @out_child being %NULL.
Packit ae235b
 *
Packit ae235b
 * Another crucial difference is that the references for @out_info and
Packit ae235b
 * @out_child are owned by @direnum (they are cached as hidden
Packit ae235b
 * properties).  You must not unref them in your own code.  This makes
Packit ae235b
 * memory management significantly easier for C code in combination
Packit ae235b
 * with loops.
Packit ae235b
 *
Packit ae235b
 * Finally, this function optionally allows retrieving a #GFile as
Packit ae235b
 * well.
Packit ae235b
 *
Packit ae235b
 * You must specify at least one of @out_info or @out_child.
Packit ae235b
 *
Packit ae235b
 * The code pattern for correctly using g_file_enumerator_iterate() from C
Packit ae235b
 * is:
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 * direnum = g_file_enumerate_children (file, ...);
Packit ae235b
 * while (TRUE)
Packit ae235b
 *   {
Packit ae235b
 *     GFileInfo *info;
Packit ae235b
 *     if (!g_file_enumerator_iterate (direnum, &info, NULL, cancellable, error))
Packit ae235b
 *       goto out;
Packit ae235b
 *     if (!info)
Packit ae235b
 *       break;
Packit ae235b
 *     ... do stuff with "info"; do not unref it! ...
Packit ae235b
 *   }
Packit ae235b
 * 
Packit ae235b
 * out:
Packit ae235b
 *   g_object_unref (direnum); // Note: frees the last @info
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_file_enumerator_iterate (GFileEnumerator  *direnum,
Packit ae235b
                           GFileInfo       **out_info,
Packit ae235b
                           GFile           **out_child,
Packit ae235b
                           GCancellable     *cancellable,
Packit ae235b
                           GError          **error)
Packit ae235b
{
Packit ae235b
  gboolean ret = FALSE;
Packit ae235b
  GError *temp_error = NULL;
Packit ae235b
  GFileInfo *ret_info = NULL;
Packit ae235b
Packit ae235b
  static GQuark cached_info_quark;
Packit ae235b
  static GQuark cached_child_quark;
Packit ae235b
  static gsize quarks_initialized;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (direnum != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (out_info != NULL || out_child != NULL, FALSE);
Packit ae235b
Packit ae235b
  if (g_once_init_enter (&quarks_initialized))
Packit ae235b
    {
Packit ae235b
      cached_info_quark = g_quark_from_static_string ("g-cached-info");
Packit ae235b
      cached_child_quark = g_quark_from_static_string ("g-cached-child");
Packit ae235b
      g_once_init_leave (&quarks_initialized, 1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  ret_info = g_file_enumerator_next_file (direnum, cancellable, &temp_error);
Packit ae235b
  if (temp_error != NULL)
Packit ae235b
    {
Packit ae235b
      g_propagate_error (error, temp_error);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (ret_info)
Packit ae235b
    { 
Packit ae235b
      if (out_child != NULL)
Packit ae235b
        {
Packit ae235b
          const char *name = g_file_info_get_name (ret_info);
Packit ae235b
Packit ae235b
          if (G_UNLIKELY (name == NULL))
Packit ae235b
            g_warning ("g_file_enumerator_iterate() created without standard::name");
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              *out_child = g_file_get_child (g_file_enumerator_get_container (direnum), name);
Packit ae235b
              g_object_set_qdata_full ((GObject*)direnum, cached_child_quark, *out_child, (GDestroyNotify)g_object_unref);
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      if (out_info != NULL)
Packit ae235b
        {
Packit ae235b
          g_object_set_qdata_full ((GObject*)direnum, cached_info_quark, ret_info, (GDestroyNotify)g_object_unref);
Packit ae235b
          *out_info = ret_info;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        g_object_unref (ret_info);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (out_info)
Packit ae235b
        *out_info = NULL;
Packit ae235b
      if (out_child)
Packit ae235b
        *out_child = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  ret = TRUE;
Packit ae235b
 out:
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_enumerator_get_container:
Packit ae235b
 * @enumerator: a #GFileEnumerator
Packit ae235b
 *
Packit ae235b
 * Get the #GFile container which is being enumerated.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the #GFile which is being enumerated.
Packit ae235b
 *
Packit ae235b
 * Since: 2.18
Packit ae235b
 */
Packit ae235b
GFile *
Packit ae235b
g_file_enumerator_get_container (GFileEnumerator *enumerator)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
Packit ae235b
Packit ae235b
  return enumerator->priv->container;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_enumerator_get_child:
Packit ae235b
 * @enumerator: a #GFileEnumerator
Packit ae235b
 * @info: a #GFileInfo gotten from g_file_enumerator_next_file()
Packit ae235b
 *   or the async equivalents.
Packit ae235b
 *
Packit ae235b
 * Return a new #GFile which refers to the file named by @info in the source
Packit ae235b
 * directory of @enumerator.  This function is primarily intended to be used
Packit ae235b
 * inside loops with g_file_enumerator_next_file().
Packit ae235b
 *
Packit ae235b
 * This is a convenience method that's equivalent to:
Packit ae235b
 * |[
Packit ae235b
 *   gchar *name = g_file_info_get_name (info);
Packit ae235b
 *   GFile *child = g_file_get_child (g_file_enumerator_get_container (enumr),
Packit ae235b
 *                                    name);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GFile for the #GFileInfo passed it.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
GFile *
Packit ae235b
g_file_enumerator_get_child (GFileEnumerator *enumerator,
Packit ae235b
                             GFileInfo       *info)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
Packit ae235b
Packit ae235b
  return g_file_get_child (enumerator->priv->container,
Packit ae235b
                           g_file_info_get_name (info));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
next_async_op_free (GList *files)
Packit ae235b
{
Packit ae235b
  g_list_free_full (files, g_object_unref);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
next_files_thread (GTask        *task,
Packit ae235b
                   gpointer      source_object,
Packit ae235b
                   gpointer      task_data,
Packit ae235b
                   GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GFileEnumerator *enumerator = source_object;
Packit ae235b
  int num_files = GPOINTER_TO_INT (task_data);
Packit ae235b
  GFileEnumeratorClass *class;
Packit ae235b
  GList *files = NULL;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GFileInfo *info;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
Packit ae235b
Packit ae235b
  for (i = 0; i < num_files; i++)
Packit ae235b
    {
Packit ae235b
      if (g_cancellable_set_error_if_cancelled (cancellable, &error))
Packit ae235b
	info = NULL;
Packit ae235b
      else
Packit ae235b
	info = class->next_file (enumerator, cancellable, &error);
Packit ae235b
      
Packit ae235b
      if (info == NULL)
Packit ae235b
	{
Packit ae235b
	  /* If we get an error after first file, return that on next operation */
Packit ae235b
	  if (error != NULL && i > 0)
Packit ae235b
	    {
Packit ae235b
	      if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
Packit ae235b
		g_error_free (error); /* Never propagate cancel errors to other call */
Packit ae235b
	      else
Packit ae235b
		enumerator->priv->outstanding_error = error;
Packit ae235b
	      error = NULL;
Packit ae235b
	    }
Packit ae235b
	      
Packit ae235b
	  break;
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	files = g_list_prepend (files, info);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (error)
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  else
Packit ae235b
    g_task_return_pointer (task, files, (GDestroyNotify)next_async_op_free);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_enumerator_real_next_files_async (GFileEnumerator     *enumerator,
Packit ae235b
					 int                  num_files,
Packit ae235b
					 int                  io_priority,
Packit ae235b
					 GCancellable        *cancellable,
Packit ae235b
					 GAsyncReadyCallback  callback,
Packit ae235b
					 gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  task = g_task_new (enumerator, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_file_enumerator_real_next_files_async);
Packit ae235b
  g_task_set_task_data (task, GINT_TO_POINTER (num_files), NULL);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
Packit ae235b
  g_task_run_in_thread (task, next_files_thread);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GList *
Packit ae235b
g_file_enumerator_real_next_files_finish (GFileEnumerator                *enumerator,
Packit ae235b
					  GAsyncResult                   *result,
Packit ae235b
					  GError                        **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, enumerator), NULL);
Packit ae235b
Packit ae235b
  return g_task_propagate_pointer (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
close_async_thread (GTask        *task,
Packit ae235b
                    gpointer      source_object,
Packit ae235b
                    gpointer      task_data,
Packit ae235b
                    GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GFileEnumerator *enumerator = source_object;
Packit ae235b
  GFileEnumeratorClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gboolean result;
Packit ae235b
Packit ae235b
  class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
Packit ae235b
  result = class->close_fn (enumerator, cancellable, &error);
Packit ae235b
  if (result)
Packit ae235b
    g_task_return_boolean (task, TRUE);
Packit ae235b
  else
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_enumerator_real_close_async (GFileEnumerator     *enumerator,
Packit ae235b
				    int                  io_priority,
Packit ae235b
				    GCancellable        *cancellable,
Packit ae235b
				    GAsyncReadyCallback  callback,
Packit ae235b
				    gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  task = g_task_new (enumerator, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_file_enumerator_real_close_async);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
  
Packit ae235b
  g_task_run_in_thread (task, close_async_thread);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_enumerator_real_close_finish (GFileEnumerator  *enumerator,
Packit ae235b
                                     GAsyncResult     *result,
Packit ae235b
                                     GError          **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, enumerator), FALSE);
Packit ae235b
Packit ae235b
  return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
}
Packit ae235b