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 "gcancellable.h"
29 #include "gasyncresult.h"
36 * SECTION:gfileinputstream
37 * @short_description: File input streaming operations
39 * @see_also: #GInputStream, #GDataInputStream, #GSeekable
41 * GFileInputStream provides input streams that take their
42 * content from a file.
44 * GFileInputStream implements #GSeekable, which allows the input
45 * stream to jump to arbitrary positions in the file, provided the
46 * filesystem of the file allows it. To find the position of a file
47 * input stream, use g_seekable_tell(). To find out if a file input
48 * stream supports seeking, use g_seekable_can_seek().
49 * To position a file input stream, use g_seekable_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,
66 const char *attributes,
68 GCancellable *cancellable,
69 GAsyncReadyCallback callback,
71 static GFileInfo *g_file_input_stream_real_query_info_finish (GFileInputStream *stream,
76 struct _GFileInputStreamPrivate {
77 GAsyncReadyCallback outstanding_callback;
80 G_DEFINE_TYPE_WITH_CODE (GFileInputStream, g_file_input_stream, G_TYPE_INPUT_STREAM,
81 G_ADD_PRIVATE (GFileInputStream)
82 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
83 g_file_input_stream_seekable_iface_init))
86 g_file_input_stream_class_init (GFileInputStreamClass *klass)
88 klass->query_info_async = g_file_input_stream_real_query_info_async;
89 klass->query_info_finish = g_file_input_stream_real_query_info_finish;
93 g_file_input_stream_seekable_iface_init (GSeekableIface *iface)
95 iface->tell = g_file_input_stream_seekable_tell;
96 iface->can_seek = g_file_input_stream_seekable_can_seek;
97 iface->seek = g_file_input_stream_seekable_seek;
98 iface->can_truncate = g_file_input_stream_seekable_can_truncate;
99 iface->truncate_fn = g_file_input_stream_seekable_truncate;
103 g_file_input_stream_init (GFileInputStream *stream)
105 stream->priv = g_file_input_stream_get_instance_private (stream);
109 * g_file_input_stream_query_info:
110 * @stream: a #GFileInputStream.
111 * @attributes: a file attribute query string.
112 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
113 * @error: a #GError location to store the error occurring, or %NULL to
116 * Queries a file input stream the given @attributes. This function blocks
117 * while querying the stream. For the asynchronous (non-blocking) version
118 * of this function, see g_file_input_stream_query_info_async(). While the
119 * stream is blocked, the stream will set the pending flag internally, and
120 * any other operations on the stream will fail with %G_IO_ERROR_PENDING.
122 * Returns: (transfer full): a #GFileInfo, or %NULL on error.
125 g_file_input_stream_query_info (GFileInputStream *stream,
126 const char *attributes,
127 GCancellable *cancellable,
130 GFileInputStreamClass *class;
131 GInputStream *input_stream;
134 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
136 input_stream = G_INPUT_STREAM (stream);
138 if (!g_input_stream_set_pending (input_stream, error))
144 g_cancellable_push_current (cancellable);
146 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
147 if (class->query_info)
148 info = class->query_info (stream, attributes, cancellable, error);
150 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
151 _("Stream doesn't support query_info"));
154 g_cancellable_pop_current (cancellable);
156 g_input_stream_clear_pending (input_stream);
162 async_ready_callback_wrapper (GObject *source_object,
166 GFileInputStream *stream = G_FILE_INPUT_STREAM (source_object);
168 g_input_stream_clear_pending (G_INPUT_STREAM (stream));
169 if (stream->priv->outstanding_callback)
170 (*stream->priv->outstanding_callback) (source_object, res, user_data);
171 g_object_unref (stream);
175 * g_file_input_stream_query_info_async:
176 * @stream: a #GFileInputStream.
177 * @attributes: a file attribute query string.
178 * @io_priority: the <link linkend="io-priority">I/O priority</link>
180 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
181 * @callback: (scope async): callback to call when the request is satisfied
182 * @user_data: (closure): the data to pass to callback function
184 * Queries the stream information asynchronously.
185 * When the operation is finished @callback will be called.
186 * You can then call g_file_input_stream_query_info_finish()
187 * to get the result of the operation.
189 * For the synchronous version of this function,
190 * see g_file_input_stream_query_info().
192 * If @cancellable is not %NULL, then the operation can be cancelled by
193 * triggering the cancellable object from another thread. If the operation
194 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set
198 g_file_input_stream_query_info_async (GFileInputStream *stream,
199 const char *attributes,
201 GCancellable *cancellable,
202 GAsyncReadyCallback callback,
205 GFileInputStreamClass *klass;
206 GInputStream *input_stream;
207 GError *error = NULL;
209 g_return_if_fail (G_IS_FILE_INPUT_STREAM (stream));
211 input_stream = G_INPUT_STREAM (stream);
213 if (!g_input_stream_set_pending (input_stream, &error))
215 g_task_report_error (stream, callback, user_data,
216 g_file_input_stream_query_info_async,
221 klass = G_FILE_INPUT_STREAM_GET_CLASS (stream);
223 stream->priv->outstanding_callback = callback;
224 g_object_ref (stream);
225 klass->query_info_async (stream, attributes, io_priority, cancellable,
226 async_ready_callback_wrapper, user_data);
230 * g_file_input_stream_query_info_finish:
231 * @stream: a #GFileInputStream.
232 * @result: a #GAsyncResult.
233 * @error: a #GError location to store the error occurring,
234 * or %NULL to ignore.
236 * Finishes an asynchronous info query operation.
238 * Returns: (transfer full): #GFileInfo.
241 g_file_input_stream_query_info_finish (GFileInputStream *stream,
242 GAsyncResult *result,
245 GFileInputStreamClass *class;
247 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
248 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
250 if (g_async_result_legacy_propagate_error (result, error))
252 else if (g_async_result_is_tagged (result, g_file_input_stream_query_info_async))
253 return g_task_propagate_pointer (G_TASK (result), error);
255 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
256 return class->query_info_finish (stream, result, error);
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 (GFileInputStream *stream)
285 GFileInputStreamClass *class;
288 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
290 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
297 can_seek = class->can_seek (stream);
304 g_file_input_stream_seekable_can_seek (GSeekable *seekable)
306 return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable));
310 g_file_input_stream_seek (GFileInputStream *stream,
313 GCancellable *cancellable,
316 GFileInputStreamClass *class;
317 GInputStream *input_stream;
320 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
322 input_stream = G_INPUT_STREAM (stream);
323 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
327 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
328 _("Seek not supported on stream"));
332 if (!g_input_stream_set_pending (input_stream, error))
336 g_cancellable_push_current (cancellable);
338 res = class->seek (stream, offset, type, cancellable, error);
341 g_cancellable_pop_current (cancellable);
343 g_input_stream_clear_pending (input_stream);
349 g_file_input_stream_seekable_seek (GSeekable *seekable,
352 GCancellable *cancellable,
355 return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
356 offset, type, cancellable, error);
360 g_file_input_stream_seekable_can_truncate (GSeekable *seekable)
366 g_file_input_stream_seekable_truncate (GSeekable *seekable,
368 GCancellable *cancellable,
371 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
372 _("Truncate not allowed on input stream"));
376 /********************************************
377 * Default implementation of async ops *
378 ********************************************/
381 query_info_async_thread (GTask *task,
382 gpointer source_object,
384 GCancellable *cancellable)
386 GFileInputStream *stream = source_object;
387 const char *attributes = task_data;
388 GFileInputStreamClass *class;
389 GError *error = NULL;
390 GFileInfo *info = NULL;
392 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
393 if (class->query_info)
394 info = class->query_info (stream, attributes, cancellable, &error);
396 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
397 _("Stream doesn't support query_info"));
400 g_task_return_error (task, error);
402 g_task_return_pointer (task, info, g_object_unref);
406 g_file_input_stream_real_query_info_async (GFileInputStream *stream,
407 const char *attributes,
409 GCancellable *cancellable,
410 GAsyncReadyCallback callback,
415 task = g_task_new (stream, cancellable, callback, user_data);
416 g_task_set_task_data (task, g_strdup (attributes), g_free);
417 g_task_set_priority (task, io_priority);
419 g_task_run_in_thread (task, query_info_async_thread);
420 g_object_unref (task);
424 g_file_input_stream_real_query_info_finish (GFileInputStream *stream,
428 g_return_val_if_fail (g_task_is_valid (res, stream), NULL);
430 return g_task_propagate_pointer (G_TASK (res), error);