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