Bumps documentation to 93% symbol coverage, touching most
[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 /**
32  * SECTION:gfileinputstream
33  * @short_description: file input streaming operations
34  * @see_also: #GInputStream, #GDataInputStream, #GSeekable.
35  * 
36  * 
37  *
38  **/
39
40 static void       g_file_input_stream_seekable_iface_init    (GSeekableIface       *iface);
41 static goffset    g_file_input_stream_seekable_tell          (GSeekable            *seekable);
42 static gboolean   g_file_input_stream_seekable_can_seek      (GSeekable            *seekable);
43 static gboolean   g_file_input_stream_seekable_seek          (GSeekable            *seekable,
44                                                               goffset               offset,
45                                                               GSeekType             type,
46                                                               GCancellable         *cancellable,
47                                                               GError              **error);
48 static gboolean   g_file_input_stream_seekable_can_truncate  (GSeekable            *seekable);
49 static gboolean   g_file_input_stream_seekable_truncate      (GSeekable            *seekable,
50                                                               goffset               offset,
51                                                               GCancellable         *cancellable,
52                                                               GError              **error);
53 static void       g_file_input_stream_real_query_info_async  (GFileInputStream     *stream,
54                                                               char                 *attributes,
55                                                               int                   io_priority,
56                                                               GCancellable         *cancellable,
57                                                               GAsyncReadyCallback   callback,
58                                                               gpointer              user_data);
59 static GFileInfo *g_file_input_stream_real_query_info_finish (GFileInputStream     *stream,
60                                                               GAsyncResult         *result,
61                                                               GError              **error);
62
63
64 G_DEFINE_TYPE_WITH_CODE (GFileInputStream, g_file_input_stream, G_TYPE_INPUT_STREAM,
65                          G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
66                                                 g_file_input_stream_seekable_iface_init))
67
68 struct _GFileInputStreamPrivate {
69   GAsyncReadyCallback outstanding_callback;
70 };
71
72 static void
73 g_file_input_stream_class_init (GFileInputStreamClass *klass)
74 {
75   g_type_class_add_private (klass, sizeof (GFileInputStreamPrivate));
76
77   klass->query_info_async = g_file_input_stream_real_query_info_async;
78   klass->query_info_finish = g_file_input_stream_real_query_info_finish;
79 }
80
81 static void
82 g_file_input_stream_seekable_iface_init (GSeekableIface *iface)
83 {
84   iface->tell = g_file_input_stream_seekable_tell;
85   iface->can_seek = g_file_input_stream_seekable_can_seek;
86   iface->seek = g_file_input_stream_seekable_seek;
87   iface->can_truncate = g_file_input_stream_seekable_can_truncate;
88   iface->truncate = g_file_input_stream_seekable_truncate;
89 }
90
91 static void
92 g_file_input_stream_init (GFileInputStream *stream)
93 {
94   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
95                                               G_TYPE_FILE_INPUT_STREAM,
96                                               GFileInputStreamPrivate);
97 }
98
99 /**
100  * g_file_input_stream_query_info:
101  * @stream: a #GFileInputStream.
102  * @attributes: a file attribute query string.
103  * @cancellable: optional #GCancellable object, %NULL to ignore. 
104  * @error: a #GError location to store the error occuring, or %NULL to 
105  * ignore.
106  *
107  * Queries a file input stream the given @attributes.his function blocks while querying
108  * the stream. For the asynchronous (non-blocking) version of this function, see 
109  * g_file_input_stream_query_info_async(). While the stream is blocked, 
110  * the stream will set the pending flag internally, and any other operations on the 
111  * stream will fail with %G_IO_ERROR_PENDING.
112  *
113  * Returns: a #GFileInfo, or %NULL on error.
114  **/
115 GFileInfo *
116 g_file_input_stream_query_info (GFileInputStream     *stream,
117                                    char                 *attributes,
118                                    GCancellable         *cancellable,
119                                    GError              **error)
120 {
121   GFileInputStreamClass *class;
122   GInputStream *input_stream;
123   GFileInfo *info;
124   
125   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
126   
127   input_stream = G_INPUT_STREAM (stream);
128   
129   if (g_input_stream_is_closed (input_stream))
130     {
131       g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
132                    _("Stream is already closed"));
133       return NULL;
134     }
135   
136   if (g_input_stream_has_pending (input_stream))
137     {
138       g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
139                    _("Stream has outstanding operation"));
140       return NULL;
141     }
142       
143   info = NULL;
144   
145   g_input_stream_set_pending (input_stream, TRUE);
146
147   if (cancellable)
148     g_push_current_cancellable (cancellable);
149   
150   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
151   if (class->query_info)
152     info = class->query_info (stream, attributes, cancellable, error);
153   else
154     g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
155                  _("Stream doesn't support query_info"));
156
157   if (cancellable)
158     g_pop_current_cancellable (cancellable);
159   
160   g_input_stream_set_pending (input_stream, FALSE);
161   
162   return info;
163 }
164
165 static void
166 async_ready_callback_wrapper (GObject *source_object,
167                               GAsyncResult *res,
168                               gpointer      user_data)
169 {
170   GFileInputStream *stream = G_FILE_INPUT_STREAM (source_object);
171
172   g_input_stream_set_pending (G_INPUT_STREAM (stream), FALSE);
173   if (stream->priv->outstanding_callback)
174     (*stream->priv->outstanding_callback) (source_object, res, user_data);
175   g_object_unref (stream);
176 }
177
178 /**
179  * g_file_input_stream_query_info_async:
180  * @stream: a #GFileInputStream.
181  * @attributes: a file attribute query string.
182  * @io_priority: the i/o priority 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 version
188  * 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
206   g_return_if_fail (G_IS_FILE_INPUT_STREAM (stream));
207
208   input_stream = G_INPUT_STREAM (stream);
209  
210   if (g_input_stream_is_closed (input_stream))
211     {
212       g_simple_async_report_error_in_idle (G_OBJECT (stream),
213                                            callback,
214                                            user_data,
215                                            G_IO_ERROR, G_IO_ERROR_CLOSED,
216                                            _("Stream is already closed"));
217       return;
218     }
219   
220   if (g_input_stream_has_pending (input_stream))
221     {
222       g_simple_async_report_error_in_idle (G_OBJECT (stream),
223                                            callback,
224                                            user_data,
225                                            G_IO_ERROR, G_IO_ERROR_PENDING,
226                                            _("Stream has outstanding operation"));
227       return;
228     }
229
230   klass = G_FILE_INPUT_STREAM_GET_CLASS (stream);
231
232   g_input_stream_set_pending (input_stream, TRUE);
233   stream->priv->outstanding_callback = callback;
234   g_object_ref (stream);
235   klass->query_info_async (stream, attributes, io_priority, cancellable,
236                               async_ready_callback_wrapper, user_data);
237 }
238
239 /**
240  * g_file_input_stream_query_info_finish:
241  * @stream: a #GFileInputStream.
242  * @result: a #GAsyncResult.
243  * @error: a #GError location to store the error occuring, or %NULL to 
244  * ignore.
245  * 
246  * Finishes an asynchronous info query operation.
247  * 
248  * Returns: #GFileInfo. 
249  **/
250 GFileInfo *
251 g_file_input_stream_query_info_finish (GFileInputStream     *stream,
252                                           GAsyncResult         *result,
253                                           GError              **error)
254 {
255   GSimpleAsyncResult *simple;
256   GFileInputStreamClass *class;
257
258   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
259   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
260
261   if (G_IS_SIMPLE_ASYNC_RESULT (result))
262     {
263       simple = G_SIMPLE_ASYNC_RESULT (result);
264       if (g_simple_async_result_propagate_error (simple, error))
265         return NULL;
266     }
267
268   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
269   return class->query_info_finish (stream, result, error);
270 }
271
272 /**
273  * g_file_input_stream_tell:
274  * @stream: a #GFileInputStream.
275  * 
276  * Gets the current position in the stream.
277  * 
278  * Returns: a #goffset with the position in the stream.
279  **/
280 goffset
281 g_file_input_stream_tell (GFileInputStream  *stream)
282 {
283   GFileInputStreamClass *class;
284   goffset offset;
285
286   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), 0);
287
288   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
289
290   offset = 0;
291   if (class->tell)
292     offset = class->tell (stream);
293
294   return offset;
295 }
296
297 static goffset
298 g_file_input_stream_seekable_tell (GSeekable *seekable)
299 {
300   return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable));
301 }
302
303 /**
304  * g_file_input_stream_can_seek:
305  * @stream: a #GFileInputStream.
306  * 
307  * Checks if a file input stream can be seeked.
308  * 
309  * Returns: %TRUE if stream can be seeked. %FALSE otherwise.
310  **/
311 gboolean
312 g_file_input_stream_can_seek (GFileInputStream  *stream)
313 {
314   GFileInputStreamClass *class;
315   gboolean can_seek;
316
317   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
318
319   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
320
321   can_seek = FALSE;
322   if (class->seek)
323     {
324       can_seek = TRUE;
325       if (class->can_seek)
326         can_seek = class->can_seek (stream);
327     }
328   
329   return can_seek;
330 }
331
332 static gboolean
333 g_file_input_stream_seekable_can_seek (GSeekable *seekable)
334 {
335   return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable));
336 }
337
338 /**
339  * g_file_input_stream_seek:
340  * @stream: a #GFileInputStream.
341  * @offset: a #goffset to seek.
342  * @type: a #GSeekType.
343  * @cancellable: optional #GCancellable object, %NULL to ignore. 
344  * @error: a #GError location to store the error occuring, or %NULL to 
345  * ignore.
346  * 
347  * Seeks in the file input stream.
348  * 
349  * If @cancellable is not %NULL, then the operation can be cancelled by
350  * triggering the cancellable object from another thread. If the operation
351  * was cancelled, the error %G_IO_ERROR_CANCELLED will be set.
352  * 
353  * Returns: %TRUE if the stream was successfully seeked to the position.
354  * %FALSE on error.
355  **/
356 gboolean
357 g_file_input_stream_seek (GFileInputStream  *stream,
358                           goffset            offset,
359                           GSeekType          type,
360                           GCancellable      *cancellable,
361                           GError           **error)
362 {
363   GFileInputStreamClass *class;
364   GInputStream *input_stream;
365   gboolean res;
366
367   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
368
369   input_stream = G_INPUT_STREAM (stream);
370   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
371
372   if (g_input_stream_is_closed (input_stream))
373     {
374       g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
375                    _("Stream is already closed"));
376       return FALSE;
377     }
378   
379   if (g_input_stream_has_pending (input_stream))
380     {
381       g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
382                    _("Stream has outstanding operation"));
383       return FALSE;
384     }
385   
386   if (!class->seek)
387     {
388       g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
389                    _("Seek not supported on stream"));
390       return FALSE;
391     }
392
393   g_input_stream_set_pending (input_stream, TRUE);
394   
395   if (cancellable)
396     g_push_current_cancellable (cancellable);
397   
398   res = class->seek (stream, offset, type, cancellable, error);
399   
400   if (cancellable)
401     g_pop_current_cancellable (cancellable);
402
403   g_input_stream_set_pending (input_stream, FALSE);
404   
405   return res;
406 }
407
408 static gboolean
409 g_file_input_stream_seekable_seek (GSeekable  *seekable,
410                                    goffset     offset,
411                                    GSeekType   type,
412                                    GCancellable  *cancellable,
413                                    GError    **error)
414 {
415   return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
416                                    offset, type, cancellable, error);
417 }
418
419 static gboolean
420 g_file_input_stream_seekable_can_truncate (GSeekable  *seekable)
421 {
422   return FALSE;
423 }
424
425 static gboolean
426 g_file_input_stream_seekable_truncate (GSeekable  *seekable,
427                                        goffset     offset,
428                                        GCancellable  *cancellable,
429                                        GError    **error)
430 {
431   g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
432                _("Truncate not allowed on input stream"));
433   return FALSE;
434 }
435
436 /********************************************
437  *   Default implementation of async ops    *
438  ********************************************/
439
440 typedef struct {
441   char *attributes;
442   GFileInfo *info;
443 } QueryInfoAsyncData;
444
445 static void
446 query_info_data_free (QueryInfoAsyncData *data)
447 {
448   if (data->info)
449     g_object_unref (data->info);
450   g_free (data->attributes);
451   g_free (data);
452 }
453
454 static void
455 query_info_async_thread (GSimpleAsyncResult *res,
456                        GObject *object,
457                        GCancellable *cancellable)
458 {
459   GFileInputStreamClass *class;
460   GError *error = NULL;
461   QueryInfoAsyncData *data;
462   GFileInfo *info;
463   
464   data = g_simple_async_result_get_op_res_gpointer (res);
465
466   info = NULL;
467   
468   class = G_FILE_INPUT_STREAM_GET_CLASS (object);
469   if (class->query_info)
470     info = class->query_info (G_FILE_INPUT_STREAM (object), data->attributes, cancellable, &error);
471   else
472     g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
473                  _("Stream doesn't support query_info"));
474
475   if (info == NULL)
476     {
477       g_simple_async_result_set_from_error (res, error);
478       g_error_free (error);
479     }
480   else
481     data->info = info;
482 }
483
484 static void
485 g_file_input_stream_real_query_info_async (GFileInputStream     *stream,
486                                               char                 *attributes,
487                                               int                   io_priority,
488                                               GCancellable         *cancellable,
489                                               GAsyncReadyCallback   callback,
490                                               gpointer              user_data)
491 {
492   GSimpleAsyncResult *res;
493   QueryInfoAsyncData *data;
494
495   data = g_new0 (QueryInfoAsyncData, 1);
496   data->attributes = g_strdup (attributes);
497   
498   res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_input_stream_real_query_info_async);
499   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
500   
501   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
502   g_object_unref (res);
503 }
504
505 static GFileInfo *
506 g_file_input_stream_real_query_info_finish (GFileInputStream     *stream,
507                                                GAsyncResult         *res,
508                                                GError              **error)
509 {
510   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
511   QueryInfoAsyncData *data;
512
513   g_assert (g_simple_async_result_get_source_tag (simple) == g_file_input_stream_real_query_info_async);
514
515   data = g_simple_async_result_get_op_res_gpointer (simple);
516   if (data->info)
517     return g_object_ref (data->info);
518   
519   return NULL;
520 }