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