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"
34 * SECTION:gfileinputstream
35 * @short_description: File input streaming operations
37 * @see_also: #GInputStream, #GDataInputStream, #GSeekable
39 * GFileInputStream provides input streams that take their
40 * content from a file.
42 * GFileInputStream implements #GSeekable, which allows
43 * the input stream to jump to arbitrary positions in the file.
46 static void g_file_input_stream_seekable_iface_init (GSeekableIface *iface);
47 static goffset g_file_input_stream_seekable_tell (GSeekable *seekable);
48 static gboolean g_file_input_stream_seekable_can_seek (GSeekable *seekable);
49 static gboolean g_file_input_stream_seekable_seek (GSeekable *seekable,
52 GCancellable *cancellable,
54 static gboolean g_file_input_stream_seekable_can_truncate (GSeekable *seekable);
55 static gboolean g_file_input_stream_seekable_truncate (GSeekable *seekable,
57 GCancellable *cancellable,
59 static void g_file_input_stream_real_query_info_async (GFileInputStream *stream,
62 GCancellable *cancellable,
63 GAsyncReadyCallback callback,
65 static GFileInfo *g_file_input_stream_real_query_info_finish (GFileInputStream *stream,
70 G_DEFINE_TYPE_WITH_CODE (GFileInputStream, g_file_input_stream, G_TYPE_INPUT_STREAM,
71 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
72 g_file_input_stream_seekable_iface_init))
74 struct _GFileInputStreamPrivate {
75 GAsyncReadyCallback outstanding_callback;
79 g_file_input_stream_class_init (GFileInputStreamClass *klass)
81 g_type_class_add_private (klass, sizeof (GFileInputStreamPrivate));
83 klass->query_info_async = g_file_input_stream_real_query_info_async;
84 klass->query_info_finish = g_file_input_stream_real_query_info_finish;
88 g_file_input_stream_seekable_iface_init (GSeekableIface *iface)
90 iface->tell = g_file_input_stream_seekable_tell;
91 iface->can_seek = g_file_input_stream_seekable_can_seek;
92 iface->seek = g_file_input_stream_seekable_seek;
93 iface->can_truncate = g_file_input_stream_seekable_can_truncate;
94 iface->truncate_fn = g_file_input_stream_seekable_truncate;
98 g_file_input_stream_init (GFileInputStream *stream)
100 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
101 G_TYPE_FILE_INPUT_STREAM,
102 GFileInputStreamPrivate);
106 * g_file_input_stream_query_info:
107 * @stream: a #GFileInputStream.
108 * @attributes: a file attribute query string.
109 * @cancellable: optional #GCancellable object, %NULL to ignore.
110 * @error: a #GError location to store the error occuring, or %NULL to
113 * Queries a file input stream the given @attributes.his function blocks
114 * while querying the stream. For the asynchronous (non-blocking) version
115 * of this function, see g_file_input_stream_query_info_async(). While the
116 * stream is blocked, the stream will set the pending flag internally, and
117 * any other operations on the stream will fail with %G_IO_ERROR_PENDING.
119 * Returns: a #GFileInfo, or %NULL on error.
122 g_file_input_stream_query_info (GFileInputStream *stream,
124 GCancellable *cancellable,
127 GFileInputStreamClass *class;
128 GInputStream *input_stream;
131 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
133 input_stream = G_INPUT_STREAM (stream);
135 if (!g_input_stream_set_pending (input_stream, error))
141 g_cancellable_push_current (cancellable);
143 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
144 if (class->query_info)
145 info = class->query_info (stream, attributes, cancellable, error);
147 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
148 _("Stream doesn't support query_info"));
151 g_cancellable_pop_current (cancellable);
153 g_input_stream_clear_pending (input_stream);
159 async_ready_callback_wrapper (GObject *source_object,
163 GFileInputStream *stream = G_FILE_INPUT_STREAM (source_object);
165 g_input_stream_clear_pending (G_INPUT_STREAM (stream));
166 if (stream->priv->outstanding_callback)
167 (*stream->priv->outstanding_callback) (source_object, res, user_data);
168 g_object_unref (stream);
172 * g_file_input_stream_query_info_async:
173 * @stream: a #GFileInputStream.
174 * @attributes: a file attribute query string.
175 * @io_priority: the <link linkend="io-priority">I/O priority</link>
177 * @cancellable: optional #GCancellable object, %NULL to ignore.
178 * @callback: callback to call when the request is satisfied
179 * @user_data: the data to pass to callback function
181 * Queries the stream information asynchronously. For the synchronous
182 * version of this function, see g_file_input_stream_query_info().
184 * If @cancellable is not %NULL, then the operation can be cancelled by
185 * triggering the cancellable object from another thread. If the operation
186 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set
190 g_file_input_stream_query_info_async (GFileInputStream *stream,
193 GCancellable *cancellable,
194 GAsyncReadyCallback callback,
197 GFileInputStreamClass *klass;
198 GInputStream *input_stream;
199 GError *error = NULL;
201 g_return_if_fail (G_IS_FILE_INPUT_STREAM (stream));
203 input_stream = G_INPUT_STREAM (stream);
205 if (!g_input_stream_set_pending (input_stream, &error))
207 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
211 g_error_free (error);
215 klass = G_FILE_INPUT_STREAM_GET_CLASS (stream);
217 stream->priv->outstanding_callback = callback;
218 g_object_ref (stream);
219 klass->query_info_async (stream, attributes, io_priority, cancellable,
220 async_ready_callback_wrapper, user_data);
224 * g_file_input_stream_query_info_finish:
225 * @stream: a #GFileInputStream.
226 * @result: a #GAsyncResult.
227 * @error: a #GError location to store the error occuring,
228 * or %NULL to ignore.
230 * Finishes an asynchronous info query operation.
232 * Returns: #GFileInfo.
235 g_file_input_stream_query_info_finish (GFileInputStream *stream,
236 GAsyncResult *result,
239 GSimpleAsyncResult *simple;
240 GFileInputStreamClass *class;
242 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
243 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
245 if (G_IS_SIMPLE_ASYNC_RESULT (result))
247 simple = G_SIMPLE_ASYNC_RESULT (result);
248 if (g_simple_async_result_propagate_error (simple, error))
252 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
253 return class->query_info_finish (stream, result, error);
257 * g_file_input_stream_tell:
258 * @stream: a #GFileInputStream.
260 * Gets the current position in the stream.
262 * Returns: a #goffset with the position in the stream.
265 g_file_input_stream_tell (GFileInputStream *stream)
267 GFileInputStreamClass *class;
270 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), 0);
272 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
276 offset = class->tell (stream);
282 g_file_input_stream_seekable_tell (GSeekable *seekable)
284 return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable));
288 * g_file_input_stream_can_seek:
289 * @stream: a #GFileInputStream.
291 * Checks if a file input stream can be seeked.
293 * Returns: %TRUE if stream can be seeked. %FALSE otherwise.
296 g_file_input_stream_can_seek (GFileInputStream *stream)
298 GFileInputStreamClass *class;
301 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
303 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
310 can_seek = class->can_seek (stream);
317 g_file_input_stream_seekable_can_seek (GSeekable *seekable)
319 return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable));
323 * g_file_input_stream_seek:
324 * @stream: a #GFileInputStream.
325 * @offset: a #goffset to seek.
326 * @type: a #GSeekType.
327 * @cancellable: optional #GCancellable object, %NULL to ignore.
328 * @error: a #GError location to store the error occuring, or
331 * Seeks in the file input stream.
333 * If @cancellable is not %NULL, then the operation can be cancelled by
334 * triggering the cancellable object from another thread. If the operation
335 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set.
337 * Returns: %TRUE if the stream was successfully seeked to the position.
341 g_file_input_stream_seek (GFileInputStream *stream,
344 GCancellable *cancellable,
347 GFileInputStreamClass *class;
348 GInputStream *input_stream;
351 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
353 input_stream = G_INPUT_STREAM (stream);
354 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
358 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
359 _("Seek not supported on stream"));
363 if (!g_input_stream_set_pending (input_stream, error))
367 g_cancellable_push_current (cancellable);
369 res = class->seek (stream, offset, type, cancellable, error);
372 g_cancellable_pop_current (cancellable);
374 g_input_stream_clear_pending (input_stream);
380 g_file_input_stream_seekable_seek (GSeekable *seekable,
383 GCancellable *cancellable,
386 return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
387 offset, type, cancellable, error);
391 g_file_input_stream_seekable_can_truncate (GSeekable *seekable)
397 g_file_input_stream_seekable_truncate (GSeekable *seekable,
399 GCancellable *cancellable,
402 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
403 _("Truncate not allowed on input stream"));
407 /********************************************
408 * Default implementation of async ops *
409 ********************************************/
414 } QueryInfoAsyncData;
417 query_info_data_free (QueryInfoAsyncData *data)
420 g_object_unref (data->info);
421 g_free (data->attributes);
426 query_info_async_thread (GSimpleAsyncResult *res,
428 GCancellable *cancellable)
430 GFileInputStreamClass *class;
431 GError *error = NULL;
432 QueryInfoAsyncData *data;
435 data = g_simple_async_result_get_op_res_gpointer (res);
439 class = G_FILE_INPUT_STREAM_GET_CLASS (object);
440 if (class->query_info)
441 info = class->query_info (G_FILE_INPUT_STREAM (object), data->attributes, cancellable, &error);
443 g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
444 _("Stream doesn't support query_info"));
448 g_simple_async_result_set_from_error (res, error);
449 g_error_free (error);
456 g_file_input_stream_real_query_info_async (GFileInputStream *stream,
459 GCancellable *cancellable,
460 GAsyncReadyCallback callback,
463 GSimpleAsyncResult *res;
464 QueryInfoAsyncData *data;
466 data = g_new0 (QueryInfoAsyncData, 1);
467 data->attributes = g_strdup (attributes);
469 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_input_stream_real_query_info_async);
470 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
472 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
473 g_object_unref (res);
477 g_file_input_stream_real_query_info_finish (GFileInputStream *stream,
481 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
482 QueryInfoAsyncData *data;
484 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_input_stream_real_query_info_async);
486 data = g_simple_async_result_get_op_res_gpointer (simple);
488 return g_object_ref (data->info);
493 #define __G_FILE_INPUT_STREAM_C__
494 #include "gioaliasdef.c"