Blame gio/ginputstream.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 <glib.h>
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
#include "ginputstream.h"
Packit ae235b
#include "gioprivate.h"
Packit ae235b
#include "gseekable.h"
Packit ae235b
#include "gcancellable.h"
Packit ae235b
#include "gasyncresult.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "gpollableinputstream.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:ginputstream
Packit ae235b
 * @short_description: Base class for implementing streaming input
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * #GInputStream has functions to read from a stream (g_input_stream_read()),
Packit ae235b
 * to close a stream (g_input_stream_close()) and to skip some content
Packit ae235b
 * (g_input_stream_skip()). 
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 _GInputStreamPrivate {
Packit ae235b
  guint closed : 1;
Packit ae235b
  guint pending : 1;
Packit ae235b
  GAsyncReadyCallback outstanding_callback;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GInputStream, g_input_stream, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static gssize   g_input_stream_real_skip         (GInputStream         *stream,
Packit ae235b
						  gsize                 count,
Packit ae235b
						  GCancellable         *cancellable,
Packit ae235b
						  GError              **error);
Packit ae235b
static void     g_input_stream_real_read_async   (GInputStream         *stream,
Packit ae235b
						  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
static gssize   g_input_stream_real_read_finish  (GInputStream         *stream,
Packit ae235b
						  GAsyncResult         *result,
Packit ae235b
						  GError              **error);
Packit ae235b
static void     g_input_stream_real_skip_async   (GInputStream         *stream,
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_input_stream_real_skip_finish  (GInputStream         *stream,
Packit ae235b
						  GAsyncResult         *result,
Packit ae235b
						  GError              **error);
Packit ae235b
static void     g_input_stream_real_close_async  (GInputStream         *stream,
Packit ae235b
						  int                   io_priority,
Packit ae235b
						  GCancellable         *cancellable,
Packit ae235b
						  GAsyncReadyCallback   callback,
Packit ae235b
						  gpointer              data);
Packit ae235b
static gboolean g_input_stream_real_close_finish (GInputStream         *stream,
Packit ae235b
						  GAsyncResult         *result,
Packit ae235b
						  GError              **error);
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_input_stream_dispose (GObject *object)
Packit ae235b
{
Packit ae235b
  GInputStream *stream;
Packit ae235b
Packit ae235b
  stream = G_INPUT_STREAM (object);
Packit ae235b
  
Packit ae235b
  if (!stream->priv->closed)
Packit ae235b
    g_input_stream_close (stream, NULL, NULL);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_input_stream_parent_class)->dispose (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_input_stream_class_init (GInputStreamClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
  
Packit ae235b
  gobject_class->dispose = g_input_stream_dispose;
Packit ae235b
  
Packit ae235b
  klass->skip = g_input_stream_real_skip;
Packit ae235b
  klass->read_async = g_input_stream_real_read_async;
Packit ae235b
  klass->read_finish = g_input_stream_real_read_finish;
Packit ae235b
  klass->skip_async = g_input_stream_real_skip_async;
Packit ae235b
  klass->skip_finish = g_input_stream_real_skip_finish;
Packit ae235b
  klass->close_async = g_input_stream_real_close_async;
Packit ae235b
  klass->close_finish = g_input_stream_real_close_finish;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_input_stream_init (GInputStream *stream)
Packit ae235b
{
Packit ae235b
  stream->priv = g_input_stream_get_instance_private (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_read:
Packit ae235b
 * @stream: a #GInputStream.
Packit ae235b
 * @buffer: (array length=count) (element-type guint8): a buffer to
Packit ae235b
 *     read data into (which should be at least count bytes long).
Packit ae235b
 * @count: the number of bytes that will be read from 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 read @count bytes from the stream into the buffer starting at
Packit ae235b
 * @buffer. Will block during this read.
Packit ae235b
 * 
Packit ae235b
 * If count is zero returns zero 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 read into the buffer 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. near the end of a file. Zero is returned on end of file
Packit ae235b
 * (or if @count is zero),  but never otherwise.
Packit ae235b
 *
Packit ae235b
 * The returned @buffer is not a nul-terminated string, it can contain nul bytes
Packit ae235b
 * at any position, and this function doesn't nul-terminate the @buffer.
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
 * Returns: Number of bytes read, or -1 on error, or 0 on end of file.
Packit ae235b
 **/
Packit ae235b
gssize
Packit ae235b
g_input_stream_read  (GInputStream  *stream,
Packit ae235b
		      void          *buffer,
Packit ae235b
		      gsize          count,
Packit ae235b
		      GCancellable  *cancellable,
Packit ae235b
		      GError       **error)
Packit ae235b
{
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
  gssize res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_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_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  if (class->read_fn == NULL) 
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                           _("Input stream doesn’t implement read"));
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_input_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->read_fn (stream, buffer, count, cancellable, error);
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_pop_current (cancellable);
Packit ae235b
  
Packit ae235b
  g_input_stream_clear_pending (stream);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_read_all:
Packit ae235b
 * @stream: a #GInputStream.
Packit ae235b
 * @buffer: (array length=count) (element-type guint8): a buffer to
Packit ae235b
 *     read data into (which should be at least count bytes long).
Packit ae235b
 * @count: the number of bytes that will be read from the stream
Packit ae235b
 * @bytes_read: (out): location to store the number of bytes that was read from 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 read @count bytes from the stream into the buffer starting at
Packit ae235b
 * @buffer. Will block during this read.
Packit ae235b
 *
Packit ae235b
 * This function is similar to g_input_stream_read(), except it tries to
Packit ae235b
 * read as many bytes as requested, only stopping on an error or end of stream.
Packit ae235b
 *
Packit ae235b
 * On a successful read of @count bytes, or if we reached the end of the
Packit ae235b
 * stream,  %TRUE is returned, and @bytes_read is set to the number of bytes
Packit ae235b
 * read into @buffer.
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_read will be set to the number of bytes that were successfully
Packit ae235b
 * read before the error was encountered.  This functionality is only
Packit ae235b
 * available from C.  If you need it from another language then you must
Packit ae235b
 * write your own loop around g_input_stream_read().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if there was an error
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_input_stream_read_all (GInputStream  *stream,
Packit ae235b
			 void          *buffer,
Packit ae235b
			 gsize          count,
Packit ae235b
			 gsize         *bytes_read,
Packit ae235b
			 GCancellable  *cancellable,
Packit ae235b
			 GError       **error)
Packit ae235b
{
Packit ae235b
  gsize _bytes_read;
Packit ae235b
  gssize res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
Packit ae235b
  g_return_val_if_fail (buffer != NULL, FALSE);
Packit ae235b
Packit ae235b
  _bytes_read = 0;
Packit ae235b
  while (_bytes_read < count)
Packit ae235b
    {
Packit ae235b
      res = g_input_stream_read (stream, (char *)buffer + _bytes_read, count - _bytes_read,
Packit ae235b
				 cancellable, error);
Packit ae235b
      if (res == -1)
Packit ae235b
	{
Packit ae235b
	  if (bytes_read)
Packit ae235b
	    *bytes_read = _bytes_read;
Packit ae235b
	  return FALSE;
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      if (res == 0)
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      _bytes_read += res;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (bytes_read)
Packit ae235b
    *bytes_read = _bytes_read;
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_read_bytes:
Packit ae235b
 * @stream: a #GInputStream.
Packit ae235b
 * @count: maximum number of bytes that will be read from the stream. Common
Packit ae235b
 * values include 4096 and 8192.
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
 * Like g_input_stream_read(), this tries to read @count bytes from
Packit ae235b
 * the stream in a blocking fashion. However, rather than reading into
Packit ae235b
 * a user-supplied buffer, this will create a new #GBytes containing
Packit ae235b
 * the data that was read. This may be easier to use from language
Packit ae235b
 * bindings.
Packit ae235b
 *
Packit ae235b
 * If count is zero, returns a zero-length #GBytes and does nothing. A
Packit ae235b
 * value of @count larger than %G_MAXSSIZE will cause a
Packit ae235b
 * %G_IO_ERROR_INVALID_ARGUMENT error.
Packit ae235b
 *
Packit ae235b
 * On success, a new #GBytes is returned. It is not an error if the
Packit ae235b
 * size of this object is not the same as the requested size, as it
Packit ae235b
 * can happen e.g. near the end of a file. A zero-length #GBytes is
Packit ae235b
 * returned on end of file (or if @count is zero), but never
Packit ae235b
 * otherwise.
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 %NULL is returned and @error is set accordingly.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a new #GBytes, or %NULL on error
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 **/
Packit ae235b
GBytes *
Packit ae235b
g_input_stream_read_bytes (GInputStream  *stream,
Packit ae235b
			   gsize          count,
Packit ae235b
			   GCancellable  *cancellable,
Packit ae235b
			   GError       **error)
Packit ae235b
{
Packit ae235b
  guchar *buf;
Packit ae235b
  gssize nread;
Packit ae235b
Packit ae235b
  buf = g_malloc (count);
Packit ae235b
  nread = g_input_stream_read (stream, buf, count, cancellable, error);
Packit ae235b
  if (nread == -1)
Packit ae235b
    {
Packit ae235b
      g_free (buf);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  else if (nread == 0)
Packit ae235b
    {
Packit ae235b
      g_free (buf);
Packit ae235b
      return g_bytes_new_static ("", 0);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    return g_bytes_new_take (buf, nread);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_skip:
Packit ae235b
 * @stream: a #GInputStream.
Packit ae235b
 * @count: the number of bytes that will be skipped from 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 skip @count bytes from the stream. Will block during the operation.
Packit ae235b
 *
Packit ae235b
 * This is identical to g_input_stream_read(), from a behaviour standpoint,
Packit ae235b
 * but the bytes that are skipped are not returned to the user. Some
Packit ae235b
 * streams have an implementation that is more efficient than reading the data.
Packit ae235b
 *
Packit ae235b
 * This function is optional for inherited classes, as the default implementation
Packit ae235b
 * emulates it using read.
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
 * Returns: Number of bytes skipped, or -1 on error
Packit ae235b
 **/
Packit ae235b
gssize
Packit ae235b
g_input_stream_skip (GInputStream  *stream,
Packit ae235b
		     gsize          count,
Packit ae235b
		     GCancellable  *cancellable,
Packit ae235b
		     GError       **error)
Packit ae235b
{
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
  gssize res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
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_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  if (!g_input_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->skip (stream, count, cancellable, error);
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_pop_current (cancellable);
Packit ae235b
  
Packit ae235b
  g_input_stream_clear_pending (stream);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_input_stream_real_skip (GInputStream  *stream,
Packit ae235b
			  gsize          count,
Packit ae235b
			  GCancellable  *cancellable,
Packit ae235b
			  GError       **error)
Packit ae235b
{
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
  gssize ret, read_bytes;
Packit ae235b
  char buffer[8192];
Packit ae235b
  GError *my_error;
Packit ae235b
Packit ae235b
  if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
Packit ae235b
    {
Packit ae235b
      if (g_seekable_seek (G_SEEKABLE (stream),
Packit ae235b
			   count,
Packit ae235b
			   G_SEEK_CUR,
Packit ae235b
			   cancellable,
Packit ae235b
			   NULL))
Packit ae235b
	return count;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* If not seekable, or seek failed, fall back to reading data: */
Packit ae235b
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  read_bytes = 0;
Packit ae235b
  while (1)
Packit ae235b
    {
Packit ae235b
      my_error = NULL;
Packit ae235b
Packit ae235b
      ret = class->read_fn (stream, buffer, MIN (sizeof (buffer), count),
Packit ae235b
                            cancellable, &my_error);
Packit ae235b
      if (ret == -1)
Packit ae235b
	{
Packit ae235b
	  if (read_bytes > 0 &&
Packit ae235b
	      my_error->domain == G_IO_ERROR &&
Packit ae235b
	      my_error->code == G_IO_ERROR_CANCELLED)
Packit ae235b
	    {
Packit ae235b
	      g_error_free (my_error);
Packit ae235b
	      return read_bytes;
Packit ae235b
	    }
Packit ae235b
Packit ae235b
	  g_propagate_error (error, my_error);
Packit ae235b
	  return -1;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      count -= ret;
Packit ae235b
      read_bytes += ret;
Packit ae235b
Packit ae235b
      if (ret == 0 || count == 0)
Packit ae235b
        return read_bytes;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_close:
Packit ae235b
 * @stream: A #GInputStream.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore
Packit ae235b
 *
Packit ae235b
 * Closes the stream, releasing resources related to it.
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
 * 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.
Packit ae235b
 *
Packit ae235b
 * If @cancellable is not %NULL, then the operation can be cancelled by
Packit ae235b
 * triggering the cancellable object from another thread. If the operation
Packit ae235b
 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
Packit ae235b
 * Cancelling a close will still leave the stream closed, but some streams
Packit ae235b
 * can use a faster close that doesn't block to e.g. check errors. 
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE on failure
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_input_stream_close (GInputStream  *stream,
Packit ae235b
		      GCancellable  *cancellable,
Packit ae235b
		      GError       **error)
Packit ae235b
{
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  if (stream->priv->closed)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  res = TRUE;
Packit ae235b
Packit ae235b
  if (!g_input_stream_set_pending (stream, error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_push_current (cancellable);
Packit ae235b
Packit ae235b
  if (class->close_fn)
Packit ae235b
    res = class->close_fn (stream, cancellable, error);
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_pop_current (cancellable);
Packit ae235b
Packit ae235b
  g_input_stream_clear_pending (stream);
Packit ae235b
  
Packit ae235b
  stream->priv->closed = TRUE;
Packit ae235b
  
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
async_ready_callback_wrapper (GObject      *source_object,
Packit ae235b
			      GAsyncResult *res,
Packit ae235b
			      gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GInputStream *stream = G_INPUT_STREAM (source_object);
Packit ae235b
Packit ae235b
  g_input_stream_clear_pending (stream);
Packit ae235b
  if (stream->priv->outstanding_callback)
Packit ae235b
    (*stream->priv->outstanding_callback) (source_object, res, user_data);
Packit ae235b
  g_object_unref (stream);
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
  GInputStream *stream = G_INPUT_STREAM (source_object);
Packit ae235b
Packit ae235b
  g_input_stream_clear_pending (stream);
Packit ae235b
  stream->priv->closed = TRUE;
Packit ae235b
  if (stream->priv->outstanding_callback)
Packit ae235b
    (*stream->priv->outstanding_callback) (source_object, res, user_data);
Packit ae235b
  g_object_unref (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_read_async:
Packit ae235b
 * @stream: A #GInputStream.
Packit ae235b
 * @buffer: (array length=count) (element-type guint8): a buffer to
Packit ae235b
 *     read data into (which should be at least count bytes long).
Packit ae235b
 * @count: the number of bytes that will be read from the stream
Packit ae235b
 * @io_priority: the [I/O priority][io-priority]
Packit ae235b
 * 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 read of @count bytes from the stream into the buffer
Packit ae235b
 * starting at @buffer. When the operation is finished @callback will be called. 
Packit ae235b
 * You can then call g_input_stream_read_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 on @stream, and will
Packit ae235b
 * result in %G_IO_ERROR_PENDING errors. 
Packit ae235b
 *
Packit ae235b
 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
Packit ae235b
 *
Packit ae235b
 * On success, the number of bytes read into the buffer will be passed to the
Packit ae235b
 * callback. It is not an error if this is not the same as the requested size, as it
Packit ae235b
 * can happen e.g. near the end of a file, but generally we try to read
Packit ae235b
 * as many bytes as requested. Zero is returned on end of file
Packit ae235b
 * (or if @count is zero),  but never otherwise.
Packit ae235b
 *
Packit ae235b
 * Any outstanding i/o request with higher priority (lower numerical value) will
Packit ae235b
 * be executed before an outstanding request with lower priority. Default
Packit ae235b
 * priority is %G_PRIORITY_DEFAULT.
Packit ae235b
 *
Packit ae235b
 * The asynchronous methods have a default fallback that uses threads to implement
Packit ae235b
 * asynchronicity, so they are optional for inheriting classes. However, if you
Packit ae235b
 * override one you must override all.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_input_stream_read_async (GInputStream        *stream,
Packit ae235b
			   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
  GInputStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_INPUT_STREAM (stream));
Packit ae235b
  g_return_if_fail (buffer != NULL);
Packit ae235b
Packit ae235b
  if (count == 0)
Packit ae235b
    {
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_input_stream_read_async);
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_report_new_error (stream, callback, user_data,
Packit ae235b
                               g_input_stream_read_async,
Packit ae235b
                               G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                               _("Too large count value passed to %s"),
Packit ae235b
                               G_STRFUNC);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_input_stream_set_pending (stream, &error))
Packit ae235b
    {
Packit ae235b
      g_task_report_error (stream, callback, user_data,
Packit ae235b
                           g_input_stream_read_async,
Packit ae235b
                           error);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  stream->priv->outstanding_callback = callback;
Packit ae235b
  g_object_ref (stream);
Packit ae235b
  class->read_async (stream, buffer, count, io_priority, cancellable,
Packit ae235b
		     async_ready_callback_wrapper, user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_read_finish:
Packit ae235b
 * @stream: a #GInputStream.
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 read operation. 
Packit ae235b
 * 
Packit ae235b
 * Returns: number of bytes read in, or -1 on error, or 0 on end of file.
Packit ae235b
 **/
Packit ae235b
gssize
Packit ae235b
g_input_stream_read_finish (GInputStream  *stream,
Packit ae235b
			    GAsyncResult  *result,
Packit ae235b
			    GError       **error)
Packit ae235b
{
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
Packit ae235b
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
Packit ae235b
Packit ae235b
  if (g_async_result_legacy_propagate_error (result, error))
Packit ae235b
    return -1;
Packit ae235b
  else if (g_async_result_is_tagged (result, g_input_stream_read_async))
Packit ae235b
    return g_task_propagate_int (G_TASK (result), error);
Packit ae235b
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  return class->read_finish (stream, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  gchar *buffer;
Packit ae235b
  gsize to_read;
Packit ae235b
  gsize bytes_read;
Packit ae235b
} AsyncReadAll;
Packit ae235b
Packit ae235b
static void
Packit ae235b
free_async_read_all (gpointer data)
Packit ae235b
{
Packit ae235b
  g_slice_free (AsyncReadAll, data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
read_all_callback (GObject      *stream,
Packit ae235b
                   GAsyncResult *result,
Packit ae235b
                   gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  AsyncReadAll *data = g_task_get_task_data (task);
Packit ae235b
  gboolean got_eof = FALSE;
Packit ae235b
Packit ae235b
  if (result)
Packit ae235b
    {
Packit ae235b
      GError *error = NULL;
Packit ae235b
      gssize nread;
Packit ae235b
Packit ae235b
      nread = g_input_stream_read_finish (G_INPUT_STREAM (stream), result, &error);
Packit ae235b
Packit ae235b
      if (nread == -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 (nread, <=, data->to_read);
Packit ae235b
      data->to_read -= nread;
Packit ae235b
      data->bytes_read += nread;
Packit ae235b
      got_eof = (nread == 0);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (got_eof || data->to_read == 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_input_stream_read_async (G_INPUT_STREAM (stream),
Packit ae235b
                               data->buffer + data->bytes_read,
Packit ae235b
                               data->to_read,
Packit ae235b
                               g_task_get_priority (task),
Packit ae235b
                               g_task_get_cancellable (task),
Packit ae235b
                               read_all_callback, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
read_all_async_thread (GTask        *task,
Packit ae235b
                       gpointer      source_object,
Packit ae235b
                       gpointer      task_data,
Packit ae235b
                       GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GInputStream *stream = source_object;
Packit ae235b
  AsyncReadAll *data = task_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  if (g_input_stream_read_all (stream, data->buffer, data->to_read, &data->bytes_read,
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_input_stream_read_all_async:
Packit ae235b
 * @stream: A #GInputStream
Packit ae235b
 * @buffer: (array length=count) (element-type guint8): a buffer to
Packit ae235b
 *     read data into (which should be at least count bytes long)
Packit ae235b
 * @count: the number of bytes that will be read from the stream
Packit ae235b
 * @io_priority: the [I/O priority][io-priority] of the request
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
Packit ae235b
 * @callback: (scope async): 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 read of @count bytes from the stream into the
Packit ae235b
 * buffer starting at @buffer.
Packit ae235b
 *
Packit ae235b
 * This is the asynchronous equivalent of g_input_stream_read_all().
Packit ae235b
 *
Packit ae235b
 * Call g_input_stream_read_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
 * Since: 2.44
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_input_stream_read_all_async (GInputStream        *stream,
Packit ae235b
                               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
  AsyncReadAll *data;
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_INPUT_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 (AsyncReadAll);
Packit ae235b
  data->buffer = buffer;
Packit ae235b
  data->to_read = count;
Packit ae235b
Packit ae235b
  g_task_set_source_tag (task, g_input_stream_read_all_async);
Packit ae235b
  g_task_set_task_data (task, data, free_async_read_all);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
Packit ae235b
  /* If async reads 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_input_stream_async_read_is_via_threads (stream))
Packit ae235b
    {
Packit ae235b
      g_task_run_in_thread (task, read_all_async_thread);
Packit ae235b
      g_object_unref (task);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    read_all_callback (G_OBJECT (stream), NULL, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_read_all_finish:
Packit ae235b
 * @stream: a #GInputStream
Packit ae235b
 * @result: a #GAsyncResult
Packit ae235b
 * @bytes_read: (out): location to store the number of bytes that was read from 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 read operation started with
Packit ae235b
 * g_input_stream_read_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_read will be set to the number of bytes that were successfully
Packit ae235b
 * read before the error was encountered.  This functionality is only
Packit ae235b
 * available from C.  If you need it from another language then you must
Packit ae235b
 * write your own loop around g_input_stream_read_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_input_stream_read_all_finish (GInputStream  *stream,
Packit ae235b
                                GAsyncResult  *result,
Packit ae235b
                                gsize         *bytes_read,
Packit ae235b
                                GError       **error)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_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_read)
Packit ae235b
    {
Packit ae235b
      AsyncReadAll *data = g_task_get_task_data (task);
Packit ae235b
Packit ae235b
      *bytes_read = data->bytes_read;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_task_propagate_boolean (task, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
read_bytes_callback (GObject      *stream,
Packit ae235b
		     GAsyncResult *result,
Packit ae235b
		     gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  guchar *buf = g_task_get_task_data (task);
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gssize nread;
Packit ae235b
  GBytes *bytes = NULL;
Packit ae235b
Packit ae235b
  nread = g_input_stream_read_finish (G_INPUT_STREAM (stream),
Packit ae235b
				      result, &error);
Packit ae235b
  if (nread == -1)
Packit ae235b
    {
Packit ae235b
      g_free (buf);
Packit ae235b
      g_task_return_error (task, error);
Packit ae235b
    }
Packit ae235b
  else if (nread == 0)
Packit ae235b
    {
Packit ae235b
      g_free (buf);
Packit ae235b
      bytes = g_bytes_new_static ("", 0);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    bytes = g_bytes_new_take (buf, nread);
Packit ae235b
Packit ae235b
  if (bytes)
Packit ae235b
    g_task_return_pointer (task, bytes, (GDestroyNotify)g_bytes_unref);
Packit ae235b
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_read_bytes_async:
Packit ae235b
 * @stream: A #GInputStream.
Packit ae235b
 * @count: the number of bytes that will be read from the stream
Packit ae235b
 * @io_priority: the [I/O priority][io-priority] of the request
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @callback: (scope async): 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 read of @count bytes from the stream into a
Packit ae235b
 * new #GBytes. When the operation is finished @callback will be
Packit ae235b
 * called. You can then call g_input_stream_read_bytes_finish() to get the
Packit ae235b
 * result of the operation.
Packit ae235b
 *
Packit ae235b
 * During an async request no other sync and async calls are allowed
Packit ae235b
 * on @stream, 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 new #GBytes will be passed to the callback. It is
Packit ae235b
 * not an error if this is smaller than the requested size, as it can
Packit ae235b
 * happen e.g. near the end of a file, but generally we try to read as
Packit ae235b
 * many bytes as requested. Zero is returned on end of file (or if
Packit ae235b
 * @count is zero), but never otherwise.
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
 * Since: 2.34
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_input_stream_read_bytes_async (GInputStream          *stream,
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
  guchar *buf;
Packit ae235b
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_input_stream_read_bytes_async);
Packit ae235b
Packit ae235b
  buf = g_malloc (count);
Packit ae235b
  g_task_set_task_data (task, buf, NULL);
Packit ae235b
Packit ae235b
  g_input_stream_read_async (stream, buf, count,
Packit ae235b
                             io_priority, cancellable,
Packit ae235b
                             read_bytes_callback, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_read_bytes_finish:
Packit ae235b
 * @stream: a #GInputStream.
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 read-into-#GBytes operation.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): the newly-allocated #GBytes, or %NULL on error
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 **/
Packit ae235b
GBytes *
Packit ae235b
g_input_stream_read_bytes_finish (GInputStream  *stream,
Packit ae235b
				  GAsyncResult  *result,
Packit ae235b
				  GError       **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
Packit ae235b
Packit ae235b
  return g_task_propagate_pointer (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_skip_async:
Packit ae235b
 * @stream: A #GInputStream.
Packit ae235b
 * @count: the number of bytes that will be skipped from the stream
Packit ae235b
 * @io_priority: the [I/O priority][io-priority] of the request
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @callback: (scope async): 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 skip of @count bytes from the stream.
Packit ae235b
 * When the operation is finished @callback will be called.
Packit ae235b
 * You can then call g_input_stream_skip_finish() to get the result
Packit ae235b
 * of the 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 %G_IO_ERROR_INVALID_ARGUMENT error.
Packit ae235b
 *
Packit ae235b
 * On success, the number of bytes skipped will be passed to the callback.
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. near the end of a file, but generally we try to skip
Packit ae235b
 * as many bytes as requested. Zero is returned on end of file
Packit ae235b
 * (or if @count is zero), but never otherwise.
Packit ae235b
 *
Packit ae235b
 * Any outstanding i/o request with higher priority (lower numerical value)
Packit ae235b
 * will be executed before an outstanding request with lower priority.
Packit ae235b
 * Default priority is %G_PRIORITY_DEFAULT.
Packit ae235b
 *
Packit ae235b
 * The asynchronous methods have a default fallback that uses threads to
Packit ae235b
 * implement asynchronicity, so they are optional for inheriting classes.
Packit ae235b
 * However, if you override one, you must override all.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_input_stream_skip_async (GInputStream        *stream,
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
  GInputStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_INPUT_STREAM (stream));
Packit ae235b
Packit ae235b
  if (count == 0)
Packit ae235b
    {
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_input_stream_skip_async);
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_report_new_error (stream, callback, user_data,
Packit ae235b
                               g_input_stream_skip_async,
Packit ae235b
                               G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                               _("Too large count value passed to %s"),
Packit ae235b
                               G_STRFUNC);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_input_stream_set_pending (stream, &error))
Packit ae235b
    {
Packit ae235b
      g_task_report_error (stream, callback, user_data,
Packit ae235b
                           g_input_stream_skip_async,
Packit ae235b
                           error);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  stream->priv->outstanding_callback = callback;
Packit ae235b
  g_object_ref (stream);
Packit ae235b
  class->skip_async (stream, count, io_priority, cancellable,
Packit ae235b
		     async_ready_callback_wrapper, user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_skip_finish:
Packit ae235b
 * @stream: a #GInputStream.
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 skip operation.
Packit ae235b
 * 
Packit ae235b
 * Returns: the size of the bytes skipped, or %-1 on error.
Packit ae235b
 **/
Packit ae235b
gssize
Packit ae235b
g_input_stream_skip_finish (GInputStream  *stream,
Packit ae235b
			    GAsyncResult  *result,
Packit ae235b
			    GError       **error)
Packit ae235b
{
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
Packit ae235b
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
Packit ae235b
Packit ae235b
  if (g_async_result_legacy_propagate_error (result, error))
Packit ae235b
    return -1;
Packit ae235b
  else if (g_async_result_is_tagged (result, g_input_stream_skip_async))
Packit ae235b
    return g_task_propagate_int (G_TASK (result), error);
Packit ae235b
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  return class->skip_finish (stream, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_close_async:
Packit ae235b
 * @stream: A #GInputStream.
Packit ae235b
 * @io_priority: the [I/O priority][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 closes of the stream, releasing resources related to it.
Packit ae235b
 * When the operation is finished @callback will be called. 
Packit ae235b
 * You can then call g_input_stream_close_finish() to get the result of the 
Packit ae235b
 * operation.
Packit ae235b
 *
Packit ae235b
 * For behaviour details see g_input_stream_close().
Packit ae235b
 *
Packit ae235b
 * The asynchronous methods have a default fallback that uses threads to implement
Packit ae235b
 * asynchronicity, so they are optional for inheriting classes. However, if you
Packit ae235b
 * override one you must override all.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_input_stream_close_async (GInputStream        *stream,
Packit ae235b
			    int                  io_priority,
Packit ae235b
			    GCancellable        *cancellable,
Packit ae235b
			    GAsyncReadyCallback  callback,
Packit ae235b
			    gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_INPUT_STREAM (stream));
Packit ae235b
Packit ae235b
  if (stream->priv->closed)
Packit ae235b
    {
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_input_stream_close_async);
Packit ae235b
      g_task_return_boolean (task, TRUE);
Packit ae235b
      g_object_unref (task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_input_stream_set_pending (stream, &error))
Packit ae235b
    {
Packit ae235b
      g_task_report_error (stream, callback, user_data,
Packit ae235b
                           g_input_stream_close_async,
Packit ae235b
                           error);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  stream->priv->outstanding_callback = callback;
Packit ae235b
  g_object_ref (stream);
Packit ae235b
  class->close_async (stream, io_priority, cancellable,
Packit ae235b
		      async_ready_close_callback_wrapper, user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_close_finish:
Packit ae235b
 * @stream: a #GInputStream.
Packit ae235b
 * @result: a #GAsyncResult.
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to 
Packit ae235b
 * ignore.
Packit ae235b
 * 
Packit ae235b
 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE if the stream was closed successfully.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_input_stream_close_finish (GInputStream  *stream,
Packit ae235b
			     GAsyncResult  *result,
Packit ae235b
			     GError       **error)
Packit ae235b
{
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
Packit ae235b
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
Packit ae235b
Packit ae235b
  if (g_async_result_legacy_propagate_error (result, error))
Packit ae235b
    return FALSE;
Packit ae235b
  else if (g_async_result_is_tagged (result, g_input_stream_close_async))
Packit ae235b
    return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  return class->close_finish (stream, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_is_closed:
Packit ae235b
 * @stream: input stream.
Packit ae235b
 * 
Packit ae235b
 * Checks if an input stream is closed.
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE if the stream is closed.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_input_stream_is_closed (GInputStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
Packit ae235b
  
Packit ae235b
  return stream->priv->closed;
Packit ae235b
}
Packit ae235b
 
Packit ae235b
/**
Packit ae235b
 * g_input_stream_has_pending:
Packit ae235b
 * @stream: input stream.
Packit ae235b
 * 
Packit ae235b
 * Checks if an input stream has pending actions.
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE if @stream has pending actions.
Packit ae235b
 **/  
Packit ae235b
gboolean
Packit ae235b
g_input_stream_has_pending (GInputStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
Packit ae235b
  
Packit ae235b
  return stream->priv->pending;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_input_stream_set_pending:
Packit ae235b
 * @stream: input stream
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_input_stream_set_pending (GInputStream *stream, GError **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_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 already an
Packit ae235b
		 * operation running against this stream when you try to start
Packit ae235b
		 * 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_input_stream_clear_pending:
Packit ae235b
 * @stream: input stream
Packit ae235b
 * 
Packit ae235b
 * Clears the pending flag on @stream.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_input_stream_clear_pending (GInputStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_INPUT_STREAM (stream));
Packit ae235b
  
Packit ae235b
  stream->priv->pending = FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< internal >
Packit ae235b
 * g_input_stream_async_read_is_via_threads:
Packit ae235b
 * @stream: input stream
Packit ae235b
 *
Packit ae235b
 * Checks if an input stream's read_async function uses threads.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @stream's read_async function uses threads.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_input_stream_async_read_is_via_threads (GInputStream *stream)
Packit ae235b
{
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  return (class->read_async == g_input_stream_real_read_async &&
Packit ae235b
      !(G_IS_POLLABLE_INPUT_STREAM (stream) &&
Packit ae235b
        g_pollable_input_stream_can_poll (G_POLLABLE_INPUT_STREAM (stream))));
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< internal >
Packit ae235b
 * g_input_stream_async_close_is_via_threads:
Packit ae235b
 * @stream: input stream
Packit ae235b
 *
Packit ae235b
 * Checks if an input 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_input_stream_async_close_is_via_threads (GInputStream *stream)
Packit ae235b
{
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  return class->close_async == g_input_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
  void   *buffer;
Packit ae235b
  gsize   count;
Packit ae235b
} ReadData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
free_read_data (ReadData *op)
Packit ae235b
{
Packit ae235b
  g_slice_free (ReadData, op);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
read_async_thread (GTask        *task,
Packit ae235b
                   gpointer      source_object,
Packit ae235b
                   gpointer      task_data,
Packit ae235b
                   GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GInputStream *stream = source_object;
Packit ae235b
  ReadData *op = task_data;
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gssize nread;
Packit ae235b
 
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  nread = class->read_fn (stream,
Packit ae235b
                          op->buffer, op->count,
Packit ae235b
                          g_task_get_cancellable (task),
Packit ae235b
                          &error);
Packit ae235b
  if (nread == -1)
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  else
Packit ae235b
    g_task_return_int (task, nread);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void read_async_pollable (GPollableInputStream *stream,
Packit ae235b
                                 GTask                *task);
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
read_async_pollable_ready (GPollableInputStream *stream,
Packit ae235b
			   gpointer              user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = user_data;
Packit ae235b
Packit ae235b
  read_async_pollable (stream, task);
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
read_async_pollable (GPollableInputStream *stream,
Packit ae235b
                     GTask                *task)
Packit ae235b
{
Packit ae235b
  ReadData *op = g_task_get_task_data (task);
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gssize nread;
Packit ae235b
Packit ae235b
  if (g_task_return_error_if_cancelled (task))
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  nread = G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
Packit ae235b
    read_nonblocking (stream, op->buffer, op->count, &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_input_stream_create_source (stream,
Packit ae235b
                                                      g_task_get_cancellable (task));
Packit ae235b
      g_task_attach_source (task, source,
Packit ae235b
                            (GSourceFunc) read_async_pollable_ready);
Packit ae235b
      g_source_unref (source);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (nread == -1)
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  else
Packit ae235b
    g_task_return_int (task, nread);
Packit ae235b
  /* g_input_stream_real_read_async() unrefs task */
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_input_stream_real_read_async (GInputStream        *stream,
Packit ae235b
				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
  ReadData *op;
Packit ae235b
  
Packit ae235b
  op = g_slice_new0 (ReadData);
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_input_stream_real_read_async);
Packit ae235b
  g_task_set_task_data (task, op, (GDestroyNotify) free_read_data);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
  op->buffer = buffer;
Packit ae235b
  op->count = count;
Packit ae235b
Packit ae235b
  if (!g_input_stream_async_read_is_via_threads (stream))
Packit ae235b
    read_async_pollable (G_POLLABLE_INPUT_STREAM (stream), task);
Packit ae235b
  else
Packit ae235b
    g_task_run_in_thread (task, read_async_thread);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_input_stream_real_read_finish (GInputStream  *stream,
Packit ae235b
				 GAsyncResult  *result,
Packit ae235b
				 GError       **error)
Packit ae235b
{
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
Packit ae235b
static void
Packit ae235b
skip_async_thread (GTask        *task,
Packit ae235b
                   gpointer      source_object,
Packit ae235b
                   gpointer      task_data,
Packit ae235b
                   GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GInputStream *stream = source_object;
Packit ae235b
  gsize count = GPOINTER_TO_SIZE (task_data);
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gssize ret;
Packit ae235b
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  ret = class->skip (stream, count,
Packit ae235b
                     g_task_get_cancellable (task),
Packit ae235b
                     &error);
Packit ae235b
  if (ret == -1)
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  else
Packit ae235b
    g_task_return_int (task, ret);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  char buffer[8192];
Packit ae235b
  gsize count;
Packit ae235b
  gsize count_skipped;
Packit ae235b
} SkipFallbackAsyncData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
skip_callback_wrapper (GObject      *source_object,
Packit ae235b
		       GAsyncResult *res,
Packit ae235b
		       gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  SkipFallbackAsyncData *data = g_task_get_task_data (task);
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gssize ret;
Packit ae235b
Packit ae235b
  ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
Packit ae235b
Packit ae235b
  if (ret > 0)
Packit ae235b
    {
Packit ae235b
      data->count -= ret;
Packit ae235b
      data->count_skipped += ret;
Packit ae235b
Packit ae235b
      if (data->count > 0)
Packit ae235b
	{
Packit ae235b
	  class = G_INPUT_STREAM_GET_CLASS (source_object);
Packit ae235b
	  class->read_async (G_INPUT_STREAM (source_object),
Packit ae235b
                             data->buffer, MIN (8192, data->count),
Packit ae235b
                             g_task_get_priority (task),
Packit ae235b
                             g_task_get_cancellable (task),
Packit ae235b
                             skip_callback_wrapper, task);
Packit ae235b
	  return;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (ret == -1 &&
Packit ae235b
      g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
Packit ae235b
      data->count_skipped)
Packit ae235b
    {
Packit ae235b
      /* No error, return partial read */
Packit ae235b
      g_clear_error (&error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (error)
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  else
Packit ae235b
    g_task_return_int (task, data->count_skipped);
Packit ae235b
  g_object_unref (task);
Packit ae235b
 }
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_input_stream_real_skip_async (GInputStream        *stream,
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
  GInputStreamClass *class;
Packit ae235b
  SkipFallbackAsyncData *data;
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_input_stream_real_skip_async);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
Packit ae235b
  if (g_input_stream_async_read_is_via_threads (stream))
Packit ae235b
    {
Packit ae235b
      /* Read is thread-using async fallback.
Packit ae235b
       * Make skip use threads too, so that we can use a possible sync skip
Packit ae235b
       * implementation. */
Packit ae235b
      g_task_set_task_data (task, GSIZE_TO_POINTER (count), NULL);
Packit ae235b
Packit ae235b
      g_task_run_in_thread (task, skip_async_thread);
Packit ae235b
      g_object_unref (task);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* TODO: Skip fallback uses too much memory, should do multiple read calls */
Packit ae235b
      
Packit ae235b
      /* There is a custom async read function, lets use that. */
Packit ae235b
      data = g_new (SkipFallbackAsyncData, 1);
Packit ae235b
      data->count = count;
Packit ae235b
      data->count_skipped = 0;
Packit ae235b
      g_task_set_task_data (task, data, g_free);
Packit ae235b
      g_task_set_check_cancellable (task, FALSE);
Packit ae235b
      class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
Packit ae235b
			 skip_callback_wrapper, task);
Packit ae235b
    }
Packit ae235b
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_input_stream_real_skip_finish (GInputStream  *stream,
Packit ae235b
				 GAsyncResult  *result,
Packit ae235b
				 GError       **error)
Packit ae235b
{
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
close_async_thread (GTask        *task,
Packit ae235b
                    gpointer      source_object,
Packit ae235b
                    gpointer      task_data,
Packit ae235b
                    GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GInputStream *stream = source_object;
Packit ae235b
  GInputStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gboolean result;
Packit ae235b
Packit ae235b
  class = G_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  if (class->close_fn)
Packit ae235b
    {
Packit ae235b
      result = class->close_fn (stream,
Packit ae235b
                                g_task_get_cancellable (task),
Packit ae235b
                                &error);
Packit ae235b
      if (!result)
Packit ae235b
        {
Packit ae235b
          g_task_return_error (task, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_task_return_boolean (task, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_input_stream_real_close_async (GInputStream        *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_source_tag (task, g_input_stream_real_close_async);
Packit ae235b
  g_task_set_check_cancellable (task, FALSE);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
  
Packit ae235b
  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_input_stream_real_close_finish (GInputStream  *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
}