Blame gio/gfileoutputstream.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
Packit ae235b
#include <glib.h>
Packit ae235b
#include <gfileoutputstream.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 "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gfileoutputstream
Packit ae235b
 * @short_description: File output streaming operations
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GOutputStream, #GDataOutputStream, #GSeekable
Packit ae235b
 * 
Packit ae235b
 * GFileOutputStream provides output streams that write their
Packit ae235b
 * content to a file.
Packit ae235b
 *
Packit ae235b
 * GFileOutputStream implements #GSeekable, which allows the output 
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 output stream, use g_seekable_tell().
Packit ae235b
 * To find out if a file output stream supports seeking, use
Packit ae235b
 * g_seekable_can_seek().To position a file output stream, use
Packit ae235b
 * g_seekable_seek(). To find out if a file output stream supports
Packit ae235b
 * truncating, use g_seekable_can_truncate(). To truncate a file output
Packit ae235b
 * stream, use g_seekable_truncate().
Packit ae235b
 **/
Packit ae235b
Packit ae235b
static void       g_file_output_stream_seekable_iface_init    (GSeekableIface       *iface);
Packit ae235b
static goffset    g_file_output_stream_seekable_tell          (GSeekable            *seekable);
Packit ae235b
static gboolean   g_file_output_stream_seekable_can_seek      (GSeekable            *seekable);
Packit ae235b
static gboolean   g_file_output_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_output_stream_seekable_can_truncate  (GSeekable            *seekable);
Packit ae235b
static gboolean   g_file_output_stream_seekable_truncate      (GSeekable            *seekable,
Packit ae235b
							       goffset               offset,
Packit ae235b
							       GCancellable         *cancellable,
Packit ae235b
							       GError              **error);
Packit ae235b
static void       g_file_output_stream_real_query_info_async  (GFileOutputStream    *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_output_stream_real_query_info_finish (GFileOutputStream    *stream,
Packit ae235b
							       GAsyncResult         *result,
Packit ae235b
							       GError              **error);
Packit ae235b
Packit ae235b
struct _GFileOutputStreamPrivate {
Packit ae235b
  GAsyncReadyCallback outstanding_callback;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GFileOutputStream, g_file_output_stream, G_TYPE_OUTPUT_STREAM,
Packit ae235b
                         G_ADD_PRIVATE (GFileOutputStream)
Packit ae235b
			 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
Packit ae235b
						g_file_output_stream_seekable_iface_init));
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_output_stream_class_init (GFileOutputStreamClass *klass)
Packit ae235b
{
Packit ae235b
  klass->query_info_async = g_file_output_stream_real_query_info_async;
Packit ae235b
  klass->query_info_finish = g_file_output_stream_real_query_info_finish;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_output_stream_seekable_iface_init (GSeekableIface *iface)
Packit ae235b
{
Packit ae235b
  iface->tell = g_file_output_stream_seekable_tell;
Packit ae235b
  iface->can_seek = g_file_output_stream_seekable_can_seek;
Packit ae235b
  iface->seek = g_file_output_stream_seekable_seek;
Packit ae235b
  iface->can_truncate = g_file_output_stream_seekable_can_truncate;
Packit ae235b
  iface->truncate_fn = g_file_output_stream_seekable_truncate;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_output_stream_init (GFileOutputStream *stream)
Packit ae235b
{
Packit ae235b
  stream->priv = g_file_output_stream_get_instance_private (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_output_stream_query_info:
Packit ae235b
 * @stream: a #GFileOutputStream.
Packit ae235b
 * @attributes: a file attribute query string.
Packit ae235b
 * @cancellable: optional #GCancellable object, %NULL to ignore. 
Packit ae235b
 * @error: a #GError, %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Queries a file output 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_output_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). In
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
GFileInfo *
Packit ae235b
g_file_output_stream_query_info (GFileOutputStream      *stream,
Packit ae235b
				    const char             *attributes,
Packit ae235b
				    GCancellable           *cancellable,
Packit ae235b
				    GError                **error)
Packit ae235b
{
Packit ae235b
  GFileOutputStreamClass *class;
Packit ae235b
  GOutputStream *output_stream;
Packit ae235b
  GFileInfo *info;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
Packit ae235b
  
Packit ae235b
  output_stream = G_OUTPUT_STREAM (stream);
Packit ae235b
  
Packit ae235b
  if (!g_output_stream_set_pending (output_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_OUTPUT_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_output_stream_clear_pending (output_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
  GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
Packit ae235b
Packit ae235b
  g_output_stream_clear_pending (G_OUTPUT_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_output_stream_query_info_async:
Packit ae235b
 * @stream: a #GFileOutputStream.
Packit ae235b
 * @attributes: a file attribute query string.
Packit ae235b
 * @io_priority: the [I/O priority][gio-GIOScheduler] of the request
Packit ae235b
 * @cancellable: optional #GCancellable object, %NULL to ignore. 
Packit ae235b
 * @callback: callback to call when the request is satisfied
Packit ae235b
 * @user_data: 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_output_stream_query_info_finish().
Packit ae235b
 * 
Packit ae235b
 * For the synchronous version of this function, see 
Packit ae235b
 * g_file_output_stream_query_info().
Packit ae235b
 *
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_file_output_stream_query_info_async (GFileOutputStream     *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
  GFileOutputStreamClass *klass;
Packit ae235b
  GOutputStream *output_stream;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
Packit ae235b
Packit ae235b
  output_stream = G_OUTPUT_STREAM (stream);
Packit ae235b
 
Packit ae235b
  if (!g_output_stream_set_pending (output_stream, &error))
Packit ae235b
    {
Packit ae235b
      g_task_report_error (stream, callback, user_data,
Packit ae235b
                           g_file_output_stream_query_info_async,
Packit ae235b
                           error);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  klass = G_FILE_OUTPUT_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_output_stream_query_info_finish:
Packit ae235b
 * @stream: a #GFileOutputStream.
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_output_stream_query_info_async().
Packit ae235b
 * 
Packit ae235b
 * Returns: (transfer full): A #GFileInfo for the finished query.
Packit ae235b
 **/
Packit ae235b
GFileInfo *
Packit ae235b
g_file_output_stream_query_info_finish (GFileOutputStream     *stream,
Packit ae235b
					   GAsyncResult         *result,
Packit ae235b
					   GError              **error)
Packit ae235b
{
Packit ae235b
  GFileOutputStreamClass *class;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_OUTPUT_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_output_stream_query_info_async))
Packit ae235b
    return g_task_propagate_pointer (G_TASK (result), error);
Packit ae235b
Packit ae235b
  class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  return class->query_info_finish (stream, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_output_stream_get_etag:
Packit ae235b
 * @stream: a #GFileOutputStream.
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
char *
Packit ae235b
g_file_output_stream_get_etag (GFileOutputStream  *stream)
Packit ae235b
{
Packit ae235b
  GFileOutputStreamClass *class;
Packit ae235b
  GOutputStream *output_stream;
Packit ae235b
  char *etag;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
Packit ae235b
  
Packit ae235b
  output_stream = G_OUTPUT_STREAM (stream);
Packit ae235b
  
Packit ae235b
  if (!g_output_stream_is_closed (output_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_OUTPUT_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_output_stream_tell (GFileOutputStream  *stream)
Packit ae235b
{
Packit ae235b
  GFileOutputStreamClass *class;
Packit ae235b
  goffset offset;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0);  
Packit ae235b
Packit ae235b
  class = G_FILE_OUTPUT_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_output_stream_seekable_tell (GSeekable *seekable)
Packit ae235b
{
Packit ae235b
  return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable));
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_output_stream_can_seek (GFileOutputStream  *stream)
Packit ae235b
{
Packit ae235b
  GFileOutputStreamClass *class;
Packit ae235b
  gboolean can_seek;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  class = G_FILE_OUTPUT_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_output_stream_seekable_can_seek (GSeekable *seekable)
Packit ae235b
{
Packit ae235b
  return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable));
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_output_stream_seek (GFileOutputStream  *stream,
Packit ae235b
			   goffset             offset,
Packit ae235b
			   GSeekType           type,
Packit ae235b
			   GCancellable       *cancellable,
Packit ae235b
			   GError            **error)
Packit ae235b
{
Packit ae235b
  GFileOutputStreamClass *class;
Packit ae235b
  GOutputStream *output_stream;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  output_stream = G_OUTPUT_STREAM (stream);
Packit ae235b
  class = G_FILE_OUTPUT_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_output_stream_set_pending (output_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_output_stream_clear_pending (output_stream);
Packit ae235b
  
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_output_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_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable),
Packit ae235b
				    offset, type, cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_output_stream_can_truncate (GFileOutputStream  *stream)
Packit ae235b
{
Packit ae235b
  GFileOutputStreamClass *class;
Packit ae235b
  gboolean can_truncate;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  class = G_FILE_OUTPUT_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_output_stream_seekable_can_truncate (GSeekable  *seekable)
Packit ae235b
{
Packit ae235b
  return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable));
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_output_stream_truncate (GFileOutputStream  *stream,
Packit ae235b
			       goffset             size,
Packit ae235b
			       GCancellable       *cancellable,
Packit ae235b
			       GError            **error)
Packit ae235b
{
Packit ae235b
  GFileOutputStreamClass *class;
Packit ae235b
  GOutputStream *output_stream;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  output_stream = G_OUTPUT_STREAM (stream);
Packit ae235b
  class = G_FILE_OUTPUT_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_output_stream_set_pending (output_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_output_stream_clear_pending (output_stream);
Packit ae235b
  
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_output_stream_seekable_truncate (GSeekable     *seekable,
Packit ae235b
					goffset        size,
Packit ae235b
					GCancellable  *cancellable,
Packit ae235b
					GError       **error)
Packit ae235b
{
Packit ae235b
  return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable),
Packit ae235b
					size, cancellable, error);
Packit ae235b
}
Packit ae235b
/********************************************
Packit ae235b
 *   Default implementation of async ops    *
Packit ae235b
 ********************************************/
Packit ae235b
Packit ae235b
static void
Packit ae235b
query_info_async_thread (GTask        *task,
Packit ae235b
                         gpointer      source_object,
Packit ae235b
                         gpointer      task_data,
Packit ae235b
                         GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GFileOutputStream *stream = source_object;
Packit ae235b
  const char *attributes = task_data;
Packit ae235b
  GFileOutputStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GFileInfo *info = NULL;
Packit ae235b
Packit ae235b
  class = G_FILE_OUTPUT_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 (info == NULL)
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
  else
Packit ae235b
    g_task_return_pointer (task, info, g_object_unref);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_output_stream_real_query_info_async (GFileOutputStream     *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
  GTask *task;
Packit ae235b
Packit ae235b
  task = g_task_new (stream, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_file_output_stream_real_query_info_async);
Packit ae235b
  g_task_set_task_data (task, g_strdup (attributes), g_free);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
  
Packit ae235b
  g_task_run_in_thread (task, query_info_async_thread);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileInfo *
Packit ae235b
g_file_output_stream_real_query_info_finish (GFileOutputStream     *stream,
Packit ae235b
					     GAsyncResult         *res,
Packit ae235b
					     GError              **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (res, stream), NULL);
Packit ae235b
Packit ae235b
  return g_task_propagate_pointer (G_TASK (res), error);
Packit ae235b
}