gspawn: Make error codes on Windows more specific
[glib.git] / gio / goutputstream.c
blob3e658e88a301a13422eb2902e92a7d5dba75da28
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.1 of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 #include "config.h"
22 #include <string.h>
23 #include "goutputstream.h"
24 #include "gcancellable.h"
25 #include "gasyncresult.h"
26 #include "gtask.h"
27 #include "ginputstream.h"
28 #include "gioerror.h"
29 #include "gioprivate.h"
30 #include "glibintl.h"
31 #include "gpollableoutputstream.h"
33 /**
34 * SECTION:goutputstream
35 * @short_description: Base class for implementing streaming output
36 * @include: gio/gio.h
38 * #GOutputStream has functions to write to a stream (g_output_stream_write()),
39 * to close a stream (g_output_stream_close()) and to flush pending writes
40 * (g_output_stream_flush()).
42 * To copy the content of an input stream to an output stream without
43 * manually handling the reads and writes, use g_output_stream_splice().
45 * See the documentation for #GIOStream for details of thread safety of
46 * streaming APIs.
48 * All of these functions have async variants too.
49 **/
51 struct _GOutputStreamPrivate {
52 guint closed : 1;
53 guint pending : 1;
54 guint closing : 1;
55 GAsyncReadyCallback outstanding_callback;
58 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GOutputStream, g_output_stream, G_TYPE_OBJECT)
60 static gssize g_output_stream_real_splice (GOutputStream *stream,
61 GInputStream *source,
62 GOutputStreamSpliceFlags flags,
63 GCancellable *cancellable,
64 GError **error);
65 static void g_output_stream_real_write_async (GOutputStream *stream,
66 const void *buffer,
67 gsize count,
68 int io_priority,
69 GCancellable *cancellable,
70 GAsyncReadyCallback callback,
71 gpointer data);
72 static gssize g_output_stream_real_write_finish (GOutputStream *stream,
73 GAsyncResult *result,
74 GError **error);
75 static void g_output_stream_real_splice_async (GOutputStream *stream,
76 GInputStream *source,
77 GOutputStreamSpliceFlags flags,
78 int io_priority,
79 GCancellable *cancellable,
80 GAsyncReadyCallback callback,
81 gpointer data);
82 static gssize g_output_stream_real_splice_finish (GOutputStream *stream,
83 GAsyncResult *result,
84 GError **error);
85 static void g_output_stream_real_flush_async (GOutputStream *stream,
86 int io_priority,
87 GCancellable *cancellable,
88 GAsyncReadyCallback callback,
89 gpointer data);
90 static gboolean g_output_stream_real_flush_finish (GOutputStream *stream,
91 GAsyncResult *result,
92 GError **error);
93 static void g_output_stream_real_close_async (GOutputStream *stream,
94 int io_priority,
95 GCancellable *cancellable,
96 GAsyncReadyCallback callback,
97 gpointer data);
98 static gboolean g_output_stream_real_close_finish (GOutputStream *stream,
99 GAsyncResult *result,
100 GError **error);
101 static gboolean g_output_stream_internal_close (GOutputStream *stream,
102 GCancellable *cancellable,
103 GError **error);
104 static void g_output_stream_internal_close_async (GOutputStream *stream,
105 int io_priority,
106 GCancellable *cancellable,
107 GAsyncReadyCallback callback,
108 gpointer data);
109 static gboolean g_output_stream_internal_close_finish (GOutputStream *stream,
110 GAsyncResult *result,
111 GError **error);
113 static void
114 g_output_stream_dispose (GObject *object)
116 GOutputStream *stream;
118 stream = G_OUTPUT_STREAM (object);
120 if (!stream->priv->closed)
121 g_output_stream_close (stream, NULL, NULL);
123 G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
126 static void
127 g_output_stream_class_init (GOutputStreamClass *klass)
129 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
131 gobject_class->dispose = g_output_stream_dispose;
133 klass->splice = g_output_stream_real_splice;
135 klass->write_async = g_output_stream_real_write_async;
136 klass->write_finish = g_output_stream_real_write_finish;
137 klass->splice_async = g_output_stream_real_splice_async;
138 klass->splice_finish = g_output_stream_real_splice_finish;
139 klass->flush_async = g_output_stream_real_flush_async;
140 klass->flush_finish = g_output_stream_real_flush_finish;
141 klass->close_async = g_output_stream_real_close_async;
142 klass->close_finish = g_output_stream_real_close_finish;
145 static void
146 g_output_stream_init (GOutputStream *stream)
148 stream->priv = g_output_stream_get_instance_private (stream);
152 * g_output_stream_write:
153 * @stream: a #GOutputStream.
154 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
155 * @count: the number of bytes to write
156 * @cancellable: (nullable): optional cancellable object
157 * @error: location to store the error occurring, or %NULL to ignore
159 * Tries to write @count bytes from @buffer into the stream. Will block
160 * during the operation.
162 * If count is 0, returns 0 and does nothing. A value of @count
163 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
165 * On success, the number of bytes written to the stream is returned.
166 * It is not an error if this is not the same as the requested size, as it
167 * can happen e.g. on a partial I/O error, or if there is not enough
168 * storage in the stream. All writes block until at least one byte
169 * is written or an error occurs; 0 is never returned (unless
170 * @count is 0).
172 * If @cancellable is not %NULL, then the operation can be cancelled by
173 * triggering the cancellable object from another thread. If the operation
174 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
175 * operation was partially finished when the operation was cancelled the
176 * partial result will be returned, without an error.
178 * On error -1 is returned and @error is set accordingly.
180 * Virtual: write_fn
182 * Returns: Number of bytes written, or -1 on error
184 gssize
185 g_output_stream_write (GOutputStream *stream,
186 const void *buffer,
187 gsize count,
188 GCancellable *cancellable,
189 GError **error)
191 GOutputStreamClass *class;
192 gssize res;
194 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
195 g_return_val_if_fail (buffer != NULL, 0);
197 if (count == 0)
198 return 0;
200 if (((gssize) count) < 0)
202 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
203 _("Too large count value passed to %s"), G_STRFUNC);
204 return -1;
207 class = G_OUTPUT_STREAM_GET_CLASS (stream);
209 if (class->write_fn == NULL)
211 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
212 _("Output stream doesn’t implement write"));
213 return -1;
216 if (!g_output_stream_set_pending (stream, error))
217 return -1;
219 if (cancellable)
220 g_cancellable_push_current (cancellable);
222 res = class->write_fn (stream, buffer, count, cancellable, error);
224 if (cancellable)
225 g_cancellable_pop_current (cancellable);
227 g_output_stream_clear_pending (stream);
229 return res;
233 * g_output_stream_write_all:
234 * @stream: a #GOutputStream.
235 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
236 * @count: the number of bytes to write
237 * @bytes_written: (out) (optional): location to store the number of bytes that was
238 * written to the stream
239 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
240 * @error: location to store the error occurring, or %NULL to ignore
242 * Tries to write @count bytes from @buffer into the stream. Will block
243 * during the operation.
245 * This function is similar to g_output_stream_write(), except it tries to
246 * write as many bytes as requested, only stopping on an error.
248 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
249 * is set to @count.
251 * If there is an error during the operation %FALSE is returned and @error
252 * is set to indicate the error status.
254 * As a special exception to the normal conventions for functions that
255 * use #GError, if this function returns %FALSE (and sets @error) then
256 * @bytes_written will be set to the number of bytes that were
257 * successfully written before the error was encountered. This
258 * functionality is only available from C. If you need it from another
259 * language then you must write your own loop around
260 * g_output_stream_write().
262 * Returns: %TRUE on success, %FALSE if there was an error
264 gboolean
265 g_output_stream_write_all (GOutputStream *stream,
266 const void *buffer,
267 gsize count,
268 gsize *bytes_written,
269 GCancellable *cancellable,
270 GError **error)
272 gsize _bytes_written;
273 gssize res;
275 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
276 g_return_val_if_fail (buffer != NULL, FALSE);
278 _bytes_written = 0;
279 while (_bytes_written < count)
281 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
282 cancellable, error);
283 if (res == -1)
285 if (bytes_written)
286 *bytes_written = _bytes_written;
287 return FALSE;
290 if (res == 0)
291 g_warning ("Write returned zero without error");
293 _bytes_written += res;
296 if (bytes_written)
297 *bytes_written = _bytes_written;
299 return TRUE;
303 * g_output_stream_printf:
304 * @stream: a #GOutputStream.
305 * @bytes_written: (out) (optional): location to store the number of bytes that was
306 * written to the stream
307 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
308 * @error: location to store the error occurring, or %NULL to ignore
309 * @format: the format string. See the printf() documentation
310 * @...: the parameters to insert into the format string
312 * This is a utility function around g_output_stream_write_all(). It
313 * uses g_strdup_vprintf() to turn @format and @... into a string that
314 * is then written to @stream.
316 * See the documentation of g_output_stream_write_all() about the
317 * behavior of the actual write operation.
319 * Note that partial writes cannot be properly checked with this
320 * function due to the variable length of the written string, if you
321 * need precise control over partial write failures, you need to
322 * create you own printf()-like wrapper around g_output_stream_write()
323 * or g_output_stream_write_all().
325 * Since: 2.40
327 * Returns: %TRUE on success, %FALSE if there was an error
329 gboolean
330 g_output_stream_printf (GOutputStream *stream,
331 gsize *bytes_written,
332 GCancellable *cancellable,
333 GError **error,
334 const gchar *format,
335 ...)
337 va_list args;
338 gboolean success;
340 va_start (args, format);
341 success = g_output_stream_vprintf (stream, bytes_written, cancellable,
342 error, format, args);
343 va_end (args);
345 return success;
349 * g_output_stream_vprintf:
350 * @stream: a #GOutputStream.
351 * @bytes_written: (out) (optional): location to store the number of bytes that was
352 * written to the stream
353 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
354 * @error: location to store the error occurring, or %NULL to ignore
355 * @format: the format string. See the printf() documentation
356 * @args: the parameters to insert into the format string
358 * This is a utility function around g_output_stream_write_all(). It
359 * uses g_strdup_vprintf() to turn @format and @args into a string that
360 * is then written to @stream.
362 * See the documentation of g_output_stream_write_all() about the
363 * behavior of the actual write operation.
365 * Note that partial writes cannot be properly checked with this
366 * function due to the variable length of the written string, if you
367 * need precise control over partial write failures, you need to
368 * create you own printf()-like wrapper around g_output_stream_write()
369 * or g_output_stream_write_all().
371 * Since: 2.40
373 * Returns: %TRUE on success, %FALSE if there was an error
375 gboolean
376 g_output_stream_vprintf (GOutputStream *stream,
377 gsize *bytes_written,
378 GCancellable *cancellable,
379 GError **error,
380 const gchar *format,
381 va_list args)
383 gchar *text;
384 gboolean success;
386 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
387 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
388 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
389 g_return_val_if_fail (format != NULL, FALSE);
391 text = g_strdup_vprintf (format, args);
392 success = g_output_stream_write_all (stream,
393 text, strlen (text),
394 bytes_written, cancellable, error);
395 g_free (text);
397 return success;
401 * g_output_stream_write_bytes:
402 * @stream: a #GOutputStream.
403 * @bytes: the #GBytes to write
404 * @cancellable: (nullable): optional cancellable object
405 * @error: location to store the error occurring, or %NULL to ignore
407 * A wrapper function for g_output_stream_write() which takes a
408 * #GBytes as input. This can be more convenient for use by language
409 * bindings or in other cases where the refcounted nature of #GBytes
410 * is helpful over a bare pointer interface.
412 * However, note that this function may still perform partial writes,
413 * just like g_output_stream_write(). If that occurs, to continue
414 * writing, you will need to create a new #GBytes containing just the
415 * remaining bytes, using g_bytes_new_from_bytes(). Passing the same
416 * #GBytes instance multiple times potentially can result in duplicated
417 * data in the output stream.
419 * Returns: Number of bytes written, or -1 on error
421 gssize
422 g_output_stream_write_bytes (GOutputStream *stream,
423 GBytes *bytes,
424 GCancellable *cancellable,
425 GError **error)
427 gsize size;
428 gconstpointer data;
430 data = g_bytes_get_data (bytes, &size);
432 return g_output_stream_write (stream,
433 data, size,
434 cancellable,
435 error);
439 * g_output_stream_flush:
440 * @stream: a #GOutputStream.
441 * @cancellable: (nullable): optional cancellable object
442 * @error: location to store the error occurring, or %NULL to ignore
444 * Forces a write of all user-space buffered data for the given
445 * @stream. Will block during the operation. Closing the stream will
446 * implicitly cause a flush.
448 * This function is optional for inherited classes.
450 * If @cancellable is not %NULL, then the operation can be cancelled by
451 * triggering the cancellable object from another thread. If the operation
452 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
454 * Returns: %TRUE on success, %FALSE on error
456 gboolean
457 g_output_stream_flush (GOutputStream *stream,
458 GCancellable *cancellable,
459 GError **error)
461 GOutputStreamClass *class;
462 gboolean res;
464 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
466 if (!g_output_stream_set_pending (stream, error))
467 return FALSE;
469 class = G_OUTPUT_STREAM_GET_CLASS (stream);
471 res = TRUE;
472 if (class->flush)
474 if (cancellable)
475 g_cancellable_push_current (cancellable);
477 res = class->flush (stream, cancellable, error);
479 if (cancellable)
480 g_cancellable_pop_current (cancellable);
483 g_output_stream_clear_pending (stream);
485 return res;
489 * g_output_stream_splice:
490 * @stream: a #GOutputStream.
491 * @source: a #GInputStream.
492 * @flags: a set of #GOutputStreamSpliceFlags.
493 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
494 * @error: a #GError location to store the error occurring, or %NULL to
495 * ignore.
497 * Splices an input stream into an output stream.
499 * Returns: a #gssize containing the size of the data spliced, or
500 * -1 if an error occurred. Note that if the number of bytes
501 * spliced is greater than %G_MAXSSIZE, then that will be
502 * returned, and there is no way to determine the actual number
503 * of bytes spliced.
505 gssize
506 g_output_stream_splice (GOutputStream *stream,
507 GInputStream *source,
508 GOutputStreamSpliceFlags flags,
509 GCancellable *cancellable,
510 GError **error)
512 GOutputStreamClass *class;
513 gssize bytes_copied;
515 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
516 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
518 if (g_input_stream_is_closed (source))
520 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
521 _("Source stream is already closed"));
522 return -1;
525 if (!g_output_stream_set_pending (stream, error))
526 return -1;
528 class = G_OUTPUT_STREAM_GET_CLASS (stream);
530 if (cancellable)
531 g_cancellable_push_current (cancellable);
533 bytes_copied = class->splice (stream, source, flags, cancellable, error);
535 if (cancellable)
536 g_cancellable_pop_current (cancellable);
538 g_output_stream_clear_pending (stream);
540 return bytes_copied;
543 static gssize
544 g_output_stream_real_splice (GOutputStream *stream,
545 GInputStream *source,
546 GOutputStreamSpliceFlags flags,
547 GCancellable *cancellable,
548 GError **error)
550 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
551 gssize n_read, n_written;
552 gsize bytes_copied;
553 char buffer[8192], *p;
554 gboolean res;
556 bytes_copied = 0;
557 if (class->write_fn == NULL)
559 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
560 _("Output stream doesn’t implement write"));
561 res = FALSE;
562 goto notsupported;
565 res = TRUE;
568 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
569 if (n_read == -1)
571 res = FALSE;
572 break;
575 if (n_read == 0)
576 break;
578 p = buffer;
579 while (n_read > 0)
581 n_written = class->write_fn (stream, p, n_read, cancellable, error);
582 if (n_written == -1)
584 res = FALSE;
585 break;
588 p += n_written;
589 n_read -= n_written;
590 bytes_copied += n_written;
593 if (bytes_copied > G_MAXSSIZE)
594 bytes_copied = G_MAXSSIZE;
596 while (res);
598 notsupported:
599 if (!res)
600 error = NULL; /* Ignore further errors */
602 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
604 /* Don't care about errors in source here */
605 g_input_stream_close (source, cancellable, NULL);
608 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
610 /* But write errors on close are bad! */
611 if (!g_output_stream_internal_close (stream, cancellable, error))
612 res = FALSE;
615 if (res)
616 return bytes_copied;
618 return -1;
621 /* Must always be called inside
622 * g_output_stream_set_pending()/g_output_stream_clear_pending(). */
623 static gboolean
624 g_output_stream_internal_close (GOutputStream *stream,
625 GCancellable *cancellable,
626 GError **error)
628 GOutputStreamClass *class;
629 gboolean res;
631 if (stream->priv->closed)
632 return TRUE;
634 class = G_OUTPUT_STREAM_GET_CLASS (stream);
636 stream->priv->closing = TRUE;
638 if (cancellable)
639 g_cancellable_push_current (cancellable);
641 if (class->flush)
642 res = class->flush (stream, cancellable, error);
643 else
644 res = TRUE;
646 if (!res)
648 /* flushing caused the error that we want to return,
649 * but we still want to close the underlying stream if possible
651 if (class->close_fn)
652 class->close_fn (stream, cancellable, NULL);
654 else
656 res = TRUE;
657 if (class->close_fn)
658 res = class->close_fn (stream, cancellable, error);
661 if (cancellable)
662 g_cancellable_pop_current (cancellable);
664 stream->priv->closing = FALSE;
665 stream->priv->closed = TRUE;
667 return res;
671 * g_output_stream_close:
672 * @stream: A #GOutputStream.
673 * @cancellable: (nullable): optional cancellable object
674 * @error: location to store the error occurring, or %NULL to ignore
676 * Closes the stream, releasing resources related to it.
678 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
679 * Closing a stream multiple times will not return an error.
681 * Closing a stream will automatically flush any outstanding buffers in the
682 * stream.
684 * Streams will be automatically closed when the last reference
685 * is dropped, but you might want to call this function to make sure
686 * resources are released as early as possible.
688 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
689 * open after the stream is closed. See the documentation for the individual
690 * stream for details.
692 * On failure the first error that happened will be reported, but the close
693 * operation will finish as much as possible. A stream that failed to
694 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
695 * is important to check and report the error to the user, otherwise
696 * there might be a loss of data as all data might not be written.
698 * If @cancellable is not %NULL, then the operation can be cancelled by
699 * triggering the cancellable object from another thread. If the operation
700 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
701 * Cancelling a close will still leave the stream closed, but there some streams
702 * can use a faster close that doesn't block to e.g. check errors. On
703 * cancellation (as with any error) there is no guarantee that all written
704 * data will reach the target.
706 * Returns: %TRUE on success, %FALSE on failure
708 gboolean
709 g_output_stream_close (GOutputStream *stream,
710 GCancellable *cancellable,
711 GError **error)
713 gboolean res;
715 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
717 if (stream->priv->closed)
718 return TRUE;
720 if (!g_output_stream_set_pending (stream, error))
721 return FALSE;
723 res = g_output_stream_internal_close (stream, cancellable, error);
725 g_output_stream_clear_pending (stream);
727 return res;
730 static void
731 async_ready_write_callback_wrapper (GObject *source_object,
732 GAsyncResult *res,
733 gpointer user_data)
735 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
736 GOutputStreamClass *class;
737 GTask *task = user_data;
738 gssize nwrote;
739 GError *error = NULL;
741 g_output_stream_clear_pending (stream);
743 if (g_async_result_legacy_propagate_error (res, &error))
744 nwrote = -1;
745 else
747 class = G_OUTPUT_STREAM_GET_CLASS (stream);
748 nwrote = class->write_finish (stream, res, &error);
751 if (nwrote >= 0)
752 g_task_return_int (task, nwrote);
753 else
754 g_task_return_error (task, error);
755 g_object_unref (task);
759 * g_output_stream_write_async:
760 * @stream: A #GOutputStream.
761 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
762 * @count: the number of bytes to write
763 * @io_priority: the io priority of the request.
764 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
765 * @callback: (scope async): callback to call when the request is satisfied
766 * @user_data: (closure): the data to pass to callback function
768 * Request an asynchronous write of @count bytes from @buffer into
769 * the stream. When the operation is finished @callback will be called.
770 * You can then call g_output_stream_write_finish() to get the result of the
771 * operation.
773 * During an async request no other sync and async calls are allowed,
774 * and will result in %G_IO_ERROR_PENDING errors.
776 * A value of @count larger than %G_MAXSSIZE will cause a
777 * %G_IO_ERROR_INVALID_ARGUMENT error.
779 * On success, the number of bytes written will be passed to the
780 * @callback. It is not an error if this is not the same as the
781 * requested size, as it can happen e.g. on a partial I/O error,
782 * but generally we try to write as many bytes as requested.
784 * You are guaranteed that this method will never fail with
785 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
786 * method will just wait until this changes.
788 * Any outstanding I/O request with higher priority (lower numerical
789 * value) will be executed before an outstanding request with lower
790 * priority. Default priority is %G_PRIORITY_DEFAULT.
792 * The asynchronous methods have a default fallback that uses threads
793 * to implement asynchronicity, so they are optional for inheriting
794 * classes. However, if you override one you must override all.
796 * For the synchronous, blocking version of this function, see
797 * g_output_stream_write().
799 * Note that no copy of @buffer will be made, so it must stay valid
800 * until @callback is called. See g_output_stream_write_bytes_async()
801 * for a #GBytes version that will automatically hold a reference to
802 * the contents (without copying) for the duration of the call.
804 void
805 g_output_stream_write_async (GOutputStream *stream,
806 const void *buffer,
807 gsize count,
808 int io_priority,
809 GCancellable *cancellable,
810 GAsyncReadyCallback callback,
811 gpointer user_data)
813 GOutputStreamClass *class;
814 GError *error = NULL;
815 GTask *task;
817 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
818 g_return_if_fail (buffer != NULL);
820 task = g_task_new (stream, cancellable, callback, user_data);
821 g_task_set_source_tag (task, g_output_stream_write_async);
822 g_task_set_priority (task, io_priority);
824 if (count == 0)
826 g_task_return_int (task, 0);
827 g_object_unref (task);
828 return;
831 if (((gssize) count) < 0)
833 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
834 _("Too large count value passed to %s"),
835 G_STRFUNC);
836 g_object_unref (task);
837 return;
840 if (!g_output_stream_set_pending (stream, &error))
842 g_task_return_error (task, error);
843 g_object_unref (task);
844 return;
847 class = G_OUTPUT_STREAM_GET_CLASS (stream);
849 class->write_async (stream, buffer, count, io_priority, cancellable,
850 async_ready_write_callback_wrapper, task);
854 * g_output_stream_write_finish:
855 * @stream: a #GOutputStream.
856 * @result: a #GAsyncResult.
857 * @error: a #GError location to store the error occurring, or %NULL to
858 * ignore.
860 * Finishes a stream write operation.
862 * Returns: a #gssize containing the number of bytes written to the stream.
864 gssize
865 g_output_stream_write_finish (GOutputStream *stream,
866 GAsyncResult *result,
867 GError **error)
869 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
870 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
871 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_write_async), FALSE);
873 /* @result is always the GTask created by g_output_stream_write_async();
874 * we called class->write_finish() from async_ready_write_callback_wrapper.
876 return g_task_propagate_int (G_TASK (result), error);
879 typedef struct
881 const guint8 *buffer;
882 gsize to_write;
883 gsize bytes_written;
884 } AsyncWriteAll;
886 static void
887 free_async_write_all (gpointer data)
889 g_slice_free (AsyncWriteAll, data);
892 static void
893 write_all_callback (GObject *stream,
894 GAsyncResult *result,
895 gpointer user_data)
897 GTask *task = user_data;
898 AsyncWriteAll *data = g_task_get_task_data (task);
900 if (result)
902 GError *error = NULL;
903 gssize nwritten;
905 nwritten = g_output_stream_write_finish (G_OUTPUT_STREAM (stream), result, &error);
907 if (nwritten == -1)
909 g_task_return_error (task, error);
910 g_object_unref (task);
911 return;
914 g_assert_cmpint (nwritten, <=, data->to_write);
915 g_warn_if_fail (nwritten > 0);
917 data->to_write -= nwritten;
918 data->bytes_written += nwritten;
921 if (data->to_write == 0)
923 g_task_return_boolean (task, TRUE);
924 g_object_unref (task);
927 else
928 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
929 data->buffer + data->bytes_written,
930 data->to_write,
931 g_task_get_priority (task),
932 g_task_get_cancellable (task),
933 write_all_callback, task);
936 static void
937 write_all_async_thread (GTask *task,
938 gpointer source_object,
939 gpointer task_data,
940 GCancellable *cancellable)
942 GOutputStream *stream = source_object;
943 AsyncWriteAll *data = task_data;
944 GError *error = NULL;
946 if (g_output_stream_write_all (stream, data->buffer, data->to_write, &data->bytes_written,
947 g_task_get_cancellable (task), &error))
948 g_task_return_boolean (task, TRUE);
949 else
950 g_task_return_error (task, error);
954 * g_output_stream_write_all_async:
955 * @stream: A #GOutputStream
956 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write
957 * @count: the number of bytes to write
958 * @io_priority: the io priority of the request
959 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
960 * @callback: (scope async): callback to call when the request is satisfied
961 * @user_data: (closure): the data to pass to callback function
963 * Request an asynchronous write of @count bytes from @buffer into
964 * the stream. When the operation is finished @callback will be called.
965 * You can then call g_output_stream_write_all_finish() to get the result of the
966 * operation.
968 * This is the asynchronous version of g_output_stream_write_all().
970 * Call g_output_stream_write_all_finish() to collect the result.
972 * Any outstanding I/O request with higher priority (lower numerical
973 * value) will be executed before an outstanding request with lower
974 * priority. Default priority is %G_PRIORITY_DEFAULT.
976 * Note that no copy of @buffer will be made, so it must stay valid
977 * until @callback is called.
979 * Since: 2.44
981 void
982 g_output_stream_write_all_async (GOutputStream *stream,
983 const void *buffer,
984 gsize count,
985 int io_priority,
986 GCancellable *cancellable,
987 GAsyncReadyCallback callback,
988 gpointer user_data)
990 AsyncWriteAll *data;
991 GTask *task;
993 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
994 g_return_if_fail (buffer != NULL || count == 0);
996 task = g_task_new (stream, cancellable, callback, user_data);
997 data = g_slice_new0 (AsyncWriteAll);
998 data->buffer = buffer;
999 data->to_write = count;
1001 g_task_set_source_tag (task, g_output_stream_write_all_async);
1002 g_task_set_task_data (task, data, free_async_write_all);
1003 g_task_set_priority (task, io_priority);
1005 /* If async writes are going to be handled via the threadpool anyway
1006 * then we may as well do it with a single dispatch instead of
1007 * bouncing in and out.
1009 if (g_output_stream_async_write_is_via_threads (stream))
1011 g_task_run_in_thread (task, write_all_async_thread);
1012 g_object_unref (task);
1014 else
1015 write_all_callback (G_OBJECT (stream), NULL, task);
1019 * g_output_stream_write_all_finish:
1020 * @stream: a #GOutputStream
1021 * @result: a #GAsyncResult
1022 * @bytes_written: (out) (optional): location to store the number of bytes that was written to the stream
1023 * @error: a #GError location to store the error occurring, or %NULL to ignore.
1025 * Finishes an asynchronous stream write operation started with
1026 * g_output_stream_write_all_async().
1028 * As a special exception to the normal conventions for functions that
1029 * use #GError, if this function returns %FALSE (and sets @error) then
1030 * @bytes_written will be set to the number of bytes that were
1031 * successfully written before the error was encountered. This
1032 * functionality is only available from C. If you need it from another
1033 * language then you must write your own loop around
1034 * g_output_stream_write_async().
1036 * Returns: %TRUE on success, %FALSE if there was an error
1038 * Since: 2.44
1040 gboolean
1041 g_output_stream_write_all_finish (GOutputStream *stream,
1042 GAsyncResult *result,
1043 gsize *bytes_written,
1044 GError **error)
1046 GTask *task;
1048 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1049 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1051 task = G_TASK (result);
1053 if (bytes_written)
1055 AsyncWriteAll *data = (AsyncWriteAll *)g_task_get_task_data (task);
1057 *bytes_written = data->bytes_written;
1060 return g_task_propagate_boolean (task, error);
1063 static void
1064 write_bytes_callback (GObject *stream,
1065 GAsyncResult *result,
1066 gpointer user_data)
1068 GTask *task = user_data;
1069 GError *error = NULL;
1070 gssize nwrote;
1072 nwrote = g_output_stream_write_finish (G_OUTPUT_STREAM (stream),
1073 result, &error);
1074 if (nwrote == -1)
1075 g_task_return_error (task, error);
1076 else
1077 g_task_return_int (task, nwrote);
1078 g_object_unref (task);
1082 * g_output_stream_write_bytes_async:
1083 * @stream: A #GOutputStream.
1084 * @bytes: The bytes to write
1085 * @io_priority: the io priority of the request.
1086 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
1087 * @callback: (scope async): callback to call when the request is satisfied
1088 * @user_data: (closure): the data to pass to callback function
1090 * This function is similar to g_output_stream_write_async(), but
1091 * takes a #GBytes as input. Due to the refcounted nature of #GBytes,
1092 * this allows the stream to avoid taking a copy of the data.
1094 * However, note that this function may still perform partial writes,
1095 * just like g_output_stream_write_async(). If that occurs, to continue
1096 * writing, you will need to create a new #GBytes containing just the
1097 * remaining bytes, using g_bytes_new_from_bytes(). Passing the same
1098 * #GBytes instance multiple times potentially can result in duplicated
1099 * data in the output stream.
1101 * For the synchronous, blocking version of this function, see
1102 * g_output_stream_write_bytes().
1104 void
1105 g_output_stream_write_bytes_async (GOutputStream *stream,
1106 GBytes *bytes,
1107 int io_priority,
1108 GCancellable *cancellable,
1109 GAsyncReadyCallback callback,
1110 gpointer user_data)
1112 GTask *task;
1113 gsize size;
1114 gconstpointer data;
1116 data = g_bytes_get_data (bytes, &size);
1118 task = g_task_new (stream, cancellable, callback, user_data);
1119 g_task_set_source_tag (task, g_output_stream_write_bytes_async);
1120 g_task_set_task_data (task, g_bytes_ref (bytes),
1121 (GDestroyNotify) g_bytes_unref);
1123 g_output_stream_write_async (stream,
1124 data, size,
1125 io_priority,
1126 cancellable,
1127 write_bytes_callback,
1128 task);
1132 * g_output_stream_write_bytes_finish:
1133 * @stream: a #GOutputStream.
1134 * @result: a #GAsyncResult.
1135 * @error: a #GError location to store the error occurring, or %NULL to
1136 * ignore.
1138 * Finishes a stream write-from-#GBytes operation.
1140 * Returns: a #gssize containing the number of bytes written to the stream.
1142 gssize
1143 g_output_stream_write_bytes_finish (GOutputStream *stream,
1144 GAsyncResult *result,
1145 GError **error)
1147 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
1148 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1150 return g_task_propagate_int (G_TASK (result), error);
1153 static void
1154 async_ready_splice_callback_wrapper (GObject *source_object,
1155 GAsyncResult *res,
1156 gpointer _data)
1158 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1159 GOutputStreamClass *class;
1160 GTask *task = _data;
1161 gssize nspliced;
1162 GError *error = NULL;
1164 g_output_stream_clear_pending (stream);
1166 if (g_async_result_legacy_propagate_error (res, &error))
1167 nspliced = -1;
1168 else
1170 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1171 nspliced = class->splice_finish (stream, res, &error);
1174 if (nspliced >= 0)
1175 g_task_return_int (task, nspliced);
1176 else
1177 g_task_return_error (task, error);
1178 g_object_unref (task);
1182 * g_output_stream_splice_async:
1183 * @stream: a #GOutputStream.
1184 * @source: a #GInputStream.
1185 * @flags: a set of #GOutputStreamSpliceFlags.
1186 * @io_priority: the io priority of the request.
1187 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
1188 * @callback: (scope async): a #GAsyncReadyCallback.
1189 * @user_data: (closure): user data passed to @callback.
1191 * Splices a stream asynchronously.
1192 * When the operation is finished @callback will be called.
1193 * You can then call g_output_stream_splice_finish() to get the
1194 * result of the operation.
1196 * For the synchronous, blocking version of this function, see
1197 * g_output_stream_splice().
1199 void
1200 g_output_stream_splice_async (GOutputStream *stream,
1201 GInputStream *source,
1202 GOutputStreamSpliceFlags flags,
1203 int io_priority,
1204 GCancellable *cancellable,
1205 GAsyncReadyCallback callback,
1206 gpointer user_data)
1208 GOutputStreamClass *class;
1209 GTask *task;
1210 GError *error = NULL;
1212 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1213 g_return_if_fail (G_IS_INPUT_STREAM (source));
1215 task = g_task_new (stream, cancellable, callback, user_data);
1216 g_task_set_source_tag (task, g_output_stream_splice_async);
1217 g_task_set_priority (task, io_priority);
1218 g_task_set_task_data (task, g_object_ref (source), g_object_unref);
1220 if (g_input_stream_is_closed (source))
1222 g_task_return_new_error (task,
1223 G_IO_ERROR, G_IO_ERROR_CLOSED,
1224 _("Source stream is already closed"));
1225 g_object_unref (task);
1226 return;
1229 if (!g_output_stream_set_pending (stream, &error))
1231 g_task_return_error (task, error);
1232 g_object_unref (task);
1233 return;
1236 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1238 class->splice_async (stream, source, flags, io_priority, cancellable,
1239 async_ready_splice_callback_wrapper, task);
1243 * g_output_stream_splice_finish:
1244 * @stream: a #GOutputStream.
1245 * @result: a #GAsyncResult.
1246 * @error: a #GError location to store the error occurring, or %NULL to
1247 * ignore.
1249 * Finishes an asynchronous stream splice operation.
1251 * Returns: a #gssize of the number of bytes spliced. Note that if the
1252 * number of bytes spliced is greater than %G_MAXSSIZE, then that
1253 * will be returned, and there is no way to determine the actual
1254 * number of bytes spliced.
1256 gssize
1257 g_output_stream_splice_finish (GOutputStream *stream,
1258 GAsyncResult *result,
1259 GError **error)
1261 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1262 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1263 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_splice_async), FALSE);
1265 /* @result is always the GTask created by g_output_stream_splice_async();
1266 * we called class->splice_finish() from async_ready_splice_callback_wrapper.
1268 return g_task_propagate_int (G_TASK (result), error);
1271 static void
1272 async_ready_flush_callback_wrapper (GObject *source_object,
1273 GAsyncResult *res,
1274 gpointer user_data)
1276 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1277 GOutputStreamClass *class;
1278 GTask *task = user_data;
1279 gboolean flushed;
1280 GError *error = NULL;
1282 g_output_stream_clear_pending (stream);
1284 if (g_async_result_legacy_propagate_error (res, &error))
1285 flushed = FALSE;
1286 else
1288 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1289 flushed = class->flush_finish (stream, res, &error);
1292 if (flushed)
1293 g_task_return_boolean (task, TRUE);
1294 else
1295 g_task_return_error (task, error);
1296 g_object_unref (task);
1300 * g_output_stream_flush_async:
1301 * @stream: a #GOutputStream.
1302 * @io_priority: the io priority of the request.
1303 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
1304 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1305 * @user_data: (closure): the data to pass to callback function
1307 * Forces an asynchronous write of all user-space buffered data for
1308 * the given @stream.
1309 * For behaviour details see g_output_stream_flush().
1311 * When the operation is finished @callback will be
1312 * called. You can then call g_output_stream_flush_finish() to get the
1313 * result of the operation.
1315 void
1316 g_output_stream_flush_async (GOutputStream *stream,
1317 int io_priority,
1318 GCancellable *cancellable,
1319 GAsyncReadyCallback callback,
1320 gpointer user_data)
1322 GOutputStreamClass *class;
1323 GTask *task;
1324 GError *error = NULL;
1326 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1328 task = g_task_new (stream, cancellable, callback, user_data);
1329 g_task_set_source_tag (task, g_output_stream_flush_async);
1330 g_task_set_priority (task, io_priority);
1332 if (!g_output_stream_set_pending (stream, &error))
1334 g_task_return_error (task, error);
1335 g_object_unref (task);
1336 return;
1339 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1341 if (class->flush_async == NULL)
1343 g_output_stream_clear_pending (stream);
1344 g_task_return_boolean (task, TRUE);
1345 g_object_unref (task);
1346 return;
1349 class->flush_async (stream, io_priority, cancellable,
1350 async_ready_flush_callback_wrapper, task);
1354 * g_output_stream_flush_finish:
1355 * @stream: a #GOutputStream.
1356 * @result: a GAsyncResult.
1357 * @error: a #GError location to store the error occurring, or %NULL to
1358 * ignore.
1360 * Finishes flushing an output stream.
1362 * Returns: %TRUE if flush operation succeeded, %FALSE otherwise.
1364 gboolean
1365 g_output_stream_flush_finish (GOutputStream *stream,
1366 GAsyncResult *result,
1367 GError **error)
1369 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1370 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1371 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_flush_async), FALSE);
1373 /* @result is always the GTask created by g_output_stream_flush_async();
1374 * we called class->flush_finish() from async_ready_flush_callback_wrapper.
1376 return g_task_propagate_boolean (G_TASK (result), error);
1380 static void
1381 async_ready_close_callback_wrapper (GObject *source_object,
1382 GAsyncResult *res,
1383 gpointer user_data)
1385 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1386 GOutputStreamClass *class;
1387 GTask *task = user_data;
1388 GError *error = g_task_get_task_data (task);
1390 stream->priv->closing = FALSE;
1391 stream->priv->closed = TRUE;
1393 if (!error && !g_async_result_legacy_propagate_error (res, &error))
1395 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1397 class->close_finish (stream, res,
1398 error ? NULL : &error);
1401 if (error != NULL)
1402 g_task_return_error (task, error);
1403 else
1404 g_task_return_boolean (task, TRUE);
1405 g_object_unref (task);
1408 static void
1409 async_ready_close_flushed_callback_wrapper (GObject *source_object,
1410 GAsyncResult *res,
1411 gpointer user_data)
1413 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1414 GOutputStreamClass *class;
1415 GTask *task = user_data;
1416 GError *error = NULL;
1418 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1420 if (!g_async_result_legacy_propagate_error (res, &error))
1422 class->flush_finish (stream, res, &error);
1425 /* propagate the possible error */
1426 if (error)
1427 g_task_set_task_data (task, error, NULL);
1429 /* we still close, even if there was a flush error */
1430 class->close_async (stream,
1431 g_task_get_priority (task),
1432 g_task_get_cancellable (task),
1433 async_ready_close_callback_wrapper, task);
1436 static void
1437 real_close_async_cb (GObject *source_object,
1438 GAsyncResult *res,
1439 gpointer user_data)
1441 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1442 GTask *task = user_data;
1443 GError *error = NULL;
1444 gboolean ret;
1446 g_output_stream_clear_pending (stream);
1448 ret = g_output_stream_internal_close_finish (stream, res, &error);
1450 if (error != NULL)
1451 g_task_return_error (task, error);
1452 else
1453 g_task_return_boolean (task, ret);
1455 g_object_unref (task);
1459 * g_output_stream_close_async:
1460 * @stream: A #GOutputStream.
1461 * @io_priority: the io priority of the request.
1462 * @cancellable: (nullable): optional cancellable object
1463 * @callback: (scope async): callback to call when the request is satisfied
1464 * @user_data: (closure): the data to pass to callback function
1466 * Requests an asynchronous close of the stream, releasing resources
1467 * related to it. When the operation is finished @callback will be
1468 * called. You can then call g_output_stream_close_finish() to get
1469 * the result of the operation.
1471 * For behaviour details see g_output_stream_close().
1473 * The asynchronous methods have a default fallback that uses threads
1474 * to implement asynchronicity, so they are optional for inheriting
1475 * classes. However, if you override one you must override all.
1477 void
1478 g_output_stream_close_async (GOutputStream *stream,
1479 int io_priority,
1480 GCancellable *cancellable,
1481 GAsyncReadyCallback callback,
1482 gpointer user_data)
1484 GTask *task;
1485 GError *error = NULL;
1487 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1489 task = g_task_new (stream, cancellable, callback, user_data);
1490 g_task_set_source_tag (task, g_output_stream_close_async);
1491 g_task_set_priority (task, io_priority);
1493 if (!g_output_stream_set_pending (stream, &error))
1495 g_task_return_error (task, error);
1496 g_object_unref (task);
1497 return;
1500 g_output_stream_internal_close_async (stream, io_priority, cancellable,
1501 real_close_async_cb, task);
1504 /* Must always be called inside
1505 * g_output_stream_set_pending()/g_output_stream_clear_pending().
1507 void
1508 g_output_stream_internal_close_async (GOutputStream *stream,
1509 int io_priority,
1510 GCancellable *cancellable,
1511 GAsyncReadyCallback callback,
1512 gpointer user_data)
1514 GOutputStreamClass *class;
1515 GTask *task;
1517 task = g_task_new (stream, cancellable, callback, user_data);
1518 g_task_set_source_tag (task, g_output_stream_internal_close_async);
1519 g_task_set_priority (task, io_priority);
1521 if (stream->priv->closed)
1523 g_task_return_boolean (task, TRUE);
1524 g_object_unref (task);
1525 return;
1528 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1529 stream->priv->closing = TRUE;
1531 /* Call close_async directly if there is no need to flush, or if the flush
1532 can be done sync (in the output stream async close thread) */
1533 if (class->flush_async == NULL ||
1534 (class->flush_async == g_output_stream_real_flush_async &&
1535 (class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
1537 class->close_async (stream, io_priority, cancellable,
1538 async_ready_close_callback_wrapper, task);
1540 else
1542 /* First do an async flush, then do the async close in the callback
1543 wrapper (see async_ready_close_flushed_callback_wrapper) */
1544 class->flush_async (stream, io_priority, cancellable,
1545 async_ready_close_flushed_callback_wrapper, task);
1549 static gboolean
1550 g_output_stream_internal_close_finish (GOutputStream *stream,
1551 GAsyncResult *result,
1552 GError **error)
1554 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1555 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1556 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_internal_close_async), FALSE);
1558 return g_task_propagate_boolean (G_TASK (result), error);
1562 * g_output_stream_close_finish:
1563 * @stream: a #GOutputStream.
1564 * @result: a #GAsyncResult.
1565 * @error: a #GError location to store the error occurring, or %NULL to
1566 * ignore.
1568 * Closes an output stream.
1570 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1572 gboolean
1573 g_output_stream_close_finish (GOutputStream *stream,
1574 GAsyncResult *result,
1575 GError **error)
1577 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1578 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1579 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_close_async), FALSE);
1581 /* @result is always the GTask created by g_output_stream_close_async();
1582 * we called class->close_finish() from async_ready_close_callback_wrapper.
1584 return g_task_propagate_boolean (G_TASK (result), error);
1588 * g_output_stream_is_closed:
1589 * @stream: a #GOutputStream.
1591 * Checks if an output stream has already been closed.
1593 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1595 gboolean
1596 g_output_stream_is_closed (GOutputStream *stream)
1598 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1600 return stream->priv->closed;
1604 * g_output_stream_is_closing:
1605 * @stream: a #GOutputStream.
1607 * Checks if an output stream is being closed. This can be
1608 * used inside e.g. a flush implementation to see if the
1609 * flush (or other i/o operation) is called from within
1610 * the closing operation.
1612 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
1614 * Since: 2.24
1616 gboolean
1617 g_output_stream_is_closing (GOutputStream *stream)
1619 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1621 return stream->priv->closing;
1625 * g_output_stream_has_pending:
1626 * @stream: a #GOutputStream.
1628 * Checks if an output stream has pending actions.
1630 * Returns: %TRUE if @stream has pending actions.
1632 gboolean
1633 g_output_stream_has_pending (GOutputStream *stream)
1635 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1637 return stream->priv->pending;
1641 * g_output_stream_set_pending:
1642 * @stream: a #GOutputStream.
1643 * @error: a #GError location to store the error occurring, or %NULL to
1644 * ignore.
1646 * Sets @stream to have actions pending. If the pending flag is
1647 * already set or @stream is closed, it will return %FALSE and set
1648 * @error.
1650 * Returns: %TRUE if pending was previously unset and is now set.
1652 gboolean
1653 g_output_stream_set_pending (GOutputStream *stream,
1654 GError **error)
1656 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1658 if (stream->priv->closed)
1660 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1661 _("Stream is already closed"));
1662 return FALSE;
1665 if (stream->priv->pending)
1667 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1668 /* Translators: This is an error you get if there is
1669 * already an operation running against this stream when
1670 * you try to start one */
1671 _("Stream has outstanding operation"));
1672 return FALSE;
1675 stream->priv->pending = TRUE;
1676 return TRUE;
1680 * g_output_stream_clear_pending:
1681 * @stream: output stream
1683 * Clears the pending flag on @stream.
1685 void
1686 g_output_stream_clear_pending (GOutputStream *stream)
1688 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1690 stream->priv->pending = FALSE;
1693 /*< internal >
1694 * g_output_stream_async_write_is_via_threads:
1695 * @stream: a #GOutputStream.
1697 * Checks if an output stream's write_async function uses threads.
1699 * Returns: %TRUE if @stream's write_async function uses threads.
1701 gboolean
1702 g_output_stream_async_write_is_via_threads (GOutputStream *stream)
1704 GOutputStreamClass *class;
1706 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1708 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1710 return (class->write_async == g_output_stream_real_write_async &&
1711 !(G_IS_POLLABLE_OUTPUT_STREAM (stream) &&
1712 g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (stream))));
1715 /*< internal >
1716 * g_output_stream_async_close_is_via_threads:
1717 * @stream: output stream
1719 * Checks if an output stream's close_async function uses threads.
1721 * Returns: %TRUE if @stream's close_async function uses threads.
1723 gboolean
1724 g_output_stream_async_close_is_via_threads (GOutputStream *stream)
1726 GOutputStreamClass *class;
1728 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1730 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1732 return class->close_async == g_output_stream_real_close_async;
1735 /********************************************
1736 * Default implementation of async ops *
1737 ********************************************/
1739 typedef struct {
1740 const void *buffer;
1741 gsize count_requested;
1742 gssize count_written;
1743 } WriteData;
1745 static void
1746 free_write_data (WriteData *op)
1748 g_slice_free (WriteData, op);
1751 static void
1752 write_async_thread (GTask *task,
1753 gpointer source_object,
1754 gpointer task_data,
1755 GCancellable *cancellable)
1757 GOutputStream *stream = source_object;
1758 WriteData *op = task_data;
1759 GOutputStreamClass *class;
1760 GError *error = NULL;
1761 gssize count_written;
1763 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1764 count_written = class->write_fn (stream, op->buffer, op->count_requested,
1765 cancellable, &error);
1766 if (count_written == -1)
1767 g_task_return_error (task, error);
1768 else
1769 g_task_return_int (task, count_written);
1772 static void write_async_pollable (GPollableOutputStream *stream,
1773 GTask *task);
1775 static gboolean
1776 write_async_pollable_ready (GPollableOutputStream *stream,
1777 gpointer user_data)
1779 GTask *task = user_data;
1781 write_async_pollable (stream, task);
1782 return FALSE;
1785 static void
1786 write_async_pollable (GPollableOutputStream *stream,
1787 GTask *task)
1789 GError *error = NULL;
1790 WriteData *op = g_task_get_task_data (task);
1791 gssize count_written;
1793 if (g_task_return_error_if_cancelled (task))
1794 return;
1796 count_written = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
1797 write_nonblocking (stream, op->buffer, op->count_requested, &error);
1799 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1801 GSource *source;
1803 g_error_free (error);
1805 source = g_pollable_output_stream_create_source (stream,
1806 g_task_get_cancellable (task));
1807 g_task_attach_source (task, source,
1808 (GSourceFunc) write_async_pollable_ready);
1809 g_source_unref (source);
1810 return;
1813 if (count_written == -1)
1814 g_task_return_error (task, error);
1815 else
1816 g_task_return_int (task, count_written);
1819 static void
1820 g_output_stream_real_write_async (GOutputStream *stream,
1821 const void *buffer,
1822 gsize count,
1823 int io_priority,
1824 GCancellable *cancellable,
1825 GAsyncReadyCallback callback,
1826 gpointer user_data)
1828 GTask *task;
1829 WriteData *op;
1831 op = g_slice_new0 (WriteData);
1832 task = g_task_new (stream, cancellable, callback, user_data);
1833 g_task_set_check_cancellable (task, FALSE);
1834 g_task_set_task_data (task, op, (GDestroyNotify) free_write_data);
1835 op->buffer = buffer;
1836 op->count_requested = count;
1838 if (!g_output_stream_async_write_is_via_threads (stream))
1839 write_async_pollable (G_POLLABLE_OUTPUT_STREAM (stream), task);
1840 else
1841 g_task_run_in_thread (task, write_async_thread);
1842 g_object_unref (task);
1845 static gssize
1846 g_output_stream_real_write_finish (GOutputStream *stream,
1847 GAsyncResult *result,
1848 GError **error)
1850 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1852 return g_task_propagate_int (G_TASK (result), error);
1855 typedef struct {
1856 GInputStream *source;
1857 GOutputStreamSpliceFlags flags;
1858 gssize n_read;
1859 gssize n_written;
1860 gsize bytes_copied;
1861 GError *error;
1862 guint8 *buffer;
1863 } SpliceData;
1865 static void
1866 free_splice_data (SpliceData *op)
1868 g_clear_pointer (&op->buffer, g_free);
1869 g_object_unref (op->source);
1870 g_clear_error (&op->error);
1871 g_free (op);
1874 static void
1875 real_splice_async_complete_cb (GTask *task)
1877 SpliceData *op = g_task_get_task_data (task);
1879 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE &&
1880 !g_input_stream_is_closed (op->source))
1881 return;
1883 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET &&
1884 !g_output_stream_is_closed (g_task_get_source_object (task)))
1885 return;
1887 if (op->error != NULL)
1889 g_task_return_error (task, op->error);
1890 op->error = NULL;
1892 else
1894 g_task_return_int (task, op->bytes_copied);
1897 g_object_unref (task);
1900 static void
1901 real_splice_async_close_input_cb (GObject *source,
1902 GAsyncResult *res,
1903 gpointer user_data)
1905 GTask *task = user_data;
1907 g_input_stream_close_finish (G_INPUT_STREAM (source), res, NULL);
1909 real_splice_async_complete_cb (task);
1912 static void
1913 real_splice_async_close_output_cb (GObject *source,
1914 GAsyncResult *res,
1915 gpointer user_data)
1917 GTask *task = G_TASK (user_data);
1918 SpliceData *op = g_task_get_task_data (task);
1919 GError **error = (op->error == NULL) ? &op->error : NULL;
1921 g_output_stream_internal_close_finish (G_OUTPUT_STREAM (source), res, error);
1923 real_splice_async_complete_cb (task);
1926 static void
1927 real_splice_async_complete (GTask *task)
1929 SpliceData *op = g_task_get_task_data (task);
1930 gboolean done = TRUE;
1932 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
1934 done = FALSE;
1935 g_input_stream_close_async (op->source, g_task_get_priority (task),
1936 g_task_get_cancellable (task),
1937 real_splice_async_close_input_cb, task);
1940 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
1942 done = FALSE;
1943 g_output_stream_internal_close_async (g_task_get_source_object (task),
1944 g_task_get_priority (task),
1945 g_task_get_cancellable (task),
1946 real_splice_async_close_output_cb,
1947 task);
1950 if (done)
1951 real_splice_async_complete_cb (task);
1954 static void real_splice_async_read_cb (GObject *source,
1955 GAsyncResult *res,
1956 gpointer user_data);
1958 static void
1959 real_splice_async_write_cb (GObject *source,
1960 GAsyncResult *res,
1961 gpointer user_data)
1963 GOutputStreamClass *class;
1964 GTask *task = G_TASK (user_data);
1965 SpliceData *op = g_task_get_task_data (task);
1966 gssize ret;
1968 class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
1970 ret = class->write_finish (G_OUTPUT_STREAM (source), res, &op->error);
1972 if (ret == -1)
1974 real_splice_async_complete (task);
1975 return;
1978 op->n_written += ret;
1979 op->bytes_copied += ret;
1980 if (op->bytes_copied > G_MAXSSIZE)
1981 op->bytes_copied = G_MAXSSIZE;
1983 if (op->n_written < op->n_read)
1985 class->write_async (g_task_get_source_object (task),
1986 op->buffer + op->n_written,
1987 op->n_read - op->n_written,
1988 g_task_get_priority (task),
1989 g_task_get_cancellable (task),
1990 real_splice_async_write_cb, task);
1991 return;
1994 g_input_stream_read_async (op->source, op->buffer, 8192,
1995 g_task_get_priority (task),
1996 g_task_get_cancellable (task),
1997 real_splice_async_read_cb, task);
2000 static void
2001 real_splice_async_read_cb (GObject *source,
2002 GAsyncResult *res,
2003 gpointer user_data)
2005 GOutputStreamClass *class;
2006 GTask *task = G_TASK (user_data);
2007 SpliceData *op = g_task_get_task_data (task);
2008 gssize ret;
2010 class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
2012 ret = g_input_stream_read_finish (op->source, res, &op->error);
2013 if (ret == -1 || ret == 0)
2015 real_splice_async_complete (task);
2016 return;
2019 op->n_read = ret;
2020 op->n_written = 0;
2022 class->write_async (g_task_get_source_object (task), op->buffer,
2023 op->n_read, g_task_get_priority (task),
2024 g_task_get_cancellable (task),
2025 real_splice_async_write_cb, task);
2028 static void
2029 splice_async_thread (GTask *task,
2030 gpointer source_object,
2031 gpointer task_data,
2032 GCancellable *cancellable)
2034 GOutputStream *stream = source_object;
2035 SpliceData *op = task_data;
2036 GOutputStreamClass *class;
2037 GError *error = NULL;
2038 gssize bytes_copied;
2040 class = G_OUTPUT_STREAM_GET_CLASS (stream);
2042 bytes_copied = class->splice (stream,
2043 op->source,
2044 op->flags,
2045 cancellable,
2046 &error);
2047 if (bytes_copied == -1)
2048 g_task_return_error (task, error);
2049 else
2050 g_task_return_int (task, bytes_copied);
2053 static void
2054 g_output_stream_real_splice_async (GOutputStream *stream,
2055 GInputStream *source,
2056 GOutputStreamSpliceFlags flags,
2057 int io_priority,
2058 GCancellable *cancellable,
2059 GAsyncReadyCallback callback,
2060 gpointer user_data)
2062 GTask *task;
2063 SpliceData *op;
2065 op = g_new0 (SpliceData, 1);
2066 task = g_task_new (stream, cancellable, callback, user_data);
2067 g_task_set_task_data (task, op, (GDestroyNotify)free_splice_data);
2068 op->flags = flags;
2069 op->source = g_object_ref (source);
2071 if (g_input_stream_async_read_is_via_threads (source) &&
2072 g_output_stream_async_write_is_via_threads (stream))
2074 g_task_run_in_thread (task, splice_async_thread);
2075 g_object_unref (task);
2077 else
2079 op->buffer = g_malloc (8192);
2080 g_input_stream_read_async (op->source, op->buffer, 8192,
2081 g_task_get_priority (task),
2082 g_task_get_cancellable (task),
2083 real_splice_async_read_cb, task);
2087 static gssize
2088 g_output_stream_real_splice_finish (GOutputStream *stream,
2089 GAsyncResult *result,
2090 GError **error)
2092 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2094 return g_task_propagate_int (G_TASK (result), error);
2098 static void
2099 flush_async_thread (GTask *task,
2100 gpointer source_object,
2101 gpointer task_data,
2102 GCancellable *cancellable)
2104 GOutputStream *stream = source_object;
2105 GOutputStreamClass *class;
2106 gboolean result;
2107 GError *error = NULL;
2109 class = G_OUTPUT_STREAM_GET_CLASS (stream);
2110 result = TRUE;
2111 if (class->flush)
2112 result = class->flush (stream, cancellable, &error);
2114 if (result)
2115 g_task_return_boolean (task, TRUE);
2116 else
2117 g_task_return_error (task, error);
2120 static void
2121 g_output_stream_real_flush_async (GOutputStream *stream,
2122 int io_priority,
2123 GCancellable *cancellable,
2124 GAsyncReadyCallback callback,
2125 gpointer user_data)
2127 GTask *task;
2129 task = g_task_new (stream, cancellable, callback, user_data);
2130 g_task_set_priority (task, io_priority);
2131 g_task_run_in_thread (task, flush_async_thread);
2132 g_object_unref (task);
2135 static gboolean
2136 g_output_stream_real_flush_finish (GOutputStream *stream,
2137 GAsyncResult *result,
2138 GError **error)
2140 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2142 return g_task_propagate_boolean (G_TASK (result), error);
2145 static void
2146 close_async_thread (GTask *task,
2147 gpointer source_object,
2148 gpointer task_data,
2149 GCancellable *cancellable)
2151 GOutputStream *stream = source_object;
2152 GOutputStreamClass *class;
2153 GError *error = NULL;
2154 gboolean result = TRUE;
2156 class = G_OUTPUT_STREAM_GET_CLASS (stream);
2158 /* Do a flush here if there is a flush function, and we did not have to do
2159 * an async flush before (see g_output_stream_close_async)
2161 if (class->flush != NULL &&
2162 (class->flush_async == NULL ||
2163 class->flush_async == g_output_stream_real_flush_async))
2165 result = class->flush (stream, cancellable, &error);
2168 /* Auto handling of cancelation disabled, and ignore
2169 cancellation, since we want to close things anyway, although
2170 possibly in a quick-n-dirty way. At least we never want to leak
2171 open handles */
2173 if (class->close_fn)
2175 /* Make sure to close, even if the flush failed (see sync close) */
2176 if (!result)
2177 class->close_fn (stream, cancellable, NULL);
2178 else
2179 result = class->close_fn (stream, cancellable, &error);
2182 if (result)
2183 g_task_return_boolean (task, TRUE);
2184 else
2185 g_task_return_error (task, error);
2188 static void
2189 g_output_stream_real_close_async (GOutputStream *stream,
2190 int io_priority,
2191 GCancellable *cancellable,
2192 GAsyncReadyCallback callback,
2193 gpointer user_data)
2195 GTask *task;
2197 task = g_task_new (stream, cancellable, callback, user_data);
2198 g_task_set_priority (task, io_priority);
2199 g_task_run_in_thread (task, close_async_thread);
2200 g_object_unref (task);
2203 static gboolean
2204 g_output_stream_real_close_finish (GOutputStream *stream,
2205 GAsyncResult *result,
2206 GError **error)
2208 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2210 return g_task_propagate_boolean (G_TASK (result), error);