Blame gio/gfileinputstream.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 <gfileinputstream.h>
Packit ae235b
#include <gseekable.h>
Packit ae235b
#include "gcancellable.h"
Packit ae235b
#include "gasyncresult.h"
Packit ae235b
#include "gtask.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gfileinputstream
Packit ae235b
 * @short_description: File input streaming operations
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GInputStream, #GDataInputStream, #GSeekable
Packit ae235b
 *
Packit ae235b
 * GFileInputStream provides input streams that take their
Packit ae235b
 * content from a file.
Packit ae235b
 *
Packit ae235b
 * GFileInputStream implements #GSeekable, which allows the input 
Packit ae235b
 * stream to jump to arbitrary positions in the file, provided the 
Packit ae235b
 * filesystem of the file allows it. To find the position of a file
Packit ae235b
 * input stream, use g_seekable_tell(). To find out if a file input
Packit ae235b
 * stream supports seeking, use g_seekable_can_seek().
Packit ae235b
 * To position a file input stream, use g_seekable_seek().
Packit ae235b
 **/
Packit ae235b
Packit ae235b
static void       g_file_input_stream_seekable_iface_init    (GSeekableIface       *iface);
Packit ae235b
static goffset    g_file_input_stream_seekable_tell          (GSeekable            *seekable);
Packit ae235b
static gboolean   g_file_input_stream_seekable_can_seek      (GSeekable            *seekable);
Packit ae235b
static gboolean   g_file_input_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_input_stream_seekable_can_truncate  (GSeekable            *seekable);
Packit ae235b
static gboolean   g_file_input_stream_seekable_truncate      (GSeekable            *seekable,
Packit ae235b
							      goffset               offset,
Packit ae235b
							      GCancellable         *cancellable,
Packit ae235b
							      GError              **error);
Packit ae235b
static void       g_file_input_stream_real_query_info_async  (GFileInputStream     *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_input_stream_real_query_info_finish (GFileInputStream     *stream,
Packit ae235b
							      GAsyncResult         *result,
Packit ae235b
							      GError              **error);
Packit ae235b
Packit ae235b
Packit ae235b
struct _GFileInputStreamPrivate {
Packit ae235b
  GAsyncReadyCallback outstanding_callback;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GFileInputStream, g_file_input_stream, G_TYPE_INPUT_STREAM,
Packit ae235b
                         G_ADD_PRIVATE (GFileInputStream)
Packit ae235b
			 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
Packit ae235b
						g_file_input_stream_seekable_iface_init))
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_input_stream_class_init (GFileInputStreamClass *klass)
Packit ae235b
{
Packit ae235b
  klass->query_info_async = g_file_input_stream_real_query_info_async;
Packit ae235b
  klass->query_info_finish = g_file_input_stream_real_query_info_finish;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_input_stream_seekable_iface_init (GSeekableIface *iface)
Packit ae235b
{
Packit ae235b
  iface->tell = g_file_input_stream_seekable_tell;
Packit ae235b
  iface->can_seek = g_file_input_stream_seekable_can_seek;
Packit ae235b
  iface->seek = g_file_input_stream_seekable_seek;
Packit ae235b
  iface->can_truncate = g_file_input_stream_seekable_can_truncate;
Packit ae235b
  iface->truncate_fn = g_file_input_stream_seekable_truncate;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_input_stream_init (GFileInputStream *stream)
Packit ae235b
{
Packit ae235b
  stream->priv = g_file_input_stream_get_instance_private (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_input_stream_query_info:
Packit ae235b
 * @stream: a #GFileInputStream.
Packit ae235b
 * @attributes: a file attribute query string.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. 
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to 
Packit ae235b
 * ignore.
Packit ae235b
 *
Packit ae235b
 * Queries a file input stream the given @attributes. This function blocks 
Packit ae235b
 * while querying the stream. For the asynchronous (non-blocking) version 
Packit ae235b
 * of this function, see g_file_input_stream_query_info_async(). While the 
Packit ae235b
 * stream is blocked, the stream will set the pending flag internally, and 
Packit ae235b
 * any other operations on the stream will fail with %G_IO_ERROR_PENDING.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GFileInfo, or %NULL on error.
Packit ae235b
 **/
Packit ae235b
GFileInfo *
Packit ae235b
g_file_input_stream_query_info (GFileInputStream  *stream,
Packit ae235b
                                const char        *attributes,
Packit ae235b
                                GCancellable      *cancellable,
Packit ae235b
                                GError           **error)
Packit ae235b
{
Packit ae235b
  GFileInputStreamClass *class;
Packit ae235b
  GInputStream *input_stream;
Packit ae235b
  GFileInfo *info;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
Packit ae235b
  
Packit ae235b
  input_stream = G_INPUT_STREAM (stream);
Packit ae235b
  
Packit ae235b
  if (!g_input_stream_set_pending (input_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_INPUT_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_input_stream_clear_pending (input_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
  GFileInputStream *stream = G_FILE_INPUT_STREAM (source_object);
Packit ae235b
Packit ae235b
  g_input_stream_clear_pending (G_INPUT_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_input_stream_query_info_async:
Packit ae235b
 * @stream: a #GFileInputStream.
Packit ae235b
 * @attributes: a file attribute query string.
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
 * Queries the stream information asynchronously.
Packit ae235b
 * When the operation is finished @callback will be called. 
Packit ae235b
 * You can then call g_file_input_stream_query_info_finish() 
Packit ae235b
 * to get the result of the operation.
Packit ae235b
 *
Packit ae235b
 * For the synchronous version of this function, 
Packit ae235b
 * see g_file_input_stream_query_info(). 
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
Packit ae235b
 *  
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_file_input_stream_query_info_async (GFileInputStream    *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
  GFileInputStreamClass *klass;
Packit ae235b
  GInputStream *input_stream;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_FILE_INPUT_STREAM (stream));
Packit ae235b
Packit ae235b
  input_stream = G_INPUT_STREAM (stream);
Packit ae235b
  
Packit ae235b
  if (!g_input_stream_set_pending (input_stream, &error))
Packit ae235b
    {
Packit ae235b
      g_task_report_error (stream, callback, user_data,
Packit ae235b
                           g_file_input_stream_query_info_async,
Packit ae235b
                           error);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  klass = G_FILE_INPUT_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_input_stream_query_info_finish:
Packit ae235b
 * @stream: a #GFileInputStream.
Packit ae235b
 * @result: a #GAsyncResult.
Packit ae235b
 * @error: a #GError location to store the error occurring, 
Packit ae235b
 *     or %NULL to ignore.
Packit ae235b
 * 
Packit ae235b
 * Finishes an asynchronous info query operation.
Packit ae235b
 * 
Packit ae235b
 * Returns: (transfer full): #GFileInfo. 
Packit ae235b
 **/
Packit ae235b
GFileInfo *
Packit ae235b
g_file_input_stream_query_info_finish (GFileInputStream  *stream,
Packit ae235b
                                       GAsyncResult      *result,
Packit ae235b
                                       GError           **error)
Packit ae235b
{
Packit ae235b
  GFileInputStreamClass *class;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_INPUT_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_input_stream_query_info_async))
Packit ae235b
    return g_task_propagate_pointer (G_TASK (result), error);
Packit ae235b
Packit ae235b
  class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
Packit ae235b
  return class->query_info_finish (stream, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static goffset
Packit ae235b
g_file_input_stream_tell (GFileInputStream *stream)
Packit ae235b
{
Packit ae235b
  GFileInputStreamClass *class;
Packit ae235b
  goffset offset;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), 0);
Packit ae235b
Packit ae235b
  class = G_FILE_INPUT_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_input_stream_seekable_tell (GSeekable *seekable)
Packit ae235b
{
Packit ae235b
  return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable));
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_input_stream_can_seek (GFileInputStream *stream)
Packit ae235b
{
Packit ae235b
  GFileInputStreamClass *class;
Packit ae235b
  gboolean can_seek;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  class = G_FILE_INPUT_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_input_stream_seekable_can_seek (GSeekable *seekable)
Packit ae235b
{
Packit ae235b
  return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable));
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_input_stream_seek (GFileInputStream  *stream,
Packit ae235b
			  goffset            offset,
Packit ae235b
			  GSeekType          type,
Packit ae235b
			  GCancellable      *cancellable,
Packit ae235b
			  GError           **error)
Packit ae235b
{
Packit ae235b
  GFileInputStreamClass *class;
Packit ae235b
  GInputStream *input_stream;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  input_stream = G_INPUT_STREAM (stream);
Packit ae235b
  class = G_FILE_INPUT_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_input_stream_set_pending (input_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_input_stream_clear_pending (input_stream);
Packit ae235b
  
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_input_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_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
Packit ae235b
				   offset, type, cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_input_stream_seekable_can_truncate (GSeekable *seekable)
Packit ae235b
{
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_file_input_stream_seekable_truncate (GSeekable     *seekable,
Packit ae235b
				       goffset        offset,
Packit ae235b
				       GCancellable  *cancellable,
Packit ae235b
				       GError       **error)
Packit ae235b
{
Packit ae235b
  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                       _("Truncate not allowed on input stream"));
Packit ae235b
  return FALSE;
Packit ae235b
}
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
  GFileInputStream *stream = source_object;
Packit ae235b
  const char *attributes = task_data;
Packit ae235b
  GFileInputStreamClass *class;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GFileInfo *info = NULL;
Packit ae235b
Packit ae235b
  class = G_FILE_INPUT_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_input_stream_real_query_info_async (GFileInputStream    *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_input_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_input_stream_real_query_info_finish (GFileInputStream  *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
}