GMemoryInputStream: fix skip_async()
[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 "gcancellable.h"
29 #include "gasyncresult.h"
30 #include "gtask.h"
31 #include "gioerror.h"
32 #include "glibintl.h"
33
34
35 /**
36  * SECTION:gfileinputstream
37  * @short_description: File input streaming operations
38  * @include: gio/gio.h
39  * @see_also: #GInputStream, #GDataInputStream, #GSeekable
40  *
41  * GFileInputStream provides input streams that take their
42  * content from a file.
43  *
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  **/
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                                                               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);
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: (allow-none): optional #GCancellable object, %NULL to ignore. 
116  * @error: a #GError location to store the error occurring, or %NULL to 
117  * ignore.
118  *
119  * Queries a file input stream the given @attributes. This 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: (transfer full): a #GFileInfo, or %NULL on error.
126  **/
127 GFileInfo *
128 g_file_input_stream_query_info (GFileInputStream  *stream,
129                                 const 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_literal (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: (allow-none): optional #GCancellable object, %NULL to ignore. 
184  * @callback: (scope async): callback to call when the request is satisfied
185  * @user_data: (closure): the data to pass to callback function
186  * 
187  * Queries the stream information asynchronously.
188  * When the operation is finished @callback will be called. 
189  * You can then call g_file_input_stream_query_info_finish() 
190  * to get the result of the operation.
191  *
192  * For the synchronous version of this function, 
193  * see g_file_input_stream_query_info(). 
194  * 
195  * If @cancellable is not %NULL, then the operation can be cancelled by
196  * triggering the cancellable object from another thread. If the operation
197  * was cancelled, the error %G_IO_ERROR_CANCELLED will be set
198  *  
199  **/
200 void
201 g_file_input_stream_query_info_async (GFileInputStream    *stream,
202                                       const char          *attributes,
203                                       int                  io_priority,
204                                       GCancellable        *cancellable,
205                                       GAsyncReadyCallback  callback,
206                                       gpointer             user_data)
207 {
208   GFileInputStreamClass *klass;
209   GInputStream *input_stream;
210   GError *error = NULL;
211
212   g_return_if_fail (G_IS_FILE_INPUT_STREAM (stream));
213
214   input_stream = G_INPUT_STREAM (stream);
215   
216   if (!g_input_stream_set_pending (input_stream, &error))
217     {
218       g_task_report_error (stream, callback, user_data,
219                            g_file_input_stream_query_info_async,
220                            error);
221       return;
222     }
223
224   klass = G_FILE_INPUT_STREAM_GET_CLASS (stream);
225
226   stream->priv->outstanding_callback = callback;
227   g_object_ref (stream);
228   klass->query_info_async (stream, attributes, io_priority, cancellable,
229                            async_ready_callback_wrapper, user_data);
230 }
231
232 /**
233  * g_file_input_stream_query_info_finish:
234  * @stream: a #GFileInputStream.
235  * @result: a #GAsyncResult.
236  * @error: a #GError location to store the error occurring, 
237  *     or %NULL to ignore.
238  * 
239  * Finishes an asynchronous info query operation.
240  * 
241  * Returns: (transfer full): #GFileInfo. 
242  **/
243 GFileInfo *
244 g_file_input_stream_query_info_finish (GFileInputStream  *stream,
245                                        GAsyncResult      *result,
246                                        GError           **error)
247 {
248   GFileInputStreamClass *class;
249
250   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
251   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
252
253   if (g_async_result_legacy_propagate_error (result, error))
254     return NULL;
255   else if (g_async_result_is_tagged (result, g_file_input_stream_query_info_async))
256     return g_task_propagate_pointer (G_TASK (result), error);
257
258   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
259   return class->query_info_finish (stream, result, error);
260 }
261
262 static goffset
263 g_file_input_stream_tell (GFileInputStream *stream)
264 {
265   GFileInputStreamClass *class;
266   goffset offset;
267
268   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), 0);
269
270   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
271
272   offset = 0;
273   if (class->tell)
274     offset = class->tell (stream);
275
276   return offset;
277 }
278
279 static goffset
280 g_file_input_stream_seekable_tell (GSeekable *seekable)
281 {
282   return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable));
283 }
284
285 static gboolean
286 g_file_input_stream_can_seek (GFileInputStream *stream)
287 {
288   GFileInputStreamClass *class;
289   gboolean can_seek;
290
291   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
292
293   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
294
295   can_seek = FALSE;
296   if (class->seek)
297     {
298       can_seek = TRUE;
299       if (class->can_seek)
300         can_seek = class->can_seek (stream);
301     }
302   
303   return can_seek;
304 }
305
306 static gboolean
307 g_file_input_stream_seekable_can_seek (GSeekable *seekable)
308 {
309   return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable));
310 }
311
312 static gboolean
313 g_file_input_stream_seek (GFileInputStream  *stream,
314                           goffset            offset,
315                           GSeekType          type,
316                           GCancellable      *cancellable,
317                           GError           **error)
318 {
319   GFileInputStreamClass *class;
320   GInputStream *input_stream;
321   gboolean res;
322
323   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
324
325   input_stream = G_INPUT_STREAM (stream);
326   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
327
328   if (!class->seek)
329     {
330       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
331                            _("Seek not supported on stream"));
332       return FALSE;
333     }
334
335   if (!g_input_stream_set_pending (input_stream, error))
336     return FALSE;
337   
338   if (cancellable)
339     g_cancellable_push_current (cancellable);
340   
341   res = class->seek (stream, offset, type, cancellable, error);
342   
343   if (cancellable)
344     g_cancellable_pop_current (cancellable);
345
346   g_input_stream_clear_pending (input_stream);
347   
348   return res;
349 }
350
351 static gboolean
352 g_file_input_stream_seekable_seek (GSeekable     *seekable,
353                                    goffset        offset,
354                                    GSeekType      type,
355                                    GCancellable  *cancellable,
356                                    GError       **error)
357 {
358   return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
359                                    offset, type, cancellable, error);
360 }
361
362 static gboolean
363 g_file_input_stream_seekable_can_truncate (GSeekable *seekable)
364 {
365   return FALSE;
366 }
367
368 static gboolean
369 g_file_input_stream_seekable_truncate (GSeekable     *seekable,
370                                        goffset        offset,
371                                        GCancellable  *cancellable,
372                                        GError       **error)
373 {
374   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
375                        _("Truncate not allowed on input stream"));
376   return FALSE;
377 }
378
379 /********************************************
380  *   Default implementation of async ops    *
381  ********************************************/
382
383 static void
384 query_info_async_thread (GTask        *task,
385                          gpointer      source_object,
386                          gpointer      task_data,
387                          GCancellable *cancellable)
388 {
389   GFileInputStream *stream = source_object;
390   const char *attributes = task_data;
391   GFileInputStreamClass *class;
392   GError *error = NULL;
393   GFileInfo *info = NULL;
394
395   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
396   if (class->query_info)
397     info = class->query_info (stream, attributes, cancellable, &error);
398   else
399     g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
400                          _("Stream doesn't support query_info"));
401
402   if (info == NULL)
403     g_task_return_error (task, error);
404   else
405     g_task_return_pointer (task, info, g_object_unref);
406 }
407
408 static void
409 g_file_input_stream_real_query_info_async (GFileInputStream    *stream,
410                                            const char          *attributes,
411                                            int                  io_priority,
412                                            GCancellable        *cancellable,
413                                            GAsyncReadyCallback  callback,
414                                            gpointer             user_data)
415 {
416   GTask *task;
417
418   task = g_task_new (stream, cancellable, callback, user_data);
419   g_task_set_task_data (task, g_strdup (attributes), g_free);
420   g_task_set_priority (task, io_priority);
421   
422   g_task_run_in_thread (task, query_info_async_thread);
423   g_object_unref (task);
424 }
425
426 static GFileInfo *
427 g_file_input_stream_real_query_info_finish (GFileInputStream  *stream,
428                                             GAsyncResult      *res,
429                                             GError           **error)
430 {
431   g_return_val_if_fail (g_task_is_valid (res, stream), NULL);
432
433   return g_task_propagate_pointer (G_TASK (res), error);
434 }