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
41 static void g_file_input_stream_seekable_iface_init (GSeekableIface *iface);
42 static goffset g_file_input_stream_seekable_tell (GSeekable *seekable);
43 static gboolean g_file_input_stream_seekable_can_seek (GSeekable *seekable);
44 static gboolean g_file_input_stream_seekable_seek (GSeekable *seekable,
47 GCancellable *cancellable,
49 static gboolean g_file_input_stream_seekable_can_truncate (GSeekable *seekable);
50 static gboolean g_file_input_stream_seekable_truncate (GSeekable *seekable,
52 GCancellable *cancellable,
54 static void g_file_input_stream_real_query_info_async (GFileInputStream *stream,
57 GCancellable *cancellable,
58 GAsyncReadyCallback callback,
60 static GFileInfo *g_file_input_stream_real_query_info_finish (GFileInputStream *stream,
65 G_DEFINE_TYPE_WITH_CODE (GFileInputStream, g_file_input_stream, G_TYPE_INPUT_STREAM,
66 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
67 g_file_input_stream_seekable_iface_init))
69 struct _GFileInputStreamPrivate {
70 GAsyncReadyCallback outstanding_callback;
74 g_file_input_stream_class_init (GFileInputStreamClass *klass)
76 g_type_class_add_private (klass, sizeof (GFileInputStreamPrivate));
78 klass->query_info_async = g_file_input_stream_real_query_info_async;
79 klass->query_info_finish = g_file_input_stream_real_query_info_finish;
83 g_file_input_stream_seekable_iface_init (GSeekableIface *iface)
85 iface->tell = g_file_input_stream_seekable_tell;
86 iface->can_seek = g_file_input_stream_seekable_can_seek;
87 iface->seek = g_file_input_stream_seekable_seek;
88 iface->can_truncate = g_file_input_stream_seekable_can_truncate;
89 iface->truncate_fn = g_file_input_stream_seekable_truncate;
93 g_file_input_stream_init (GFileInputStream *stream)
95 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
96 G_TYPE_FILE_INPUT_STREAM,
97 GFileInputStreamPrivate);
101 * g_file_input_stream_query_info:
102 * @stream: a #GFileInputStream.
103 * @attributes: a file attribute query string.
104 * @cancellable: optional #GCancellable object, %NULL to ignore.
105 * @error: a #GError location to store the error occuring, or %NULL to
108 * Queries a file input stream the given @attributes.his function blocks
109 * while querying the stream. For the asynchronous (non-blocking) version
110 * of this function, see g_file_input_stream_query_info_async(). While the
111 * stream is blocked, the stream will set the pending flag internally, and
112 * any other operations on the stream will fail with %G_IO_ERROR_PENDING.
114 * Returns: a #GFileInfo, or %NULL on error.
117 g_file_input_stream_query_info (GFileInputStream *stream,
119 GCancellable *cancellable,
122 GFileInputStreamClass *class;
123 GInputStream *input_stream;
126 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
128 input_stream = G_INPUT_STREAM (stream);
130 if (!g_input_stream_set_pending (input_stream, error))
136 g_cancellable_push_current (cancellable);
138 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
139 if (class->query_info)
140 info = class->query_info (stream, attributes, cancellable, error);
142 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
143 _("Stream doesn't support query_info"));
146 g_cancellable_pop_current (cancellable);
148 g_input_stream_clear_pending (input_stream);
154 async_ready_callback_wrapper (GObject *source_object,
158 GFileInputStream *stream = G_FILE_INPUT_STREAM (source_object);
160 g_input_stream_clear_pending (G_INPUT_STREAM (stream));
161 if (stream->priv->outstanding_callback)
162 (*stream->priv->outstanding_callback) (source_object, res, user_data);
163 g_object_unref (stream);
167 * g_file_input_stream_query_info_async:
168 * @stream: a #GFileInputStream.
169 * @attributes: a file attribute query string.
170 * @io_priority: the <link linkend="io-priority">I/O priority</link>
172 * @cancellable: optional #GCancellable object, %NULL to ignore.
173 * @callback: callback to call when the request is satisfied
174 * @user_data: the data to pass to callback function
176 * Queries the stream information asynchronously. For the synchronous
177 * version of this function, see g_file_input_stream_query_info().
179 * If @cancellable is not %NULL, then the operation can be cancelled by
180 * triggering the cancellable object from another thread. If the operation
181 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set
185 g_file_input_stream_query_info_async (GFileInputStream *stream,
188 GCancellable *cancellable,
189 GAsyncReadyCallback callback,
192 GFileInputStreamClass *klass;
193 GInputStream *input_stream;
194 GError *error = NULL;
196 g_return_if_fail (G_IS_FILE_INPUT_STREAM (stream));
198 input_stream = G_INPUT_STREAM (stream);
200 if (!g_input_stream_set_pending (input_stream, &error))
202 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
206 g_error_free (error);
210 klass = G_FILE_INPUT_STREAM_GET_CLASS (stream);
212 stream->priv->outstanding_callback = callback;
213 g_object_ref (stream);
214 klass->query_info_async (stream, attributes, io_priority, cancellable,
215 async_ready_callback_wrapper, user_data);
219 * g_file_input_stream_query_info_finish:
220 * @stream: a #GFileInputStream.
221 * @result: a #GAsyncResult.
222 * @error: a #GError location to store the error occuring,
223 * or %NULL to ignore.
225 * Finishes an asynchronous info query operation.
227 * Returns: #GFileInfo.
230 g_file_input_stream_query_info_finish (GFileInputStream *stream,
231 GAsyncResult *result,
234 GSimpleAsyncResult *simple;
235 GFileInputStreamClass *class;
237 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
238 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
240 if (G_IS_SIMPLE_ASYNC_RESULT (result))
242 simple = G_SIMPLE_ASYNC_RESULT (result);
243 if (g_simple_async_result_propagate_error (simple, error))
247 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
248 return class->query_info_finish (stream, result, error);
252 * g_file_input_stream_tell:
253 * @stream: a #GFileInputStream.
255 * Gets the current position in the stream.
257 * Returns: a #goffset with the position in the stream.
260 g_file_input_stream_tell (GFileInputStream *stream)
262 GFileInputStreamClass *class;
265 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), 0);
267 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
271 offset = class->tell (stream);
277 g_file_input_stream_seekable_tell (GSeekable *seekable)
279 return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable));
283 * g_file_input_stream_can_seek:
284 * @stream: a #GFileInputStream.
286 * Checks if a file input stream can be seeked.
288 * Returns: %TRUE if stream can be seeked. %FALSE otherwise.
291 g_file_input_stream_can_seek (GFileInputStream *stream)
293 GFileInputStreamClass *class;
296 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
298 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
305 can_seek = class->can_seek (stream);
312 g_file_input_stream_seekable_can_seek (GSeekable *seekable)
314 return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable));
318 * g_file_input_stream_seek:
319 * @stream: a #GFileInputStream.
320 * @offset: a #goffset to seek.
321 * @type: a #GSeekType.
322 * @cancellable: optional #GCancellable object, %NULL to ignore.
323 * @error: a #GError location to store the error occuring, or
326 * Seeks in the file input stream.
328 * If @cancellable is not %NULL, then the operation can be cancelled by
329 * triggering the cancellable object from another thread. If the operation
330 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set.
332 * Returns: %TRUE if the stream was successfully seeked to the position.
336 g_file_input_stream_seek (GFileInputStream *stream,
339 GCancellable *cancellable,
342 GFileInputStreamClass *class;
343 GInputStream *input_stream;
346 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
348 input_stream = G_INPUT_STREAM (stream);
349 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
353 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
354 _("Seek not supported on stream"));
358 if (!g_input_stream_set_pending (input_stream, error))
362 g_cancellable_push_current (cancellable);
364 res = class->seek (stream, offset, type, cancellable, error);
367 g_cancellable_pop_current (cancellable);
369 g_input_stream_clear_pending (input_stream);
375 g_file_input_stream_seekable_seek (GSeekable *seekable,
378 GCancellable *cancellable,
381 return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
382 offset, type, cancellable, error);
386 g_file_input_stream_seekable_can_truncate (GSeekable *seekable)
392 g_file_input_stream_seekable_truncate (GSeekable *seekable,
394 GCancellable *cancellable,
397 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
398 _("Truncate not allowed on input stream"));
402 /********************************************
403 * Default implementation of async ops *
404 ********************************************/
409 } QueryInfoAsyncData;
412 query_info_data_free (QueryInfoAsyncData *data)
415 g_object_unref (data->info);
416 g_free (data->attributes);
421 query_info_async_thread (GSimpleAsyncResult *res,
423 GCancellable *cancellable)
425 GFileInputStreamClass *class;
426 GError *error = NULL;
427 QueryInfoAsyncData *data;
430 data = g_simple_async_result_get_op_res_gpointer (res);
434 class = G_FILE_INPUT_STREAM_GET_CLASS (object);
435 if (class->query_info)
436 info = class->query_info (G_FILE_INPUT_STREAM (object), data->attributes, cancellable, &error);
438 g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
439 _("Stream doesn't support query_info"));
443 g_simple_async_result_set_from_error (res, error);
444 g_error_free (error);
451 g_file_input_stream_real_query_info_async (GFileInputStream *stream,
454 GCancellable *cancellable,
455 GAsyncReadyCallback callback,
458 GSimpleAsyncResult *res;
459 QueryInfoAsyncData *data;
461 data = g_new0 (QueryInfoAsyncData, 1);
462 data->attributes = g_strdup (attributes);
464 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_input_stream_real_query_info_async);
465 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
467 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
468 g_object_unref (res);
472 g_file_input_stream_real_query_info_finish (GFileInputStream *stream,
476 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
477 QueryInfoAsyncData *data;
479 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_input_stream_real_query_info_async);
481 data = g_simple_async_result_get_op_res_gpointer (simple);
483 return g_object_ref (data->info);
488 #define __G_FILE_INPUT_STREAM_C__
489 #include "gioaliasdef.c"