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 the input
43 * stream to jump to arbitrary positions in the file, provided the
44 * filesystem of the file allows it. In addition to the generic
45 * g_seekable_ API, GFileInputStream has its own API for seeking
46 * and positioning. To find the position of a file input stream,
47 * use g_file_input_stream_tell(). To find out if a file input
48 * stream supports seeking, use g_file_input_stream_can_seek().
49 * To position a file input stream, use g_file_input_stream_seek().
52 static void g_file_input_stream_seekable_iface_init (GSeekableIface *iface);
53 static goffset g_file_input_stream_seekable_tell (GSeekable *seekable);
54 static gboolean g_file_input_stream_seekable_can_seek (GSeekable *seekable);
55 static gboolean g_file_input_stream_seekable_seek (GSeekable *seekable,
58 GCancellable *cancellable,
60 static gboolean g_file_input_stream_seekable_can_truncate (GSeekable *seekable);
61 static gboolean g_file_input_stream_seekable_truncate (GSeekable *seekable,
63 GCancellable *cancellable,
65 static void g_file_input_stream_real_query_info_async (GFileInputStream *stream,
68 GCancellable *cancellable,
69 GAsyncReadyCallback callback,
71 static GFileInfo *g_file_input_stream_real_query_info_finish (GFileInputStream *stream,
76 G_DEFINE_TYPE_WITH_CODE (GFileInputStream, g_file_input_stream, G_TYPE_INPUT_STREAM,
77 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
78 g_file_input_stream_seekable_iface_init))
80 struct _GFileInputStreamPrivate {
81 GAsyncReadyCallback outstanding_callback;
85 g_file_input_stream_class_init (GFileInputStreamClass *klass)
87 g_type_class_add_private (klass, sizeof (GFileInputStreamPrivate));
89 klass->query_info_async = g_file_input_stream_real_query_info_async;
90 klass->query_info_finish = g_file_input_stream_real_query_info_finish;
94 g_file_input_stream_seekable_iface_init (GSeekableIface *iface)
96 iface->tell = g_file_input_stream_seekable_tell;
97 iface->can_seek = g_file_input_stream_seekable_can_seek;
98 iface->seek = g_file_input_stream_seekable_seek;
99 iface->can_truncate = g_file_input_stream_seekable_can_truncate;
100 iface->truncate_fn = g_file_input_stream_seekable_truncate;
104 g_file_input_stream_init (GFileInputStream *stream)
106 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
107 G_TYPE_FILE_INPUT_STREAM,
108 GFileInputStreamPrivate);
112 * g_file_input_stream_query_info:
113 * @stream: a #GFileInputStream.
114 * @attributes: a file attribute query string.
115 * @cancellable: optional #GCancellable object, %NULL to ignore.
116 * @error: a #GError location to store the error occuring, or %NULL to
119 * Queries a file input stream the given @attributes. This function blocks
120 * while querying the stream. For the asynchronous (non-blocking) version
121 * of this function, see g_file_input_stream_query_info_async(). While the
122 * stream is blocked, the stream will set the pending flag internally, and
123 * any other operations on the stream will fail with %G_IO_ERROR_PENDING.
125 * Returns: a #GFileInfo, or %NULL on error.
128 g_file_input_stream_query_info (GFileInputStream *stream,
130 GCancellable *cancellable,
133 GFileInputStreamClass *class;
134 GInputStream *input_stream;
137 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
139 input_stream = G_INPUT_STREAM (stream);
141 if (!g_input_stream_set_pending (input_stream, error))
147 g_cancellable_push_current (cancellable);
149 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
150 if (class->query_info)
151 info = class->query_info (stream, attributes, cancellable, error);
153 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
154 _("Stream doesn't support query_info"));
157 g_cancellable_pop_current (cancellable);
159 g_input_stream_clear_pending (input_stream);
165 async_ready_callback_wrapper (GObject *source_object,
169 GFileInputStream *stream = G_FILE_INPUT_STREAM (source_object);
171 g_input_stream_clear_pending (G_INPUT_STREAM (stream));
172 if (stream->priv->outstanding_callback)
173 (*stream->priv->outstanding_callback) (source_object, res, user_data);
174 g_object_unref (stream);
178 * g_file_input_stream_query_info_async:
179 * @stream: a #GFileInputStream.
180 * @attributes: a file attribute query string.
181 * @io_priority: the <link linkend="io-priority">I/O priority</link>
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.
188 * When the operation is finished @callback will be called.
189 * You can then call g_file_input_stream_query_info_finish()
190 * to get the result of the operation.
192 * For the synchronous version of this function,
193 * see g_file_input_stream_query_info().
195 * If @cancellable is not %NULL, then the operation can be cancelled by
196 * triggering the cancellable object from another thread. If the operation
197 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set
201 g_file_input_stream_query_info_async (GFileInputStream *stream,
204 GCancellable *cancellable,
205 GAsyncReadyCallback callback,
208 GFileInputStreamClass *klass;
209 GInputStream *input_stream;
210 GError *error = NULL;
212 g_return_if_fail (G_IS_FILE_INPUT_STREAM (stream));
214 input_stream = G_INPUT_STREAM (stream);
216 if (!g_input_stream_set_pending (input_stream, &error))
218 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
222 g_error_free (error);
226 klass = G_FILE_INPUT_STREAM_GET_CLASS (stream);
228 stream->priv->outstanding_callback = callback;
229 g_object_ref (stream);
230 klass->query_info_async (stream, attributes, io_priority, cancellable,
231 async_ready_callback_wrapper, user_data);
235 * g_file_input_stream_query_info_finish:
236 * @stream: a #GFileInputStream.
237 * @result: a #GAsyncResult.
238 * @error: a #GError location to store the error occuring,
239 * or %NULL to ignore.
241 * Finishes an asynchronous info query operation.
243 * Returns: #GFileInfo.
246 g_file_input_stream_query_info_finish (GFileInputStream *stream,
247 GAsyncResult *result,
250 GSimpleAsyncResult *simple;
251 GFileInputStreamClass *class;
253 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
254 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
256 if (G_IS_SIMPLE_ASYNC_RESULT (result))
258 simple = G_SIMPLE_ASYNC_RESULT (result);
259 if (g_simple_async_result_propagate_error (simple, error))
263 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
264 return class->query_info_finish (stream, result, error);
268 g_file_input_stream_tell (GFileInputStream *stream)
270 GFileInputStreamClass *class;
273 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), 0);
275 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
279 offset = class->tell (stream);
285 g_file_input_stream_seekable_tell (GSeekable *seekable)
287 return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable));
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 (GFileInputStream *stream,
321 GCancellable *cancellable,
324 GFileInputStreamClass *class;
325 GInputStream *input_stream;
328 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
330 input_stream = G_INPUT_STREAM (stream);
331 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
335 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
336 _("Seek not supported on stream"));
340 if (!g_input_stream_set_pending (input_stream, error))
344 g_cancellable_push_current (cancellable);
346 res = class->seek (stream, offset, type, cancellable, error);
349 g_cancellable_pop_current (cancellable);
351 g_input_stream_clear_pending (input_stream);
357 g_file_input_stream_seekable_seek (GSeekable *seekable,
360 GCancellable *cancellable,
363 return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
364 offset, type, cancellable, error);
368 g_file_input_stream_seekable_can_truncate (GSeekable *seekable)
374 g_file_input_stream_seekable_truncate (GSeekable *seekable,
376 GCancellable *cancellable,
379 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
380 _("Truncate not allowed on input stream"));
384 /********************************************
385 * Default implementation of async ops *
386 ********************************************/
391 } QueryInfoAsyncData;
394 query_info_data_free (QueryInfoAsyncData *data)
397 g_object_unref (data->info);
398 g_free (data->attributes);
403 query_info_async_thread (GSimpleAsyncResult *res,
405 GCancellable *cancellable)
407 GFileInputStreamClass *class;
408 GError *error = NULL;
409 QueryInfoAsyncData *data;
412 data = g_simple_async_result_get_op_res_gpointer (res);
416 class = G_FILE_INPUT_STREAM_GET_CLASS (object);
417 if (class->query_info)
418 info = class->query_info (G_FILE_INPUT_STREAM (object), data->attributes, cancellable, &error);
420 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
421 _("Stream doesn't support query_info"));
425 g_simple_async_result_set_from_error (res, error);
426 g_error_free (error);
433 g_file_input_stream_real_query_info_async (GFileInputStream *stream,
436 GCancellable *cancellable,
437 GAsyncReadyCallback callback,
440 GSimpleAsyncResult *res;
441 QueryInfoAsyncData *data;
443 data = g_new0 (QueryInfoAsyncData, 1);
444 data->attributes = g_strdup (attributes);
446 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_input_stream_real_query_info_async);
447 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
449 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
450 g_object_unref (res);
454 g_file_input_stream_real_query_info_finish (GFileInputStream *stream,
458 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
459 QueryInfoAsyncData *data;
461 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_input_stream_real_query_info_async);
463 data = g_simple_async_result_get_op_res_gpointer (simple);
465 return g_object_ref (data->info);
470 #define __G_FILE_INPUT_STREAM_C__
471 #include "gioaliasdef.c"