gio/tests/file: skip the file monitor tests if using GPollFileMonitor
[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 "gcancellable.h"
30 #include "gasyncresult.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_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
219                                             callback,
220                                             user_data,
221                                             error);
222       return;
223     }
224
225   klass = G_FILE_INPUT_STREAM_GET_CLASS (stream);
226
227   stream->priv->outstanding_callback = callback;
228   g_object_ref (stream);
229   klass->query_info_async (stream, attributes, io_priority, cancellable,
230                               async_ready_callback_wrapper, user_data);
231 }
232
233 /**
234  * g_file_input_stream_query_info_finish:
235  * @stream: a #GFileInputStream.
236  * @result: a #GAsyncResult.
237  * @error: a #GError location to store the error occurring, 
238  *     or %NULL to ignore.
239  * 
240  * Finishes an asynchronous info query operation.
241  * 
242  * Returns: (transfer full): #GFileInfo. 
243  **/
244 GFileInfo *
245 g_file_input_stream_query_info_finish (GFileInputStream  *stream,
246                                        GAsyncResult      *result,
247                                        GError           **error)
248 {
249   GFileInputStreamClass *class;
250
251   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), NULL);
252   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
253
254   if (g_async_result_legacy_propagate_error (result, error))
255     return NULL;
256
257   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
258   return class->query_info_finish (stream, result, error);
259 }
260
261 static goffset
262 g_file_input_stream_tell (GFileInputStream *stream)
263 {
264   GFileInputStreamClass *class;
265   goffset offset;
266
267   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), 0);
268
269   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
270
271   offset = 0;
272   if (class->tell)
273     offset = class->tell (stream);
274
275   return offset;
276 }
277
278 static goffset
279 g_file_input_stream_seekable_tell (GSeekable *seekable)
280 {
281   return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable));
282 }
283
284 static gboolean
285 g_file_input_stream_can_seek (GFileInputStream *stream)
286 {
287   GFileInputStreamClass *class;
288   gboolean can_seek;
289
290   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
291
292   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
293
294   can_seek = FALSE;
295   if (class->seek)
296     {
297       can_seek = TRUE;
298       if (class->can_seek)
299         can_seek = class->can_seek (stream);
300     }
301   
302   return can_seek;
303 }
304
305 static gboolean
306 g_file_input_stream_seekable_can_seek (GSeekable *seekable)
307 {
308   return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable));
309 }
310
311 static gboolean
312 g_file_input_stream_seek (GFileInputStream  *stream,
313                           goffset            offset,
314                           GSeekType          type,
315                           GCancellable      *cancellable,
316                           GError           **error)
317 {
318   GFileInputStreamClass *class;
319   GInputStream *input_stream;
320   gboolean res;
321
322   g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream), FALSE);
323
324   input_stream = G_INPUT_STREAM (stream);
325   class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
326
327   if (!class->seek)
328     {
329       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
330                            _("Seek not supported on stream"));
331       return FALSE;
332     }
333
334   if (!g_input_stream_set_pending (input_stream, error))
335     return FALSE;
336   
337   if (cancellable)
338     g_cancellable_push_current (cancellable);
339   
340   res = class->seek (stream, offset, type, cancellable, error);
341   
342   if (cancellable)
343     g_cancellable_pop_current (cancellable);
344
345   g_input_stream_clear_pending (input_stream);
346   
347   return res;
348 }
349
350 static gboolean
351 g_file_input_stream_seekable_seek (GSeekable     *seekable,
352                                    goffset        offset,
353                                    GSeekType      type,
354                                    GCancellable  *cancellable,
355                                    GError       **error)
356 {
357   return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable),
358                                    offset, type, cancellable, error);
359 }
360
361 static gboolean
362 g_file_input_stream_seekable_can_truncate (GSeekable *seekable)
363 {
364   return FALSE;
365 }
366
367 static gboolean
368 g_file_input_stream_seekable_truncate (GSeekable     *seekable,
369                                        goffset        offset,
370                                        GCancellable  *cancellable,
371                                        GError       **error)
372 {
373   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
374                        _("Truncate not allowed on input stream"));
375   return FALSE;
376 }
377
378 /********************************************
379  *   Default implementation of async ops    *
380  ********************************************/
381
382 typedef struct {
383   char *attributes;
384   GFileInfo *info;
385 } QueryInfoAsyncData;
386
387 static void
388 query_info_data_free (QueryInfoAsyncData *data)
389 {
390   if (data->info)
391     g_object_unref (data->info);
392   g_free (data->attributes);
393   g_free (data);
394 }
395
396 static void
397 query_info_async_thread (GSimpleAsyncResult *res,
398                          GObject            *object,
399                          GCancellable       *cancellable)
400 {
401   GFileInputStreamClass *class;
402   GError *error = NULL;
403   QueryInfoAsyncData *data;
404   GFileInfo *info;
405   
406   data = g_simple_async_result_get_op_res_gpointer (res);
407
408   info = NULL;
409   
410   class = G_FILE_INPUT_STREAM_GET_CLASS (object);
411   if (class->query_info)
412     info = class->query_info (G_FILE_INPUT_STREAM (object), data->attributes, cancellable, &error);
413   else
414     g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
415                          _("Stream doesn't support query_info"));
416
417   if (info == NULL)
418     g_simple_async_result_take_error (res, error);
419   else
420     data->info = info;
421 }
422
423 static void
424 g_file_input_stream_real_query_info_async (GFileInputStream    *stream,
425                                            const char          *attributes,
426                                            int                  io_priority,
427                                            GCancellable        *cancellable,
428                                            GAsyncReadyCallback  callback,
429                                            gpointer             user_data)
430 {
431   GSimpleAsyncResult *res;
432   QueryInfoAsyncData *data;
433
434   data = g_new0 (QueryInfoAsyncData, 1);
435   data->attributes = g_strdup (attributes);
436   
437   res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_input_stream_real_query_info_async);
438   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
439   
440   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
441   g_object_unref (res);
442 }
443
444 static GFileInfo *
445 g_file_input_stream_real_query_info_finish (GFileInputStream  *stream,
446                                             GAsyncResult      *res,
447                                             GError           **error)
448 {
449   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
450   QueryInfoAsyncData *data;
451
452   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_input_stream_real_query_info_async);
453
454   if (g_simple_async_result_propagate_error (simple, error))
455     return NULL;
456
457   data = g_simple_async_result_get_op_res_gpointer (simple);
458   if (data->info)
459     return g_object_ref (data->info);
460   
461   return NULL;
462 }