Tizen 2.1 base
[platform/upstream/glib2.0.git] / gio / gfileiostream.c
1 /* GIO - GLib Input, IO 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 <gfileiostream.h>
27 #include <gseekable.h>
28 #include "gsimpleasyncresult.h"
29 #include "gasyncresult.h"
30 #include "gcancellable.h"
31 #include "gioerror.h"
32 #include "gfileoutputstream.h"
33 #include "glibintl.h"
34
35
36 /**
37  * SECTION:gfileiostream
38  * @short_description:  File read and write streaming operations
39  * @include: gio/gio.h
40  * @see_also: #GIOStream, #GFileInputStream, #GFileOutputStream, #GSeekable
41  *
42  * GFileIOStream provides io streams that both read and write to the same
43  * file handle.
44  *
45  * GFileIOStream implements #GSeekable, which allows the io
46  * stream to jump to arbitrary positions in the file and to truncate
47  * the file, provided the filesystem of the file supports these
48  * operations.
49  *
50  * To find the position of a file io stream, use
51  * g_seekable_tell().
52  *
53  * To find out if a file io stream supports seeking, use g_seekable_can_seek().
54  * To position a file io stream, use g_seekable_seek().
55  * To find out if a file io stream supports truncating, use
56  * g_seekable_can_truncate(). To truncate a file io
57  * stream, use g_seekable_truncate().
58  *
59  * The default implementation of all the #GFileIOStream operations
60  * and the implementation of #GSeekable just call into the same operations
61  * on the output stream.
62  * Since: 2.22
63  **/
64
65 static void       g_file_io_stream_seekable_iface_init    (GSeekableIface       *iface);
66 static goffset    g_file_io_stream_seekable_tell          (GSeekable            *seekable);
67 static gboolean   g_file_io_stream_seekable_can_seek      (GSeekable            *seekable);
68 static gboolean   g_file_io_stream_seekable_seek          (GSeekable            *seekable,
69                                                            goffset               offset,
70                                                            GSeekType             type,
71                                                            GCancellable         *cancellable,
72                                                            GError              **error);
73 static gboolean   g_file_io_stream_seekable_can_truncate  (GSeekable            *seekable);
74 static gboolean   g_file_io_stream_seekable_truncate      (GSeekable            *seekable,
75                                                            goffset               offset,
76                                                            GCancellable         *cancellable,
77                                                            GError              **error);
78 static void       g_file_io_stream_real_query_info_async  (GFileIOStream    *stream,
79                                                            const char           *attributes,
80                                                            int                   io_priority,
81                                                            GCancellable         *cancellable,
82                                                            GAsyncReadyCallback   callback,
83                                                            gpointer              user_data);
84 static GFileInfo *g_file_io_stream_real_query_info_finish (GFileIOStream    *stream,
85                                                            GAsyncResult         *result,
86                                                            GError              **error);
87
88 G_DEFINE_TYPE_WITH_CODE (GFileIOStream, g_file_io_stream, G_TYPE_IO_STREAM,
89                          G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
90                                                 g_file_io_stream_seekable_iface_init));
91
92 struct _GFileIOStreamPrivate {
93   GAsyncReadyCallback outstanding_callback;
94 };
95
96 static void
97 g_file_io_stream_seekable_iface_init (GSeekableIface *iface)
98 {
99   iface->tell = g_file_io_stream_seekable_tell;
100   iface->can_seek = g_file_io_stream_seekable_can_seek;
101   iface->seek = g_file_io_stream_seekable_seek;
102   iface->can_truncate = g_file_io_stream_seekable_can_truncate;
103   iface->truncate_fn = g_file_io_stream_seekable_truncate;
104 }
105
106 static void
107 g_file_io_stream_init (GFileIOStream *stream)
108 {
109   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
110                                               G_TYPE_FILE_IO_STREAM,
111                                               GFileIOStreamPrivate);
112 }
113
114 /**
115  * g_file_io_stream_query_info:
116  * @stream: a #GFileIOStream.
117  * @attributes: a file attribute query string.
118  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
119  * @error: a #GError, %NULL to ignore.
120  *
121  * Queries a file io stream for the given @attributes.
122  * This function blocks while querying the stream. For the asynchronous
123  * version of this function, see g_file_io_stream_query_info_async().
124  * While the stream is blocked, the stream will set the pending flag
125  * internally, and any other operations on the stream will fail with
126  * %G_IO_ERROR_PENDING.
127  *
128  * Can fail if the stream was already closed (with @error being set to
129  * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
130  * set to %G_IO_ERROR_PENDING), or if querying info is not supported for
131  * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). I
132  * all cases of failure, %NULL will be returned.
133  *
134  * If @cancellable is not %NULL, then the operation can be cancelled by
135  * triggering the cancellable object from another thread. If the operation
136  * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will
137  * be returned.
138  *
139  * Returns: (transfer full): a #GFileInfo for the @stream, or %NULL on error.
140  *
141  * Since: 2.22
142  **/
143 GFileInfo *
144 g_file_io_stream_query_info (GFileIOStream      *stream,
145                              const char             *attributes,
146                              GCancellable           *cancellable,
147                              GError                **error)
148 {
149   GFileIOStreamClass *class;
150   GIOStream *io_stream;
151   GFileInfo *info;
152
153   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
154
155   io_stream = G_IO_STREAM (stream);
156
157   if (!g_io_stream_set_pending (io_stream, error))
158     return NULL;
159
160   info = NULL;
161
162   if (cancellable)
163     g_cancellable_push_current (cancellable);
164
165   class = G_FILE_IO_STREAM_GET_CLASS (stream);
166   if (class->query_info)
167     info = class->query_info (stream, attributes, cancellable, error);
168   else
169     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
170                          _("Stream doesn't support query_info"));
171
172   if (cancellable)
173     g_cancellable_pop_current (cancellable);
174
175   g_io_stream_clear_pending (io_stream);
176
177   return info;
178 }
179
180 static void
181 async_ready_callback_wrapper (GObject *source_object,
182                               GAsyncResult *res,
183                               gpointer      user_data)
184 {
185   GFileIOStream *stream = G_FILE_IO_STREAM (source_object);
186
187   g_io_stream_clear_pending (G_IO_STREAM (stream));
188   if (stream->priv->outstanding_callback)
189     (*stream->priv->outstanding_callback) (source_object, res, user_data);
190   g_object_unref (stream);
191 }
192
193 /**
194  * g_file_io_stream_query_info_async:
195  * @stream: a #GFileIOStream.
196  * @attributes: a file attribute query string.
197  * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link>
198  *     of the request.
199  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
200  * @callback: (scope async): callback to call when the request is satisfied
201  * @user_data: (closure): the data to pass to callback function
202  *
203  * Asynchronously queries the @stream for a #GFileInfo. When completed,
204  * @callback will be called with a #GAsyncResult which can be used to
205  * finish the operation with g_file_io_stream_query_info_finish().
206  *
207  * For the synchronous version of this function, see
208  * g_file_io_stream_query_info().
209  *
210  * Since: 2.22
211  **/
212 void
213 g_file_io_stream_query_info_async (GFileIOStream     *stream,
214                                           const char           *attributes,
215                                           int                   io_priority,
216                                           GCancellable         *cancellable,
217                                           GAsyncReadyCallback   callback,
218                                           gpointer              user_data)
219 {
220   GFileIOStreamClass *klass;
221   GIOStream *io_stream;
222   GError *error = NULL;
223
224   g_return_if_fail (G_IS_FILE_IO_STREAM (stream));
225
226   io_stream = G_IO_STREAM (stream);
227
228   if (!g_io_stream_set_pending (io_stream, &error))
229     {
230       g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
231                                             callback,
232                                             user_data,
233                                             error);
234       return;
235     }
236
237   klass = G_FILE_IO_STREAM_GET_CLASS (stream);
238
239   stream->priv->outstanding_callback = callback;
240   g_object_ref (stream);
241   klass->query_info_async (stream, attributes, io_priority, cancellable,
242                               async_ready_callback_wrapper, user_data);
243 }
244
245 /**
246  * g_file_io_stream_query_info_finish:
247  * @stream: a #GFileIOStream.
248  * @result: a #GAsyncResult.
249  * @error: a #GError, %NULL to ignore.
250  *
251  * Finalizes the asynchronous query started
252  * by g_file_io_stream_query_info_async().
253  *
254  * Returns: (transfer full): A #GFileInfo for the finished query.
255  *
256  * Since: 2.22
257  **/
258 GFileInfo *
259 g_file_io_stream_query_info_finish (GFileIOStream     *stream,
260                                            GAsyncResult         *result,
261                                            GError              **error)
262 {
263   GSimpleAsyncResult *simple;
264   GFileIOStreamClass *class;
265
266   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
267   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
268
269   if (G_IS_SIMPLE_ASYNC_RESULT (result))
270     {
271       simple = G_SIMPLE_ASYNC_RESULT (result);
272       if (g_simple_async_result_propagate_error (simple, error))
273         return NULL;
274     }
275
276   class = G_FILE_IO_STREAM_GET_CLASS (stream);
277   return class->query_info_finish (stream, result, error);
278 }
279
280 /**
281  * g_file_io_stream_get_etag:
282  * @stream: a #GFileIOStream.
283  *
284  * Gets the entity tag for the file when it has been written.
285  * This must be called after the stream has been written
286  * and closed, as the etag can change while writing.
287  *
288  * Returns: the entity tag for the stream.
289  *
290  * Since: 2.22
291  **/
292 char *
293 g_file_io_stream_get_etag (GFileIOStream  *stream)
294 {
295   GFileIOStreamClass *class;
296   GIOStream *io_stream;
297   char *etag;
298
299   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
300
301   io_stream = G_IO_STREAM (stream);
302
303   if (!g_io_stream_is_closed (io_stream))
304     {
305       g_warning ("stream is not closed yet, can't get etag");
306       return NULL;
307     }
308
309   etag = NULL;
310
311   class = G_FILE_IO_STREAM_GET_CLASS (stream);
312   if (class->get_etag)
313     etag = class->get_etag (stream);
314
315   return etag;
316 }
317
318 static goffset
319 g_file_io_stream_tell (GFileIOStream  *stream)
320 {
321   GFileIOStreamClass *class;
322   goffset offset;
323
324   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), 0);
325
326   class = G_FILE_IO_STREAM_GET_CLASS (stream);
327
328   offset = 0;
329   if (class->tell)
330     offset = class->tell (stream);
331
332   return offset;
333 }
334
335 static goffset
336 g_file_io_stream_seekable_tell (GSeekable *seekable)
337 {
338   return g_file_io_stream_tell (G_FILE_IO_STREAM (seekable));
339 }
340
341 static gboolean
342 g_file_io_stream_can_seek (GFileIOStream  *stream)
343 {
344   GFileIOStreamClass *class;
345   gboolean can_seek;
346
347   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
348
349   class = G_FILE_IO_STREAM_GET_CLASS (stream);
350
351   can_seek = FALSE;
352   if (class->seek)
353     {
354       can_seek = TRUE;
355       if (class->can_seek)
356         can_seek = class->can_seek (stream);
357     }
358
359   return can_seek;
360 }
361
362 static gboolean
363 g_file_io_stream_seekable_can_seek (GSeekable *seekable)
364 {
365   return g_file_io_stream_can_seek (G_FILE_IO_STREAM (seekable));
366 }
367
368 static gboolean
369 g_file_io_stream_seek (GFileIOStream  *stream,
370                        goffset             offset,
371                        GSeekType           type,
372                        GCancellable       *cancellable,
373                        GError            **error)
374 {
375   GFileIOStreamClass *class;
376   GIOStream *io_stream;
377   gboolean res;
378
379   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
380
381   io_stream = G_IO_STREAM (stream);
382   class = G_FILE_IO_STREAM_GET_CLASS (stream);
383
384   if (!class->seek)
385     {
386       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
387                            _("Seek not supported on stream"));
388       return FALSE;
389     }
390
391   if (!g_io_stream_set_pending (io_stream, error))
392     return FALSE;
393
394   if (cancellable)
395     g_cancellable_push_current (cancellable);
396
397   res = class->seek (stream, offset, type, cancellable, error);
398
399   if (cancellable)
400     g_cancellable_pop_current (cancellable);
401
402   g_io_stream_clear_pending (io_stream);
403
404   return res;
405 }
406
407 static gboolean
408 g_file_io_stream_seekable_seek (GSeekable  *seekable,
409                                     goffset     offset,
410                                     GSeekType   type,
411                                     GCancellable  *cancellable,
412                                     GError    **error)
413 {
414   return g_file_io_stream_seek (G_FILE_IO_STREAM (seekable),
415                                 offset, type, cancellable, error);
416 }
417
418 static gboolean
419 g_file_io_stream_can_truncate (GFileIOStream  *stream)
420 {
421   GFileIOStreamClass *class;
422   gboolean can_truncate;
423
424   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
425
426   class = G_FILE_IO_STREAM_GET_CLASS (stream);
427
428   can_truncate = FALSE;
429   if (class->truncate_fn)
430     {
431       can_truncate = TRUE;
432       if (class->can_truncate)
433         can_truncate = class->can_truncate (stream);
434     }
435
436   return can_truncate;
437 }
438
439 static gboolean
440 g_file_io_stream_seekable_can_truncate (GSeekable  *seekable)
441 {
442   return g_file_io_stream_can_truncate (G_FILE_IO_STREAM (seekable));
443 }
444
445 static gboolean
446 g_file_io_stream_truncate (GFileIOStream  *stream,
447                            goffset             size,
448                            GCancellable       *cancellable,
449                            GError            **error)
450 {
451   GFileIOStreamClass *class;
452   GIOStream *io_stream;
453   gboolean res;
454
455   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
456
457   io_stream = G_IO_STREAM (stream);
458   class = G_FILE_IO_STREAM_GET_CLASS (stream);
459
460   if (!class->truncate_fn)
461     {
462       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
463                            _("Truncate not supported on stream"));
464       return FALSE;
465     }
466
467   if (!g_io_stream_set_pending (io_stream, error))
468     return FALSE;
469
470   if (cancellable)
471     g_cancellable_push_current (cancellable);
472
473   res = class->truncate_fn (stream, size, cancellable, error);
474
475   if (cancellable)
476     g_cancellable_pop_current (cancellable);
477
478   g_io_stream_clear_pending (io_stream);
479
480   return res;
481 }
482
483 static gboolean
484 g_file_io_stream_seekable_truncate (GSeekable     *seekable,
485                                     goffset        size,
486                                     GCancellable  *cancellable,
487                                     GError       **error)
488 {
489   return g_file_io_stream_truncate (G_FILE_IO_STREAM (seekable),
490                                         size, cancellable, error);
491 }
492 /*****************************************************
493  *   Default implementations based on output stream  *
494  *****************************************************/
495
496 static goffset
497 g_file_io_stream_real_tell (GFileIOStream    *stream)
498 {
499   GOutputStream *out;
500   GSeekable *seekable;
501
502   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
503   seekable = G_SEEKABLE (out);
504
505   return g_seekable_tell (seekable);
506 }
507
508 static gboolean
509 g_file_io_stream_real_can_seek (GFileIOStream    *stream)
510 {
511   GOutputStream *out;
512   GSeekable *seekable;
513
514   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
515   seekable = G_SEEKABLE (out);
516
517   return g_seekable_can_seek (seekable);
518 }
519
520 static gboolean
521 g_file_io_stream_real_seek (GFileIOStream    *stream,
522                             goffset           offset,
523                             GSeekType         type,
524                             GCancellable     *cancellable,
525                             GError          **error)
526 {
527   GOutputStream *out;
528   GSeekable *seekable;
529
530   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
531   seekable = G_SEEKABLE (out);
532
533   return g_seekable_seek (seekable, offset, type, cancellable, error);
534 }
535
536 static  gboolean
537 g_file_io_stream_real_can_truncate (GFileIOStream    *stream)
538 {
539   GOutputStream *out;
540   GSeekable *seekable;
541
542   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
543   seekable = G_SEEKABLE (out);
544
545   return g_seekable_can_truncate (seekable);
546 }
547
548 static gboolean
549 g_file_io_stream_real_truncate_fn (GFileIOStream    *stream,
550                                    goffset               size,
551                                    GCancellable         *cancellable,
552                                    GError              **error)
553 {
554   GOutputStream *out;
555   GSeekable *seekable;
556
557   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
558   seekable = G_SEEKABLE (out);
559
560   return g_seekable_truncate (seekable, size, cancellable, error);
561 }
562
563 static char *
564 g_file_io_stream_real_get_etag (GFileIOStream    *stream)
565 {
566   GOutputStream *out;
567   GFileOutputStream *file_out;
568
569   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
570   file_out = G_FILE_OUTPUT_STREAM (out);
571
572   return g_file_output_stream_get_etag (file_out);
573 }
574
575 static GFileInfo *
576 g_file_io_stream_real_query_info (GFileIOStream    *stream,
577                                   const char           *attributes,
578                                   GCancellable         *cancellable,
579                                   GError              **error)
580 {
581   GOutputStream *out;
582   GFileOutputStream *file_out;
583
584   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
585   file_out = G_FILE_OUTPUT_STREAM (out);
586
587   return g_file_output_stream_query_info (file_out,
588                                           attributes, cancellable, error);
589 }
590
591 typedef struct {
592   GObject *object;
593   GAsyncReadyCallback callback;
594   gpointer user_data;
595 } AsyncOpWrapper;
596
597 static AsyncOpWrapper *
598 async_op_wrapper_new (gpointer object,
599                       GAsyncReadyCallback callback,
600                       gpointer user_data)
601 {
602   AsyncOpWrapper *data;
603
604   data = g_new0 (AsyncOpWrapper, 1);
605   data->object = g_object_ref (object);
606   data->callback = callback;
607   data->user_data = user_data;
608
609   return data;
610 }
611
612 static void
613 async_op_wrapper_callback (GObject *source_object,
614                            GAsyncResult *res,
615                            gpointer user_data)
616 {
617   AsyncOpWrapper *data  = user_data;
618   data->callback (data->object, res, data->user_data);
619   g_object_unref (data->object);
620   g_free (data);
621 }
622
623 static void
624 g_file_io_stream_real_query_info_async (GFileIOStream     *stream,
625                                         const char           *attributes,
626                                         int                   io_priority,
627                                         GCancellable         *cancellable,
628                                         GAsyncReadyCallback   callback,
629                                         gpointer              user_data)
630 {
631   GOutputStream *out;
632   GFileOutputStream *file_out;
633   AsyncOpWrapper *data;
634
635   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
636   file_out = G_FILE_OUTPUT_STREAM (out);
637
638   data = async_op_wrapper_new (stream, callback, user_data);
639   g_file_output_stream_query_info_async (file_out,
640                                          attributes, io_priority,
641                                          cancellable, async_op_wrapper_callback, data);
642 }
643
644 static GFileInfo *
645 g_file_io_stream_real_query_info_finish (GFileIOStream     *stream,
646                                          GAsyncResult      *res,
647                                          GError           **error)
648 {
649   GOutputStream *out;
650   GFileOutputStream *file_out;
651
652   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
653   file_out = G_FILE_OUTPUT_STREAM (out);
654
655   return g_file_output_stream_query_info_finish (file_out, res, error);
656 }
657
658 static void
659 g_file_io_stream_class_init (GFileIOStreamClass *klass)
660 {
661   g_type_class_add_private (klass, sizeof (GFileIOStreamPrivate));
662
663   klass->tell = g_file_io_stream_real_tell;
664   klass->can_seek = g_file_io_stream_real_can_seek;
665   klass->seek = g_file_io_stream_real_seek;
666   klass->can_truncate = g_file_io_stream_real_can_truncate;
667   klass->truncate_fn = g_file_io_stream_real_truncate_fn;
668   klass->query_info = g_file_io_stream_real_query_info;
669   klass->query_info_async = g_file_io_stream_real_query_info_async;
670   klass->query_info_finish = g_file_io_stream_real_query_info_finish;
671   klass->get_etag = g_file_io_stream_real_get_etag;
672 }