GSettings: small internal refactor
[glib.git] / gio / gfileinputstream.c
blob05eaa04ded63eb87d0cd4f3e5138856248badf1e
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
23 #include "config.h"
25 #include <glib.h>
26 #include <gfileinputstream.h>
27 #include <gseekable.h>
28 #include "gcancellable.h"
29 #include "gasyncresult.h"
30 #include "gtask.h"
31 #include "gioerror.h"
32 #include "glibintl.h"
35 /**
36 * SECTION:gfileinputstream
37 * @short_description: File input streaming operations
38 * @include: gio/gio.h
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().
50 **/
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,
56 goffset offset,
57 GSeekType type,
58 GCancellable *cancellable,
59 GError **error);
60 static gboolean g_file_input_stream_seekable_can_truncate (GSeekable *seekable);
61 static gboolean g_file_input_stream_seekable_truncate (GSeekable *seekable,
62 goffset offset,
63 GCancellable *cancellable,
64 GError **error);
65 static void g_file_input_stream_real_query_info_async (GFileInputStream *stream,
66 const char *attributes,
67 int io_priority,
68 GCancellable *cancellable,
69 GAsyncReadyCallback callback,
70 gpointer user_data);
71 static GFileInfo *g_file_input_stream_real_query_info_finish (GFileInputStream *stream,
72 GAsyncResult *result,
73 GError **error);
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))
85 static void
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;
92 static void
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;
102 static void
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
114 * ignore.
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.
124 GFileInfo *
125 g_file_input_stream_query_info (GFileInputStream *stream,
126 const char *attributes,
127 GCancellable *cancellable,
128 GError **error)
130 GFileInputStreamClass *class;
131 GInputStream *input_stream;
132 GFileInfo *info;
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))
139 return NULL;
141 info = NULL;
143 if (cancellable)
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);
149 else
150 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
151 _("Stream doesn't support query_info"));
153 if (cancellable)
154 g_cancellable_pop_current (cancellable);
156 g_input_stream_clear_pending (input_stream);
158 return info;
161 static void
162 async_ready_callback_wrapper (GObject *source_object,
163 GAsyncResult *res,
164 gpointer user_data)
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>
179 * of the request.
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
197 void
198 g_file_input_stream_query_info_async (GFileInputStream *stream,
199 const char *attributes,
200 int io_priority,
201 GCancellable *cancellable,
202 GAsyncReadyCallback callback,
203 gpointer user_data)
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,
217 error);
218 return;
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.
240 GFileInfo *
241 g_file_input_stream_query_info_finish (GFileInputStream *stream,
242 GAsyncResult *result,
243 GError **error)
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))
251 return NULL;
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);
259 static goffset
260 g_file_input_stream_tell (GFileInputStream *stream)
262 GFileInputStreamClass *class;
263 goffset offset;
265 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), 0);
267 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
269 offset = 0;
270 if (class->tell)
271 offset = class->tell (stream);
273 return offset;
276 static goffset
277 g_file_input_stream_seekable_tell (GSeekable *seekable)
279 return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable));
282 static gboolean
283 g_file_input_stream_can_seek (GFileInputStream *stream)
285 GFileInputStreamClass *class;
286 gboolean can_seek;
288 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
290 class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
292 can_seek = FALSE;
293 if (class->seek)
295 can_seek = TRUE;
296 if (class->can_seek)
297 can_seek = class->can_seek (stream);
300 return can_seek;
303 static gboolean
304 g_file_input_stream_seekable_can_seek (GSeekable *seekable)
306 return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable));
309 static gboolean
310 g_file_input_stream_seek (GFileInputStream *stream,
311 goffset offset,
312 GSeekType type,
313 GCancellable *cancellable,
314 GError **error)
316 GFileInputStreamClass *class;
317 GInputStream *input_stream;
318 gboolean res;
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);
325 if (!class->seek)
327 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
328 _("Seek not supported on stream"));
329 return FALSE;
332 if (!g_input_stream_set_pending (input_stream, error))
333 return FALSE;
335 if (cancellable)
336 g_cancellable_push_current (cancellable);
338 res = class->seek (stream, offset, type, cancellable, error);
340 if (cancellable)
341 g_cancellable_pop_current (cancellable);
343 g_input_stream_clear_pending (input_stream);
345 return res;
348 static gboolean
349 g_file_input_stream_seekable_seek (GSeekable *seekable,
350 goffset offset,
351 GSeekType type,
352 GCancellable *cancellable,
353 GError **error)
355 return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
356 offset, type, cancellable, error);
359 static gboolean
360 g_file_input_stream_seekable_can_truncate (GSeekable *seekable)
362 return FALSE;
365 static gboolean
366 g_file_input_stream_seekable_truncate (GSeekable *seekable,
367 goffset offset,
368 GCancellable *cancellable,
369 GError **error)
371 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
372 _("Truncate not allowed on input stream"));
373 return FALSE;
376 /********************************************
377 * Default implementation of async ops *
378 ********************************************/
380 static void
381 query_info_async_thread (GTask *task,
382 gpointer source_object,
383 gpointer task_data,
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);
395 else
396 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
397 _("Stream doesn't support query_info"));
399 if (info == NULL)
400 g_task_return_error (task, error);
401 else
402 g_task_return_pointer (task, info, g_object_unref);
405 static void
406 g_file_input_stream_real_query_info_async (GFileInputStream *stream,
407 const char *attributes,
408 int io_priority,
409 GCancellable *cancellable,
410 GAsyncReadyCallback callback,
411 gpointer user_data)
413 GTask *task;
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);
423 static GFileInfo *
424 g_file_input_stream_real_query_info_finish (GFileInputStream *stream,
425 GAsyncResult *res,
426 GError **error)
428 g_return_val_if_fail (g_task_is_valid (res, stream), NULL);
430 return g_task_propagate_pointer (G_TASK (res), error);