Drop use of xinclude in GTestDBus 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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22
23 #include <glib.h>
24 #include <gfileinputstream.h>
25 #include <gseekable.h>
26 #include "gcancellable.h"
27 #include "gasyncresult.h"
28 #include "gtask.h"
29 #include "gioerror.h"
30 #include "glibintl.h"
31
32
33 /**
34  * SECTION:gfileinputstream
35  * @short_description: File input streaming operations
36  * @include: gio/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. To find the position of a file
45  * input stream, use g_seekable_tell(). To find out if a file input
46  * stream supports seeking, use g_seekable_can_seek().
47  * To position a file input stream, use g_seekable_seek().
48  **/
49
50 static void       g_file_input_stream_seekable_iface_init    (GSeekableIface       *iface);
51 static goffset    g_file_input_stream_seekable_tell          (GSeekable            *seekable);
52 static gboolean   g_file_input_stream_seekable_can_seek      (GSeekable            *seekable);
53 static gboolean   g_file_input_stream_seekable_seek          (GSeekable            *seekable,
54                                                               goffset               offset,
55                                                               GSeekType             type,
56                                                               GCancellable         *cancellable,
57                                                               GError              **error);
58 static gboolean   g_file_input_stream_seekable_can_truncate  (GSeekable            *seekable);
59 static gboolean   g_file_input_stream_seekable_truncate      (GSeekable            *seekable,
60                                                               goffset               offset,
61                                                               GCancellable         *cancellable,
62                                                               GError              **error);
63 static void       g_file_input_stream_real_query_info_async  (GFileInputStream     *stream,
64                                                               const char           *attributes,
65                                                               int                   io_priority,
66                                                               GCancellable         *cancellable,
67                                                               GAsyncReadyCallback   callback,
68                                                               gpointer              user_data);
69 static GFileInfo *g_file_input_stream_real_query_info_finish (GFileInputStream     *stream,
70                                                               GAsyncResult         *result,
71                                                               GError              **error);
72
73
74 struct _GFileInputStreamPrivate {
75   GAsyncReadyCallback outstanding_callback;
76 };
77
78 G_DEFINE_TYPE_WITH_CODE (GFileInputStream, g_file_input_stream, G_TYPE_INPUT_STREAM,
79                          G_ADD_PRIVATE (GFileInputStream)
80                          G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
81                                                 g_file_input_stream_seekable_iface_init))
82
83 static void
84 g_file_input_stream_class_init (GFileInputStreamClass *klass)
85 {
86   klass->query_info_async = g_file_input_stream_real_query_info_async;
87   klass->query_info_finish = g_file_input_stream_real_query_info_finish;
88 }
89
90 static void
91 g_file_input_stream_seekable_iface_init (GSeekableIface *iface)
92 {
93   iface->tell = g_file_input_stream_seekable_tell;
94   iface->can_seek = g_file_input_stream_seekable_can_seek;
95   iface->seek = g_file_input_stream_seekable_seek;
96   iface->can_truncate = g_file_input_stream_seekable_can_truncate;
97   iface->truncate_fn = g_file_input_stream_seekable_truncate;
98 }
99
100 static void
101 g_file_input_stream_init (GFileInputStream *stream)
102 {
103   stream->priv = g_file_input_stream_get_instance_private (stream);
104 }
105
106 /**
107  * g_file_input_stream_query_info:
108  * @stream: a #GFileInputStream.
109  * @attributes: a file attribute query string.
110  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. 
111  * @error: a #GError location to store the error occurring, or %NULL to 
112  * ignore.
113  *
114  * Queries a file input stream the given @attributes. This function blocks 
115  * while querying the stream. For the asynchronous (non-blocking) version 
116  * of this function, see g_file_input_stream_query_info_async(). While the 
117  * stream is blocked, the stream will set the pending flag internally, and 
118  * any other operations on the stream will fail with %G_IO_ERROR_PENDING.
119  *
120  * Returns: (transfer full): a #GFileInfo, or %NULL on error.
121  **/
122 GFileInfo *
123 g_file_input_stream_query_info (GFileInputStream  *stream,
124                                 const char        *attributes,
125                                 GCancellable      *cancellable,
126                                 GError           **error)
127 {
128   GFileInputStreamClass *class;
129   GInputStream *input_stream;
130   GFileInfo *info;
131   
132   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
133   
134   input_stream = G_INPUT_STREAM (stream);
135   
136   if (!g_input_stream_set_pending (input_stream, error))
137     return NULL;
138       
139   info = NULL;
140   
141   if (cancellable)
142     g_cancellable_push_current (cancellable);
143   
144   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
145   if (class->query_info)
146     info = class->query_info (stream, attributes, cancellable, error);
147   else
148     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
149                          _("Stream doesn't support query_info"));
150
151   if (cancellable)
152     g_cancellable_pop_current (cancellable);
153   
154   g_input_stream_clear_pending (input_stream);
155   
156   return info;
157 }
158
159 static void
160 async_ready_callback_wrapper (GObject      *source_object,
161                               GAsyncResult *res,
162                               gpointer      user_data)
163 {
164   GFileInputStream *stream = G_FILE_INPUT_STREAM (source_object);
165
166   g_input_stream_clear_pending (G_INPUT_STREAM (stream));
167   if (stream->priv->outstanding_callback)
168     (*stream->priv->outstanding_callback) (source_object, res, user_data);
169   g_object_unref (stream);
170 }
171
172 /**
173  * g_file_input_stream_query_info_async:
174  * @stream: a #GFileInputStream.
175  * @attributes: a file attribute query string.
176  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
177  *     of the request.
178  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. 
179  * @callback: (scope async): callback to call when the request is satisfied
180  * @user_data: (closure): the data to pass to callback function
181  * 
182  * Queries the stream information asynchronously.
183  * When the operation is finished @callback will be called. 
184  * You can then call g_file_input_stream_query_info_finish() 
185  * to get the result of the operation.
186  *
187  * For the synchronous version of this function, 
188  * 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                                       const 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_task_report_error (stream, callback, user_data,
214                            g_file_input_stream_query_info_async,
215                            error);
216       return;
217     }
218
219   klass = G_FILE_INPUT_STREAM_GET_CLASS (stream);
220
221   stream->priv->outstanding_callback = callback;
222   g_object_ref (stream);
223   klass->query_info_async (stream, attributes, io_priority, cancellable,
224                            async_ready_callback_wrapper, user_data);
225 }
226
227 /**
228  * g_file_input_stream_query_info_finish:
229  * @stream: a #GFileInputStream.
230  * @result: a #GAsyncResult.
231  * @error: a #GError location to store the error occurring, 
232  *     or %NULL to ignore.
233  * 
234  * Finishes an asynchronous info query operation.
235  * 
236  * Returns: (transfer full): #GFileInfo. 
237  **/
238 GFileInfo *
239 g_file_input_stream_query_info_finish (GFileInputStream  *stream,
240                                        GAsyncResult      *result,
241                                        GError           **error)
242 {
243   GFileInputStreamClass *class;
244
245   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
246   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
247
248   if (g_async_result_legacy_propagate_error (result, error))
249     return NULL;
250   else if (g_async_result_is_tagged (result, g_file_input_stream_query_info_async))
251     return g_task_propagate_pointer (G_TASK (result), error);
252
253   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
254   return class->query_info_finish (stream, result, error);
255 }
256
257 static goffset
258 g_file_input_stream_tell (GFileInputStream *stream)
259 {
260   GFileInputStreamClass *class;
261   goffset offset;
262
263   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), 0);
264
265   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
266
267   offset = 0;
268   if (class->tell)
269     offset = class->tell (stream);
270
271   return offset;
272 }
273
274 static goffset
275 g_file_input_stream_seekable_tell (GSeekable *seekable)
276 {
277   return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable));
278 }
279
280 static gboolean
281 g_file_input_stream_can_seek (GFileInputStream *stream)
282 {
283   GFileInputStreamClass *class;
284   gboolean can_seek;
285
286   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
287
288   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
289
290   can_seek = FALSE;
291   if (class->seek)
292     {
293       can_seek = TRUE;
294       if (class->can_seek)
295         can_seek = class->can_seek (stream);
296     }
297   
298   return can_seek;
299 }
300
301 static gboolean
302 g_file_input_stream_seekable_can_seek (GSeekable *seekable)
303 {
304   return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable));
305 }
306
307 static gboolean
308 g_file_input_stream_seek (GFileInputStream  *stream,
309                           goffset            offset,
310                           GSeekType          type,
311                           GCancellable      *cancellable,
312                           GError           **error)
313 {
314   GFileInputStreamClass *class;
315   GInputStream *input_stream;
316   gboolean res;
317
318   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
319
320   input_stream = G_INPUT_STREAM (stream);
321   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
322
323   if (!class->seek)
324     {
325       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
326                            _("Seek not supported on stream"));
327       return FALSE;
328     }
329
330   if (!g_input_stream_set_pending (input_stream, error))
331     return FALSE;
332   
333   if (cancellable)
334     g_cancellable_push_current (cancellable);
335   
336   res = class->seek (stream, offset, type, cancellable, error);
337   
338   if (cancellable)
339     g_cancellable_pop_current (cancellable);
340
341   g_input_stream_clear_pending (input_stream);
342   
343   return res;
344 }
345
346 static gboolean
347 g_file_input_stream_seekable_seek (GSeekable     *seekable,
348                                    goffset        offset,
349                                    GSeekType      type,
350                                    GCancellable  *cancellable,
351                                    GError       **error)
352 {
353   return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
354                                    offset, type, cancellable, error);
355 }
356
357 static gboolean
358 g_file_input_stream_seekable_can_truncate (GSeekable *seekable)
359 {
360   return FALSE;
361 }
362
363 static gboolean
364 g_file_input_stream_seekable_truncate (GSeekable     *seekable,
365                                        goffset        offset,
366                                        GCancellable  *cancellable,
367                                        GError       **error)
368 {
369   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
370                        _("Truncate not allowed on input stream"));
371   return FALSE;
372 }
373
374 /********************************************
375  *   Default implementation of async ops    *
376  ********************************************/
377
378 static void
379 query_info_async_thread (GTask        *task,
380                          gpointer      source_object,
381                          gpointer      task_data,
382                          GCancellable *cancellable)
383 {
384   GFileInputStream *stream = source_object;
385   const char *attributes = task_data;
386   GFileInputStreamClass *class;
387   GError *error = NULL;
388   GFileInfo *info = NULL;
389
390   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
391   if (class->query_info)
392     info = class->query_info (stream, attributes, cancellable, &error);
393   else
394     g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
395                          _("Stream doesn't support query_info"));
396
397   if (info == NULL)
398     g_task_return_error (task, error);
399   else
400     g_task_return_pointer (task, info, g_object_unref);
401 }
402
403 static void
404 g_file_input_stream_real_query_info_async (GFileInputStream    *stream,
405                                            const char          *attributes,
406                                            int                  io_priority,
407                                            GCancellable        *cancellable,
408                                            GAsyncReadyCallback  callback,
409                                            gpointer             user_data)
410 {
411   GTask *task;
412
413   task = g_task_new (stream, cancellable, callback, user_data);
414   g_task_set_task_data (task, g_strdup (attributes), g_free);
415   g_task_set_priority (task, io_priority);
416   
417   g_task_run_in_thread (task, query_info_async_thread);
418   g_object_unref (task);
419 }
420
421 static GFileInfo *
422 g_file_input_stream_real_query_info_finish (GFileInputStream  *stream,
423                                             GAsyncResult      *res,
424                                             GError           **error)
425 {
426   g_return_val_if_fail (g_task_is_valid (res, stream), NULL);
427
428   return g_task_propagate_pointer (G_TASK (res), error);
429 }