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