Blame gio/gfileiostream.c

Packit ae235b
/* GIO - GLib Input, IO 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
Packit ae235b
#include <glib.h>
Packit ae235b
#include <gfileiostream.h>
Packit ae235b
#include <gseekable.h>
Packit ae235b
#include "gasyncresult.h"
Packit ae235b
#include "gtask.h"
Packit ae235b
#include "gcancellable.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "gfileoutputstream.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gfileiostream
Packit ae235b
 * @short_description:  File read and write streaming operations
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GIOStream, #GFileInputStream, #GFileOutputStream, #GSeekable
Packit ae235b
 *
Packit ae235b
 * GFileIOStream provides io streams that both read and write to the same
Packit ae235b
 * file handle.
Packit ae235b
 *
Packit ae235b
 * GFileIOStream implements #GSeekable, which allows the io
Packit ae235b
 * stream to jump to arbitrary positions in the file and to truncate
Packit ae235b
 * the file, provided the filesystem of the file supports these
Packit ae235b
 * operations.
Packit ae235b
 *
Packit ae235b
 * To find the position of a file io stream, use
Packit ae235b
 * g_seekable_tell().
Packit ae235b
 *
Packit ae235b
 * To find out if a file io stream supports seeking, use g_seekable_can_seek().
Packit ae235b
 * To position a file io stream, use g_seekable_seek().
Packit ae235b
 * To find out if a file io stream supports truncating, use
Packit ae235b
 * g_seekable_can_truncate(). To truncate a file io
Packit ae235b
 * stream, use g_seekable_truncate().
Packit ae235b
 *
Packit ae235b
 * The default implementation of all the #GFileIOStream operations
Packit ae235b
 * and the implementation of #GSeekable just call into the same operations
Packit ae235b
 * on the output stream.
Packit ae235b
 * Since: 2.22
Packit ae235b
 **/
Packit ae235b
Packit ae235b
static void       g_file_io_stream_seekable_iface_init    (GSeekableIface       *iface);
Packit ae235b
static goffset    g_file_io_stream_seekable_tell          (GSeekable            *seekable);
Packit ae235b
static gboolean   g_file_io_stream_seekable_can_seek      (GSeekable            *seekable);
Packit ae235b
static gboolean   g_file_io_stream_seekable_seek          (GSeekable            *seekable,
Packit ae235b
							   goffset               offset,
Packit ae235b
							   GSeekType             type,
Packit ae235b
							   GCancellable         *cancellable,
Packit ae235b
							   GError              **error);
Packit ae235b
static gboolean   g_file_io_stream_seekable_can_truncate  (GSeekable            *seekable);
Packit ae235b
static gboolean   g_file_io_stream_seekable_truncate      (GSeekable            *seekable,
Packit ae235b
							   goffset               offset,
Packit ae235b
							   GCancellable         *cancellable,
Packit ae235b
							   GError              **error);
Packit ae235b
static void       g_file_io_stream_real_query_info_async  (GFileIOStream    *stream,
Packit ae235b
							   const char           *attributes,
Packit ae235b
							   int                   io_priority,
Packit ae235b
							   GCancellable         *cancellable,
Packit ae235b
							   GAsyncReadyCallback   callback,
Packit ae235b
							   gpointer              user_data);
Packit ae235b
static GFileInfo *g_file_io_stream_real_query_info_finish (GFileIOStream    *stream,
Packit ae235b
							   GAsyncResult         *result,
Packit ae235b
							   GError              **error);
Packit ae235b
Packit ae235b
struct _GFileIOStreamPrivate {
Packit ae235b
  GAsyncReadyCallback outstanding_callback;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GFileIOStream, g_file_io_stream, G_TYPE_IO_STREAM,
Packit ae235b
                         G_ADD_PRIVATE (GFileIOStream)
Packit ae235b
			 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
Packit ae235b
						g_file_io_stream_seekable_iface_init))
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_io_stream_seekable_iface_init (GSeekableIface *iface)
Packit ae235b
{
Packit ae235b
  iface->tell = g_file_io_stream_seekable_tell;
Packit ae235b
  iface->can_seek = g_file_io_stream_seekable_can_seek;
Packit ae235b
  iface->seek = g_file_io_stream_seekable_seek;
Packit ae235b
  iface->can_truncate = g_file_io_stream_seekable_can_truncate;
Packit ae235b
  iface->truncate_fn = g_file_io_stream_seekable_truncate;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_io_stream_init (GFileIOStream *stream)
Packit ae235b
{
Packit ae235b
  stream->priv = g_file_io_stream_get_instance_private (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_io_stream_query_info:
Packit ae235b
 * @stream: a #GFileIOStream.
Packit ae235b
 * @attributes: a file attribute query string.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @error: a #GError, %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Queries a file io stream for the given @attributes.
Packit ae235b
 * This function blocks while querying the stream. For the asynchronous
Packit ae235b
 * version of this function, see g_file_io_stream_query_info_async().
Packit ae235b
 * While the stream is blocked, the stream will set the pending flag
Packit ae235b
 * internally, and any other operations on the stream will fail with
Packit ae235b
 * %G_IO_ERROR_PENDING.
Packit ae235b
 *
Packit ae235b
 * Can fail if the stream was already closed (with @error being set to
Packit ae235b
 * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
Packit ae235b
 * set to %G_IO_ERROR_PENDING), or if querying info is not supported for
Packit ae235b
 * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). I
Packit ae235b
 * all cases of failure, %NULL will be returned.
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 set, and %NULL will
Packit ae235b
 * be returned.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GFileInfo for the @stream, or %NULL on error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 **/
Packit ae235b
GFileInfo *
Packit ae235b
g_file_io_stream_query_info (GFileIOStream      *stream,
Packit ae235b
			     const char             *attributes,
Packit ae235b
			     GCancellable           *cancellable,
Packit ae235b
			     GError                **error)
Packit ae235b
{
Packit ae235b
  GFileIOStreamClass *class;
Packit ae235b
  GIOStream *io_stream;
Packit ae235b
  GFileInfo *info;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
Packit ae235b
Packit ae235b
  io_stream = G_IO_STREAM (stream);
Packit ae235b
Packit ae235b
  if (!g_io_stream_set_pending (io_stream, error))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  info = NULL;
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_push_current (cancellable);
Packit ae235b
Packit ae235b
  class = G_FILE_IO_STREAM_GET_CLASS (stream);
Packit ae235b
  if (class->query_info)
Packit ae235b
    info = class->query_info (stream, attributes, cancellable, error);
Packit ae235b
  else
Packit ae235b
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                         _("Stream doesn’t support query_info"));
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_pop_current (cancellable);
Packit ae235b
Packit ae235b
  g_io_stream_clear_pending (io_stream);
Packit ae235b
Packit ae235b
  return info;
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
  GFileIOStream *stream = G_FILE_IO_STREAM (source_object);
Packit ae235b
Packit ae235b
  g_io_stream_clear_pending (G_IO_STREAM (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
/**
Packit ae235b
 * g_file_io_stream_query_info_async:
Packit ae235b
 * @stream: a #GFileIOStream.
Packit ae235b
 * @attributes: a file attribute query string.
Packit ae235b
 * @io_priority: the [I/O priority][gio-GIOScheduler] 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
 * Asynchronously queries the @stream for a #GFileInfo. When completed,
Packit ae235b
 * @callback will be called with a #GAsyncResult which can be used to
Packit ae235b
 * finish the operation with g_file_io_stream_query_info_finish().
Packit ae235b
 *
Packit ae235b
 * For the synchronous version of this function, see
Packit ae235b
 * g_file_io_stream_query_info().
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_file_io_stream_query_info_async (GFileIOStream     *stream,
Packit ae235b
					  const char           *attributes,
Packit ae235b
					  int                   io_priority,
Packit ae235b
					  GCancellable         *cancellable,
Packit ae235b
					  GAsyncReadyCallback   callback,
Packit ae235b
					  gpointer              user_data)
Packit ae235b
{
Packit ae235b
  GFileIOStreamClass *klass;
Packit ae235b
  GIOStream *io_stream;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_FILE_IO_STREAM (stream));
Packit ae235b
Packit ae235b
  io_stream = G_IO_STREAM (stream);
Packit ae235b
Packit ae235b
  if (!g_io_stream_set_pending (io_stream, &error))
Packit ae235b
    {
Packit ae235b
      g_task_report_error (stream, callback, user_data,
Packit ae235b
                           g_file_io_stream_query_info_async,
Packit ae235b
                           error);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  klass = G_FILE_IO_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  stream->priv->outstanding_callback = callback;
Packit ae235b
  g_object_ref (stream);
Packit ae235b
  klass->query_info_async (stream, attributes, io_priority, cancellable,
Packit ae235b
                           async_ready_callback_wrapper, user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_io_stream_query_info_finish:
Packit ae235b
 * @stream: a #GFileIOStream.
Packit ae235b
 * @result: a #GAsyncResult.
Packit ae235b
 * @error: a #GError, %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Finalizes the asynchronous query started
Packit ae235b
 * by g_file_io_stream_query_info_async().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): A #GFileInfo for the finished query.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 **/
Packit ae235b
GFileInfo *
Packit ae235b
g_file_io_stream_query_info_finish (GFileIOStream     *stream,
Packit ae235b
				    GAsyncResult         *result,
Packit ae235b
				    GError              **error)
Packit ae235b
{
Packit ae235b
  GFileIOStreamClass *class;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
Packit ae235b
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
Packit ae235b
Packit ae235b
  if (g_async_result_legacy_propagate_error (result, error))
Packit ae235b
    return NULL;
Packit ae235b
  else if (g_async_result_is_tagged (result, g_file_io_stream_query_info_async))
Packit ae235b
    return g_task_propagate_pointer (G_TASK (result), error);
Packit ae235b
Packit ae235b
  class = G_FILE_IO_STREAM_GET_CLASS (stream);
Packit ae235b
  return class->query_info_finish (stream, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_io_stream_get_etag:
Packit ae235b
 * @stream: a #GFileIOStream.
Packit ae235b
 *
Packit ae235b
 * Gets the entity tag for the file when it has been written.
Packit ae235b
 * This must be called after the stream has been written
Packit ae235b
 * and closed, as the etag can change while writing.
Packit ae235b
 *
Packit ae235b
 * Returns: the entity tag for the stream.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 **/
Packit ae235b
char *
Packit ae235b
g_file_io_stream_get_etag (GFileIOStream  *stream)
Packit ae235b
{
Packit ae235b
  GFileIOStreamClass *class;
Packit ae235b
  GIOStream *io_stream;
Packit ae235b
  char *etag;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
Packit ae235b
Packit ae235b
  io_stream = G_IO_STREAM (stream);
Packit ae235b
Packit ae235b
  if (!g_io_stream_is_closed (io_stream))
Packit ae235b
    {
Packit ae235b
      g_warning ("stream is not closed yet, can't get etag");
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  etag = NULL;
Packit ae235b
Packit ae235b
  class = G_FILE_IO_STREAM_GET_CLASS (stream);
Packit ae235b
  if (class->get_etag)
Packit ae235b
    etag = class->get_etag (stream);
Packit ae235b
Packit ae235b
  return etag;
Packit ae235b
}
Packit ae235b
Packit ae235b
static goffset
Packit ae235b
g_file_io_stream_tell (GFileIOStream  *stream)
Packit ae235b
{
Packit ae235b
  GFileIOStreamClass *class;
Packit ae235b
  goffset offset;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), 0);
Packit ae235b
Packit ae235b
  class = G_FILE_IO_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  offset = 0;
Packit ae235b
  if (class->tell)
Packit ae235b
    offset = class->tell (stream);
Packit ae235b
Packit ae235b
  return offset;
Packit ae235b
}
Packit ae235b
Packit ae235b
static goffset
Packit ae235b
g_file_io_stream_seekable_tell (GSeekable *seekable)
Packit ae235b
{
Packit ae235b
  return g_file_io_stream_tell (G_FILE_IO_STREAM (seekable));
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_io_stream_can_seek (GFileIOStream  *stream)
Packit ae235b
{
Packit ae235b
  GFileIOStreamClass *class;
Packit ae235b
  gboolean can_seek;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  class = G_FILE_IO_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  can_seek = FALSE;
Packit ae235b
  if (class->seek)
Packit ae235b
    {
Packit ae235b
      can_seek = TRUE;
Packit ae235b
      if (class->can_seek)
Packit ae235b
	can_seek = class->can_seek (stream);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return can_seek;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_io_stream_seekable_can_seek (GSeekable *seekable)
Packit ae235b
{
Packit ae235b
  return g_file_io_stream_can_seek (G_FILE_IO_STREAM (seekable));
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_io_stream_seek (GFileIOStream  *stream,
Packit ae235b
		       goffset             offset,
Packit ae235b
		       GSeekType           type,
Packit ae235b
		       GCancellable       *cancellable,
Packit ae235b
		       GError            **error)
Packit ae235b
{
Packit ae235b
  GFileIOStreamClass *class;
Packit ae235b
  GIOStream *io_stream;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  io_stream = G_IO_STREAM (stream);
Packit ae235b
  class = G_FILE_IO_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  if (!class->seek)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                           _("Seek not supported on stream"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_io_stream_set_pending (io_stream, error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_push_current (cancellable);
Packit ae235b
Packit ae235b
  res = class->seek (stream, offset, type, cancellable, error);
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_pop_current (cancellable);
Packit ae235b
Packit ae235b
  g_io_stream_clear_pending (io_stream);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_io_stream_seekable_seek (GSeekable  *seekable,
Packit ae235b
				    goffset     offset,
Packit ae235b
				    GSeekType   type,
Packit ae235b
				    GCancellable  *cancellable,
Packit ae235b
				    GError    **error)
Packit ae235b
{
Packit ae235b
  return g_file_io_stream_seek (G_FILE_IO_STREAM (seekable),
Packit ae235b
				offset, type, cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_io_stream_can_truncate (GFileIOStream  *stream)
Packit ae235b
{
Packit ae235b
  GFileIOStreamClass *class;
Packit ae235b
  gboolean can_truncate;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  class = G_FILE_IO_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  can_truncate = FALSE;
Packit ae235b
  if (class->truncate_fn)
Packit ae235b
    {
Packit ae235b
      can_truncate = TRUE;
Packit ae235b
      if (class->can_truncate)
Packit ae235b
	can_truncate = class->can_truncate (stream);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return can_truncate;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_io_stream_seekable_can_truncate (GSeekable  *seekable)
Packit ae235b
{
Packit ae235b
  return g_file_io_stream_can_truncate (G_FILE_IO_STREAM (seekable));
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_io_stream_truncate (GFileIOStream  *stream,
Packit ae235b
			   goffset             size,
Packit ae235b
			   GCancellable       *cancellable,
Packit ae235b
			   GError            **error)
Packit ae235b
{
Packit ae235b
  GFileIOStreamClass *class;
Packit ae235b
  GIOStream *io_stream;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  io_stream = G_IO_STREAM (stream);
Packit ae235b
  class = G_FILE_IO_STREAM_GET_CLASS (stream);
Packit ae235b
Packit ae235b
  if (!class->truncate_fn)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                           _("Truncate not supported on stream"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_io_stream_set_pending (io_stream, error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_push_current (cancellable);
Packit ae235b
Packit ae235b
  res = class->truncate_fn (stream, size, cancellable, error);
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_pop_current (cancellable);
Packit ae235b
Packit ae235b
  g_io_stream_clear_pending (io_stream);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_io_stream_seekable_truncate (GSeekable     *seekable,
Packit ae235b
				    goffset        size,
Packit ae235b
				    GCancellable  *cancellable,
Packit ae235b
				    GError       **error)
Packit ae235b
{
Packit ae235b
  return g_file_io_stream_truncate (G_FILE_IO_STREAM (seekable),
Packit ae235b
					size, cancellable, error);
Packit ae235b
}
Packit ae235b
/*****************************************************
Packit ae235b
 *   Default implementations based on output stream  *
Packit ae235b
 *****************************************************/
Packit ae235b
Packit ae235b
static goffset
Packit ae235b
g_file_io_stream_real_tell (GFileIOStream    *stream)
Packit ae235b
{
Packit ae235b
  GOutputStream *out;
Packit ae235b
  GSeekable *seekable;
Packit ae235b
Packit ae235b
  out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
Packit ae235b
  seekable = G_SEEKABLE (out);
Packit ae235b
Packit ae235b
  return g_seekable_tell (seekable);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_io_stream_real_can_seek (GFileIOStream    *stream)
Packit ae235b
{
Packit ae235b
  GOutputStream *out;
Packit ae235b
  GSeekable *seekable;
Packit ae235b
Packit ae235b
  out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
Packit ae235b
  seekable = G_SEEKABLE (out);
Packit ae235b
Packit ae235b
  return g_seekable_can_seek (seekable);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_io_stream_real_seek (GFileIOStream    *stream,
Packit ae235b
			    goffset           offset,
Packit ae235b
			    GSeekType         type,
Packit ae235b
			    GCancellable     *cancellable,
Packit ae235b
			    GError          **error)
Packit ae235b
{
Packit ae235b
  GOutputStream *out;
Packit ae235b
  GSeekable *seekable;
Packit ae235b
Packit ae235b
  out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
Packit ae235b
  seekable = G_SEEKABLE (out);
Packit ae235b
Packit ae235b
  return g_seekable_seek (seekable, offset, type, cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static  gboolean
Packit ae235b
g_file_io_stream_real_can_truncate (GFileIOStream    *stream)
Packit ae235b
{
Packit ae235b
  GOutputStream *out;
Packit ae235b
  GSeekable *seekable;
Packit ae235b
Packit ae235b
  out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
Packit ae235b
  seekable = G_SEEKABLE (out);
Packit ae235b
Packit ae235b
  return g_seekable_can_truncate (seekable);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_io_stream_real_truncate_fn (GFileIOStream    *stream,
Packit ae235b
				   goffset               size,
Packit ae235b
				   GCancellable         *cancellable,
Packit ae235b
				   GError              **error)
Packit ae235b
{
Packit ae235b
  GOutputStream *out;
Packit ae235b
  GSeekable *seekable;
Packit ae235b
Packit ae235b
  out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
Packit ae235b
  seekable = G_SEEKABLE (out);
Packit ae235b
Packit ae235b
  return g_seekable_truncate (seekable, size, cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
g_file_io_stream_real_get_etag (GFileIOStream    *stream)
Packit ae235b
{
Packit ae235b
  GOutputStream *out;
Packit ae235b
  GFileOutputStream *file_out;
Packit ae235b
Packit ae235b
  out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
Packit ae235b
  file_out = G_FILE_OUTPUT_STREAM (out);
Packit ae235b
Packit ae235b
  return g_file_output_stream_get_etag (file_out);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileInfo *
Packit ae235b
g_file_io_stream_real_query_info (GFileIOStream    *stream,
Packit ae235b
				  const char           *attributes,
Packit ae235b
				  GCancellable         *cancellable,
Packit ae235b
				  GError              **error)
Packit ae235b
{
Packit ae235b
  GOutputStream *out;
Packit ae235b
  GFileOutputStream *file_out;
Packit ae235b
Packit ae235b
  out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
Packit ae235b
  file_out = G_FILE_OUTPUT_STREAM (out);
Packit ae235b
Packit ae235b
  return g_file_output_stream_query_info (file_out,
Packit ae235b
					  attributes, cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GObject *object;
Packit ae235b
  GAsyncReadyCallback callback;
Packit ae235b
  gpointer user_data;
Packit ae235b
} AsyncOpWrapper;
Packit ae235b
Packit ae235b
static AsyncOpWrapper *
Packit ae235b
async_op_wrapper_new (gpointer object,
Packit ae235b
		      GAsyncReadyCallback callback,
Packit ae235b
		      gpointer user_data)
Packit ae235b
{
Packit ae235b
  AsyncOpWrapper *data;
Packit ae235b
Packit ae235b
  data = g_new0 (AsyncOpWrapper, 1);
Packit ae235b
  data->object = g_object_ref (object);
Packit ae235b
  data->callback = callback;
Packit ae235b
  data->user_data = user_data;
Packit ae235b
Packit ae235b
  return data;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
async_op_wrapper_callback (GObject *source_object,
Packit ae235b
			   GAsyncResult *res,
Packit ae235b
			   gpointer user_data)
Packit ae235b
{
Packit ae235b
  AsyncOpWrapper *data  = user_data;
Packit ae235b
  data->callback (data->object, res, data->user_data);
Packit ae235b
  g_object_unref (data->object);
Packit ae235b
  g_free (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_io_stream_real_query_info_async (GFileIOStream     *stream,
Packit ae235b
					const char           *attributes,
Packit ae235b
					int                   io_priority,
Packit ae235b
					GCancellable         *cancellable,
Packit ae235b
					GAsyncReadyCallback   callback,
Packit ae235b
					gpointer              user_data)
Packit ae235b
{
Packit ae235b
  GOutputStream *out;
Packit ae235b
  GFileOutputStream *file_out;
Packit ae235b
  AsyncOpWrapper *data;
Packit ae235b
Packit ae235b
  out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
Packit ae235b
  file_out = G_FILE_OUTPUT_STREAM (out);
Packit ae235b
Packit ae235b
  data = async_op_wrapper_new (stream, callback, user_data);
Packit ae235b
  g_file_output_stream_query_info_async (file_out,
Packit ae235b
					 attributes, io_priority,
Packit ae235b
					 cancellable, async_op_wrapper_callback, data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileInfo *
Packit ae235b
g_file_io_stream_real_query_info_finish (GFileIOStream     *stream,
Packit ae235b
					 GAsyncResult      *res,
Packit ae235b
					 GError           **error)
Packit ae235b
{
Packit ae235b
  GOutputStream *out;
Packit ae235b
  GFileOutputStream *file_out;
Packit ae235b
Packit ae235b
  out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
Packit ae235b
  file_out = G_FILE_OUTPUT_STREAM (out);
Packit ae235b
Packit ae235b
  return g_file_output_stream_query_info_finish (file_out, res, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_io_stream_class_init (GFileIOStreamClass *klass)
Packit ae235b
{
Packit ae235b
  klass->tell = g_file_io_stream_real_tell;
Packit ae235b
  klass->can_seek = g_file_io_stream_real_can_seek;
Packit ae235b
  klass->seek = g_file_io_stream_real_seek;
Packit ae235b
  klass->can_truncate = g_file_io_stream_real_can_truncate;
Packit ae235b
  klass->truncate_fn = g_file_io_stream_real_truncate_fn;
Packit ae235b
  klass->query_info = g_file_io_stream_real_query_info;
Packit ae235b
  klass->query_info_async = g_file_io_stream_real_query_info_async;
Packit ae235b
  klass->query_info_finish = g_file_io_stream_real_query_info_finish;
Packit ae235b
  klass->get_etag = g_file_io_stream_real_get_etag;
Packit ae235b
}