Blame gio/goutputstream.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 <string.h>
Packit ae235b
#include "goutputstream.h"
Packit ae235b
#include "gcancellable.h"
Packit ae235b
#include "gasyncresult.h"
Packit ae235b
#include "gtask.h"
Packit ae235b
#include "ginputstream.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "gioprivate.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
#include "gpollableoutputstream.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:goutputstream
Packit ae235b
 * @short_description: Base class for implementing streaming output
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * #GOutputStream has functions to write to a stream (g_output_stream_write()),
Packit ae235b
 * to close a stream (g_output_stream_close()) and to flush pending writes
Packit ae235b
 * (g_output_stream_flush()). 
Packit ae235b
 *
Packit ae235b
 * To copy the content of an input stream to an output stream without 
Packit ae235b
 * manually handling the reads and writes, use g_output_stream_splice().
Packit ae235b
 *
Packit ae235b
 * See the documentation for #GIOStream for details of thread safety of
Packit ae235b
 * streaming APIs.
Packit ae235b
 *
Packit ae235b
 * All of these functions have async variants too.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
struct _GOutputStreamPrivate {
Packit ae235b
  guint closed : 1;
Packit ae235b
  guint pending : 1;
Packit ae235b
  guint closing : 1;
Packit ae235b
  GAsyncReadyCallback outstanding_callback;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GOutputStream, g_output_stream, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static gssize   g_output_stream_real_splice        (GOutputStream             *stream,
Packit ae235b
						    GInputStream              *source,
Packit ae235b
						    GOutputStreamSpliceFlags   flags,
Packit ae235b
						    GCancellable              *cancellable,
Packit ae235b
						    GError                   **error);
Packit ae235b
static void     g_output_stream_real_write_async   (GOutputStream             *stream,
Packit ae235b
						    const void                *buffer,
Packit ae235b
						    gsize                      count,
Packit ae235b
						    int                        io_priority,
Packit ae235b
						    GCancellable              *cancellable,
Packit ae235b
						    GAsyncReadyCallback        callback,
Packit ae235b
						    gpointer                   data);
Packit ae235b
static gssize   g_output_stream_real_write_finish  (GOutputStream             *stream,
Packit ae235b
						    GAsyncResult              *result,
Packit ae235b
						    GError                   **error);
Packit ae235b
static void     g_output_stream_real_splice_async  (GOutputStream             *stream,
Packit ae235b
						    GInputStream              *source,
Packit ae235b
						    GOutputStreamSpliceFlags   flags,
Packit ae235b
						    int                        io_priority,
Packit ae235b
						    GCancellable              *cancellable,
Packit ae235b
						    GAsyncReadyCallback        callback,
Packit ae235b
						    gpointer                   data);
Packit ae235b
static gssize   g_output_stream_real_splice_finish (GOutputStream             *stream,
Packit ae235b
						    GAsyncResult              *result,
Packit ae235b
						    GError                   **error);
Packit ae235b
static void     g_output_stream_real_flush_async   (GOutputStream             *stream,
Packit ae235b
						    int                        io_priority,
Packit ae235b
						    GCancellable              *cancellable,
Packit ae235b
						    GAsyncReadyCallback        callback,
Packit ae235b
						    gpointer                   data);
Packit ae235b
static gboolean g_output_stream_real_flush_finish  (GOutputStream             *stream,
Packit ae235b
						    GAsyncResult              *result,
Packit ae235b
						    GError                   **error);
Packit ae235b
static void     g_output_stream_real_close_async   (GOutputStream             *stream,
Packit ae235b
						    int                        io_priority,
Packit ae235b
						    GCancellable              *cancellable,
Packit ae235b
						    GAsyncReadyCallback        callback,
Packit ae235b
						    gpointer                   data);
Packit ae235b
static gboolean g_output_stream_real_close_finish  (GOutputStream             *stream,
Packit ae235b
						    GAsyncResult              *result,
Packit ae235b
						    GError                   **error);
Packit ae235b
static gboolean g_output_stream_internal_close     (GOutputStream             *stream,
Packit ae235b
                                                    GCancellable              *cancellable,
Packit ae235b
                                                    GError                   **error);
Packit ae235b
static void     g_output_stream_internal_close_async (GOutputStream           *stream,
Packit ae235b
                                                      int                      io_priority,
Packit ae235b
                                                      GCancellable            *cancellable,
Packit ae235b
                                                      GAsyncReadyCallback      callback,
Packit ae235b
                                                      gpointer                 data);
Packit ae235b
static gboolean g_output_stream_internal_close_finish (GOutputStream          *stream,
Packit ae235b
                                                       GAsyncResult           *result,
Packit ae235b
                                                       GError                **error);
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_output_stream_dispose (GObject *object)
Packit ae235b
{
Packit ae235b
  GOutputStream *stream;
Packit ae235b
Packit ae235b
  stream = G_OUTPUT_STREAM (object);
Packit ae235b
  
Packit ae235b
  if (!stream->priv->closed)
Packit ae235b
    g_output_stream_close (stream, NULL, NULL);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_output_stream_class_init (GOutputStreamClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->dispose = g_output_stream_dispose;
Packit ae235b
Packit ae235b
  klass->splice = g_output_stream_real_splice;
Packit ae235b
  
Packit ae235b
  klass->write_async = g_output_stream_real_write_async;
Packit ae235b
  klass->write_finish = g_output_stream_real_write_finish;
Packit ae235b
  klass->splice_async = g_output_stream_real_splice_async;
Packit ae235b
  klass->splice_finish = g_output_stream_real_splice_finish;
Packit ae235b
  klass->flush_async = g_output_stream_real_flush_async;
Packit ae235b
  klass->flush_finish = g_output_stream_real_flush_finish;
Packit ae235b
  klass->close_async = g_output_stream_real_close_async;
Packit ae235b
  klass->close_finish = g_output_stream_real_close_finish;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_output_stream_init (GOutputStream *stream)
Packit ae235b
{
Packit ae235b
  stream->priv = g_output_stream_get_instance_private (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_write:
Packit ae235b
 * @stream: a #GOutputStream.
Packit ae235b
 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write. 
Packit ae235b
 * @count: the number of bytes to write
Packit ae235b
 * @cancellable: (nullable): optional cancellable object
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore
Packit ae235b
 *
Packit ae235b
 * Tries to write @count bytes from @buffer into the stream. Will block
Packit ae235b
 * during the operation.
Packit ae235b
 * 
Packit ae235b
 * If count is 0, returns 0 and does nothing. A value of @count
Packit ae235b
 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
Packit ae235b
 *
Packit ae235b
 * On success, the number of bytes written to the stream is returned.
Packit ae235b
 * It is not an error if this is not the same as the requested size, as it
Packit ae235b
 * can happen e.g. on a partial I/O error, or if there is not enough
Packit ae235b
 * storage in the stream. All writes block until at least one byte
Packit ae235b
 * is written or an error occurs; 0 is never returned (unless
Packit ae235b
 * @count is 0).
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. If an
Packit ae235b
 * operation was partially finished when the operation was cancelled the
Packit ae235b
 * partial result will be returned, without an error.
Packit ae235b
 *
Packit ae235b
 * On error -1 is returned and @error is set accordingly.
Packit ae235b
 * 
Packit ae235b
 * Virtual: write_fn
Packit ae235b
 *
Packit ae235b
 * Returns: Number of bytes written, or -1 on error
Packit ae235b
 **/
Packit ae235b
gssize
Packit ae235b
g_output_stream_write (GOutputStream  *stream,
Packit ae235b
		       const void     *buffer,
Packit ae235b
		       gsize           count,
Packit ae235b
		       GCancellable   *cancellable,
Packit ae235b
		       GError        **error)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  gssize res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
Packit ae235b
  g_return_val_if_fail (buffer != NULL, 0);
Packit ae235b
Packit ae235b
  if (count == 0)
Packit ae235b
    return 0;
Packit ae235b
  
Packit ae235b
  if (((gssize) count) < 0)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
		   _("Too large count value passed to %s"), G_STRFUNC);
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  if (class->write_fn == NULL) 
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                           _("Output stream doesn’t implement write"));
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (!g_output_stream_set_pending (stream, error))
Packit ae235b
    return -1;
Packit ae235b
  
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_push_current (cancellable);
Packit ae235b
  
Packit ae235b
  res = class->write_fn (stream, buffer, count, cancellable, error);
Packit ae235b
  
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_pop_current (cancellable);
Packit ae235b
  
Packit ae235b
  g_output_stream_clear_pending (stream);
Packit ae235b
Packit ae235b
  return res; 
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_write_all:
Packit ae235b
 * @stream: a #GOutputStream.
Packit ae235b
 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write. 
Packit ae235b
 * @count: the number of bytes to write
Packit ae235b
 * @bytes_written: (out) (optional): location to store the number of bytes that was
Packit ae235b
 *     written to the stream
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
 * Tries to write @count bytes from @buffer into the stream. Will block
Packit ae235b
 * during the operation.
Packit ae235b
 * 
Packit ae235b
 * This function is similar to g_output_stream_write(), except it tries to
Packit ae235b
 * write as many bytes as requested, only stopping on an error.
Packit ae235b
 *
Packit ae235b
 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
Packit ae235b
 * is set to @count.
Packit ae235b
 * 
Packit ae235b
 * If there is an error during the operation %FALSE is returned and @error
Packit ae235b
 * is set to indicate the error status.
Packit ae235b
 *
Packit ae235b
 * As a special exception to the normal conventions for functions that
Packit ae235b
 * use #GError, if this function returns %FALSE (and sets @error) then
Packit ae235b
 * @bytes_written will be set to the number of bytes that were
Packit ae235b
 * successfully written before the error was encountered.  This
Packit ae235b
 * functionality is only available from C.  If you need it from another
Packit ae235b
 * language then you must write your own loop around
Packit ae235b
 * g_output_stream_write().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if there was an error
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_output_stream_write_all (GOutputStream  *stream,
Packit ae235b
			   const void     *buffer,
Packit ae235b
			   gsize           count,
Packit ae235b
			   gsize          *bytes_written,
Packit ae235b
			   GCancellable   *cancellable,
Packit ae235b
			   GError        **error)
Packit ae235b
{
Packit ae235b
  gsize _bytes_written;
Packit ae235b
  gssize res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
  g_return_val_if_fail (buffer != NULL, FALSE);
Packit ae235b
Packit ae235b
  _bytes_written = 0;
Packit ae235b
  while (_bytes_written < count)
Packit ae235b
    {
Packit ae235b
      res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
Packit ae235b
				   cancellable, error);
Packit ae235b
      if (res == -1)
Packit ae235b
	{
Packit ae235b
	  if (bytes_written)
Packit ae235b
	    *bytes_written = _bytes_written;
Packit ae235b
	  return FALSE;
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      if (res == 0)
Packit ae235b
	g_warning ("Write returned zero without error");
Packit ae235b
Packit ae235b
      _bytes_written += res;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (bytes_written)
Packit ae235b
    *bytes_written = _bytes_written;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_printf:
Packit ae235b
 * @stream: a #GOutputStream.
Packit ae235b
 * @bytes_written: (out) (optional): location to store the number of bytes that was
Packit ae235b
 *     written to the stream
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
 * @format: the format string. See the printf() documentation
Packit ae235b
 * @...: the parameters to insert into the format string
Packit ae235b
 *
Packit ae235b
 * This is a utility function around g_output_stream_write_all(). It
Packit ae235b
 * uses g_strdup_vprintf() to turn @format and @... into a string that
Packit ae235b
 * is then written to @stream.
Packit ae235b
 *
Packit ae235b
 * See the documentation of g_output_stream_write_all() about the
Packit ae235b
 * behavior of the actual write operation.
Packit ae235b
 *
Packit ae235b
 * Note that partial writes cannot be properly checked with this
Packit ae235b
 * function due to the variable length of the written string, if you
Packit ae235b
 * need precise control over partial write failures, you need to
Packit ae235b
 * create you own printf()-like wrapper around g_output_stream_write()
Packit ae235b
 * or g_output_stream_write_all().
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if there was an error
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_output_stream_printf (GOutputStream  *stream,
Packit ae235b
                        gsize          *bytes_written,
Packit ae235b
                        GCancellable   *cancellable,
Packit ae235b
                        GError        **error,
Packit ae235b
                        const gchar    *format,
Packit ae235b
                        ...)
Packit ae235b
{
Packit ae235b
  va_list  args;
Packit ae235b
  gboolean success;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  success = g_output_stream_vprintf (stream, bytes_written, cancellable,
Packit ae235b
                                     error, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  return success;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_vprintf:
Packit ae235b
 * @stream: a #GOutputStream.
Packit ae235b
 * @bytes_written: (out) (optional): location to store the number of bytes that was
Packit ae235b
 *     written to the stream
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
 * @format: the format string. See the printf() documentation
Packit ae235b
 * @args: the parameters to insert into the format string
Packit ae235b
 *
Packit ae235b
 * This is a utility function around g_output_stream_write_all(). It
Packit ae235b
 * uses g_strdup_vprintf() to turn @format and @args into a string that
Packit ae235b
 * is then written to @stream.
Packit ae235b
 *
Packit ae235b
 * See the documentation of g_output_stream_write_all() about the
Packit ae235b
 * behavior of the actual write operation.
Packit ae235b
 *
Packit ae235b
 * Note that partial writes cannot be properly checked with this
Packit ae235b
 * function due to the variable length of the written string, if you
Packit ae235b
 * need precise control over partial write failures, you need to
Packit ae235b
 * create you own printf()-like wrapper around g_output_stream_write()
Packit ae235b
 * or g_output_stream_write_all().
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if there was an error
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_output_stream_vprintf (GOutputStream  *stream,
Packit ae235b
                         gsize          *bytes_written,
Packit ae235b
                         GCancellable   *cancellable,
Packit ae235b
                         GError        **error,
Packit ae235b
                         const gchar    *format,
Packit ae235b
                         va_list         args)
Packit ae235b
{
Packit ae235b
  gchar    *text;
Packit ae235b
  gboolean  success;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (format != NULL, FALSE);
Packit ae235b
Packit ae235b
  text = g_strdup_vprintf (format, args);
Packit ae235b
  success = g_output_stream_write_all (stream,
Packit ae235b
                                       text, strlen (text),
Packit ae235b
                                       bytes_written, cancellable, error);
Packit ae235b
  g_free (text);
Packit ae235b
Packit ae235b
  return success;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_write_bytes:
Packit ae235b
 * @stream: a #GOutputStream.
Packit ae235b
 * @bytes: the #GBytes to write
Packit ae235b
 * @cancellable: (nullable): optional cancellable object
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore
Packit ae235b
 *
Packit ae235b
 * A wrapper function for g_output_stream_write() which takes a
Packit ae235b
 * #GBytes as input.  This can be more convenient for use by language
Packit ae235b
 * bindings or in other cases where the refcounted nature of #GBytes
Packit ae235b
 * is helpful over a bare pointer interface.
Packit ae235b
 *
Packit ae235b
 * However, note that this function may still perform partial writes,
Packit ae235b
 * just like g_output_stream_write().  If that occurs, to continue
Packit ae235b
 * writing, you will need to create a new #GBytes containing just the
Packit ae235b
 * remaining bytes, using g_bytes_new_from_bytes(). Passing the same
Packit ae235b
 * #GBytes instance multiple times potentially can result in duplicated
Packit ae235b
 * data in the output stream.
Packit ae235b
 *
Packit ae235b
 * Returns: Number of bytes written, or -1 on error
Packit ae235b
 **/
Packit ae235b
gssize
Packit ae235b
g_output_stream_write_bytes (GOutputStream  *stream,
Packit ae235b
			     GBytes         *bytes,
Packit ae235b
			     GCancellable   *cancellable,
Packit ae235b
			     GError        **error)
Packit ae235b
{
Packit ae235b
  gsize size;
Packit ae235b
  gconstpointer data;
Packit ae235b
Packit ae235b
  data = g_bytes_get_data (bytes, &size);
Packit ae235b
Packit ae235b
  return g_output_stream_write (stream,
Packit ae235b
                                data, size,
Packit ae235b
				cancellable,
Packit ae235b
				error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_flush:
Packit ae235b
 * @stream: a #GOutputStream.
Packit ae235b
 * @cancellable: (nullable): optional cancellable object
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore
Packit ae235b
 *
Packit ae235b
 * Forces a write of all user-space buffered data for the given
Packit ae235b
 * @stream. Will block during the operation. Closing the stream will
Packit ae235b
 * implicitly cause a flush.
Packit ae235b
 *
Packit ae235b
 * This function is optional for inherited classes.
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
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE on error
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_output_stream_flush (GOutputStream  *stream,
Packit ae235b
                       GCancellable   *cancellable,
Packit ae235b
                       GError        **error)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  if (!g_output_stream_set_pending (stream, error))
Packit ae235b
    return FALSE;
Packit ae235b
  
Packit ae235b
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  res = TRUE;
Packit ae235b
  if (class->flush)
Packit ae235b
    {
Packit ae235b
      if (cancellable)
Packit ae235b
	g_cancellable_push_current (cancellable);
Packit ae235b
      
Packit ae235b
      res = class->flush (stream, cancellable, error);
Packit ae235b
      
Packit ae235b
      if (cancellable)
Packit ae235b
	g_cancellable_pop_current (cancellable);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  g_output_stream_clear_pending (stream);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_splice:
Packit ae235b
 * @stream: a #GOutputStream.
Packit ae235b
 * @source: a #GInputStream.
Packit ae235b
 * @flags: a set of #GOutputStreamSpliceFlags.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to
Packit ae235b
 * ignore.
Packit ae235b
 *
Packit ae235b
 * Splices an input stream into an output stream.
Packit ae235b
 *
Packit ae235b
 * Returns: a #gssize containing the size of the data spliced, or
Packit ae235b
 *     -1 if an error occurred. Note that if the number of bytes
Packit ae235b
 *     spliced is greater than %G_MAXSSIZE, then that will be
Packit ae235b
 *     returned, and there is no way to determine the actual number
Packit ae235b
 *     of bytes spliced.
Packit ae235b
 **/
Packit ae235b
gssize
Packit ae235b
g_output_stream_splice (GOutputStream             *stream,
Packit ae235b
			GInputStream              *source,
Packit ae235b
			GOutputStreamSpliceFlags   flags,
Packit ae235b
			GCancellable              *cancellable,
Packit ae235b
			GError                   **error)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  gssize bytes_copied;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
Packit ae235b
Packit ae235b
  if (g_input_stream_is_closed (source))
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
Packit ae235b
                           _("Source stream is already closed"));
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_output_stream_set_pending (stream, error))
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_push_current (cancellable);
Packit ae235b
Packit ae235b
  bytes_copied = class->splice (stream, source, flags, cancellable, error);
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_pop_current (cancellable);
Packit ae235b
Packit ae235b
  g_output_stream_clear_pending (stream);
Packit ae235b
Packit ae235b
  return bytes_copied;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_output_stream_real_splice (GOutputStream             *stream,
Packit ae235b
                             GInputStream              *source,
Packit ae235b
                             GOutputStreamSpliceFlags   flags,
Packit ae235b
                             GCancellable              *cancellable,
Packit ae235b
                             GError                   **error)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  gssize n_read, n_written;
Packit ae235b
  gsize bytes_copied;
Packit ae235b
  char buffer[8192], *p;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  bytes_copied = 0;
Packit ae235b
  if (class->write_fn == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                           _("Output stream doesn’t implement write"));
Packit ae235b
      res = FALSE;
Packit ae235b
      goto notsupported;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  res = TRUE;
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
Packit ae235b
      if (n_read == -1)
Packit ae235b
	{
Packit ae235b
	  res = FALSE;
Packit ae235b
	  break;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (n_read == 0)
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      p = buffer;
Packit ae235b
      while (n_read > 0)
Packit ae235b
	{
Packit ae235b
	  n_written = class->write_fn (stream, p, n_read, cancellable, error);
Packit ae235b
	  if (n_written == -1)
Packit ae235b
	    {
Packit ae235b
	      res = FALSE;
Packit ae235b
	      break;
Packit ae235b
	    }
Packit ae235b
Packit ae235b
	  p += n_written;
Packit ae235b
	  n_read -= n_written;
Packit ae235b
	  bytes_copied += n_written;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (bytes_copied > G_MAXSSIZE)
Packit ae235b
	bytes_copied = G_MAXSSIZE;
Packit ae235b
    }
Packit ae235b
  while (res);
Packit ae235b
Packit ae235b
 notsupported:
Packit ae235b
  if (!res)
Packit ae235b
    error = NULL; /* Ignore further errors */
Packit ae235b
Packit ae235b
  if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
Packit ae235b
    {
Packit ae235b
      /* Don't care about errors in source here */
Packit ae235b
      g_input_stream_close (source, cancellable, NULL);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
Packit ae235b
    {
Packit ae235b
      /* But write errors on close are bad! */
Packit ae235b
      if (!g_output_stream_internal_close (stream, cancellable, error))
Packit ae235b
        res = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (res)
Packit ae235b
    return bytes_copied;
Packit ae235b
Packit ae235b
  return -1;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Must always be called inside
Packit ae235b
 * g_output_stream_set_pending()/g_output_stream_clear_pending(). */
Packit ae235b
static gboolean
Packit ae235b
g_output_stream_internal_close (GOutputStream  *stream,
Packit ae235b
                                GCancellable   *cancellable,
Packit ae235b
                                GError        **error)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  if (stream->priv->closed)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  stream->priv->closing = TRUE;
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_push_current (cancellable);
Packit ae235b
Packit ae235b
  if (class->flush)
Packit ae235b
    res = class->flush (stream, cancellable, error);
Packit ae235b
  else
Packit ae235b
    res = TRUE;
Packit ae235b
Packit ae235b
  if (!res)
Packit ae235b
    {
Packit ae235b
      /* flushing caused the error that we want to return,
Packit ae235b
       * but we still want to close the underlying stream if possible
Packit ae235b
       */
Packit ae235b
      if (class->close_fn)
Packit ae235b
        class->close_fn (stream, cancellable, NULL);
Packit ae235b
    }
Packit ae235b
  else
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
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_pop_current (cancellable);
Packit ae235b
Packit ae235b
  stream->priv->closing = FALSE;
Packit ae235b
  stream->priv->closed = TRUE;
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_close:
Packit ae235b
 * @stream: A #GOutputStream.
Packit ae235b
 * @cancellable: (nullable): optional cancellable object
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.
Packit ae235b
 *
Packit ae235b
 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
Packit ae235b
 * Closing a stream multiple times will not return an error.
Packit ae235b
 *
Packit ae235b
 * Closing a stream will automatically flush any outstanding buffers in the
Packit ae235b
 * 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 descriptor)
Packit ae235b
 * open after the stream is closed. See the documentation for the individual
Packit ae235b
 * stream for details.
Packit ae235b
 *
Packit ae235b
 * On failure the first error that happened will be reported, but the close
Packit ae235b
 * operation will finish as much as possible. A stream that failed to
Packit ae235b
 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
Packit ae235b
 * is important to check and report the error to the user, otherwise
Packit ae235b
 * 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 there some streams
Packit ae235b
 * can use a faster close that doesn't block to e.g. check errors. On
Packit ae235b
 * cancellation (as with any error) there is no guarantee that all written
Packit ae235b
 * data will reach the target. 
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE on failure
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_output_stream_close (GOutputStream  *stream,
Packit ae235b
		       GCancellable   *cancellable,
Packit ae235b
		       GError        **error)
Packit ae235b
{
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  if (stream->priv->closed)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  if (!g_output_stream_set_pending (stream, error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  res = g_output_stream_internal_close (stream, cancellable, error);
Packit ae235b
Packit ae235b
  g_output_stream_clear_pending (stream);
Packit ae235b
  
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
async_ready_write_callback_wrapper (GObject      *source_object,
Packit ae235b
                                    GAsyncResult *res,
Packit ae235b
                                    gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  gssize nwrote;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_output_stream_clear_pending (stream);
Packit ae235b
  
Packit ae235b
  if (g_async_result_legacy_propagate_error (res, &error))
Packit ae235b
    nwrote = -1;
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
      nwrote = class->write_finish (stream, res, &error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (nwrote >= 0)
Packit ae235b
    g_task_return_int (task, nwrote);
Packit ae235b
  else
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_write_async:
Packit ae235b
 * @stream: A #GOutputStream.
Packit ae235b
 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write. 
Packit ae235b
 * @count: the number of bytes to write
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): callback to call when the request is satisfied
Packit ae235b
 * @user_data: (closure): the data to pass to callback function
Packit ae235b
 *
Packit ae235b
 * Request an asynchronous write of @count bytes from @buffer into 
Packit ae235b
 * the stream. When the operation is finished @callback will be called.
Packit ae235b
 * You can then call g_output_stream_write_finish() to get the result of the 
Packit ae235b
 * operation.
Packit ae235b
 *
Packit ae235b
 * During an async request no other sync and async calls are allowed, 
Packit ae235b
 * and will result in %G_IO_ERROR_PENDING errors. 
Packit ae235b
 *
Packit ae235b
 * A value of @count larger than %G_MAXSSIZE will cause a 
Packit ae235b
 * %G_IO_ERROR_INVALID_ARGUMENT error.
Packit ae235b
 *
Packit ae235b
 * On success, the number of bytes written will be passed to the
Packit ae235b
 * @callback. It is not an error if this is not the same as the 
Packit ae235b
 * requested size, as it can happen e.g. on a partial I/O error, 
Packit ae235b
 * but generally we try to write as many bytes as requested. 
Packit ae235b
 *
Packit ae235b
 * You are guaranteed that this method will never fail with
Packit ae235b
 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
Packit ae235b
 * method will just wait until this changes.
Packit ae235b
 *
Packit ae235b
 * Any outstanding I/O request with higher priority (lower numerical 
Packit ae235b
 * value) will be executed before an outstanding request with lower 
Packit ae235b
 * priority. Default priority is %G_PRIORITY_DEFAULT.
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
 * For the synchronous, blocking version of this function, see 
Packit ae235b
 * g_output_stream_write().
Packit ae235b
 *
Packit ae235b
 * Note that no copy of @buffer will be made, so it must stay valid
Packit ae235b
 * until @callback is called. See g_output_stream_write_bytes_async()
Packit ae235b
 * for a #GBytes version that will automatically hold a reference to
Packit ae235b
 * the contents (without copying) for the duration of the call.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_output_stream_write_async (GOutputStream       *stream,
Packit ae235b
			     const void          *buffer,
Packit ae235b
			     gsize                count,
Packit ae235b
			     int                  io_priority,
Packit ae235b
			     GCancellable        *cancellable,
Packit ae235b
			     GAsyncReadyCallback  callback,
Packit ae235b
			     gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
Packit ae235b
  g_return_if_fail (buffer != NULL);
Packit ae235b
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_output_stream_write_async);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
Packit ae235b
  if (count == 0)
Packit ae235b
    {
Packit ae235b
      g_task_return_int (task, 0);
Packit ae235b
      g_object_unref (task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (((gssize) count) < 0)
Packit ae235b
    {
Packit ae235b
      g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                               _("Too large count value passed to %s"),
Packit ae235b
                               G_STRFUNC);
Packit ae235b
      g_object_unref (task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_output_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_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  class->write_async (stream, buffer, count, io_priority, cancellable,
Packit ae235b
                      async_ready_write_callback_wrapper, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_write_finish:
Packit ae235b
 * @stream: a #GOutputStream.
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 a stream write operation.
Packit ae235b
 * 
Packit ae235b
 * Returns: a #gssize containing the number of bytes written to the stream.
Packit ae235b
 **/
Packit ae235b
gssize
Packit ae235b
g_output_stream_write_finish (GOutputStream  *stream,
Packit ae235b
                              GAsyncResult   *result,
Packit ae235b
                              GError        **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
Packit ae235b
  g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_write_async), FALSE);
Packit ae235b
Packit ae235b
  /* @result is always the GTask created by g_output_stream_write_async();
Packit ae235b
   * we called class->write_finish() from async_ready_write_callback_wrapper.
Packit ae235b
   */
Packit ae235b
  return g_task_propagate_int (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  const guint8 *buffer;
Packit ae235b
  gsize to_write;
Packit ae235b
  gsize bytes_written;
Packit ae235b
} AsyncWriteAll;
Packit ae235b
Packit ae235b
static void
Packit ae235b
free_async_write_all (gpointer data)
Packit ae235b
{
Packit ae235b
  g_slice_free (AsyncWriteAll, data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
write_all_callback (GObject      *stream,
Packit ae235b
                    GAsyncResult *result,
Packit ae235b
                    gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  AsyncWriteAll *data = g_task_get_task_data (task);
Packit ae235b
Packit ae235b
  if (result)
Packit ae235b
    {
Packit ae235b
      GError *error = NULL;
Packit ae235b
      gssize nwritten;
Packit ae235b
Packit ae235b
      nwritten = g_output_stream_write_finish (G_OUTPUT_STREAM (stream), result, &error);
Packit ae235b
Packit ae235b
      if (nwritten == -1)
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
      g_assert_cmpint (nwritten, <=, data->to_write);
Packit ae235b
      g_warn_if_fail (nwritten > 0);
Packit ae235b
Packit ae235b
      data->to_write -= nwritten;
Packit ae235b
      data->bytes_written += nwritten;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (data->to_write == 0)
Packit ae235b
    {
Packit ae235b
      g_task_return_boolean (task, TRUE);
Packit ae235b
      g_object_unref (task);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  else
Packit ae235b
    g_output_stream_write_async (G_OUTPUT_STREAM (stream),
Packit ae235b
                                 data->buffer + data->bytes_written,
Packit ae235b
                                 data->to_write,
Packit ae235b
                                 g_task_get_priority (task),
Packit ae235b
                                 g_task_get_cancellable (task),
Packit ae235b
                                 write_all_callback, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
write_all_async_thread (GTask        *task,
Packit ae235b
                        gpointer      source_object,
Packit ae235b
                        gpointer      task_data,
Packit ae235b
                        GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GOutputStream *stream = source_object;
Packit ae235b
  AsyncWriteAll *data = task_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  if (g_output_stream_write_all (stream, data->buffer, data->to_write, &data->bytes_written,
Packit ae235b
                                 g_task_get_cancellable (task), &error))
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
/**
Packit ae235b
 * g_output_stream_write_all_async:
Packit ae235b
 * @stream: A #GOutputStream
Packit ae235b
 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write
Packit ae235b
 * @count: the number of bytes to write
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): callback to call when the request is satisfied
Packit ae235b
 * @user_data: (closure): the data to pass to callback function
Packit ae235b
 *
Packit ae235b
 * Request an asynchronous write of @count bytes from @buffer into
Packit ae235b
 * the stream. When the operation is finished @callback will be called.
Packit ae235b
 * You can then call g_output_stream_write_all_finish() to get the result of the
Packit ae235b
 * operation.
Packit ae235b
 *
Packit ae235b
 * This is the asynchronous version of g_output_stream_write_all().
Packit ae235b
 *
Packit ae235b
 * Call g_output_stream_write_all_finish() to collect the result.
Packit ae235b
 *
Packit ae235b
 * Any outstanding I/O request with higher priority (lower numerical
Packit ae235b
 * value) will be executed before an outstanding request with lower
Packit ae235b
 * priority. Default priority is %G_PRIORITY_DEFAULT.
Packit ae235b
 *
Packit ae235b
 * Note that no copy of @buffer will be made, so it must stay valid
Packit ae235b
 * until @callback is called.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_output_stream_write_all_async (GOutputStream       *stream,
Packit ae235b
                                 const void          *buffer,
Packit ae235b
                                 gsize                count,
Packit ae235b
                                 int                  io_priority,
Packit ae235b
                                 GCancellable        *cancellable,
Packit ae235b
                                 GAsyncReadyCallback  callback,
Packit ae235b
                                 gpointer             user_data)
Packit ae235b
{
Packit ae235b
  AsyncWriteAll *data;
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
Packit ae235b
  g_return_if_fail (buffer != NULL || count == 0);
Packit ae235b
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  data = g_slice_new0 (AsyncWriteAll);
Packit ae235b
  data->buffer = buffer;
Packit ae235b
  data->to_write = count;
Packit ae235b
Packit ae235b
  g_task_set_source_tag (task, g_output_stream_write_all_async);
Packit ae235b
  g_task_set_task_data (task, data, free_async_write_all);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
Packit ae235b
  /* If async writes are going to be handled via the threadpool anyway
Packit ae235b
   * then we may as well do it with a single dispatch instead of
Packit ae235b
   * bouncing in and out.
Packit ae235b
   */
Packit ae235b
  if (g_output_stream_async_write_is_via_threads (stream))
Packit ae235b
    {
Packit ae235b
      g_task_run_in_thread (task, write_all_async_thread);
Packit ae235b
      g_object_unref (task);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    write_all_callback (G_OBJECT (stream), NULL, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_write_all_finish:
Packit ae235b
 * @stream: a #GOutputStream
Packit ae235b
 * @result: a #GAsyncResult
Packit ae235b
 * @bytes_written: (out) (optional): location to store the number of bytes that was written to the stream
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Finishes an asynchronous stream write operation started with
Packit ae235b
 * g_output_stream_write_all_async().
Packit ae235b
 *
Packit ae235b
 * As a special exception to the normal conventions for functions that
Packit ae235b
 * use #GError, if this function returns %FALSE (and sets @error) then
Packit ae235b
 * @bytes_written will be set to the number of bytes that were
Packit ae235b
 * successfully written before the error was encountered.  This
Packit ae235b
 * functionality is only available from C.  If you need it from another
Packit ae235b
 * language then you must write your own loop around
Packit ae235b
 * g_output_stream_write_async().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if there was an error
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_output_stream_write_all_finish (GOutputStream  *stream,
Packit ae235b
                                  GAsyncResult   *result,
Packit ae235b
                                  gsize          *bytes_written,
Packit ae235b
                                  GError        **error)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
Packit ae235b
Packit ae235b
  task = G_TASK (result);
Packit ae235b
Packit ae235b
  if (bytes_written)
Packit ae235b
    {
Packit ae235b
      AsyncWriteAll *data = (AsyncWriteAll *)g_task_get_task_data (task);
Packit ae235b
Packit ae235b
      *bytes_written = data->bytes_written;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_task_propagate_boolean (task, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
write_bytes_callback (GObject      *stream,
Packit ae235b
                      GAsyncResult *result,
Packit ae235b
                      gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gssize nwrote;
Packit ae235b
Packit ae235b
  nwrote = g_output_stream_write_finish (G_OUTPUT_STREAM (stream),
Packit ae235b
                                         result, &error);
Packit ae235b
  if (nwrote == -1)
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  else
Packit ae235b
    g_task_return_int (task, nwrote);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_write_bytes_async:
Packit ae235b
 * @stream: A #GOutputStream.
Packit ae235b
 * @bytes: The bytes to write
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): callback to call when the request is satisfied
Packit ae235b
 * @user_data: (closure): the data to pass to callback function
Packit ae235b
 *
Packit ae235b
 * This function is similar to g_output_stream_write_async(), but
Packit ae235b
 * takes a #GBytes as input.  Due to the refcounted nature of #GBytes,
Packit ae235b
 * this allows the stream to avoid taking a copy of the data.
Packit ae235b
 *
Packit ae235b
 * However, note that this function may still perform partial writes,
Packit ae235b
 * just like g_output_stream_write_async(). If that occurs, to continue
Packit ae235b
 * writing, you will need to create a new #GBytes containing just the
Packit ae235b
 * remaining bytes, using g_bytes_new_from_bytes(). Passing the same
Packit ae235b
 * #GBytes instance multiple times potentially can result in duplicated
Packit ae235b
 * data in the output stream.
Packit ae235b
 *
Packit ae235b
 * For the synchronous, blocking version of this function, see
Packit ae235b
 * g_output_stream_write_bytes().
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_output_stream_write_bytes_async (GOutputStream       *stream,
Packit ae235b
				   GBytes              *bytes,
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
  gsize size;
Packit ae235b
  gconstpointer data;
Packit ae235b
Packit ae235b
  data = g_bytes_get_data (bytes, &size);
Packit ae235b
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_output_stream_write_bytes_async);
Packit ae235b
  g_task_set_task_data (task, g_bytes_ref (bytes),
Packit ae235b
                        (GDestroyNotify) g_bytes_unref);
Packit ae235b
Packit ae235b
  g_output_stream_write_async (stream,
Packit ae235b
                               data, size,
Packit ae235b
                               io_priority,
Packit ae235b
                               cancellable,
Packit ae235b
                               write_bytes_callback,
Packit ae235b
                               task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_write_bytes_finish:
Packit ae235b
 * @stream: a #GOutputStream.
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 a stream write-from-#GBytes operation.
Packit ae235b
 *
Packit ae235b
 * Returns: a #gssize containing the number of bytes written to the stream.
Packit ae235b
 **/
Packit ae235b
gssize
Packit ae235b
g_output_stream_write_bytes_finish (GOutputStream  *stream,
Packit ae235b
				    GAsyncResult   *result,
Packit ae235b
				    GError        **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, stream), -1);
Packit ae235b
Packit ae235b
  return g_task_propagate_int (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
async_ready_splice_callback_wrapper (GObject      *source_object,
Packit ae235b
                                     GAsyncResult *res,
Packit ae235b
                                     gpointer     _data)
Packit ae235b
{
Packit ae235b
  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  GTask *task = _data;
Packit ae235b
  gssize nspliced;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_output_stream_clear_pending (stream);
Packit ae235b
  
Packit ae235b
  if (g_async_result_legacy_propagate_error (res, &error))
Packit ae235b
    nspliced = -1;
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
      nspliced = class->splice_finish (stream, res, &error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (nspliced >= 0)
Packit ae235b
    g_task_return_int (task, nspliced);
Packit ae235b
  else
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_splice_async:
Packit ae235b
 * @stream: a #GOutputStream.
Packit ae235b
 * @source: a #GInputStream. 
Packit ae235b
 * @flags: a set of #GOutputStreamSpliceFlags.
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
 * Splices a stream asynchronously.
Packit ae235b
 * When the operation is finished @callback will be called.
Packit ae235b
 * You can then call g_output_stream_splice_finish() to get the 
Packit ae235b
 * result of the operation.
Packit ae235b
 *
Packit ae235b
 * For the synchronous, blocking version of this function, see 
Packit ae235b
 * g_output_stream_splice().
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_output_stream_splice_async (GOutputStream            *stream,
Packit ae235b
			      GInputStream             *source,
Packit ae235b
			      GOutputStreamSpliceFlags  flags,
Packit ae235b
			      int                       io_priority,
Packit ae235b
			      GCancellable             *cancellable,
Packit ae235b
			      GAsyncReadyCallback       callback,
Packit ae235b
			      gpointer                  user_data)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  GTask *task;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
Packit ae235b
  g_return_if_fail (G_IS_INPUT_STREAM (source));
Packit ae235b
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_output_stream_splice_async);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
  g_task_set_task_data (task, g_object_ref (source), g_object_unref);
Packit ae235b
Packit ae235b
  if (g_input_stream_is_closed (source))
Packit ae235b
    {
Packit ae235b
      g_task_return_new_error (task,
Packit ae235b
                               G_IO_ERROR, G_IO_ERROR_CLOSED,
Packit ae235b
                               _("Source stream is already closed"));
Packit ae235b
      g_object_unref (task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (!g_output_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_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  class->splice_async (stream, source, flags, io_priority, cancellable,
Packit ae235b
                       async_ready_splice_callback_wrapper, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_splice_finish:
Packit ae235b
 * @stream: a #GOutputStream.
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 stream splice operation.
Packit ae235b
 * 
Packit ae235b
 * Returns: a #gssize of the number of bytes spliced. Note that if the
Packit ae235b
 *     number of bytes spliced is greater than %G_MAXSSIZE, then that
Packit ae235b
 *     will be returned, and there is no way to determine the actual
Packit ae235b
 *     number of bytes spliced.
Packit ae235b
 **/
Packit ae235b
gssize
Packit ae235b
g_output_stream_splice_finish (GOutputStream  *stream,
Packit ae235b
			       GAsyncResult   *result,
Packit ae235b
			       GError        **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
Packit ae235b
  g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_splice_async), FALSE);
Packit ae235b
Packit ae235b
  /* @result is always the GTask created by g_output_stream_splice_async();
Packit ae235b
   * we called class->splice_finish() from async_ready_splice_callback_wrapper.
Packit ae235b
   */
Packit ae235b
  return g_task_propagate_int (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
async_ready_flush_callback_wrapper (GObject      *source_object,
Packit ae235b
                                    GAsyncResult *res,
Packit ae235b
                                    gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  gboolean flushed;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_output_stream_clear_pending (stream);
Packit ae235b
  
Packit ae235b
  if (g_async_result_legacy_propagate_error (res, &error))
Packit ae235b
    flushed = FALSE;
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
      flushed = class->flush_finish (stream, res, &error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (flushed)
Packit ae235b
    g_task_return_boolean (task, TRUE);
Packit ae235b
  else
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_flush_async:
Packit ae235b
 * @stream: a #GOutputStream.
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 to call when the request is satisfied
Packit ae235b
 * @user_data: (closure): the data to pass to callback function
Packit ae235b
 * 
Packit ae235b
 * Forces an asynchronous write of all user-space buffered data for
Packit ae235b
 * the given @stream.
Packit ae235b
 * For behaviour details see g_output_stream_flush().
Packit ae235b
 *
Packit ae235b
 * When the operation is finished @callback will be 
Packit ae235b
 * called. You can then call g_output_stream_flush_finish() to get the 
Packit ae235b
 * result of the operation.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_output_stream_flush_async (GOutputStream       *stream,
Packit ae235b
                             int                  io_priority,
Packit ae235b
                             GCancellable        *cancellable,
Packit ae235b
                             GAsyncReadyCallback  callback,
Packit ae235b
                             gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  GTask *task;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
Packit ae235b
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_output_stream_flush_async);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
Packit ae235b
  if (!g_output_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_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  
Packit ae235b
  if (class->flush_async == NULL)
Packit ae235b
    {
Packit ae235b
      g_output_stream_clear_pending (stream);
Packit ae235b
      g_task_return_boolean (task, TRUE);
Packit ae235b
      g_object_unref (task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
      
Packit ae235b
  class->flush_async (stream, io_priority, cancellable,
Packit ae235b
                      async_ready_flush_callback_wrapper, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_flush_finish:
Packit ae235b
 * @stream: a #GOutputStream.
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 flushing an output stream.
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE if flush operation succeeded, %FALSE otherwise.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_output_stream_flush_finish (GOutputStream  *stream,
Packit ae235b
                              GAsyncResult   *result,
Packit ae235b
                              GError        **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
Packit ae235b
  g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_flush_async), FALSE);
Packit ae235b
Packit ae235b
  /* @result is always the GTask created by g_output_stream_flush_async();
Packit ae235b
   * we called class->flush_finish() from async_ready_flush_callback_wrapper.
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
async_ready_close_callback_wrapper (GObject      *source_object,
Packit ae235b
                                    GAsyncResult *res,
Packit ae235b
                                    gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  GError *error = g_task_get_task_data (task);
Packit ae235b
Packit ae235b
  stream->priv->closing = FALSE;
Packit ae235b
  stream->priv->closed = TRUE;
Packit ae235b
Packit ae235b
  if (!error && !g_async_result_legacy_propagate_error (res, &error))
Packit ae235b
    {
Packit ae235b
      class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
      class->close_finish (stream, res,
Packit ae235b
                           error ? NULL : &error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (error != NULL)
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  else
Packit ae235b
    g_task_return_boolean (task, TRUE);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
async_ready_close_flushed_callback_wrapper (GObject      *source_object,
Packit ae235b
                                            GAsyncResult *res,
Packit ae235b
                                            gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  if (!g_async_result_legacy_propagate_error (res, &error))
Packit ae235b
    {
Packit ae235b
      class->flush_finish (stream, res, &error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* propagate the possible error */
Packit ae235b
  if (error)
Packit ae235b
    g_task_set_task_data (task, error, NULL);
Packit ae235b
Packit ae235b
  /* we still close, even if there was a flush error */
Packit ae235b
  class->close_async (stream,
Packit ae235b
                      g_task_get_priority (task),
Packit ae235b
                      g_task_get_cancellable (task),
Packit ae235b
                      async_ready_close_callback_wrapper, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
real_close_async_cb (GObject      *source_object,
Packit ae235b
                     GAsyncResult *res,
Packit ae235b
                     gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gboolean ret;
Packit ae235b
Packit ae235b
  g_output_stream_clear_pending (stream);
Packit ae235b
Packit ae235b
  ret = g_output_stream_internal_close_finish (stream, res, &error);
Packit ae235b
Packit ae235b
  if (error != NULL)
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  else
Packit ae235b
    g_task_return_boolean (task, ret);
Packit ae235b
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_close_async:
Packit ae235b
 * @stream: A #GOutputStream.
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_output_stream_close_finish() to get 
Packit ae235b
 * the result of the operation.
Packit ae235b
 *
Packit ae235b
 * For behaviour details see g_output_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
void
Packit ae235b
g_output_stream_close_async (GOutputStream       *stream,
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
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
Packit ae235b
  
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_output_stream_close_async);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
Packit ae235b
  if (!g_output_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
  g_output_stream_internal_close_async (stream, io_priority, cancellable,
Packit ae235b
                                        real_close_async_cb, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Must always be called inside
Packit ae235b
 * g_output_stream_set_pending()/g_output_stream_clear_pending().
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_output_stream_internal_close_async (GOutputStream       *stream,
Packit ae235b
                                      int                  io_priority,
Packit ae235b
                                      GCancellable        *cancellable,
Packit ae235b
                                      GAsyncReadyCallback  callback,
Packit ae235b
                                      gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *class;
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_output_stream_internal_close_async);
Packit ae235b
  g_task_set_priority (task, io_priority);
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
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  stream->priv->closing = TRUE;
Packit ae235b
Packit ae235b
  /* Call close_async directly if there is no need to flush, or if the flush
Packit ae235b
     can be done sync (in the output stream async close thread) */
Packit ae235b
  if (class->flush_async == NULL ||
Packit ae235b
      (class->flush_async == g_output_stream_real_flush_async &&
Packit ae235b
       (class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
Packit ae235b
    {
Packit ae235b
      class->close_async (stream, io_priority, cancellable,
Packit ae235b
                          async_ready_close_callback_wrapper, task);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* First do an async flush, then do the async close in the callback
Packit ae235b
         wrapper (see async_ready_close_flushed_callback_wrapper) */
Packit ae235b
      class->flush_async (stream, io_priority, cancellable,
Packit ae235b
                          async_ready_close_flushed_callback_wrapper, task);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_output_stream_internal_close_finish (GOutputStream  *stream,
Packit ae235b
                                       GAsyncResult   *result,
Packit ae235b
                                       GError        **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
Packit ae235b
  g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_internal_close_async), FALSE);
Packit ae235b
Packit ae235b
  return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_close_finish:
Packit ae235b
 * @stream: a #GOutputStream.
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 an output stream.
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_output_stream_close_finish (GOutputStream  *stream,
Packit ae235b
                              GAsyncResult   *result,
Packit ae235b
                              GError        **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
Packit ae235b
  g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_close_async), FALSE);
Packit ae235b
Packit ae235b
  /* @result is always the GTask created by g_output_stream_close_async();
Packit ae235b
   * we called class->close_finish() from async_ready_close_callback_wrapper.
Packit ae235b
   */
Packit ae235b
  return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_is_closed:
Packit ae235b
 * @stream: a #GOutputStream.
Packit ae235b
 * 
Packit ae235b
 * Checks if an output stream has already been closed.
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE if @stream is closed. %FALSE otherwise. 
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_output_stream_is_closed (GOutputStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
Packit ae235b
  
Packit ae235b
  return stream->priv->closed;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_is_closing:
Packit ae235b
 * @stream: a #GOutputStream.
Packit ae235b
 *
Packit ae235b
 * Checks if an output stream is being closed. This can be
Packit ae235b
 * used inside e.g. a flush implementation to see if the
Packit ae235b
 * flush (or other i/o operation) is called from within
Packit ae235b
 * the closing operation.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
Packit ae235b
 *
Packit ae235b
 * Since: 2.24
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_output_stream_is_closing (GOutputStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
Packit ae235b
Packit ae235b
  return stream->priv->closing;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_has_pending:
Packit ae235b
 * @stream: a #GOutputStream.
Packit ae235b
 * 
Packit ae235b
 * Checks if an output stream has pending actions.
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE if @stream has pending actions. 
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_output_stream_has_pending (GOutputStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
  
Packit ae235b
  return stream->priv->pending;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_output_stream_set_pending:
Packit ae235b
 * @stream: a #GOutputStream.
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
gboolean
Packit ae235b
g_output_stream_set_pending (GOutputStream *stream,
Packit ae235b
			     GError **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_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_output_stream_clear_pending:
Packit ae235b
 * @stream: output stream
Packit ae235b
 * 
Packit ae235b
 * Clears the pending flag on @stream.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_output_stream_clear_pending (GOutputStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
Packit ae235b
  
Packit ae235b
  stream->priv->pending = FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< internal >
Packit ae235b
 * g_output_stream_async_write_is_via_threads:
Packit ae235b
 * @stream: a #GOutputStream.
Packit ae235b
 *
Packit ae235b
 * Checks if an output stream's write_async function uses threads.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @stream's write_async function uses threads.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_output_stream_async_write_is_via_threads (GOutputStream *stream)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  return (class->write_async == g_output_stream_real_write_async &&
Packit ae235b
      !(G_IS_POLLABLE_OUTPUT_STREAM (stream) &&
Packit ae235b
        g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (stream))));
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< internal >
Packit ae235b
 * g_output_stream_async_close_is_via_threads:
Packit ae235b
 * @stream: output stream
Packit ae235b
 *
Packit ae235b
 * Checks if an output stream's close_async function uses threads.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @stream's close_async function uses threads.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_output_stream_async_close_is_via_threads (GOutputStream *stream)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  return class->close_async == g_output_stream_real_close_async;
Packit ae235b
}
Packit ae235b
Packit ae235b
/********************************************
Packit ae235b
 *   Default implementation of async ops    *
Packit ae235b
 ********************************************/
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  const void         *buffer;
Packit ae235b
  gsize               count_requested;
Packit ae235b
  gssize              count_written;
Packit ae235b
} WriteData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
free_write_data (WriteData *op)
Packit ae235b
{
Packit ae235b
  g_slice_free (WriteData, op);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
write_async_thread (GTask        *task,
Packit ae235b
                    gpointer      source_object,
Packit ae235b
                    gpointer      task_data,
Packit ae235b
                    GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GOutputStream *stream = source_object;
Packit ae235b
  WriteData *op = task_data;
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gssize count_written;
Packit ae235b
Packit ae235b
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  count_written = class->write_fn (stream, op->buffer, op->count_requested,
Packit ae235b
                                   cancellable, &error);
Packit ae235b
  if (count_written == -1)
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  else
Packit ae235b
    g_task_return_int (task, count_written);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void write_async_pollable (GPollableOutputStream *stream,
Packit ae235b
                                  GTask                 *task);
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
write_async_pollable_ready (GPollableOutputStream *stream,
Packit ae235b
			    gpointer               user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = user_data;
Packit ae235b
Packit ae235b
  write_async_pollable (stream, task);
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
write_async_pollable (GPollableOutputStream *stream,
Packit ae235b
                      GTask                 *task)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
  WriteData *op = g_task_get_task_data (task);
Packit ae235b
  gssize count_written;
Packit ae235b
Packit ae235b
  if (g_task_return_error_if_cancelled (task))
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  count_written = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
Packit ae235b
    write_nonblocking (stream, op->buffer, op->count_requested, &error);
Packit ae235b
Packit ae235b
  if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
Packit ae235b
    {
Packit ae235b
      GSource *source;
Packit ae235b
Packit ae235b
      g_error_free (error);
Packit ae235b
Packit ae235b
      source = g_pollable_output_stream_create_source (stream,
Packit ae235b
                                                       g_task_get_cancellable (task));
Packit ae235b
      g_task_attach_source (task, source,
Packit ae235b
                            (GSourceFunc) write_async_pollable_ready);
Packit ae235b
      g_source_unref (source);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (count_written == -1)
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  else
Packit ae235b
    g_task_return_int (task, count_written);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_output_stream_real_write_async (GOutputStream       *stream,
Packit ae235b
                                  const void          *buffer,
Packit ae235b
                                  gsize                count,
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
  WriteData *op;
Packit ae235b
Packit ae235b
  op = g_slice_new0 (WriteData);
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_check_cancellable (task, FALSE);
Packit ae235b
  g_task_set_task_data (task, op, (GDestroyNotify) free_write_data);
Packit ae235b
  op->buffer = buffer;
Packit ae235b
  op->count_requested = count;
Packit ae235b
Packit ae235b
  if (!g_output_stream_async_write_is_via_threads (stream))
Packit ae235b
    write_async_pollable (G_POLLABLE_OUTPUT_STREAM (stream), task);
Packit ae235b
  else
Packit ae235b
    g_task_run_in_thread (task, write_async_thread);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_output_stream_real_write_finish (GOutputStream  *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_int (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GInputStream *source;
Packit ae235b
  GOutputStreamSpliceFlags flags;
Packit ae235b
  gssize n_read;
Packit ae235b
  gssize n_written;
Packit ae235b
  gsize bytes_copied;
Packit ae235b
  GError *error;
Packit ae235b
  guint8 *buffer;
Packit ae235b
} SpliceData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
free_splice_data (SpliceData *op)
Packit ae235b
{
Packit ae235b
  g_clear_pointer (&op->buffer, g_free);
Packit ae235b
  g_object_unref (op->source);
Packit ae235b
  g_clear_error (&op->error);
Packit ae235b
  g_free (op);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
real_splice_async_complete_cb (GTask *task)
Packit ae235b
{
Packit ae235b
  SpliceData *op = g_task_get_task_data (task);
Packit ae235b
Packit ae235b
  if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE &&
Packit ae235b
      !g_input_stream_is_closed (op->source))
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET &&
Packit ae235b
      !g_output_stream_is_closed (g_task_get_source_object (task)))
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  if (op->error != NULL)
Packit ae235b
    {
Packit ae235b
      g_task_return_error (task, op->error);
Packit ae235b
      op->error = NULL;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_task_return_int (task, op->bytes_copied);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
real_splice_async_close_input_cb (GObject      *source,
Packit ae235b
                                  GAsyncResult *res,
Packit ae235b
                                  gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = user_data;
Packit ae235b
Packit ae235b
  g_input_stream_close_finish (G_INPUT_STREAM (source), res, NULL);
Packit ae235b
Packit ae235b
  real_splice_async_complete_cb (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
real_splice_async_close_output_cb (GObject      *source,
Packit ae235b
                                   GAsyncResult *res,
Packit ae235b
                                   gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = G_TASK (user_data);
Packit ae235b
  SpliceData *op = g_task_get_task_data (task);
Packit ae235b
  GError **error = (op->error == NULL) ? &op->error : NULL;
Packit ae235b
Packit ae235b
  g_output_stream_internal_close_finish (G_OUTPUT_STREAM (source), res, error);
Packit ae235b
Packit ae235b
  real_splice_async_complete_cb (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
real_splice_async_complete (GTask *task)
Packit ae235b
{
Packit ae235b
  SpliceData *op = g_task_get_task_data (task);
Packit ae235b
  gboolean done = TRUE;
Packit ae235b
Packit ae235b
  if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
Packit ae235b
    {
Packit ae235b
      done = FALSE;
Packit ae235b
      g_input_stream_close_async (op->source, g_task_get_priority (task),
Packit ae235b
                                  g_task_get_cancellable (task),
Packit ae235b
                                  real_splice_async_close_input_cb, task);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
Packit ae235b
    {
Packit ae235b
      done = FALSE;
Packit ae235b
      g_output_stream_internal_close_async (g_task_get_source_object (task),
Packit ae235b
                                            g_task_get_priority (task),
Packit ae235b
                                            g_task_get_cancellable (task),
Packit ae235b
                                            real_splice_async_close_output_cb,
Packit ae235b
                                            task);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (done)
Packit ae235b
    real_splice_async_complete_cb (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void real_splice_async_read_cb (GObject      *source,
Packit ae235b
                                       GAsyncResult *res,
Packit ae235b
                                       gpointer      user_data);
Packit ae235b
Packit ae235b
static void
Packit ae235b
real_splice_async_write_cb (GObject      *source,
Packit ae235b
                            GAsyncResult *res,
Packit ae235b
                            gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  GTask *task = G_TASK (user_data);
Packit ae235b
  SpliceData *op = g_task_get_task_data (task);
Packit ae235b
  gssize ret;
Packit ae235b
Packit ae235b
  class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
Packit ae235b
Packit ae235b
  ret = class->write_finish (G_OUTPUT_STREAM (source), res, &op->error);
Packit ae235b
Packit ae235b
  if (ret == -1)
Packit ae235b
    {
Packit ae235b
      real_splice_async_complete (task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  op->n_written += ret;
Packit ae235b
  op->bytes_copied += ret;
Packit ae235b
  if (op->bytes_copied > G_MAXSSIZE)
Packit ae235b
    op->bytes_copied = G_MAXSSIZE;
Packit ae235b
Packit ae235b
  if (op->n_written < op->n_read)
Packit ae235b
    {
Packit ae235b
      class->write_async (g_task_get_source_object (task),
Packit ae235b
                          op->buffer + op->n_written,
Packit ae235b
                          op->n_read - op->n_written,
Packit ae235b
                          g_task_get_priority (task),
Packit ae235b
                          g_task_get_cancellable (task),
Packit ae235b
                          real_splice_async_write_cb, task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_input_stream_read_async (op->source, op->buffer, 8192,
Packit ae235b
                             g_task_get_priority (task),
Packit ae235b
                             g_task_get_cancellable (task),
Packit ae235b
                             real_splice_async_read_cb, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
real_splice_async_read_cb (GObject      *source,
Packit ae235b
                           GAsyncResult *res,
Packit ae235b
                           gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  GTask *task = G_TASK (user_data);
Packit ae235b
  SpliceData *op = g_task_get_task_data (task);
Packit ae235b
  gssize ret;
Packit ae235b
Packit ae235b
  class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
Packit ae235b
Packit ae235b
  ret = g_input_stream_read_finish (op->source, res, &op->error);
Packit ae235b
  if (ret == -1 || ret == 0)
Packit ae235b
    {
Packit ae235b
      real_splice_async_complete (task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  op->n_read = ret;
Packit ae235b
  op->n_written = 0;
Packit ae235b
Packit ae235b
  class->write_async (g_task_get_source_object (task), op->buffer,
Packit ae235b
                      op->n_read, g_task_get_priority (task),
Packit ae235b
                      g_task_get_cancellable (task),
Packit ae235b
                      real_splice_async_write_cb, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
splice_async_thread (GTask        *task,
Packit ae235b
                     gpointer      source_object,
Packit ae235b
                     gpointer      task_data,
Packit ae235b
                     GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GOutputStream *stream = source_object;
Packit ae235b
  SpliceData *op = task_data;
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gssize bytes_copied;
Packit ae235b
Packit ae235b
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  
Packit ae235b
  bytes_copied = class->splice (stream,
Packit ae235b
                                op->source,
Packit ae235b
                                op->flags,
Packit ae235b
                                cancellable,
Packit ae235b
                                &error);
Packit ae235b
  if (bytes_copied == -1)
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  else
Packit ae235b
    g_task_return_int (task, bytes_copied);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_output_stream_real_splice_async (GOutputStream             *stream,
Packit ae235b
                                   GInputStream              *source,
Packit ae235b
                                   GOutputStreamSpliceFlags   flags,
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
  SpliceData *op;
Packit ae235b
Packit ae235b
  op = g_new0 (SpliceData, 1);
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_task_data (task, op, (GDestroyNotify)free_splice_data);
Packit ae235b
  op->flags = flags;
Packit ae235b
  op->source = g_object_ref (source);
Packit ae235b
Packit ae235b
  if (g_input_stream_async_read_is_via_threads (source) &&
Packit ae235b
      g_output_stream_async_write_is_via_threads (stream))
Packit ae235b
    {
Packit ae235b
      g_task_run_in_thread (task, splice_async_thread);
Packit ae235b
      g_object_unref (task);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      op->buffer = g_malloc (8192);
Packit ae235b
      g_input_stream_read_async (op->source, op->buffer, 8192,
Packit ae235b
                                 g_task_get_priority (task),
Packit ae235b
                                 g_task_get_cancellable (task),
Packit ae235b
                                 real_splice_async_read_cb, task);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_output_stream_real_splice_finish (GOutputStream  *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_int (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
flush_async_thread (GTask        *task,
Packit ae235b
                    gpointer      source_object,
Packit ae235b
                    gpointer      task_data,
Packit ae235b
                    GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GOutputStream *stream = source_object;
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  gboolean result;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  result = TRUE;
Packit ae235b
  if (class->flush)
Packit ae235b
    result = class->flush (stream, cancellable, &error);
Packit ae235b
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_output_stream_real_flush_async (GOutputStream       *stream,
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 (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
  g_task_run_in_thread (task, flush_async_thread);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_output_stream_real_flush_finish (GOutputStream  *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
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
  GOutputStream *stream = source_object;
Packit ae235b
  GOutputStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gboolean result = TRUE;
Packit ae235b
Packit ae235b
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  /* Do a flush here if there is a flush function, and we did not have to do
Packit ae235b
   * an async flush before (see g_output_stream_close_async)
Packit ae235b
   */
Packit ae235b
  if (class->flush != NULL &&
Packit ae235b
      (class->flush_async == NULL ||
Packit ae235b
       class->flush_async == g_output_stream_real_flush_async))
Packit ae235b
    {
Packit ae235b
      result = class->flush (stream, cancellable, &error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Auto handling of cancelation disabled, and ignore
Packit ae235b
     cancellation, since we want to close things anyway, although
Packit ae235b
     possibly in a quick-n-dirty way. At least we never want to leak
Packit ae235b
     open handles */
Packit ae235b
Packit ae235b
  if (class->close_fn)
Packit ae235b
    {
Packit ae235b
      /* Make sure to close, even if the flush failed (see sync close) */
Packit ae235b
      if (!result)
Packit ae235b
        class->close_fn (stream, cancellable, NULL);
Packit ae235b
      else
Packit ae235b
        result = class->close_fn (stream, cancellable, &error);
Packit ae235b
    }
Packit ae235b
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_output_stream_real_close_async (GOutputStream       *stream,
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 (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_priority (task, io_priority);
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_output_stream_real_close_finish (GOutputStream  *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
}