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