1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <gfileinputstream.h>
27 #include <gseekable.h>
28 #include "gsimpleasyncresult.h"
32 * SECTION:gfileinputstream
33 * @short_description: file input streaming operations
34 * @see_also: #GInputStream, #GDataInputStream, #GSeekable.
40 static void g_file_input_stream_seekable_iface_init (GSeekableIface *iface);
41 static goffset g_file_input_stream_seekable_tell (GSeekable *seekable);
42 static gboolean g_file_input_stream_seekable_can_seek (GSeekable *seekable);
43 static gboolean g_file_input_stream_seekable_seek (GSeekable *seekable,
46 GCancellable *cancellable,
48 static gboolean g_file_input_stream_seekable_can_truncate (GSeekable *seekable);
49 static gboolean g_file_input_stream_seekable_truncate (GSeekable *seekable,
51 GCancellable *cancellable,
53 static void g_file_input_stream_real_query_info_async (GFileInputStream *stream,
56 GCancellable *cancellable,
57 GAsyncReadyCallback callback,
59 static GFileInfo *g_file_input_stream_real_query_info_finish (GFileInputStream *stream,
64 G_DEFINE_TYPE_WITH_CODE (GFileInputStream, g_file_input_stream, G_TYPE_INPUT_STREAM,
65 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
66 g_file_input_stream_seekable_iface_init))
68 struct _GFileInputStreamPrivate {
69 GAsyncReadyCallback outstanding_callback;
73 g_file_input_stream_class_init (GFileInputStreamClass *klass)
75 g_type_class_add_private (klass, sizeof (GFileInputStreamPrivate));
77 klass->query_info_async = g_file_input_stream_real_query_info_async;
78 klass->query_info_finish = g_file_input_stream_real_query_info_finish;
82 g_file_input_stream_seekable_iface_init (GSeekableIface *iface)
84 iface->tell = g_file_input_stream_seekable_tell;
85 iface->can_seek = g_file_input_stream_seekable_can_seek;
86 iface->seek = g_file_input_stream_seekable_seek;
87 iface->can_truncate = g_file_input_stream_seekable_can_truncate;
88 iface->truncate = g_file_input_stream_seekable_truncate;
92 g_file_input_stream_init (GFileInputStream *stream)
94 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
95 G_TYPE_FILE_INPUT_STREAM,
96 GFileInputStreamPrivate);
100 * g_file_input_stream_query_info:
101 * @stream: a #GFileInputStream.
102 * @attributes: a file attribute query string.
103 * @cancellable: optional #GCancellable object, %NULL to ignore.
104 * @error: a #GError location to store the error occuring, or %NULL to
107 * Queries a file input stream the given @attributes.his function blocks while querying
108 * the stream. For the asynchronous (non-blocking) version of this function, see
109 * g_file_input_stream_query_info_async(). While the stream is blocked,
110 * the stream will set the pending flag internally, and any other operations on the
111 * stream will fail with %G_IO_ERROR_PENDING.
113 * Returns: a #GFileInfo, or %NULL on error.
116 g_file_input_stream_query_info (GFileInputStream *stream,
118 GCancellable *cancellable,
121 GFileInputStreamClass *class;
122 GInputStream *input_stream;
125 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
127 input_stream = G_INPUT_STREAM (stream);
129 if (g_input_stream_is_closed (input_stream))
131 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
132 _("Stream is already closed"));
136 if (g_input_stream_has_pending (input_stream))
138 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
139 _("Stream has outstanding operation"));
145 g_input_stream_set_pending (input_stream, TRUE);
148 g_push_current_cancellable (cancellable);
150 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
151 if (class->query_info)
152 info = class->query_info (stream, attributes, cancellable, error);
154 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
155 _("Stream doesn't support query_info"));
158 g_pop_current_cancellable (cancellable);
160 g_input_stream_set_pending (input_stream, FALSE);
166 async_ready_callback_wrapper (GObject *source_object,
170 GFileInputStream *stream = G_FILE_INPUT_STREAM (source_object);
172 g_input_stream_set_pending (G_INPUT_STREAM (stream), FALSE);
173 if (stream->priv->outstanding_callback)
174 (*stream->priv->outstanding_callback) (source_object, res, user_data);
175 g_object_unref (stream);
179 * g_file_input_stream_query_info_async:
180 * @stream: a #GFileInputStream.
181 * @attributes: a file attribute query string.
182 * @io_priority: the i/o priority of the request.
183 * @cancellable: optional #GCancellable object, %NULL to ignore.
184 * @callback: callback to call when the request is satisfied
185 * @user_data: the data to pass to callback function
187 * Queries the stream information asynchronously. For the synchronous version
188 * of this function, see g_file_input_stream_query_info().
190 * If @cancellable is not %NULL, then the operation can be cancelled by
191 * triggering the cancellable object from another thread. If the operation
192 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set
196 g_file_input_stream_query_info_async (GFileInputStream *stream,
199 GCancellable *cancellable,
200 GAsyncReadyCallback callback,
203 GFileInputStreamClass *klass;
204 GInputStream *input_stream;
206 g_return_if_fail (G_IS_FILE_INPUT_STREAM (stream));
208 input_stream = G_INPUT_STREAM (stream);
210 if (g_input_stream_is_closed (input_stream))
212 g_simple_async_report_error_in_idle (G_OBJECT (stream),
215 G_IO_ERROR, G_IO_ERROR_CLOSED,
216 _("Stream is already closed"));
220 if (g_input_stream_has_pending (input_stream))
222 g_simple_async_report_error_in_idle (G_OBJECT (stream),
225 G_IO_ERROR, G_IO_ERROR_PENDING,
226 _("Stream has outstanding operation"));
230 klass = G_FILE_INPUT_STREAM_GET_CLASS (stream);
232 g_input_stream_set_pending (input_stream, TRUE);
233 stream->priv->outstanding_callback = callback;
234 g_object_ref (stream);
235 klass->query_info_async (stream, attributes, io_priority, cancellable,
236 async_ready_callback_wrapper, user_data);
240 * g_file_input_stream_query_info_finish:
241 * @stream: a #GFileInputStream.
242 * @result: a #GAsyncResult.
243 * @error: a #GError location to store the error occuring, or %NULL to
246 * Finishes an asynchronous info query operation.
248 * Returns: #GFileInfo.
251 g_file_input_stream_query_info_finish (GFileInputStream *stream,
252 GAsyncResult *result,
255 GSimpleAsyncResult *simple;
256 GFileInputStreamClass *class;
258 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
259 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
261 if (G_IS_SIMPLE_ASYNC_RESULT (result))
263 simple = G_SIMPLE_ASYNC_RESULT (result);
264 if (g_simple_async_result_propagate_error (simple, error))
268 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
269 return class->query_info_finish (stream, result, error);
273 * g_file_input_stream_tell:
274 * @stream: a #GFileInputStream.
276 * Gets the current position in the stream.
278 * Returns: a #goffset with the position in the stream.
281 g_file_input_stream_tell (GFileInputStream *stream)
283 GFileInputStreamClass *class;
286 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), 0);
288 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
292 offset = class->tell (stream);
298 g_file_input_stream_seekable_tell (GSeekable *seekable)
300 return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable));
304 * g_file_input_stream_can_seek:
305 * @stream: a #GFileInputStream.
307 * Checks if a file input stream can be seeked.
309 * Returns: %TRUE if stream can be seeked. %FALSE otherwise.
312 g_file_input_stream_can_seek (GFileInputStream *stream)
314 GFileInputStreamClass *class;
317 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
319 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
326 can_seek = class->can_seek (stream);
333 g_file_input_stream_seekable_can_seek (GSeekable *seekable)
335 return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable));
339 * g_file_input_stream_seek:
340 * @stream: a #GFileInputStream.
341 * @offset: a #goffset to seek.
342 * @type: a #GSeekType.
343 * @cancellable: optional #GCancellable object, %NULL to ignore.
344 * @error: a #GError location to store the error occuring, or %NULL to
347 * Seeks in the file input stream.
349 * If @cancellable is not %NULL, then the operation can be cancelled by
350 * triggering the cancellable object from another thread. If the operation
351 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set.
353 * Returns: %TRUE if the stream was successfully seeked to the position.
357 g_file_input_stream_seek (GFileInputStream *stream,
360 GCancellable *cancellable,
363 GFileInputStreamClass *class;
364 GInputStream *input_stream;
367 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
369 input_stream = G_INPUT_STREAM (stream);
370 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
372 if (g_input_stream_is_closed (input_stream))
374 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
375 _("Stream is already closed"));
379 if (g_input_stream_has_pending (input_stream))
381 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
382 _("Stream has outstanding operation"));
388 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
389 _("Seek not supported on stream"));
393 g_input_stream_set_pending (input_stream, TRUE);
396 g_push_current_cancellable (cancellable);
398 res = class->seek (stream, offset, type, cancellable, error);
401 g_pop_current_cancellable (cancellable);
403 g_input_stream_set_pending (input_stream, FALSE);
409 g_file_input_stream_seekable_seek (GSeekable *seekable,
412 GCancellable *cancellable,
415 return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
416 offset, type, cancellable, error);
420 g_file_input_stream_seekable_can_truncate (GSeekable *seekable)
426 g_file_input_stream_seekable_truncate (GSeekable *seekable,
428 GCancellable *cancellable,
431 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
432 _("Truncate not allowed on input stream"));
436 /********************************************
437 * Default implementation of async ops *
438 ********************************************/
443 } QueryInfoAsyncData;
446 query_info_data_free (QueryInfoAsyncData *data)
449 g_object_unref (data->info);
450 g_free (data->attributes);
455 query_info_async_thread (GSimpleAsyncResult *res,
457 GCancellable *cancellable)
459 GFileInputStreamClass *class;
460 GError *error = NULL;
461 QueryInfoAsyncData *data;
464 data = g_simple_async_result_get_op_res_gpointer (res);
468 class = G_FILE_INPUT_STREAM_GET_CLASS (object);
469 if (class->query_info)
470 info = class->query_info (G_FILE_INPUT_STREAM (object), data->attributes, cancellable, &error);
472 g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
473 _("Stream doesn't support query_info"));
477 g_simple_async_result_set_from_error (res, error);
478 g_error_free (error);
485 g_file_input_stream_real_query_info_async (GFileInputStream *stream,
488 GCancellable *cancellable,
489 GAsyncReadyCallback callback,
492 GSimpleAsyncResult *res;
493 QueryInfoAsyncData *data;
495 data = g_new0 (QueryInfoAsyncData, 1);
496 data->attributes = g_strdup (attributes);
498 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_input_stream_real_query_info_async);
499 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
501 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
502 g_object_unref (res);
506 g_file_input_stream_real_query_info_finish (GFileInputStream *stream,
510 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
511 QueryInfoAsyncData *data;
513 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_input_stream_real_query_info_async);
515 data = g_simple_async_result_get_op_res_gpointer (simple);
517 return g_object_ref (data->info);