57056dba97375f90e9617163d7c2c82f35c31fa2
[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 <link linkend="io-priority">I/O priority</link> 
185  *     of the request.
186  * @cancellable: optional #GCancellable object, %NULL to ignore. 
187  * @callback: callback to call when the request is satisfied
188  * @user_data: the data to pass to callback function
189  * 
190  * Queries the stream information asynchronously. For the synchronous 
191  * version of this function, see g_file_input_stream_query_info(). 
192  * 
193  * If @cancellable is not %NULL, then the operation can be cancelled by
194  * triggering the cancellable object from another thread. If the operation
195  * was cancelled, the error %G_IO_ERROR_CANCELLED will be set
196  *  
197  **/
198 void
199 g_file_input_stream_query_info_async (GFileInputStream    *stream,
200                                       char                *attributes,
201                                       int                  io_priority,
202                                       GCancellable        *cancellable,
203                                       GAsyncReadyCallback  callback,
204                                       gpointer             user_data)
205 {
206   GFileInputStreamClass *klass;
207   GInputStream *input_stream;
208
209   g_return_if_fail (G_IS_FILE_INPUT_STREAM (stream));
210
211   input_stream = G_INPUT_STREAM (stream);
212  
213   if (g_input_stream_is_closed (input_stream))
214     {
215       g_simple_async_report_error_in_idle (G_OBJECT (stream),
216                                            callback,
217                                            user_data,
218                                            G_IO_ERROR, G_IO_ERROR_CLOSED,
219                                            _("Stream is already closed"));
220       return;
221     }
222   
223   if (g_input_stream_has_pending (input_stream))
224     {
225       g_simple_async_report_error_in_idle (G_OBJECT (stream),
226                                            callback,
227                                            user_data,
228                                            G_IO_ERROR, G_IO_ERROR_PENDING,
229                                            _("Stream has outstanding operation"));
230       return;
231     }
232
233   klass = G_FILE_INPUT_STREAM_GET_CLASS (stream);
234
235   g_input_stream_set_pending (input_stream, TRUE);
236   stream->priv->outstanding_callback = callback;
237   g_object_ref (stream);
238   klass->query_info_async (stream, attributes, io_priority, cancellable,
239                               async_ready_callback_wrapper, user_data);
240 }
241
242 /**
243  * g_file_input_stream_query_info_finish:
244  * @stream: a #GFileInputStream.
245  * @result: a #GAsyncResult.
246  * @error: a #GError location to store the error occuring, 
247  *     or %NULL to ignore.
248  * 
249  * Finishes an asynchronous info query operation.
250  * 
251  * Returns: #GFileInfo. 
252  **/
253 GFileInfo *
254 g_file_input_stream_query_info_finish (GFileInputStream  *stream,
255                                        GAsyncResult      *result,
256                                        GError           **error)
257 {
258   GSimpleAsyncResult *simple;
259   GFileInputStreamClass *class;
260
261   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
262   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
263
264   if (G_IS_SIMPLE_ASYNC_RESULT (result))
265     {
266       simple = G_SIMPLE_ASYNC_RESULT (result);
267       if (g_simple_async_result_propagate_error (simple, error))
268         return NULL;
269     }
270
271   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
272   return class->query_info_finish (stream, result, error);
273 }
274
275 /**
276  * g_file_input_stream_tell:
277  * @stream: a #GFileInputStream.
278  * 
279  * Gets the current position in the stream.
280  * 
281  * Returns: a #goffset with the position in the stream.
282  **/
283 goffset
284 g_file_input_stream_tell (GFileInputStream *stream)
285 {
286   GFileInputStreamClass *class;
287   goffset offset;
288
289   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), 0);
290
291   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
292
293   offset = 0;
294   if (class->tell)
295     offset = class->tell (stream);
296
297   return offset;
298 }
299
300 static goffset
301 g_file_input_stream_seekable_tell (GSeekable *seekable)
302 {
303   return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable));
304 }
305
306 /**
307  * g_file_input_stream_can_seek:
308  * @stream: a #GFileInputStream.
309  * 
310  * Checks if a file input stream can be seeked.
311  * 
312  * Returns: %TRUE if stream can be seeked. %FALSE otherwise.
313  **/
314 gboolean
315 g_file_input_stream_can_seek (GFileInputStream *stream)
316 {
317   GFileInputStreamClass *class;
318   gboolean can_seek;
319
320   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
321
322   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
323
324   can_seek = FALSE;
325   if (class->seek)
326     {
327       can_seek = TRUE;
328       if (class->can_seek)
329         can_seek = class->can_seek (stream);
330     }
331   
332   return can_seek;
333 }
334
335 static gboolean
336 g_file_input_stream_seekable_can_seek (GSeekable *seekable)
337 {
338   return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable));
339 }
340
341 /**
342  * g_file_input_stream_seek:
343  * @stream: a #GFileInputStream.
344  * @offset: a #goffset to seek.
345  * @type: a #GSeekType.
346  * @cancellable: optional #GCancellable object, %NULL to ignore. 
347  * @error: a #GError location to store the error occuring, or 
348  *     %NULL to ignore.
349  * 
350  * Seeks in the file input stream.
351  * 
352  * If @cancellable is not %NULL, then the operation can be cancelled by
353  * triggering the cancellable object from another thread. If the operation
354  * was cancelled, the error %G_IO_ERROR_CANCELLED will be set.
355  * 
356  * Returns: %TRUE if the stream was successfully seeked to the position.
357  * %FALSE on error.
358  **/
359 gboolean
360 g_file_input_stream_seek (GFileInputStream  *stream,
361                           goffset            offset,
362                           GSeekType          type,
363                           GCancellable      *cancellable,
364                           GError           **error)
365 {
366   GFileInputStreamClass *class;
367   GInputStream *input_stream;
368   gboolean res;
369
370   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
371
372   input_stream = G_INPUT_STREAM (stream);
373   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
374
375   if (g_input_stream_is_closed (input_stream))
376     {
377       g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
378                    _("Stream is already closed"));
379       return FALSE;
380     }
381   
382   if (g_input_stream_has_pending (input_stream))
383     {
384       g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
385                    _("Stream has outstanding operation"));
386       return FALSE;
387     }
388   
389   if (!class->seek)
390     {
391       g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
392                    _("Seek not supported on stream"));
393       return FALSE;
394     }
395
396   g_input_stream_set_pending (input_stream, TRUE);
397   
398   if (cancellable)
399     g_push_current_cancellable (cancellable);
400   
401   res = class->seek (stream, offset, type, cancellable, error);
402   
403   if (cancellable)
404     g_pop_current_cancellable (cancellable);
405
406   g_input_stream_set_pending (input_stream, FALSE);
407   
408   return res;
409 }
410
411 static gboolean
412 g_file_input_stream_seekable_seek (GSeekable     *seekable,
413                                    goffset        offset,
414                                    GSeekType      type,
415                                    GCancellable  *cancellable,
416                                    GError       **error)
417 {
418   return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
419                                    offset, type, cancellable, error);
420 }
421
422 static gboolean
423 g_file_input_stream_seekable_can_truncate (GSeekable *seekable)
424 {
425   return FALSE;
426 }
427
428 static gboolean
429 g_file_input_stream_seekable_truncate (GSeekable     *seekable,
430                                        goffset        offset,
431                                        GCancellable  *cancellable,
432                                        GError       **error)
433 {
434   g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
435                _("Truncate not allowed on input stream"));
436   return FALSE;
437 }
438
439 /********************************************
440  *   Default implementation of async ops    *
441  ********************************************/
442
443 typedef struct {
444   char *attributes;
445   GFileInfo *info;
446 } QueryInfoAsyncData;
447
448 static void
449 query_info_data_free (QueryInfoAsyncData *data)
450 {
451   if (data->info)
452     g_object_unref (data->info);
453   g_free (data->attributes);
454   g_free (data);
455 }
456
457 static void
458 query_info_async_thread (GSimpleAsyncResult *res,
459                          GObject            *object,
460                          GCancellable       *cancellable)
461 {
462   GFileInputStreamClass *class;
463   GError *error = NULL;
464   QueryInfoAsyncData *data;
465   GFileInfo *info;
466   
467   data = g_simple_async_result_get_op_res_gpointer (res);
468
469   info = NULL;
470   
471   class = G_FILE_INPUT_STREAM_GET_CLASS (object);
472   if (class->query_info)
473     info = class->query_info (G_FILE_INPUT_STREAM (object), data->attributes, cancellable, &error);
474   else
475     g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
476                  _("Stream doesn't support query_info"));
477
478   if (info == NULL)
479     {
480       g_simple_async_result_set_from_error (res, error);
481       g_error_free (error);
482     }
483   else
484     data->info = info;
485 }
486
487 static void
488 g_file_input_stream_real_query_info_async (GFileInputStream    *stream,
489                                            char                *attributes,
490                                            int                  io_priority,
491                                            GCancellable        *cancellable,
492                                            GAsyncReadyCallback  callback,
493                                            gpointer             user_data)
494 {
495   GSimpleAsyncResult *res;
496   QueryInfoAsyncData *data;
497
498   data = g_new0 (QueryInfoAsyncData, 1);
499   data->attributes = g_strdup (attributes);
500   
501   res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_input_stream_real_query_info_async);
502   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
503   
504   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
505   g_object_unref (res);
506 }
507
508 static GFileInfo *
509 g_file_input_stream_real_query_info_finish (GFileInputStream  *stream,
510                                             GAsyncResult      *res,
511                                             GError           **error)
512 {
513   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
514   QueryInfoAsyncData *data;
515
516   g_assert (g_simple_async_result_get_source_tag (simple) == g_file_input_stream_real_query_info_async);
517
518   data = g_simple_async_result_get_op_res_gpointer (simple);
519   if (data->info)
520     return g_object_ref (data->info);
521   
522   return NULL;
523 }
524
525 #define __G_FILE_INPUT_STREAM_C__
526 #include "gioaliasdef.c"
527