Include "config.h" instead of <config.h> Command used: find -name
[platform/upstream/glib.git] / gio / goutputstream.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 #include "goutputstream.h"
25 #include "gsimpleasyncresult.h"
26 #include "glibintl.h"
27
28 #include "gioalias.h"
29
30 /**
31  * SECTION:goutputstream
32  * @short_description: Base class for implementing streaming output
33  * @include: gio/gio.h
34  *
35  *
36  **/
37
38 G_DEFINE_TYPE (GOutputStream, g_output_stream, G_TYPE_OBJECT);
39
40 struct _GOutputStreamPrivate {
41   guint closed : 1;
42   guint pending : 1;
43   guint cancelled : 1;
44   GAsyncReadyCallback outstanding_callback;
45 };
46
47 static gssize   g_output_stream_real_splice        (GOutputStream             *stream,
48                                                     GInputStream              *source,
49                                                     GOutputStreamSpliceFlags   flags,
50                                                     GCancellable              *cancellable,
51                                                     GError                   **error);
52 static void     g_output_stream_real_write_async   (GOutputStream             *stream,
53                                                     const void                *buffer,
54                                                     gsize                      count,
55                                                     int                        io_priority,
56                                                     GCancellable              *cancellable,
57                                                     GAsyncReadyCallback        callback,
58                                                     gpointer                   data);
59 static gssize   g_output_stream_real_write_finish  (GOutputStream             *stream,
60                                                     GAsyncResult              *result,
61                                                     GError                   **error);
62 static void     g_output_stream_real_splice_async  (GOutputStream             *stream,
63                                                     GInputStream              *source,
64                                                     GOutputStreamSpliceFlags   flags,
65                                                     int                        io_priority,
66                                                     GCancellable              *cancellable,
67                                                     GAsyncReadyCallback        callback,
68                                                     gpointer                   data);
69 static gssize   g_output_stream_real_splice_finish (GOutputStream             *stream,
70                                                     GAsyncResult              *result,
71                                                     GError                   **error);
72 static void     g_output_stream_real_flush_async   (GOutputStream             *stream,
73                                                     int                        io_priority,
74                                                     GCancellable              *cancellable,
75                                                     GAsyncReadyCallback        callback,
76                                                     gpointer                   data);
77 static gboolean g_output_stream_real_flush_finish  (GOutputStream             *stream,
78                                                     GAsyncResult              *result,
79                                                     GError                   **error);
80 static void     g_output_stream_real_close_async   (GOutputStream             *stream,
81                                                     int                        io_priority,
82                                                     GCancellable              *cancellable,
83                                                     GAsyncReadyCallback        callback,
84                                                     gpointer                   data);
85 static gboolean g_output_stream_real_close_finish  (GOutputStream             *stream,
86                                                     GAsyncResult              *result,
87                                                     GError                   **error);
88
89 static void
90 g_output_stream_finalize (GObject *object)
91 {
92   GOutputStream *stream;
93
94   stream = G_OUTPUT_STREAM (object);
95
96   G_OBJECT_CLASS (g_output_stream_parent_class)->finalize (object);
97 }
98
99 static void
100 g_output_stream_dispose (GObject *object)
101 {
102   GOutputStream *stream;
103
104   stream = G_OUTPUT_STREAM (object);
105   
106   if (!stream->priv->closed)
107     g_output_stream_close (stream, NULL, NULL);
108
109   G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
110 }
111
112 static void
113 g_output_stream_class_init (GOutputStreamClass *klass)
114 {
115   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
116   
117   g_type_class_add_private (klass, sizeof (GOutputStreamPrivate));
118   
119   gobject_class->finalize = g_output_stream_finalize;
120   gobject_class->dispose = g_output_stream_dispose;
121
122   klass->splice = g_output_stream_real_splice;
123   
124   klass->write_async = g_output_stream_real_write_async;
125   klass->write_finish = g_output_stream_real_write_finish;
126   klass->splice_async = g_output_stream_real_splice_async;
127   klass->splice_finish = g_output_stream_real_splice_finish;
128   klass->flush_async = g_output_stream_real_flush_async;
129   klass->flush_finish = g_output_stream_real_flush_finish;
130   klass->close_async = g_output_stream_real_close_async;
131   klass->close_finish = g_output_stream_real_close_finish;
132 }
133
134 static void
135 g_output_stream_init (GOutputStream *stream)
136 {
137   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
138                                               G_TYPE_OUTPUT_STREAM,
139                                               GOutputStreamPrivate);
140 }
141
142 /**
143  * g_output_stream_write:
144  * @stream: a #GOutputStream.
145  * @buffer: the buffer containing the data to write. 
146  * @count: the number of bytes to write
147  * @cancellable: optional cancellable object
148  * @error: location to store the error occuring, or %NULL to ignore
149  *
150  * Tries to write @count bytes from @buffer into the stream. Will block
151  * during the operation.
152  * 
153  * If count is zero returns zero and does nothing. A value of @count
154  * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
155  *
156  * On success, the number of bytes written to the stream is returned.
157  * It is not an error if this is not the same as the requested size, as it
158  * can happen e.g. on a partial i/o error, or if there is not enough
159  * storage in the stream. All writes either block until at least one byte
160  * is written, so zero is never returned (unless @count is zero).
161  * 
162  * If @cancellable is not NULL, then the operation can be cancelled by
163  * triggering the cancellable object from another thread. If the operation
164  * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
165  * operation was partially finished when the operation was cancelled the
166  * partial result will be returned, without an error.
167  *
168  * On error -1 is returned and @error is set accordingly.
169  * 
170  * Return value: Number of bytes written, or -1 on error
171  **/
172 gssize
173 g_output_stream_write (GOutputStream  *stream,
174                        const void     *buffer,
175                        gsize           count,
176                        GCancellable   *cancellable,
177                        GError        **error)
178 {
179   GOutputStreamClass *class;
180   gssize res;
181
182   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
183   g_return_val_if_fail (buffer != NULL, 0);
184
185   if (count == 0)
186     return 0;
187   
188   if (((gssize) count) < 0)
189     {
190       g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
191                    _("Too large count value passed to %s"), G_STRFUNC);
192       return -1;
193     }
194
195   class = G_OUTPUT_STREAM_GET_CLASS (stream);
196
197   if (class->write_fn == NULL) 
198     {
199       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
200                            _("Output stream doesn't implement write"));
201       return -1;
202     }
203   
204   if (!g_output_stream_set_pending (stream, error))
205     return -1;
206   
207   if (cancellable)
208     g_cancellable_push_current (cancellable);
209   
210   res = class->write_fn (stream, buffer, count, cancellable, error);
211   
212   if (cancellable)
213     g_cancellable_pop_current (cancellable);
214   
215   g_output_stream_clear_pending (stream);
216
217   return res; 
218 }
219
220 /**
221  * g_output_stream_write_all:
222  * @stream: a #GOutputStream.
223  * @buffer: the buffer containing the data to write. 
224  * @count: the number of bytes to write
225  * @bytes_written: location to store the number of bytes that was 
226  *     written to the stream
227  * @cancellable: optional #GCancellable object, %NULL to ignore.
228  * @error: location to store the error occuring, or %NULL to ignore
229  *
230  * Tries to write @count bytes from @buffer into the stream. Will block
231  * during the operation.
232  * 
233  * This function is similar to g_output_stream_write(), except it tries to
234  * write as many bytes as requested, only stopping on an error.
235  *
236  * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
237  * is set to @count.
238  * 
239  * If there is an error during the operation FALSE is returned and @error
240  * is set to indicate the error status, @bytes_written is updated to contain
241  * the number of bytes written into the stream before the error occurred.
242  *
243  * Return value: %TRUE on success, %FALSE if there was an error
244  **/
245 gboolean
246 g_output_stream_write_all (GOutputStream  *stream,
247                            const void     *buffer,
248                            gsize           count,
249                            gsize          *bytes_written,
250                            GCancellable   *cancellable,
251                            GError        **error)
252 {
253   gsize _bytes_written;
254   gssize res;
255
256   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
257   g_return_val_if_fail (buffer != NULL, FALSE);
258
259   _bytes_written = 0;
260   while (_bytes_written < count)
261     {
262       res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
263                                    cancellable, error);
264       if (res == -1)
265         {
266           if (bytes_written)
267             *bytes_written = _bytes_written;
268           return FALSE;
269         }
270       
271       if (res == 0)
272         g_warning ("Write returned zero without error");
273
274       _bytes_written += res;
275     }
276   
277   if (bytes_written)
278     *bytes_written = _bytes_written;
279
280   return TRUE;
281 }
282
283 /**
284  * g_output_stream_flush:
285  * @stream: a #GOutputStream.
286  * @cancellable: optional cancellable object
287  * @error: location to store the error occuring, or %NULL to ignore
288  *
289  * Flushed any outstanding buffers in the stream. Will block during 
290  * the operation. Closing the stream will implicitly cause a flush.
291  *
292  * This function is optional for inherited classes.
293  * 
294  * If @cancellable is not %NULL, then the operation can be cancelled by
295  * triggering the cancellable object from another thread. If the operation
296  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
297  *
298  * Return value: %TRUE on success, %FALSE on error
299  **/
300 gboolean
301 g_output_stream_flush (GOutputStream  *stream,
302                        GCancellable   *cancellable,
303                        GError        **error)
304 {
305   GOutputStreamClass *class;
306   gboolean res;
307
308   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
309
310   if (!g_output_stream_set_pending (stream, error))
311     return FALSE;
312   
313   class = G_OUTPUT_STREAM_GET_CLASS (stream);
314
315   res = TRUE;
316   if (class->flush)
317     {
318       if (cancellable)
319         g_cancellable_push_current (cancellable);
320       
321       res = class->flush (stream, cancellable, error);
322       
323       if (cancellable)
324         g_cancellable_pop_current (cancellable);
325     }
326   
327   g_output_stream_clear_pending (stream);
328
329   return res;
330 }
331
332 /**
333  * g_output_stream_splice:
334  * @stream: a #GOutputStream.
335  * @source: a #GInputStream.
336  * @flags: a set of #GOutputStreamSpliceFlags.
337  * @cancellable: optional #GCancellable object, %NULL to ignore. 
338  * @error: a #GError location to store the error occuring, or %NULL to 
339  * ignore.
340  *
341  * Splices an input stream into an output stream.
342  *
343  * Returns: a #gssize containing the size of the data spliced.
344  **/
345 gssize
346 g_output_stream_splice (GOutputStream             *stream,
347                         GInputStream              *source,
348                         GOutputStreamSpliceFlags   flags,
349                         GCancellable              *cancellable,
350                         GError                   **error)
351 {
352   GOutputStreamClass *class;
353   gboolean res;
354
355   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
356   g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
357
358   if (g_input_stream_is_closed (source))
359     {
360       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
361                            _("Source stream is already closed"));
362       return -1;
363     }
364
365   if (!g_output_stream_set_pending (stream, error))
366     return -1;
367   
368   class = G_OUTPUT_STREAM_GET_CLASS (stream);
369
370   res = TRUE;
371   if (cancellable)
372     g_cancellable_push_current (cancellable);
373       
374   res = class->splice (stream, source, flags, cancellable, error);
375       
376   if (cancellable)
377     g_cancellable_pop_current (cancellable);
378   
379   g_output_stream_clear_pending (stream);
380
381   return res;
382 }
383
384 static gssize
385 g_output_stream_real_splice (GOutputStream             *stream,
386                              GInputStream              *source,
387                              GOutputStreamSpliceFlags   flags,
388                              GCancellable              *cancellable,
389                              GError                   **error)
390 {
391   GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
392   gssize n_read, n_written;
393   gssize bytes_copied;
394   char buffer[8192], *p;
395   gboolean res;
396
397   bytes_copied = 0;
398   if (class->write_fn == NULL) 
399     {
400       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
401                            _("Output stream doesn't implement write"));
402       res = FALSE;
403       goto notsupported;
404     }
405   
406   res = TRUE;
407   do 
408     {
409       n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
410       if (n_read == -1)
411         {
412           res = FALSE;
413           break;
414         }
415         
416       if (n_read == 0)
417         break;
418
419       p = buffer;
420       while (n_read > 0)
421         {
422           n_written = class->write_fn (stream, p, n_read, cancellable, error);
423           if (n_written == -1)
424             {
425               res = FALSE;
426               break;
427             }
428
429           p += n_written;
430           n_read -= n_written;
431           bytes_copied += n_written;
432         }
433     }
434   while (res);
435
436  notsupported:
437   if (!res)
438     error = NULL; /* Ignore further errors */
439
440   if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
441     {
442       /* Don't care about errors in source here */
443       g_input_stream_close (source, cancellable, NULL);
444     }
445
446   if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
447     {
448       /* But write errors on close are bad! */
449       if (!class->close_fn (stream, cancellable, error))
450         res = FALSE;
451     }
452
453   if (res)
454     return bytes_copied;
455   
456   return -1;
457 }
458
459
460 /**
461  * g_output_stream_close:
462  * @stream: A #GOutputStream.
463  * @cancellable: optional cancellable object
464  * @error: location to store the error occuring, or %NULL to ignore
465  *
466  * Closes the stream, releasing resources related to it.
467  *
468  * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
469  * Closing a stream multiple times will not return an error.
470  *
471  * Closing a stream will automatically flush any outstanding buffers in the
472  * stream.
473  *
474  * Streams will be automatically closed when the last reference
475  * is dropped, but you might want to call this function to make sure 
476  * resources are released as early as possible.
477  *
478  * Some streams might keep the backing store of the stream (e.g. a file descriptor)
479  * open after the stream is closed. See the documentation for the individual
480  * stream for details.
481  *
482  * On failure the first error that happened will be reported, but the close
483  * operation will finish as much as possible. A stream that failed to
484  * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
485  * is important to check and report the error to the user, otherwise
486  * there might be a loss of data as all data might not be written.
487  * 
488  * If @cancellable is not NULL, then the operation can be cancelled by
489  * triggering the cancellable object from another thread. If the operation
490  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
491  * Cancelling a close will still leave the stream closed, but there some streams
492  * can use a faster close that doesn't block to e.g. check errors. On
493  * cancellation (as with any error) there is no guarantee that all written
494  * data will reach the target. 
495  *
496  * Return value: %TRUE on success, %FALSE on failure
497  **/
498 gboolean
499 g_output_stream_close (GOutputStream  *stream,
500                        GCancellable   *cancellable,
501                        GError        **error)
502 {
503   GOutputStreamClass *class;
504   gboolean res;
505
506   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
507
508   class = G_OUTPUT_STREAM_GET_CLASS (stream);
509
510   if (stream->priv->closed)
511     return TRUE;
512
513   if (!g_output_stream_set_pending (stream, error))
514     return FALSE;
515
516   if (cancellable)
517     g_cancellable_push_current (cancellable);
518
519   if (class->flush)
520     res = class->flush (stream, cancellable, error);
521   else
522     res = TRUE;
523   
524   if (!res)
525     {
526       /* flushing caused the error that we want to return,
527        * but we still want to close the underlying stream if possible
528        */
529       if (class->close_fn)
530         class->close_fn (stream, cancellable, NULL);
531     }
532   else
533     {
534       res = TRUE;
535       if (class->close_fn)
536         res = class->close_fn (stream, cancellable, error);
537     }
538   
539   if (cancellable)
540     g_cancellable_pop_current (cancellable);
541   
542   stream->priv->closed = TRUE;
543   g_output_stream_clear_pending (stream);
544   
545   return res;
546 }
547
548 static void
549 async_ready_callback_wrapper (GObject      *source_object,
550                               GAsyncResult *res,
551                               gpointer      user_data)
552 {
553   GOutputStream *stream = G_OUTPUT_STREAM (source_object);
554
555   g_output_stream_clear_pending (stream);
556   if (stream->priv->outstanding_callback)
557     (*stream->priv->outstanding_callback) (source_object, res, user_data);
558   g_object_unref (stream);
559 }
560
561 static void
562 async_ready_close_callback_wrapper (GObject      *source_object,
563                                     GAsyncResult *res,
564                                     gpointer      user_data)
565 {
566   GOutputStream *stream = G_OUTPUT_STREAM (source_object);
567
568   stream->priv->closed = TRUE;
569   g_output_stream_clear_pending (stream);
570   if (stream->priv->outstanding_callback)
571     (*stream->priv->outstanding_callback) (source_object, res, user_data);
572   g_object_unref (stream);
573 }
574
575 /**
576  * g_output_stream_write_async:
577  * @stream: A #GOutputStream.
578  * @buffer: the buffer containing the data to write. 
579  * @count: the number of bytes to write
580  * @io_priority: the io priority of the request.
581  * @cancellable: optional #GCancellable object, %NULL to ignore.
582  * @callback: callback to call when the request is satisfied
583  * @user_data: the data to pass to callback function
584  *
585  * Request an asynchronous write of @count bytes from @buffer into 
586  * the stream. When the operation is finished @callback will be called.
587  * You can then call g_output_stream_write_finish() to get the result of the 
588  * operation.
589  *
590  * During an async request no other sync and async calls are allowed, 
591  * and will result in %G_IO_ERROR_PENDING errors. 
592  *
593  * A value of @count larger than %G_MAXSSIZE will cause a 
594  * %G_IO_ERROR_INVALID_ARGUMENT error.
595  *
596  * On success, the number of bytes written will be passed to the
597  * @callback. It is not an error if this is not the same as the 
598  * requested size, as it can happen e.g. on a partial I/O error, 
599  * but generally we try to write as many bytes as requested. 
600  *
601  * Any outstanding I/O request with higher priority (lower numerical 
602  * value) will be executed before an outstanding request with lower 
603  * priority. Default priority is %G_PRIORITY_DEFAULT.
604  *
605  * The asyncronous methods have a default fallback that uses threads 
606  * to implement asynchronicity, so they are optional for inheriting 
607  * classes. However, if you override one you must override all.
608  *
609  * For the synchronous, blocking version of this function, see 
610  * g_output_stream_write().
611  **/
612 void
613 g_output_stream_write_async (GOutputStream       *stream,
614                              const void          *buffer,
615                              gsize                count,
616                              int                  io_priority,
617                              GCancellable        *cancellable,
618                              GAsyncReadyCallback  callback,
619                              gpointer             user_data)
620 {
621   GOutputStreamClass *class;
622   GSimpleAsyncResult *simple;
623   GError *error = NULL;
624
625   g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
626   g_return_if_fail (buffer != NULL);
627
628   if (count == 0)
629     {
630       simple = g_simple_async_result_new (G_OBJECT (stream),
631                                           callback,
632                                           user_data,
633                                           g_output_stream_write_async);
634       g_simple_async_result_complete_in_idle (simple);
635       g_object_unref (simple);
636       return;
637     }
638
639   if (((gssize) count) < 0)
640     {
641       g_simple_async_report_error_in_idle (G_OBJECT (stream),
642                                            callback,
643                                            user_data,
644                                            G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
645                                            _("Too large count value passed to %s"),
646                                            G_STRFUNC);
647       return;
648     }
649
650   if (!g_output_stream_set_pending (stream, &error))
651     {
652       g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
653                                             callback,
654                                             user_data,
655                                             error);
656       g_error_free (error);
657       return;
658     }
659   
660   class = G_OUTPUT_STREAM_GET_CLASS (stream);
661
662   stream->priv->outstanding_callback = callback;
663   g_object_ref (stream);
664   class->write_async (stream, buffer, count, io_priority, cancellable,
665                       async_ready_callback_wrapper, user_data);
666 }
667
668 /**
669  * g_output_stream_write_finish:
670  * @stream: a #GOutputStream.
671  * @result: a #GAsyncResult.
672  * @error: a #GError location to store the error occuring, or %NULL to 
673  * ignore.
674  * 
675  * Finishes a stream write operation.
676  * 
677  * Returns: a #gssize containing the number of bytes written to the stream.
678  **/
679 gssize
680 g_output_stream_write_finish (GOutputStream  *stream,
681                               GAsyncResult   *result,
682                               GError        **error)
683 {
684   GSimpleAsyncResult *simple;
685   GOutputStreamClass *class;
686
687   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
688   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
689
690   if (G_IS_SIMPLE_ASYNC_RESULT (result))
691     {
692       simple = G_SIMPLE_ASYNC_RESULT (result);
693       if (g_simple_async_result_propagate_error (simple, error))
694         return -1;
695
696       /* Special case writes of 0 bytes */
697       if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
698         return 0;
699     }
700   
701   class = G_OUTPUT_STREAM_GET_CLASS (stream);
702   return class->write_finish (stream, result, error);
703 }
704
705 typedef struct {
706   GInputStream *source;
707   gpointer user_data;
708   GAsyncReadyCallback callback;
709 } SpliceUserData;
710
711 static void
712 async_ready_splice_callback_wrapper (GObject      *source_object,
713                                      GAsyncResult *res,
714                                      gpointer     _data)
715 {
716   GOutputStream *stream = G_OUTPUT_STREAM (source_object);
717   SpliceUserData *data = _data;
718   
719   g_output_stream_clear_pending (stream);
720   
721   if (data->callback)
722     (*data->callback) (source_object, res, data->user_data);
723   
724   g_object_unref (stream);
725   g_object_unref (data->source);
726   g_free (data);
727 }
728
729 /**
730  * g_output_stream_splice_async:
731  * @stream: a #GOutputStream.
732  * @source: a #GInputStream. 
733  * @flags: a set of #GOutputStreamSpliceFlags.
734  * @io_priority: the io priority of the request.
735  * @cancellable: optional #GCancellable object, %NULL to ignore. 
736  * @callback: a #GAsyncReadyCallback. 
737  * @user_data: user data passed to @callback.
738  * 
739  * Splices a stream asynchronously.
740  * When the operation is finished @callback will be called.
741  * You can then call g_output_stream_splice_finish() to get the 
742  * result of the operation.
743  *
744  * For the synchronous, blocking version of this function, see 
745  * g_output_stream_splice().
746  **/
747 void
748 g_output_stream_splice_async (GOutputStream            *stream,
749                               GInputStream             *source,
750                               GOutputStreamSpliceFlags  flags,
751                               int                       io_priority,
752                               GCancellable             *cancellable,
753                               GAsyncReadyCallback       callback,
754                               gpointer                  user_data)
755 {
756   GOutputStreamClass *class;
757   SpliceUserData *data;
758   GError *error = NULL;
759
760   g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
761   g_return_if_fail (G_IS_INPUT_STREAM (source));
762
763   if (g_input_stream_is_closed (source))
764     {
765       g_simple_async_report_error_in_idle (G_OBJECT (stream),
766                                            callback,
767                                            user_data,
768                                            G_IO_ERROR, G_IO_ERROR_CLOSED,
769                                            _("Source stream is already closed"));
770       return;
771     }
772   
773   if (!g_output_stream_set_pending (stream, &error))
774     {
775       g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
776                                             callback,
777                                             user_data,
778                                             error);
779       g_error_free (error);
780       return;
781     }
782
783   class = G_OUTPUT_STREAM_GET_CLASS (stream);
784
785   data = g_new0 (SpliceUserData, 1);
786   data->callback = callback;
787   data->user_data = user_data;
788   data->source = g_object_ref (source);
789   
790   g_object_ref (stream);
791   class->splice_async (stream, source, flags, io_priority, cancellable,
792                       async_ready_splice_callback_wrapper, data);
793 }
794
795 /**
796  * g_output_stream_splice_finish:
797  * @stream: a #GOutputStream.
798  * @result: a #GAsyncResult.
799  * @error: a #GError location to store the error occuring, or %NULL to 
800  * ignore.
801  *
802  * Finishes an asynchronous stream splice operation.
803  * 
804  * Returns: a #gssize of the number of bytes spliced.
805  **/
806 gssize
807 g_output_stream_splice_finish (GOutputStream  *stream,
808                                GAsyncResult   *result,
809                                GError        **error)
810 {
811   GSimpleAsyncResult *simple;
812   GOutputStreamClass *class;
813
814   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
815   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
816
817   if (G_IS_SIMPLE_ASYNC_RESULT (result))
818     {
819       simple = G_SIMPLE_ASYNC_RESULT (result);
820       if (g_simple_async_result_propagate_error (simple, error))
821         return -1;
822     }
823   
824   class = G_OUTPUT_STREAM_GET_CLASS (stream);
825   return class->splice_finish (stream, result, error);
826 }
827
828 /**
829  * g_output_stream_flush_async:
830  * @stream: a #GOutputStream.
831  * @io_priority: the io priority of the request.
832  * @cancellable: optional #GCancellable object, %NULL to ignore.
833  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
834  * @user_data: the data to pass to callback function
835  * 
836  * Flushes a stream asynchronously.
837  * For behaviour details see g_output_stream_flush().
838  *
839  * When the operation is finished @callback will be 
840  * called. You can then call g_output_stream_flush_finish() to get the 
841  * result of the operation.
842  **/
843 void
844 g_output_stream_flush_async (GOutputStream       *stream,
845                              int                  io_priority,
846                              GCancellable        *cancellable,
847                              GAsyncReadyCallback  callback,
848                              gpointer             user_data)
849 {
850   GOutputStreamClass *class;
851   GSimpleAsyncResult *simple;
852   GError *error = NULL;
853
854   g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
855
856   if (!g_output_stream_set_pending (stream, &error))
857     {
858       g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
859                                             callback,
860                                             user_data,
861                                             error);
862       g_error_free (error);
863       return;
864     }
865
866   stream->priv->outstanding_callback = callback;
867   g_object_ref (stream);
868
869   class = G_OUTPUT_STREAM_GET_CLASS (stream);
870   
871   if (class->flush_async == NULL)
872     {
873       simple = g_simple_async_result_new (G_OBJECT (stream),
874                                           async_ready_callback_wrapper,
875                                           user_data,
876                                           g_output_stream_flush_async);
877       g_simple_async_result_complete_in_idle (simple);
878       g_object_unref (simple);
879       return;
880     }
881       
882   class->flush_async (stream, io_priority, cancellable,
883                       async_ready_callback_wrapper, user_data);
884 }
885
886 /**
887  * g_output_stream_flush_finish:
888  * @stream: a #GOutputStream.
889  * @result: a GAsyncResult.
890  * @error: a #GError location to store the error occuring, or %NULL to 
891  * ignore.
892  * 
893  * Finishes flushing an output stream.
894  * 
895  * Returns: %TRUE if flush operation suceeded, %FALSE otherwise.
896  **/
897 gboolean
898 g_output_stream_flush_finish (GOutputStream  *stream,
899                               GAsyncResult   *result,
900                               GError        **error)
901 {
902   GSimpleAsyncResult *simple;
903   GOutputStreamClass *klass;
904
905   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
906   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
907
908   if (G_IS_SIMPLE_ASYNC_RESULT (result))
909     {
910       simple = G_SIMPLE_ASYNC_RESULT (result);
911       if (g_simple_async_result_propagate_error (simple, error))
912         return FALSE;
913
914       /* Special case default implementation */
915       if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
916         return TRUE;
917     }
918
919   klass = G_OUTPUT_STREAM_GET_CLASS (stream);
920   return klass->flush_finish (stream, result, error);
921 }
922
923
924 /**
925  * g_output_stream_close_async:
926  * @stream: A #GOutputStream.
927  * @io_priority: the io priority of the request.
928  * @callback: callback to call when the request is satisfied
929  * @user_data: the data to pass to callback function
930  * @cancellable: optional cancellable object
931  *
932  * Requests an asynchronous close of the stream, releasing resources 
933  * related to it. When the operation is finished @callback will be 
934  * called. You can then call g_output_stream_close_finish() to get 
935  * the result of the operation.
936  *
937  * For behaviour details see g_output_stream_close().
938  *
939  * The asyncronous methods have a default fallback that uses threads 
940  * to implement asynchronicity, so they are optional for inheriting 
941  * classes. However, if you override one you must override all.
942  **/
943 void
944 g_output_stream_close_async (GOutputStream       *stream,
945                              int                  io_priority,
946                              GCancellable        *cancellable,
947                              GAsyncReadyCallback  callback,
948                              gpointer             user_data)
949 {
950   GOutputStreamClass *class;
951   GSimpleAsyncResult *simple;
952   GError *error = NULL;
953
954   g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
955   
956   if (stream->priv->closed)
957     {
958       simple = g_simple_async_result_new (G_OBJECT (stream),
959                                           callback,
960                                           user_data,
961                                           g_output_stream_close_async);
962       g_simple_async_result_complete_in_idle (simple);
963       g_object_unref (simple);
964       return;
965     }
966
967   if (!g_output_stream_set_pending (stream, &error))
968     {
969       g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
970                                             callback,
971                                             user_data,
972                                             error);
973       g_error_free (error);
974       return;
975     }
976   
977   class = G_OUTPUT_STREAM_GET_CLASS (stream);
978   stream->priv->outstanding_callback = callback;
979   g_object_ref (stream);
980   class->close_async (stream, io_priority, cancellable,
981                       async_ready_close_callback_wrapper, user_data);
982 }
983
984 /**
985  * g_output_stream_close_finish:
986  * @stream: a #GOutputStream.
987  * @result: a #GAsyncResult.
988  * @error: a #GError location to store the error occuring, or %NULL to 
989  * ignore.
990  * 
991  * Closes an output stream.
992  * 
993  * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
994  **/
995 gboolean
996 g_output_stream_close_finish (GOutputStream  *stream,
997                               GAsyncResult   *result,
998                               GError        **error)
999 {
1000   GSimpleAsyncResult *simple;
1001   GOutputStreamClass *class;
1002
1003   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1004   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1005
1006   if (G_IS_SIMPLE_ASYNC_RESULT (result))
1007     {
1008       simple = G_SIMPLE_ASYNC_RESULT (result);
1009       if (g_simple_async_result_propagate_error (simple, error))
1010         return FALSE;
1011
1012       /* Special case already closed */
1013       if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1014         return TRUE;
1015     }
1016
1017   class = G_OUTPUT_STREAM_GET_CLASS (stream);
1018   return class->close_finish (stream, result, error);
1019 }
1020
1021 /**
1022  * g_output_stream_is_closed:
1023  * @stream: a #GOutputStream.
1024  * 
1025  * Checks if an output stream has already been closed.
1026  * 
1027  * Returns: %TRUE if @stream is closed. %FALSE otherwise. 
1028  **/
1029 gboolean
1030 g_output_stream_is_closed (GOutputStream *stream)
1031 {
1032   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1033   
1034   return stream->priv->closed;
1035 }
1036
1037 /**
1038  * g_output_stream_has_pending:
1039  * @stream: a #GOutputStream.
1040  * 
1041  * Checks if an ouput stream has pending actions.
1042  * 
1043  * Returns: %TRUE if @stream has pending actions. 
1044  **/
1045 gboolean
1046 g_output_stream_has_pending (GOutputStream *stream)
1047 {
1048   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1049   
1050   return stream->priv->pending;
1051 }
1052
1053 /**
1054  * g_output_stream_set_pending:
1055  * @stream: a #GOutputStream.
1056  * @error: a #GError location to store the error occuring, or %NULL to 
1057  * ignore.
1058  * 
1059  * Sets @stream to have actions pending. If the pending flag is
1060  * already set or @stream is closed, it will return %FALSE and set
1061  * @error.
1062  *
1063  * Return value: %TRUE if pending was previously unset and is now set.
1064  **/
1065 gboolean
1066 g_output_stream_set_pending (GOutputStream *stream,
1067                              GError **error)
1068 {
1069   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1070   
1071   if (stream->priv->closed)
1072     {
1073       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1074                            _("Stream is already closed"));
1075       return FALSE;
1076     }
1077   
1078   if (stream->priv->pending)
1079     {
1080       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1081                            /* Translators: This is an error you get if there is
1082                             * already an operation running against this stream when
1083                             * you try to start one */
1084                            _("Stream has outstanding operation"));
1085       return FALSE;
1086     }
1087   
1088   stream->priv->pending = TRUE;
1089   return TRUE;
1090 }
1091
1092 /**
1093  * g_output_stream_clear_pending:
1094  * @stream: output stream
1095  * 
1096  * Clears the pending flag on @stream.
1097  **/
1098 void
1099 g_output_stream_clear_pending (GOutputStream *stream)
1100 {
1101   g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1102   
1103   stream->priv->pending = FALSE;
1104 }
1105
1106
1107 /********************************************
1108  *   Default implementation of async ops    *
1109  ********************************************/
1110
1111 typedef struct {
1112   const void         *buffer;
1113   gsize               count_requested;
1114   gssize              count_written;
1115 } WriteData;
1116
1117 static void
1118 write_async_thread (GSimpleAsyncResult *res,
1119                     GObject            *object,
1120                     GCancellable       *cancellable)
1121 {
1122   WriteData *op;
1123   GOutputStreamClass *class;
1124   GError *error = NULL;
1125
1126   class = G_OUTPUT_STREAM_GET_CLASS (object);
1127   op = g_simple_async_result_get_op_res_gpointer (res);
1128   op->count_written = class->write_fn (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1129                                        cancellable, &error);
1130   if (op->count_written == -1)
1131     {
1132       g_simple_async_result_set_from_error (res, error);
1133       g_error_free (error);
1134     }
1135 }
1136
1137 static void
1138 g_output_stream_real_write_async (GOutputStream       *stream,
1139                                   const void          *buffer,
1140                                   gsize                count,
1141                                   int                  io_priority,
1142                                   GCancellable        *cancellable,
1143                                   GAsyncReadyCallback  callback,
1144                                   gpointer             user_data)
1145 {
1146   GSimpleAsyncResult *res;
1147   WriteData *op;
1148
1149   op = g_new0 (WriteData, 1);
1150   res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1151   g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1152   op->buffer = buffer;
1153   op->count_requested = count;
1154   
1155   g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1156   g_object_unref (res);
1157 }
1158
1159 static gssize
1160 g_output_stream_real_write_finish (GOutputStream  *stream,
1161                                    GAsyncResult   *result,
1162                                    GError        **error)
1163 {
1164   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1165   WriteData *op;
1166
1167   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1168   op = g_simple_async_result_get_op_res_gpointer (simple);
1169   return op->count_written;
1170 }
1171
1172 typedef struct {
1173   GInputStream *source;
1174   GOutputStreamSpliceFlags flags;
1175   gssize bytes_copied;
1176 } SpliceData;
1177
1178 static void
1179 splice_async_thread (GSimpleAsyncResult *result,
1180                      GObject            *object,
1181                      GCancellable       *cancellable)
1182 {
1183   SpliceData *op;
1184   GOutputStreamClass *class;
1185   GError *error = NULL;
1186   GOutputStream *stream;
1187
1188   stream = G_OUTPUT_STREAM (object);
1189   class = G_OUTPUT_STREAM_GET_CLASS (object);
1190   op = g_simple_async_result_get_op_res_gpointer (result);
1191   
1192   op->bytes_copied = class->splice (stream,
1193                                     op->source,
1194                                     op->flags,
1195                                     cancellable,
1196                                     &error);
1197   if (op->bytes_copied == -1)
1198     {
1199       g_simple_async_result_set_from_error (result, error);
1200       g_error_free (error);
1201     }
1202 }
1203
1204 static void
1205 g_output_stream_real_splice_async (GOutputStream             *stream,
1206                                    GInputStream              *source,
1207                                    GOutputStreamSpliceFlags   flags,
1208                                    int                        io_priority,
1209                                    GCancellable              *cancellable,
1210                                    GAsyncReadyCallback        callback,
1211                                    gpointer                   user_data)
1212 {
1213   GSimpleAsyncResult *res;
1214   SpliceData *op;
1215
1216   op = g_new0 (SpliceData, 1);
1217   res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1218   g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1219   op->flags = flags;
1220   op->source = source;
1221
1222   /* TODO: In the case where both source and destintion have
1223      non-threadbased async calls we can use a true async copy here */
1224   
1225   g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1226   g_object_unref (res);
1227 }
1228
1229 static gssize
1230 g_output_stream_real_splice_finish (GOutputStream  *stream,
1231                                     GAsyncResult   *result,
1232                                     GError        **error)
1233 {
1234   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1235   SpliceData *op;
1236
1237   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1238   op = g_simple_async_result_get_op_res_gpointer (simple);
1239   return op->bytes_copied;
1240 }
1241
1242
1243 static void
1244 flush_async_thread (GSimpleAsyncResult *res,
1245                     GObject            *object,
1246                     GCancellable       *cancellable)
1247 {
1248   GOutputStreamClass *class;
1249   gboolean result;
1250   GError *error = NULL;
1251
1252   class = G_OUTPUT_STREAM_GET_CLASS (object);
1253   result = TRUE;
1254   if (class->flush)
1255     result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1256
1257   if (!result)
1258     {
1259       g_simple_async_result_set_from_error (res, error);
1260       g_error_free (error);
1261     }
1262 }
1263
1264 static void
1265 g_output_stream_real_flush_async (GOutputStream       *stream,
1266                                   int                  io_priority,
1267                                   GCancellable        *cancellable,
1268                                   GAsyncReadyCallback  callback,
1269                                   gpointer             user_data)
1270 {
1271   GSimpleAsyncResult *res;
1272
1273   res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1274   
1275   g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1276   g_object_unref (res);
1277 }
1278
1279 static gboolean
1280 g_output_stream_real_flush_finish (GOutputStream  *stream,
1281                                    GAsyncResult   *result,
1282                                    GError        **error)
1283 {
1284   return TRUE;
1285 }
1286
1287 static void
1288 close_async_thread (GSimpleAsyncResult *res,
1289                     GObject            *object,
1290                     GCancellable       *cancellable)
1291 {
1292   GOutputStreamClass *class;
1293   GError *error = NULL;
1294   gboolean result;
1295
1296   /* Auto handling of cancelation disabled, and ignore
1297      cancellation, since we want to close things anyway, although
1298      possibly in a quick-n-dirty way. At least we never want to leak
1299      open handles */
1300   
1301   class = G_OUTPUT_STREAM_GET_CLASS (object);
1302   result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
1303   if (!result)
1304     {
1305       g_simple_async_result_set_from_error (res, error);
1306       g_error_free (error);
1307     }
1308 }
1309
1310 static void
1311 g_output_stream_real_close_async (GOutputStream       *stream,
1312                                   int                  io_priority,
1313                                   GCancellable        *cancellable,
1314                                   GAsyncReadyCallback  callback,
1315                                   gpointer             user_data)
1316 {
1317   GSimpleAsyncResult *res;
1318   
1319   res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1320
1321   g_simple_async_result_set_handle_cancellation (res, FALSE);
1322   
1323   g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1324   g_object_unref (res);
1325 }
1326
1327 static gboolean
1328 g_output_stream_real_close_finish (GOutputStream  *stream,
1329                                    GAsyncResult   *result,
1330                                    GError        **error)
1331 {
1332   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1333   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);
1334   return TRUE;
1335 }
1336
1337 #define __G_OUTPUT_STREAM_C__
1338 #include "gioaliasdef.c"