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