GSocketControlMessage: clean up confusing code
[platform/upstream/glib.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: 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: 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: optional #GCancellable object, %NULL to ignore.
200  * @callback: callback to call when the request is satisfied
201  * @user_data: 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_gerror_in_idle (G_OBJECT (stream),
231                                             callback,
232                                             user_data,
233                                             error);
234       g_error_free (error);
235       return;
236     }
237
238   klass = G_FILE_IO_STREAM_GET_CLASS (stream);
239
240   stream->priv->outstanding_callback = callback;
241   g_object_ref (stream);
242   klass->query_info_async (stream, attributes, io_priority, cancellable,
243                               async_ready_callback_wrapper, user_data);
244 }
245
246 /**
247  * g_file_io_stream_query_info_finish:
248  * @stream: a #GFileIOStream.
249  * @result: a #GAsyncResult.
250  * @error: a #GError, %NULL to ignore.
251  *
252  * Finalizes the asynchronous query started
253  * by g_file_io_stream_query_info_async().
254  *
255  * Returns: A #GFileInfo for the finished query.
256  *
257  * Since: 2.22
258  **/
259 GFileInfo *
260 g_file_io_stream_query_info_finish (GFileIOStream     *stream,
261                                            GAsyncResult         *result,
262                                            GError              **error)
263 {
264   GSimpleAsyncResult *simple;
265   GFileIOStreamClass *class;
266
267   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
268   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
269
270   if (G_IS_SIMPLE_ASYNC_RESULT (result))
271     {
272       simple = G_SIMPLE_ASYNC_RESULT (result);
273       if (g_simple_async_result_propagate_error (simple, error))
274         return NULL;
275     }
276
277   class = G_FILE_IO_STREAM_GET_CLASS (stream);
278   return class->query_info_finish (stream, result, error);
279 }
280
281 /**
282  * g_file_io_stream_get_etag:
283  * @stream: a #GFileIOStream.
284  *
285  * Gets the entity tag for the file when it has been written.
286  * This must be called after the stream has been written
287  * and closed, as the etag can change while writing.
288  *
289  * Returns: the entity tag for the stream.
290  *
291  * Since: 2.22
292  **/
293 char *
294 g_file_io_stream_get_etag (GFileIOStream  *stream)
295 {
296   GFileIOStreamClass *class;
297   GIOStream *io_stream;
298   char *etag;
299
300   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
301
302   io_stream = G_IO_STREAM (stream);
303
304   if (!g_io_stream_is_closed (io_stream))
305     {
306       g_warning ("stream is not closed yet, can't get etag");
307       return NULL;
308     }
309
310   etag = NULL;
311
312   class = G_FILE_IO_STREAM_GET_CLASS (stream);
313   if (class->get_etag)
314     etag = class->get_etag (stream);
315
316   return etag;
317 }
318
319 static goffset
320 g_file_io_stream_tell (GFileIOStream  *stream)
321 {
322   GFileIOStreamClass *class;
323   goffset offset;
324
325   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), 0);
326
327   class = G_FILE_IO_STREAM_GET_CLASS (stream);
328
329   offset = 0;
330   if (class->tell)
331     offset = class->tell (stream);
332
333   return offset;
334 }
335
336 static goffset
337 g_file_io_stream_seekable_tell (GSeekable *seekable)
338 {
339   return g_file_io_stream_tell (G_FILE_IO_STREAM (seekable));
340 }
341
342 static gboolean
343 g_file_io_stream_can_seek (GFileIOStream  *stream)
344 {
345   GFileIOStreamClass *class;
346   gboolean can_seek;
347
348   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
349
350   class = G_FILE_IO_STREAM_GET_CLASS (stream);
351
352   can_seek = FALSE;
353   if (class->seek)
354     {
355       can_seek = TRUE;
356       if (class->can_seek)
357         can_seek = class->can_seek (stream);
358     }
359
360   return can_seek;
361 }
362
363 static gboolean
364 g_file_io_stream_seekable_can_seek (GSeekable *seekable)
365 {
366   return g_file_io_stream_can_seek (G_FILE_IO_STREAM (seekable));
367 }
368
369 static gboolean
370 g_file_io_stream_seek (GFileIOStream  *stream,
371                        goffset             offset,
372                        GSeekType           type,
373                        GCancellable       *cancellable,
374                        GError            **error)
375 {
376   GFileIOStreamClass *class;
377   GIOStream *io_stream;
378   gboolean res;
379
380   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
381
382   io_stream = G_IO_STREAM (stream);
383   class = G_FILE_IO_STREAM_GET_CLASS (stream);
384
385   if (!class->seek)
386     {
387       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
388                            _("Seek not supported on stream"));
389       return FALSE;
390     }
391
392   if (!g_io_stream_set_pending (io_stream, error))
393     return FALSE;
394
395   if (cancellable)
396     g_cancellable_push_current (cancellable);
397
398   res = class->seek (stream, offset, type, cancellable, error);
399
400   if (cancellable)
401     g_cancellable_pop_current (cancellable);
402
403   g_io_stream_clear_pending (io_stream);
404
405   return res;
406 }
407
408 static gboolean
409 g_file_io_stream_seekable_seek (GSeekable  *seekable,
410                                     goffset     offset,
411                                     GSeekType   type,
412                                     GCancellable  *cancellable,
413                                     GError    **error)
414 {
415   return g_file_io_stream_seek (G_FILE_IO_STREAM (seekable),
416                                 offset, type, cancellable, error);
417 }
418
419 static gboolean
420 g_file_io_stream_can_truncate (GFileIOStream  *stream)
421 {
422   GFileIOStreamClass *class;
423   gboolean can_truncate;
424
425   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
426
427   class = G_FILE_IO_STREAM_GET_CLASS (stream);
428
429   can_truncate = FALSE;
430   if (class->truncate_fn)
431     {
432       can_truncate = TRUE;
433       if (class->can_truncate)
434         can_truncate = class->can_truncate (stream);
435     }
436
437   return can_truncate;
438 }
439
440 static gboolean
441 g_file_io_stream_seekable_can_truncate (GSeekable  *seekable)
442 {
443   return g_file_io_stream_can_truncate (G_FILE_IO_STREAM (seekable));
444 }
445
446 static gboolean
447 g_file_io_stream_truncate (GFileIOStream  *stream,
448                            goffset             size,
449                            GCancellable       *cancellable,
450                            GError            **error)
451 {
452   GFileIOStreamClass *class;
453   GIOStream *io_stream;
454   gboolean res;
455
456   g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
457
458   io_stream = G_IO_STREAM (stream);
459   class = G_FILE_IO_STREAM_GET_CLASS (stream);
460
461   if (!class->truncate_fn)
462     {
463       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
464                            _("Truncate not supported on stream"));
465       return FALSE;
466     }
467
468   if (!g_io_stream_set_pending (io_stream, error))
469     return FALSE;
470
471   if (cancellable)
472     g_cancellable_push_current (cancellable);
473
474   res = class->truncate_fn (stream, size, cancellable, error);
475
476   if (cancellable)
477     g_cancellable_pop_current (cancellable);
478
479   g_io_stream_clear_pending (io_stream);
480
481   return res;
482 }
483
484 static gboolean
485 g_file_io_stream_seekable_truncate (GSeekable     *seekable,
486                                     goffset        size,
487                                     GCancellable  *cancellable,
488                                     GError       **error)
489 {
490   return g_file_io_stream_truncate (G_FILE_IO_STREAM (seekable),
491                                         size, cancellable, error);
492 }
493 /*****************************************************
494  *   Default implementations based on output stream  *
495  *****************************************************/
496
497 static goffset
498 g_file_io_stream_real_tell (GFileIOStream    *stream)
499 {
500   GOutputStream *out;
501   GSeekable *seekable;
502
503   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
504   seekable = G_SEEKABLE (out);
505
506   return g_seekable_tell (seekable);
507 }
508
509 static gboolean
510 g_file_io_stream_real_can_seek (GFileIOStream    *stream)
511 {
512   GOutputStream *out;
513   GSeekable *seekable;
514
515   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
516   seekable = G_SEEKABLE (out);
517
518   return g_seekable_can_seek (seekable);
519 }
520
521 static gboolean
522 g_file_io_stream_real_seek (GFileIOStream    *stream,
523                             goffset           offset,
524                             GSeekType         type,
525                             GCancellable     *cancellable,
526                             GError          **error)
527 {
528   GOutputStream *out;
529   GSeekable *seekable;
530
531   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
532   seekable = G_SEEKABLE (out);
533
534   return g_seekable_seek (seekable, offset, type, cancellable, error);
535 }
536
537 static  gboolean
538 g_file_io_stream_real_can_truncate (GFileIOStream    *stream)
539 {
540   GOutputStream *out;
541   GSeekable *seekable;
542
543   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
544   seekable = G_SEEKABLE (out);
545
546   return g_seekable_can_truncate (seekable);
547 }
548
549 static gboolean
550 g_file_io_stream_real_truncate_fn (GFileIOStream    *stream,
551                                    goffset               size,
552                                    GCancellable         *cancellable,
553                                    GError              **error)
554 {
555   GOutputStream *out;
556   GSeekable *seekable;
557
558   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
559   seekable = G_SEEKABLE (out);
560
561   return g_seekable_truncate (seekable, size, cancellable, error);
562 }
563
564 static char *
565 g_file_io_stream_real_get_etag (GFileIOStream    *stream)
566 {
567   GOutputStream *out;
568   GFileOutputStream *file_out;
569
570   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
571   file_out = G_FILE_OUTPUT_STREAM (out);
572
573   return g_file_output_stream_get_etag (file_out);
574 }
575
576 static GFileInfo *
577 g_file_io_stream_real_query_info (GFileIOStream    *stream,
578                                   const char           *attributes,
579                                   GCancellable         *cancellable,
580                                   GError              **error)
581 {
582   GOutputStream *out;
583   GFileOutputStream *file_out;
584
585   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
586   file_out = G_FILE_OUTPUT_STREAM (out);
587
588   return g_file_output_stream_query_info (file_out,
589                                           attributes, cancellable, error);
590 }
591
592 typedef struct {
593   GObject *object;
594   GAsyncReadyCallback callback;
595   gpointer user_data;
596 } AsyncOpWrapper;
597
598 static AsyncOpWrapper *
599 async_op_wrapper_new (gpointer object,
600                       GAsyncReadyCallback callback,
601                       gpointer user_data)
602 {
603   AsyncOpWrapper *data;
604
605   data = g_new0 (AsyncOpWrapper, 1);
606   data->object = g_object_ref (object);
607   data->callback = callback;
608   data->user_data = user_data;
609
610   return data;
611 }
612
613 static void
614 async_op_wrapper_callback (GObject *source_object,
615                            GAsyncResult *res,
616                            gpointer user_data)
617 {
618   AsyncOpWrapper *data  = user_data;
619   data->callback (data->object, res, data->user_data);
620   g_object_unref (data->object);
621   g_free (data);
622 }
623
624 static void
625 g_file_io_stream_real_query_info_async (GFileIOStream     *stream,
626                                         const char           *attributes,
627                                         int                   io_priority,
628                                         GCancellable         *cancellable,
629                                         GAsyncReadyCallback   callback,
630                                         gpointer              user_data)
631 {
632   GOutputStream *out;
633   GFileOutputStream *file_out;
634   AsyncOpWrapper *data;
635
636   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
637   file_out = G_FILE_OUTPUT_STREAM (out);
638
639   data = async_op_wrapper_new (stream, callback, user_data);
640   g_file_output_stream_query_info_async (file_out,
641                                          attributes, io_priority,
642                                          cancellable, async_op_wrapper_callback, data);
643 }
644
645 static GFileInfo *
646 g_file_io_stream_real_query_info_finish (GFileIOStream     *stream,
647                                          GAsyncResult      *res,
648                                          GError           **error)
649 {
650   GOutputStream *out;
651   GFileOutputStream *file_out;
652
653   out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
654   file_out = G_FILE_OUTPUT_STREAM (out);
655
656   return g_file_output_stream_query_info_finish (file_out, res, error);
657 }
658
659 static void
660 g_file_io_stream_class_init (GFileIOStreamClass *klass)
661 {
662   g_type_class_add_private (klass, sizeof (GFileIOStreamPrivate));
663
664   klass->tell = g_file_io_stream_real_tell;
665   klass->can_seek = g_file_io_stream_real_can_seek;
666   klass->seek = g_file_io_stream_real_seek;
667   klass->can_truncate = g_file_io_stream_real_can_truncate;
668   klass->truncate_fn = g_file_io_stream_real_truncate_fn;
669   klass->query_info = g_file_io_stream_real_query_info;
670   klass->query_info_async = g_file_io_stream_real_query_info_async;
671   klass->query_info_finish = g_file_io_stream_real_query_info_finish;
672   klass->get_etag = g_file_io_stream_real_get_etag;
673 }