gfileinputstream.c 13.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22 23 24 25

#include <glib.h>
#include <gfileinputstream.h>
#include <gseekable.h>
26 27
#include "gcancellable.h"
#include "gasyncresult.h"
28
#include "gtask.h"
29
#include "gioerror.h"
30 31
#include "glibintl.h"

32

33 34
/**
 * SECTION:gfileinputstream
Matthias Clasen's avatar
Matthias Clasen committed
35
 * @short_description: File input streaming operations
Matthias Clasen's avatar
Matthias Clasen committed
36
 * @include: gio/gio.h
37
 * @see_also: #GInputStream, #GDataInputStream, #GSeekable
38
 *
Matthias Clasen's avatar
Matthias Clasen committed
39 40 41
 * GFileInputStream provides input streams that take their
 * content from a file.
 *
Matthias Clasen's avatar
Matthias Clasen committed
42 43
 * GFileInputStream implements #GSeekable, which allows the input 
 * stream to jump to arbitrary positions in the file, provided the 
44 45
 * filesystem of the file allows it. To find the position of a file
 * input stream, use g_seekable_tell(). To find out if a file input
46
 * stream supports seeking, use g_seekable_can_seek().
47
 * To position a file input stream, use g_seekable_seek().
48 49
 **/

50 51 52 53 54 55 56 57 58 59 60 61 62 63
static void       g_file_input_stream_seekable_iface_init    (GSeekableIface       *iface);
static goffset    g_file_input_stream_seekable_tell          (GSeekable            *seekable);
static gboolean   g_file_input_stream_seekable_can_seek      (GSeekable            *seekable);
static gboolean   g_file_input_stream_seekable_seek          (GSeekable            *seekable,
							      goffset               offset,
							      GSeekType             type,
							      GCancellable         *cancellable,
							      GError              **error);
static gboolean   g_file_input_stream_seekable_can_truncate  (GSeekable            *seekable);
static gboolean   g_file_input_stream_seekable_truncate      (GSeekable            *seekable,
							      goffset               offset,
							      GCancellable         *cancellable,
							      GError              **error);
static void       g_file_input_stream_real_query_info_async  (GFileInputStream     *stream,
64
							      const char           *attributes,
65 66 67 68 69 70 71 72 73 74 75 76 77
							      int                   io_priority,
							      GCancellable         *cancellable,
							      GAsyncReadyCallback   callback,
							      gpointer              user_data);
static GFileInfo *g_file_input_stream_real_query_info_finish (GFileInputStream     *stream,
							      GAsyncResult         *result,
							      GError              **error);


struct _GFileInputStreamPrivate {
  GAsyncReadyCallback outstanding_callback;
};

78 79 80 81 82
G_DEFINE_TYPE_WITH_CODE (GFileInputStream, g_file_input_stream, G_TYPE_INPUT_STREAM,
                         G_ADD_PRIVATE (GFileInputStream)
			 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
						g_file_input_stream_seekable_iface_init))

83 84 85 86 87 88 89 90 91 92 93 94 95 96
static void
g_file_input_stream_class_init (GFileInputStreamClass *klass)
{
  klass->query_info_async = g_file_input_stream_real_query_info_async;
  klass->query_info_finish = g_file_input_stream_real_query_info_finish;
}

static void
g_file_input_stream_seekable_iface_init (GSeekableIface *iface)
{
  iface->tell = g_file_input_stream_seekable_tell;
  iface->can_seek = g_file_input_stream_seekable_can_seek;
  iface->seek = g_file_input_stream_seekable_seek;
  iface->can_truncate = g_file_input_stream_seekable_can_truncate;
97
  iface->truncate_fn = g_file_input_stream_seekable_truncate;
98 99 100 101 102
}

static void
g_file_input_stream_init (GFileInputStream *stream)
{
103
  stream->priv = g_file_input_stream_get_instance_private (stream);
104 105 106 107
}

/**
 * g_file_input_stream_query_info:
108 109
 * @stream: a #GFileInputStream.
 * @attributes: a file attribute query string.
110
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. 
Matthias Clasen's avatar
Matthias Clasen committed
111
 * @error: a #GError location to store the error occurring, or %NULL to 
112
 * ignore.
113
 *
114
 * Queries a file input stream the given @attributes. This function blocks 
Matthias Clasen's avatar
Matthias Clasen committed
115 116 117 118
 * while querying the stream. For the asynchronous (non-blocking) version 
 * of this function, see g_file_input_stream_query_info_async(). While the 
 * stream is blocked, the stream will set the pending flag internally, and 
 * any other operations on the stream will fail with %G_IO_ERROR_PENDING.
119
 *
120
 * Returns: (transfer full): a #GFileInfo, or %NULL on error.
121 122
 **/
GFileInfo *
Matthias Clasen's avatar
Matthias Clasen committed
123
g_file_input_stream_query_info (GFileInputStream  *stream,
124
                                const char        *attributes,
Matthias Clasen's avatar
Matthias Clasen committed
125 126
                                GCancellable      *cancellable,
                                GError           **error)
127 128 129 130 131 132 133 134 135
{
  GFileInputStreamClass *class;
  GInputStream *input_stream;
  GFileInfo *info;
  
  g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
  
  input_stream = G_INPUT_STREAM (stream);
  
136 137
  if (!g_input_stream_set_pending (input_stream, error))
    return NULL;
138 139 140 141
      
  info = NULL;
  
  if (cancellable)
142
    g_cancellable_push_current (cancellable);
143 144 145 146 147
  
  class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
  if (class->query_info)
    info = class->query_info (stream, attributes, cancellable, error);
  else
148
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
149
                         _("Stream doesn’t support query_info"));
150 151

  if (cancellable)
152
    g_cancellable_pop_current (cancellable);
153
  
154
  g_input_stream_clear_pending (input_stream);
155 156 157 158 159
  
  return info;
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
160 161 162
async_ready_callback_wrapper (GObject      *source_object,
                              GAsyncResult *res,
                              gpointer      user_data)
163 164 165
{
  GFileInputStream *stream = G_FILE_INPUT_STREAM (source_object);

166
  g_input_stream_clear_pending (G_INPUT_STREAM (stream));
167 168 169 170 171 172 173
  if (stream->priv->outstanding_callback)
    (*stream->priv->outstanding_callback) (source_object, res, user_data);
  g_object_unref (stream);
}

/**
 * g_file_input_stream_query_info_async:
174 175
 * @stream: a #GFileInputStream.
 * @attributes: a file attribute query string.
176
 * @io_priority: the [I/O priority][io-priority] of the request
177
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. 
178 179
 * @callback: (scope async): callback to call when the request is satisfied
 * @user_data: (closure): the data to pass to callback function
180
 * 
181 182 183 184 185 186 187
 * Queries the stream information asynchronously.
 * When the operation is finished @callback will be called. 
 * You can then call g_file_input_stream_query_info_finish() 
 * to get the result of the operation.
 *
 * For the synchronous version of this function, 
 * see g_file_input_stream_query_info(). 
188 189 190 191
 * 
 * If @cancellable is not %NULL, then the operation can be cancelled by
 * triggering the cancellable object from another thread. If the operation
 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set
192 193 194
 *  
 **/
void
Matthias Clasen's avatar
Matthias Clasen committed
195
g_file_input_stream_query_info_async (GFileInputStream    *stream,
196
                                      const char          *attributes,
Matthias Clasen's avatar
Matthias Clasen committed
197 198 199 200
                                      int                  io_priority,
                                      GCancellable        *cancellable,
                                      GAsyncReadyCallback  callback,
                                      gpointer             user_data)
201 202 203
{
  GFileInputStreamClass *klass;
  GInputStream *input_stream;
204
  GError *error = NULL;
205 206 207 208 209

  g_return_if_fail (G_IS_FILE_INPUT_STREAM (stream));

  input_stream = G_INPUT_STREAM (stream);
  
210
  if (!g_input_stream_set_pending (input_stream, &error))
211
    {
212 213 214
      g_task_report_error (stream, callback, user_data,
                           g_file_input_stream_query_info_async,
                           error);
215 216 217 218 219 220 221 222
      return;
    }

  klass = G_FILE_INPUT_STREAM_GET_CLASS (stream);

  stream->priv->outstanding_callback = callback;
  g_object_ref (stream);
  klass->query_info_async (stream, attributes, io_priority, cancellable,
223
                           async_ready_callback_wrapper, user_data);
224 225 226 227
}

/**
 * g_file_input_stream_query_info_finish:
228 229
 * @stream: a #GFileInputStream.
 * @result: a #GAsyncResult.
Matthias Clasen's avatar
Matthias Clasen committed
230
 * @error: a #GError location to store the error occurring, 
Matthias Clasen's avatar
Matthias Clasen committed
231
 *     or %NULL to ignore.
232 233 234
 * 
 * Finishes an asynchronous info query operation.
 * 
235
 * Returns: (transfer full): #GFileInfo. 
236 237
 **/
GFileInfo *
Matthias Clasen's avatar
Matthias Clasen committed
238 239 240
g_file_input_stream_query_info_finish (GFileInputStream  *stream,
                                       GAsyncResult      *result,
                                       GError           **error)
241 242 243 244 245 246
{
  GFileInputStreamClass *class;

  g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);

247 248
  if (g_async_result_legacy_propagate_error (result, error))
    return NULL;
249 250
  else if (g_async_result_is_tagged (result, g_file_input_stream_query_info_async))
    return g_task_propagate_pointer (G_TASK (result), error);
251 252 253 254 255

  class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
  return class->query_info_finish (stream, result, error);
}

256
static goffset
Matthias Clasen's avatar
Matthias Clasen committed
257
g_file_input_stream_tell (GFileInputStream *stream)
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
{
  GFileInputStreamClass *class;
  goffset offset;

  g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), 0);

  class = G_FILE_INPUT_STREAM_GET_CLASS (stream);

  offset = 0;
  if (class->tell)
    offset = class->tell (stream);

  return offset;
}

static goffset
g_file_input_stream_seekable_tell (GSeekable *seekable)
{
  return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable));
}

279
static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
280
g_file_input_stream_can_seek (GFileInputStream *stream)
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
{
  GFileInputStreamClass *class;
  gboolean can_seek;

  g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);

  class = G_FILE_INPUT_STREAM_GET_CLASS (stream);

  can_seek = FALSE;
  if (class->seek)
    {
      can_seek = TRUE;
      if (class->can_seek)
	can_seek = class->can_seek (stream);
    }
  
  return can_seek;
}

static gboolean
g_file_input_stream_seekable_can_seek (GSeekable *seekable)
{
  return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable));
}

306
static gboolean
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
g_file_input_stream_seek (GFileInputStream  *stream,
			  goffset            offset,
			  GSeekType          type,
			  GCancellable      *cancellable,
			  GError           **error)
{
  GFileInputStreamClass *class;
  GInputStream *input_stream;
  gboolean res;

  g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);

  input_stream = G_INPUT_STREAM (stream);
  class = G_FILE_INPUT_STREAM_GET_CLASS (stream);

  if (!class->seek)
    {
324 325
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
                           _("Seek not supported on stream"));
326 327 328
      return FALSE;
    }

329 330
  if (!g_input_stream_set_pending (input_stream, error))
    return FALSE;
331 332
  
  if (cancellable)
333
    g_cancellable_push_current (cancellable);
334 335 336 337
  
  res = class->seek (stream, offset, type, cancellable, error);
  
  if (cancellable)
338
    g_cancellable_pop_current (cancellable);
339

340
  g_input_stream_clear_pending (input_stream);
341 342 343 344 345
  
  return res;
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
346 347 348
g_file_input_stream_seekable_seek (GSeekable     *seekable,
				   goffset        offset,
				   GSeekType      type,
349
				   GCancellable  *cancellable,
Matthias Clasen's avatar
Matthias Clasen committed
350
				   GError       **error)
351 352 353 354 355 356
{
  return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
				   offset, type, cancellable, error);
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
357
g_file_input_stream_seekable_can_truncate (GSeekable *seekable)
358 359 360 361 362
{
  return FALSE;
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
363 364
g_file_input_stream_seekable_truncate (GSeekable     *seekable,
				       goffset        offset,
365
				       GCancellable  *cancellable,
Matthias Clasen's avatar
Matthias Clasen committed
366
				       GError       **error)
367
{
368 369
  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
                       _("Truncate not allowed on input stream"));
370 371 372 373 374 375 376 377
  return FALSE;
}

/********************************************
 *   Default implementation of async ops    *
 ********************************************/

static void
378 379 380 381
query_info_async_thread (GTask        *task,
                         gpointer      source_object,
                         gpointer      task_data,
                         GCancellable *cancellable)
382
{
383 384
  GFileInputStream *stream = source_object;
  const char *attributes = task_data;
385 386
  GFileInputStreamClass *class;
  GError *error = NULL;
387
  GFileInfo *info = NULL;
388

389
  class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
390
  if (class->query_info)
391
    info = class->query_info (stream, attributes, cancellable, &error);
392
  else
393
    g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
394
                         _("Stream doesn’t support query_info"));
395 396

  if (info == NULL)
397
    g_task_return_error (task, error);
398
  else
399
    g_task_return_pointer (task, info, g_object_unref);
400 401 402
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
403
g_file_input_stream_real_query_info_async (GFileInputStream    *stream,
404
                                           const char          *attributes,
Matthias Clasen's avatar
Matthias Clasen committed
405 406 407 408
                                           int                  io_priority,
                                           GCancellable        *cancellable,
                                           GAsyncReadyCallback  callback,
                                           gpointer             user_data)
409
{
410
  GTask *task;
411

412
  task = g_task_new (stream, cancellable, callback, user_data);
413
  g_task_set_source_tag (task, g_file_input_stream_real_query_info_async);
414 415
  g_task_set_task_data (task, g_strdup (attributes), g_free);
  g_task_set_priority (task, io_priority);
416
  
417 418
  g_task_run_in_thread (task, query_info_async_thread);
  g_object_unref (task);
419 420 421
}

static GFileInfo *
Matthias Clasen's avatar
Matthias Clasen committed
422 423 424
g_file_input_stream_real_query_info_finish (GFileInputStream  *stream,
                                            GAsyncResult      *res,
                                            GError           **error)
425
{
426
  g_return_val_if_fail (g_task_is_valid (res, stream), NULL);
427

428
  return g_task_propagate_pointer (G_TASK (res), error);
429
}