More docs
[platform/upstream/glib.git] / gio / gfileinputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include <glib.h>
26 #include <gfileinputstream.h>
27 #include <gseekable.h>
28 #include "gsimpleasyncresult.h"
29 #include "glibintl.h"
30
31 #include "gioalias.h"
32
33 /**
34  * SECTION:gfileinputstream
35  * @short_description: File input streaming operations
36  * @include: gio.h
37  * @see_also: #GInputStream, #GDataInputStream, #GSeekable
38  *
39  * GFileInputStream provides input streams that take their
40  * content from a file.
41  *
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().
50  **/
51
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                                                               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);
74
75
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))
79
80 struct _GFileInputStreamPrivate {
81   GAsyncReadyCallback outstanding_callback;
82 };
83
84 static void
85 g_file_input_stream_class_init (GFileInputStreamClass *klass)
86 {
87   g_type_class_add_private (klass, sizeof (GFileInputStreamPrivate));
88
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;
91 }
92
93 static void
94 g_file_input_stream_seekable_iface_init (GSeekableIface *iface)
95 {
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;
101 }
102
103 static void
104 g_file_input_stream_init (GFileInputStream *stream)
105 {
106   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
107                                               G_TYPE_FILE_INPUT_STREAM,
108                                               GFileInputStreamPrivate);
109 }
110
111 /**
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 
117  * ignore.
118  *
119  * Queries a file input stream the given @attributes.his 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.
124  *
125  * Returns: a #GFileInfo, or %NULL on error.
126  **/
127 GFileInfo *
128 g_file_input_stream_query_info (GFileInputStream  *stream,
129                                 char              *attributes,
130                                 GCancellable      *cancellable,
131                                 GError           **error)
132 {
133   GFileInputStreamClass *class;
134   GInputStream *input_stream;
135   GFileInfo *info;
136   
137   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
138   
139   input_stream = G_INPUT_STREAM (stream);
140   
141   if (!g_input_stream_set_pending (input_stream, error))
142     return NULL;
143       
144   info = NULL;
145   
146   if (cancellable)
147     g_cancellable_push_current (cancellable);
148   
149   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
150   if (class->query_info)
151     info = class->query_info (stream, attributes, cancellable, error);
152   else
153     g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
154                  _("Stream doesn't support query_info"));
155
156   if (cancellable)
157     g_cancellable_pop_current (cancellable);
158   
159   g_input_stream_clear_pending (input_stream);
160   
161   return info;
162 }
163
164 static void
165 async_ready_callback_wrapper (GObject      *source_object,
166                               GAsyncResult *res,
167                               gpointer      user_data)
168 {
169   GFileInputStream *stream = G_FILE_INPUT_STREAM (source_object);
170
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);
175 }
176
177 /**
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> 
182  *     of the request.
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
186  * 
187  * Queries the stream information asynchronously. For the synchronous 
188  * version of this function, see g_file_input_stream_query_info(). 
189  * 
190  * If @cancellable is not %NULL, then the operation can be cancelled by
191  * triggering the cancellable object from another thread. If the operation
192  * was cancelled, the error %G_IO_ERROR_CANCELLED will be set
193  *  
194  **/
195 void
196 g_file_input_stream_query_info_async (GFileInputStream    *stream,
197                                       char                *attributes,
198                                       int                  io_priority,
199                                       GCancellable        *cancellable,
200                                       GAsyncReadyCallback  callback,
201                                       gpointer             user_data)
202 {
203   GFileInputStreamClass *klass;
204   GInputStream *input_stream;
205   GError *error = NULL;
206
207   g_return_if_fail (G_IS_FILE_INPUT_STREAM (stream));
208
209   input_stream = G_INPUT_STREAM (stream);
210   
211   if (!g_input_stream_set_pending (input_stream, &error))
212     {
213       g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
214                                             callback,
215                                             user_data,
216                                             error);
217       g_error_free (error);
218       return;
219     }
220
221   klass = G_FILE_INPUT_STREAM_GET_CLASS (stream);
222
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);
227 }
228
229 /**
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 occuring, 
234  *     or %NULL to ignore.
235  * 
236  * Finishes an asynchronous info query operation.
237  * 
238  * Returns: #GFileInfo. 
239  **/
240 GFileInfo *
241 g_file_input_stream_query_info_finish (GFileInputStream  *stream,
242                                        GAsyncResult      *result,
243                                        GError           **error)
244 {
245   GSimpleAsyncResult *simple;
246   GFileInputStreamClass *class;
247
248   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
249   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
250
251   if (G_IS_SIMPLE_ASYNC_RESULT (result))
252     {
253       simple = G_SIMPLE_ASYNC_RESULT (result);
254       if (g_simple_async_result_propagate_error (simple, error))
255         return NULL;
256     }
257
258   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
259   return class->query_info_finish (stream, result, error);
260 }
261
262 /**
263  * g_file_input_stream_tell:
264  * @stream: a #GFileInputStream.
265  * 
266  * Gets the current position in the stream.
267  * 
268  * Returns: a #goffset with the position in the stream.
269  **/
270 goffset
271 g_file_input_stream_tell (GFileInputStream *stream)
272 {
273   GFileInputStreamClass *class;
274   goffset offset;
275
276   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), 0);
277
278   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
279
280   offset = 0;
281   if (class->tell)
282     offset = class->tell (stream);
283
284   return offset;
285 }
286
287 static goffset
288 g_file_input_stream_seekable_tell (GSeekable *seekable)
289 {
290   return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable));
291 }
292
293 /**
294  * g_file_input_stream_can_seek:
295  * @stream: a #GFileInputStream.
296  * 
297  * Checks if a file input stream can be seeked.
298  * 
299  * Returns: %TRUE if stream can be seeked. %FALSE otherwise.
300  **/
301 gboolean
302 g_file_input_stream_can_seek (GFileInputStream *stream)
303 {
304   GFileInputStreamClass *class;
305   gboolean can_seek;
306
307   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
308
309   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
310
311   can_seek = FALSE;
312   if (class->seek)
313     {
314       can_seek = TRUE;
315       if (class->can_seek)
316         can_seek = class->can_seek (stream);
317     }
318   
319   return can_seek;
320 }
321
322 static gboolean
323 g_file_input_stream_seekable_can_seek (GSeekable *seekable)
324 {
325   return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable));
326 }
327
328 /**
329  * g_file_input_stream_seek:
330  * @stream: a #GFileInputStream.
331  * @offset: a #goffset to seek.
332  * @type: a #GSeekType.
333  * @cancellable: optional #GCancellable object, %NULL to ignore. 
334  * @error: a #GError location to store the error occuring, or 
335  *     %NULL to ignore.
336  * 
337  * Seeks in the file input stream.
338  * 
339  * If @cancellable is not %NULL, then the operation can be cancelled by
340  * triggering the cancellable object from another thread. If the operation
341  * was cancelled, the error %G_IO_ERROR_CANCELLED will be set.
342  * 
343  * Returns: %TRUE if the stream was successfully seeked to the position.
344  * %FALSE on error.
345  **/
346 gboolean
347 g_file_input_stream_seek (GFileInputStream  *stream,
348                           goffset            offset,
349                           GSeekType          type,
350                           GCancellable      *cancellable,
351                           GError           **error)
352 {
353   GFileInputStreamClass *class;
354   GInputStream *input_stream;
355   gboolean res;
356
357   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
358
359   input_stream = G_INPUT_STREAM (stream);
360   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
361
362   if (!class->seek)
363     {
364       g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
365                    _("Seek not supported on stream"));
366       return FALSE;
367     }
368
369   if (!g_input_stream_set_pending (input_stream, error))
370     return FALSE;
371   
372   if (cancellable)
373     g_cancellable_push_current (cancellable);
374   
375   res = class->seek (stream, offset, type, cancellable, error);
376   
377   if (cancellable)
378     g_cancellable_pop_current (cancellable);
379
380   g_input_stream_clear_pending (input_stream);
381   
382   return res;
383 }
384
385 static gboolean
386 g_file_input_stream_seekable_seek (GSeekable     *seekable,
387                                    goffset        offset,
388                                    GSeekType      type,
389                                    GCancellable  *cancellable,
390                                    GError       **error)
391 {
392   return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
393                                    offset, type, cancellable, error);
394 }
395
396 static gboolean
397 g_file_input_stream_seekable_can_truncate (GSeekable *seekable)
398 {
399   return FALSE;
400 }
401
402 static gboolean
403 g_file_input_stream_seekable_truncate (GSeekable     *seekable,
404                                        goffset        offset,
405                                        GCancellable  *cancellable,
406                                        GError       **error)
407 {
408   g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
409                _("Truncate not allowed on input stream"));
410   return FALSE;
411 }
412
413 /********************************************
414  *   Default implementation of async ops    *
415  ********************************************/
416
417 typedef struct {
418   char *attributes;
419   GFileInfo *info;
420 } QueryInfoAsyncData;
421
422 static void
423 query_info_data_free (QueryInfoAsyncData *data)
424 {
425   if (data->info)
426     g_object_unref (data->info);
427   g_free (data->attributes);
428   g_free (data);
429 }
430
431 static void
432 query_info_async_thread (GSimpleAsyncResult *res,
433                          GObject            *object,
434                          GCancellable       *cancellable)
435 {
436   GFileInputStreamClass *class;
437   GError *error = NULL;
438   QueryInfoAsyncData *data;
439   GFileInfo *info;
440   
441   data = g_simple_async_result_get_op_res_gpointer (res);
442
443   info = NULL;
444   
445   class = G_FILE_INPUT_STREAM_GET_CLASS (object);
446   if (class->query_info)
447     info = class->query_info (G_FILE_INPUT_STREAM (object), data->attributes, cancellable, &error);
448   else
449     g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
450                  _("Stream doesn't support query_info"));
451
452   if (info == NULL)
453     {
454       g_simple_async_result_set_from_error (res, error);
455       g_error_free (error);
456     }
457   else
458     data->info = info;
459 }
460
461 static void
462 g_file_input_stream_real_query_info_async (GFileInputStream    *stream,
463                                            char                *attributes,
464                                            int                  io_priority,
465                                            GCancellable        *cancellable,
466                                            GAsyncReadyCallback  callback,
467                                            gpointer             user_data)
468 {
469   GSimpleAsyncResult *res;
470   QueryInfoAsyncData *data;
471
472   data = g_new0 (QueryInfoAsyncData, 1);
473   data->attributes = g_strdup (attributes);
474   
475   res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_input_stream_real_query_info_async);
476   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
477   
478   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
479   g_object_unref (res);
480 }
481
482 static GFileInfo *
483 g_file_input_stream_real_query_info_finish (GFileInputStream  *stream,
484                                             GAsyncResult      *res,
485                                             GError           **error)
486 {
487   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
488   QueryInfoAsyncData *data;
489
490   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_input_stream_real_query_info_async);
491
492   data = g_simple_async_result_get_op_res_gpointer (simple);
493   if (data->info)
494     return g_object_ref (data->info);
495   
496   return NULL;
497 }
498
499 #define __G_FILE_INPUT_STREAM_C__
500 #include "gioaliasdef.c"
501