Blame gio/giostream.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright © 2008 codethink
Packit ae235b
 * Copyright © 2009 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
 * Authors: Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 *          Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include <glib.h>
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
#include "giostream.h"
Packit ae235b
#include "gasyncresult.h"
Packit ae235b
#include "gioprivate.h"
Packit ae235b
#include "gtask.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:giostream
Packit ae235b
 * @short_description: Base class for implementing read/write streams
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GInputStream, #GOutputStream
Packit ae235b
 *
Packit ae235b
 * GIOStream represents an object that has both read and write streams.
Packit ae235b
 * Generally the two streams act as separate input and output streams,
Packit ae235b
 * but they share some common resources and state. For instance, for
Packit ae235b
 * seekable streams, both streams may use the same position.
Packit ae235b
 *
Packit ae235b
 * Examples of #GIOStream objects are #GSocketConnection, which represents
Packit ae235b
 * a two-way network connection; and #GFileIOStream, which represents a
Packit ae235b
 * file handle opened in read-write mode.
Packit ae235b
 *
Packit ae235b
 * To do the actual reading and writing you need to get the substreams
Packit ae235b
 * with g_io_stream_get_input_stream() and g_io_stream_get_output_stream().
Packit ae235b
 *
Packit ae235b
 * The #GIOStream object owns the input and the output streams, not the other
Packit ae235b
 * way around, so keeping the substreams alive will not keep the #GIOStream
Packit ae235b
 * object alive. If the #GIOStream object is freed it will be closed, thus
Packit ae235b
 * closing the substreams, so even if the substreams stay alive they will
Packit ae235b
 * always return %G_IO_ERROR_CLOSED for all operations.
Packit ae235b
 *
Packit ae235b
 * To close a stream use g_io_stream_close() which will close the common
Packit ae235b
 * stream object and also the individual substreams. You can also close
Packit ae235b
 * the substreams themselves. In most cases this only marks the
Packit ae235b
 * substream as closed, so further I/O on it fails but common state in the
Packit ae235b
 * #GIOStream may still be open. However, some streams may support
Packit ae235b
 * "half-closed" states where one direction of the stream is actually shut down.
Packit ae235b
 *
Packit ae235b
 * Operations on #GIOStreams cannot be started while another operation on the
Packit ae235b
 * #GIOStream or its substreams is in progress. Specifically, an application can
Packit ae235b
 * read from the #GInputStream and write to the #GOutputStream simultaneously
Packit ae235b
 * (either in separate threads, or as asynchronous operations in the same
Packit ae235b
 * thread), but an application cannot start any #GIOStream operation while there
Packit ae235b
 * is a #GIOStream, #GInputStream or #GOutputStream operation in progress, and
Packit ae235b
 * an application can’t start any #GInputStream or #GOutputStream operation
Packit ae235b
 * while there is a #GIOStream operation in progress.
Packit ae235b
 *
Packit ae235b
 * This is a product of individual stream operations being associated with a
Packit ae235b
 * given #GMainContext (the thread-default context at the time the operation was
Packit ae235b
 * started), rather than entire streams being associated with a single
Packit ae235b
 * #GMainContext.
Packit ae235b
 *
Packit ae235b
 * GIO may run operations on #GIOStreams from other (worker) threads, and this
Packit ae235b
 * may be exposed to application code in the behaviour of wrapper streams, such
Packit ae235b
 * as #GBufferedInputStream or #GTlsConnection. With such wrapper APIs,
Packit ae235b
 * application code may only run operations on the base (wrapped) stream when
Packit ae235b
 * the wrapper stream is idle. Note that the semantics of such operations may
Packit ae235b
 * not be well-defined due to the state the wrapper stream leaves the base
Packit ae235b
 * stream in (though they are guaranteed not to crash).
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  PROP_0,
Packit ae235b
  PROP_INPUT_STREAM,
Packit ae235b
  PROP_OUTPUT_STREAM,
Packit ae235b
  PROP_CLOSED
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GIOStreamPrivate {
Packit ae235b
  guint closed : 1;
Packit ae235b
  guint pending : 1;
Packit ae235b
};
Packit ae235b
Packit ae235b
static gboolean g_io_stream_real_close        (GIOStream            *stream,
Packit ae235b
					       GCancellable         *cancellable,
Packit ae235b
					       GError              **error);
Packit ae235b
static void     g_io_stream_real_close_async  (GIOStream            *stream,
Packit ae235b
					       int                   io_priority,
Packit ae235b
					       GCancellable         *cancellable,
Packit ae235b
					       GAsyncReadyCallback   callback,
Packit ae235b
					       gpointer              user_data);
Packit ae235b
static gboolean g_io_stream_real_close_finish (GIOStream            *stream,
Packit ae235b
					       GAsyncResult         *result,
Packit ae235b
					       GError              **error);
Packit ae235b
Packit ae235b
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GIOStream, g_io_stream, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_io_stream_dispose (GObject *object)
Packit ae235b
{
Packit ae235b
  GIOStream *stream;
Packit ae235b
Packit ae235b
  stream = G_IO_STREAM (object);
Packit ae235b
Packit ae235b
  if (!stream->priv->closed)
Packit ae235b
    g_io_stream_close (stream, NULL, NULL);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_io_stream_parent_class)->dispose (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_io_stream_init (GIOStream *stream)
Packit ae235b
{
Packit ae235b
  stream->priv = g_io_stream_get_instance_private (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_io_stream_get_property (GObject    *object,
Packit ae235b
			  guint       prop_id,
Packit ae235b
			  GValue     *value,
Packit ae235b
			  GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  GIOStream *stream = G_IO_STREAM (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
      case PROP_CLOSED:
Packit ae235b
        g_value_set_boolean (value, stream->priv->closed);
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      case PROP_INPUT_STREAM:
Packit ae235b
        g_value_set_object (value, g_io_stream_get_input_stream (stream));
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      case PROP_OUTPUT_STREAM:
Packit ae235b
        g_value_set_object (value, g_io_stream_get_output_stream (stream));
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      default:
Packit ae235b
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_io_stream_class_init (GIOStreamClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->dispose = g_io_stream_dispose;
Packit ae235b
  gobject_class->get_property = g_io_stream_get_property;
Packit ae235b
Packit ae235b
  klass->close_fn = g_io_stream_real_close;
Packit ae235b
  klass->close_async = g_io_stream_real_close_async;
Packit ae235b
  klass->close_finish = g_io_stream_real_close_finish;
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_CLOSED,
Packit ae235b
                                   g_param_spec_boolean ("closed",
Packit ae235b
                                                         P_("Closed"),
Packit ae235b
                                                         P_("Is the stream closed"),
Packit ae235b
                                                         FALSE,
Packit ae235b
                                                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
Packit ae235b
				   g_param_spec_object ("input-stream",
Packit ae235b
							P_("Input stream"),
Packit ae235b
							P_("The GInputStream to read from"),
Packit ae235b
							G_TYPE_INPUT_STREAM,
Packit ae235b
							G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
Packit ae235b
				   g_param_spec_object ("output-stream",
Packit ae235b
							P_("Output stream"),
Packit ae235b
							P_("The GOutputStream to write to"),
Packit ae235b
							G_TYPE_OUTPUT_STREAM,
Packit ae235b
							G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_stream_is_closed:
Packit ae235b
 * @stream: a #GIOStream
Packit ae235b
 *
Packit ae235b
 * Checks if a stream is closed.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the stream is closed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_io_stream_is_closed (GIOStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_IO_STREAM (stream), TRUE);
Packit ae235b
Packit ae235b
  return stream->priv->closed;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_stream_get_input_stream:
Packit ae235b
 * @stream: a #GIOStream
Packit ae235b
 *
Packit ae235b
 * Gets the input stream for this object. This is used
Packit ae235b
 * for reading.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): a #GInputStream, owned by the #GIOStream.
Packit ae235b
 * Do not free.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GInputStream *
Packit ae235b
g_io_stream_get_input_stream (GIOStream *stream)
Packit ae235b
{
Packit ae235b
  GIOStreamClass *klass;
Packit ae235b
Packit ae235b
  klass = G_IO_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  g_assert (klass->get_input_stream != NULL);
Packit ae235b
Packit ae235b
  return klass->get_input_stream (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_stream_get_output_stream:
Packit ae235b
 * @stream: a #GIOStream
Packit ae235b
 *
Packit ae235b
 * Gets the output stream for this object. This is used for
Packit ae235b
 * writing.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): a #GOutputStream, owned by the #GIOStream.
Packit ae235b
 * Do not free.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GOutputStream *
Packit ae235b
g_io_stream_get_output_stream (GIOStream *stream)
Packit ae235b
{
Packit ae235b
  GIOStreamClass *klass;
Packit ae235b
Packit ae235b
  klass = G_IO_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  g_assert (klass->get_output_stream != NULL);
Packit ae235b
  return klass->get_output_stream (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_stream_has_pending:
Packit ae235b
 * @stream: a #GIOStream
Packit ae235b
 *
Packit ae235b
 * Checks if a stream has pending actions.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @stream has pending actions.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_io_stream_has_pending (GIOStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  return stream->priv->pending;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_stream_set_pending:
Packit ae235b
 * @stream: a #GIOStream
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to
Packit ae235b
 *     ignore
Packit ae235b
 *
Packit ae235b
 * Sets @stream to have actions pending. If the pending flag is
Packit ae235b
 * already set or @stream is closed, it will return %FALSE and set
Packit ae235b
 * @error.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if pending was previously unset and is now set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_io_stream_set_pending (GIOStream  *stream,
Packit ae235b
			 GError    **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  if (stream->priv->closed)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
Packit ae235b
                           _("Stream is already closed"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (stream->priv->pending)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
Packit ae235b
                           /* Translators: This is an error you get if there is
Packit ae235b
                            * already an operation running against this stream when
Packit ae235b
                            * you try to start one */
Packit ae235b
                           _("Stream has outstanding operation"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  stream->priv->pending = TRUE;
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_stream_clear_pending:
Packit ae235b
 * @stream: a #GIOStream
Packit ae235b
 *
Packit ae235b
 * Clears the pending flag on @stream.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_io_stream_clear_pending (GIOStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_IO_STREAM (stream));
Packit ae235b
Packit ae235b
  stream->priv->pending = FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_io_stream_real_close (GIOStream     *stream,
Packit ae235b
			GCancellable  *cancellable,
Packit ae235b
			GError       **error)
Packit ae235b
{
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  res = g_output_stream_close (g_io_stream_get_output_stream (stream),
Packit ae235b
			       cancellable, error);
Packit ae235b
Packit ae235b
  /* If this errored out, unset error so that we don't report
Packit ae235b
     further errors, but still do the following ops */
Packit ae235b
  if (error != NULL && *error != NULL)
Packit ae235b
    error = NULL;
Packit ae235b
Packit ae235b
  res &= g_input_stream_close (g_io_stream_get_input_stream (stream),
Packit ae235b
			       cancellable, error);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_stream_close:
Packit ae235b
 * @stream: a #GIOStream
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
 * Closes the stream, releasing resources related to it. This will also
Packit ae235b
 * close the individual input and output streams, if they are not already
Packit ae235b
 * closed.
Packit ae235b
 *
Packit ae235b
 * Once the stream is closed, all other operations will return
Packit ae235b
 * %G_IO_ERROR_CLOSED. Closing a stream multiple times will not
Packit ae235b
 * return an error.
Packit ae235b
 *
Packit ae235b
 * Closing a stream will automatically flush any outstanding buffers
Packit ae235b
 * in the stream.
Packit ae235b
 *
Packit ae235b
 * Streams will be automatically closed when the last reference
Packit ae235b
 * is dropped, but you might want to call this function to make sure
Packit ae235b
 * resources are released as early as possible.
Packit ae235b
 *
Packit ae235b
 * Some streams might keep the backing store of the stream (e.g. a file
Packit ae235b
 * descriptor) open after the stream is closed. See the documentation for
Packit ae235b
 * the individual stream for details.
Packit ae235b
 *
Packit ae235b
 * On failure the first error that happened will be reported, but the
Packit ae235b
 * close operation will finish as much as possible. A stream that failed
Packit ae235b
 * to close will still return %G_IO_ERROR_CLOSED for all operations.
Packit ae235b
 * Still, it is important to check and report the error to the user,
Packit ae235b
 * otherwise there might be a loss of data as all data might not be written.
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.
Packit ae235b
 * Cancelling a close will still leave the stream closed, but some streams
Packit ae235b
 * can use a faster close that doesn't block to e.g. check errors.
Packit ae235b
 *
Packit ae235b
 * The default implementation of this method just calls close on the
Packit ae235b
 * individual input/output streams.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE on failure
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_io_stream_close (GIOStream     *stream,
Packit ae235b
		   GCancellable  *cancellable,
Packit ae235b
		   GError       **error)
Packit ae235b
{
Packit ae235b
  GIOStreamClass *class;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  class = G_IO_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  if (stream->priv->closed)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  if (!g_io_stream_set_pending (stream, error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_push_current (cancellable);
Packit ae235b
Packit ae235b
  res = TRUE;
Packit ae235b
  if (class->close_fn)
Packit ae235b
    res = class->close_fn (stream, cancellable, error);
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_pop_current (cancellable);
Packit ae235b
Packit ae235b
  stream->priv->closed = TRUE;
Packit ae235b
  g_io_stream_clear_pending (stream);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
async_ready_close_callback_wrapper (GObject      *source_object,
Packit ae235b
                                    GAsyncResult *res,
Packit ae235b
                                    gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GIOStream *stream = G_IO_STREAM (source_object);
Packit ae235b
  GIOStreamClass *klass = G_IO_STREAM_GET_CLASS (stream);
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gboolean success;
Packit ae235b
Packit ae235b
  stream->priv->closed = TRUE;
Packit ae235b
  g_io_stream_clear_pending (stream);
Packit ae235b
Packit ae235b
  if (g_async_result_legacy_propagate_error (res, &error))
Packit ae235b
    success = FALSE;
Packit ae235b
  else
Packit ae235b
    success = klass->close_finish (stream, res, &error);
Packit ae235b
Packit ae235b
  if (error)
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  else
Packit ae235b
    g_task_return_boolean (task, success);
Packit ae235b
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_stream_close_async:
Packit ae235b
 * @stream: a #GIOStream
Packit ae235b
 * @io_priority: the io priority of the request
Packit ae235b
 * @cancellable: (nullable): optional cancellable object
Packit ae235b
 * @callback: (scope async): callback to call when the request is satisfied
Packit ae235b
 * @user_data: (closure): the data to pass to callback function
Packit ae235b
 *
Packit ae235b
 * Requests an asynchronous close of the stream, releasing resources
Packit ae235b
 * related to it. When the operation is finished @callback will be
Packit ae235b
 * called. You can then call g_io_stream_close_finish() to get
Packit ae235b
 * the result of the operation.
Packit ae235b
 *
Packit ae235b
 * For behaviour details see g_io_stream_close().
Packit ae235b
 *
Packit ae235b
 * The asynchronous methods have a default fallback that uses threads
Packit ae235b
 * to implement asynchronicity, so they are optional for inheriting
Packit ae235b
 * classes. However, if you override one you must override all.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_io_stream_close_async (GIOStream           *stream,
Packit ae235b
			 int                  io_priority,
Packit ae235b
			 GCancellable        *cancellable,
Packit ae235b
			 GAsyncReadyCallback  callback,
Packit ae235b
			 gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GIOStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_IO_STREAM (stream));
Packit ae235b
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_io_stream_close_async);
Packit ae235b
Packit ae235b
  if (stream->priv->closed)
Packit ae235b
    {
Packit ae235b
      g_task_return_boolean (task, TRUE);
Packit ae235b
      g_object_unref (task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_io_stream_set_pending (stream, &error))
Packit ae235b
    {
Packit ae235b
      g_task_return_error (task, error);
Packit ae235b
      g_object_unref (task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  class = G_IO_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  class->close_async (stream, io_priority, cancellable,
Packit ae235b
		      async_ready_close_callback_wrapper, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_stream_close_finish:
Packit ae235b
 * @stream: a #GIOStream
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
 * Closes a stream.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_io_stream_close_finish (GIOStream     *stream,
Packit ae235b
			  GAsyncResult  *result,
Packit ae235b
			  GError       **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
Packit ae235b
Packit ae235b
  return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
}
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
  GIOStream *stream = source_object;
Packit ae235b
  GIOStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gboolean result;
Packit ae235b
Packit ae235b
  class = G_IO_STREAM_GET_CLASS (stream);
Packit ae235b
  if (class->close_fn)
Packit ae235b
    {
Packit ae235b
      result = class->close_fn (stream,
Packit ae235b
                                g_task_get_cancellable (task),
Packit ae235b
                                &error);
Packit ae235b
      if (!result)
Packit ae235b
        {
Packit ae235b
          g_task_return_error (task, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_task_return_boolean (task, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  gint pending;
Packit ae235b
} CloseAsyncData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
stream_close_complete (GObject      *source,
Packit ae235b
                       GAsyncResult *result,
Packit ae235b
                       gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  CloseAsyncData *data;
Packit ae235b
Packit ae235b
  data = g_task_get_task_data (task);
Packit ae235b
  data->pending--;
Packit ae235b
Packit ae235b
  if (G_IS_OUTPUT_STREAM (source))
Packit ae235b
    {
Packit ae235b
      GError *error = NULL;
Packit ae235b
Packit ae235b
      /* Match behaviour with the sync route and give precedent to the
Packit ae235b
       * error returned from closing the output stream.
Packit ae235b
       */
Packit ae235b
      g_output_stream_close_finish (G_OUTPUT_STREAM (source), result, &error);
Packit ae235b
      if (error)
Packit ae235b
        {
Packit ae235b
          if (data->error)
Packit ae235b
            g_error_free (data->error);
Packit ae235b
          data->error = error;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    g_input_stream_close_finish (G_INPUT_STREAM (source), result, data->error ? NULL : &data->error);
Packit ae235b
Packit ae235b
  if (data->pending == 0)
Packit ae235b
    {
Packit ae235b
      if (data->error)
Packit ae235b
        g_task_return_error (task, data->error);
Packit ae235b
      else
Packit ae235b
        g_task_return_boolean (task, TRUE);
Packit ae235b
Packit ae235b
      g_slice_free (CloseAsyncData, data);
Packit ae235b
      g_object_unref (task);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_io_stream_real_close_async (GIOStream           *stream,
Packit ae235b
			      int                  io_priority,
Packit ae235b
			      GCancellable        *cancellable,
Packit ae235b
			      GAsyncReadyCallback  callback,
Packit ae235b
			      gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GInputStream *input;
Packit ae235b
  GOutputStream *output;
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_io_stream_real_close_async);
Packit ae235b
  g_task_set_check_cancellable (task, FALSE);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
Packit ae235b
  input = g_io_stream_get_input_stream (stream);
Packit ae235b
  output = g_io_stream_get_output_stream (stream);
Packit ae235b
Packit ae235b
  if (g_input_stream_async_close_is_via_threads (input) && g_output_stream_async_close_is_via_threads (output))
Packit ae235b
    {
Packit ae235b
      /* No sense in dispatching to the thread twice -- just do it all
Packit ae235b
       * in one go.
Packit ae235b
       */
Packit ae235b
      g_task_run_in_thread (task, close_async_thread);
Packit ae235b
      g_object_unref (task);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      CloseAsyncData *data;
Packit ae235b
Packit ae235b
      /* We should avoid dispatching to another thread in case either
Packit ae235b
       * object that would not do it for itself because it may not be
Packit ae235b
       * threadsafe.
Packit ae235b
       */
Packit ae235b
      data = g_slice_new (CloseAsyncData);
Packit ae235b
      data->error = NULL;
Packit ae235b
      data->pending = 2;
Packit ae235b
Packit ae235b
      g_task_set_task_data (task, data, NULL);
Packit ae235b
      g_input_stream_close_async (input, io_priority, cancellable, stream_close_complete, task);
Packit ae235b
      g_output_stream_close_async (output, io_priority, cancellable, stream_close_complete, task);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_io_stream_real_close_finish (GIOStream     *stream,
Packit ae235b
			       GAsyncResult  *result,
Packit ae235b
			       GError       **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
Packit ae235b
Packit ae235b
  return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GIOStream *stream1;
Packit ae235b
  GIOStream *stream2;
Packit ae235b
  GIOStreamSpliceFlags flags;
Packit ae235b
  gint io_priority;
Packit ae235b
  GCancellable *cancellable;
Packit ae235b
  gulong cancelled_id;
Packit ae235b
  GCancellable *op1_cancellable;
Packit ae235b
  GCancellable *op2_cancellable;
Packit ae235b
  guint completed;
Packit ae235b
  GError *error;
Packit ae235b
} SpliceContext;
Packit ae235b
Packit ae235b
static void
Packit ae235b
splice_context_free (SpliceContext *ctx)
Packit ae235b
{
Packit ae235b
  g_object_unref (ctx->stream1);
Packit ae235b
  g_object_unref (ctx->stream2);
Packit ae235b
  if (ctx->cancellable != NULL)
Packit ae235b
    g_object_unref (ctx->cancellable);
Packit ae235b
  g_object_unref (ctx->op1_cancellable);
Packit ae235b
  g_object_unref (ctx->op2_cancellable);
Packit ae235b
  g_clear_error (&ctx->error);
Packit ae235b
  g_slice_free (SpliceContext, ctx);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
splice_complete (GTask         *task,
Packit ae235b
                 SpliceContext *ctx)
Packit ae235b
{
Packit ae235b
  if (ctx->cancelled_id != 0)
Packit ae235b
    g_cancellable_disconnect (ctx->cancellable, ctx->cancelled_id);
Packit ae235b
  ctx->cancelled_id = 0;
Packit ae235b
Packit ae235b
  if (ctx->error != NULL)
Packit ae235b
    {
Packit ae235b
      g_task_return_error (task, ctx->error);
Packit ae235b
      ctx->error = NULL;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    g_task_return_boolean (task, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
splice_close_cb (GObject      *iostream,
Packit ae235b
                 GAsyncResult *res,
Packit ae235b
                 gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  SpliceContext *ctx = g_task_get_task_data (task);
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_io_stream_close_finish (G_IO_STREAM (iostream), res, &error);
Packit ae235b
Packit ae235b
  ctx->completed++;
Packit ae235b
Packit ae235b
  /* Keep the first error that occurred */
Packit ae235b
  if (error != NULL && ctx->error == NULL)
Packit ae235b
    ctx->error = error;
Packit ae235b
  else
Packit ae235b
    g_clear_error (&error);
Packit ae235b
Packit ae235b
  /* If all operations are done, complete now */
Packit ae235b
  if (ctx->completed == 4)
Packit ae235b
    splice_complete (task, ctx);
Packit ae235b
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
splice_cb (GObject      *ostream,
Packit ae235b
           GAsyncResult *res,
Packit ae235b
           gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  SpliceContext *ctx = g_task_get_task_data (task);
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_output_stream_splice_finish (G_OUTPUT_STREAM (ostream), res, &error);
Packit ae235b
Packit ae235b
  ctx->completed++;
Packit ae235b
Packit ae235b
  /* ignore cancellation error if it was not requested by the user */
Packit ae235b
  if (error != NULL &&
Packit ae235b
      g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
Packit ae235b
      (ctx->cancellable == NULL ||
Packit ae235b
       !g_cancellable_is_cancelled (ctx->cancellable)))
Packit ae235b
    g_clear_error (&error);
Packit ae235b
Packit ae235b
  /* Keep the first error that occurred */
Packit ae235b
  if (error != NULL && ctx->error == NULL)
Packit ae235b
    ctx->error = error;
Packit ae235b
  else
Packit ae235b
    g_clear_error (&error);
Packit ae235b
Packit ae235b
   if (ctx->completed == 1 &&
Packit ae235b
       (ctx->flags & G_IO_STREAM_SPLICE_WAIT_FOR_BOTH) == 0)
Packit ae235b
    {
Packit ae235b
      /* We don't want to wait for the 2nd operation to finish, cancel it */
Packit ae235b
      g_cancellable_cancel (ctx->op1_cancellable);
Packit ae235b
      g_cancellable_cancel (ctx->op2_cancellable);
Packit ae235b
    }
Packit ae235b
  else if (ctx->completed == 2)
Packit ae235b
    {
Packit ae235b
      if (ctx->cancellable == NULL ||
Packit ae235b
          !g_cancellable_is_cancelled (ctx->cancellable))
Packit ae235b
        {
Packit ae235b
          g_cancellable_reset (ctx->op1_cancellable);
Packit ae235b
          g_cancellable_reset (ctx->op2_cancellable);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      /* Close the IO streams if needed */
Packit ae235b
      if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM1) != 0)
Packit ae235b
	{
Packit ae235b
	  g_io_stream_close_async (ctx->stream1,
Packit ae235b
                                   g_task_get_priority (task),
Packit ae235b
                                   ctx->op1_cancellable,
Packit ae235b
                                   splice_close_cb, g_object_ref (task));
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
        ctx->completed++;
Packit ae235b
Packit ae235b
      if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM2) != 0)
Packit ae235b
	{
Packit ae235b
	  g_io_stream_close_async (ctx->stream2,
Packit ae235b
                                   g_task_get_priority (task),
Packit ae235b
                                   ctx->op2_cancellable,
Packit ae235b
                                   splice_close_cb, g_object_ref (task));
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
        ctx->completed++;
Packit ae235b
Packit ae235b
      /* If all operations are done, complete now */
Packit ae235b
      if (ctx->completed == 4)
Packit ae235b
        splice_complete (task, ctx);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
splice_cancelled_cb (GCancellable *cancellable,
Packit ae235b
                     GTask        *task)
Packit ae235b
{
Packit ae235b
  SpliceContext *ctx;
Packit ae235b
Packit ae235b
  ctx = g_task_get_task_data (task);
Packit ae235b
  g_cancellable_cancel (ctx->op1_cancellable);
Packit ae235b
  g_cancellable_cancel (ctx->op2_cancellable);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_stream_splice_async:
Packit ae235b
 * @stream1: a #GIOStream.
Packit ae235b
 * @stream2: a #GIOStream.
Packit ae235b
 * @flags: a set of #GIOStreamSpliceFlags.
Packit ae235b
 * @io_priority: the io priority of the request.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback.
Packit ae235b
 * @user_data: (closure): user data passed to @callback.
Packit ae235b
 *
Packit ae235b
 * Asyncronously splice the output stream of @stream1 to the input stream of
Packit ae235b
 * @stream2, and splice the output stream of @stream2 to the input stream of
Packit ae235b
 * @stream1.
Packit ae235b
 *
Packit ae235b
 * When the operation is finished @callback will be called.
Packit ae235b
 * You can then call g_io_stream_splice_finish() to get the
Packit ae235b
 * result of the operation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_io_stream_splice_async (GIOStream            *stream1,
Packit ae235b
                          GIOStream            *stream2,
Packit ae235b
                          GIOStreamSpliceFlags  flags,
Packit ae235b
                          gint                  io_priority,
Packit ae235b
                          GCancellable         *cancellable,
Packit ae235b
                          GAsyncReadyCallback   callback,
Packit ae235b
                          gpointer              user_data)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
  SpliceContext *ctx;
Packit ae235b
  GInputStream *istream;
Packit ae235b
  GOutputStream *ostream;
Packit ae235b
Packit ae235b
  if (cancellable != NULL && g_cancellable_is_cancelled (cancellable))
Packit ae235b
    {
Packit ae235b
      g_task_report_new_error (NULL, callback, user_data,
Packit ae235b
                               g_io_stream_splice_async,
Packit ae235b
                               G_IO_ERROR, G_IO_ERROR_CANCELLED,
Packit ae235b
                               "Operation has been cancelled");
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  ctx = g_slice_new0 (SpliceContext);
Packit ae235b
  ctx->stream1 = g_object_ref (stream1);
Packit ae235b
  ctx->stream2 = g_object_ref (stream2);
Packit ae235b
  ctx->flags = flags;
Packit ae235b
  ctx->op1_cancellable = g_cancellable_new ();
Packit ae235b
  ctx->op2_cancellable = g_cancellable_new ();
Packit ae235b
  ctx->completed = 0;
Packit ae235b
Packit ae235b
  task = g_task_new (NULL, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_io_stream_splice_async);
Packit ae235b
  g_task_set_task_data (task, ctx, (GDestroyNotify) splice_context_free);
Packit ae235b
Packit ae235b
  if (cancellable != NULL)
Packit ae235b
    {
Packit ae235b
      ctx->cancellable = g_object_ref (cancellable);
Packit ae235b
      ctx->cancelled_id = g_cancellable_connect (cancellable,
Packit ae235b
          G_CALLBACK (splice_cancelled_cb), g_object_ref (task),
Packit ae235b
          g_object_unref);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  istream = g_io_stream_get_input_stream (stream1);
Packit ae235b
  ostream = g_io_stream_get_output_stream (stream2);
Packit ae235b
  g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
Packit ae235b
      io_priority, ctx->op1_cancellable, splice_cb,
Packit ae235b
      g_object_ref (task));
Packit ae235b
Packit ae235b
  istream = g_io_stream_get_input_stream (stream2);
Packit ae235b
  ostream = g_io_stream_get_output_stream (stream1);
Packit ae235b
  g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
Packit ae235b
      io_priority, ctx->op2_cancellable, splice_cb,
Packit ae235b
      g_object_ref (task));
Packit ae235b
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_stream_splice_finish:
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 an asynchronous io stream splice operation.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE otherwise.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_io_stream_splice_finish (GAsyncResult  *result,
Packit ae235b
                           GError       **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);
Packit ae235b
Packit ae235b
  return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
}