2007-11-26 17:13:05 +01:00
|
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2006-2007 Red Hat, Inc.
|
|
|
|
|
*
|
2022-05-18 10:12:45 +02:00
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
|
*
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-05-27 18:21:30 +02:00
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
2014-01-23 12:58:29 +01:00
|
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
|
|
|
|
* Author: Alexander Larsson <alexl@redhat.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2008-06-22 17:10:51 +02:00
|
|
|
|
#include "config.h"
|
2013-11-25 23:26:21 +01:00
|
|
|
|
#include <string.h>
|
2007-11-26 17:13:05 +01:00
|
|
|
|
#include "goutputstream.h"
|
2008-07-01 08:32:35 +02:00
|
|
|
|
#include "gcancellable.h"
|
|
|
|
|
#include "gasyncresult.h"
|
2012-08-02 21:49:59 +02:00
|
|
|
|
#include "gtask.h"
|
2008-07-01 08:32:35 +02:00
|
|
|
|
#include "ginputstream.h"
|
|
|
|
|
#include "gioerror.h"
|
2013-02-24 00:42:49 +01:00
|
|
|
|
#include "gioprivate.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
|
#include "glibintl.h"
|
2012-02-04 22:46:29 +01:00
|
|
|
|
#include "gpollableoutputstream.h"
|
2007-11-28 13:39:07 +01:00
|
|
|
|
|
2007-11-27 15:00:13 +01:00
|
|
|
|
/**
|
2023-10-23 15:39:59 +02:00
|
|
|
|
* GOutputStream:
|
2007-12-09 16:51:12 +01:00
|
|
|
|
*
|
2023-10-23 15:39:59 +02:00
|
|
|
|
* `GOutputStream` is a base class for implementing streaming output.
|
|
|
|
|
*
|
|
|
|
|
* It has functions to write to a stream ([method@Gio.OutputStream.write]),
|
|
|
|
|
* to close a stream ([method@Gio.OutputStream.close]) and to flush pending
|
|
|
|
|
* writes ([method@Gio.OutputStream.flush]).
|
2007-11-27 15:00:13 +01:00
|
|
|
|
*
|
2008-08-13 07:35:10 +02:00
|
|
|
|
* To copy the content of an input stream to an output stream without
|
2023-10-23 15:39:59 +02:00
|
|
|
|
* manually handling the reads and writes, use [method@Gio.OutputStream.splice].
|
2014-09-26 15:01:16 +02:00
|
|
|
|
*
|
2023-10-23 15:39:59 +02:00
|
|
|
|
* See the documentation for [class@Gio.IOStream] for details of thread safety
|
|
|
|
|
* of streaming APIs.
|
2008-08-13 07:35:10 +02:00
|
|
|
|
*
|
|
|
|
|
* All of these functions have async variants too.
|
2023-10-23 15:39:59 +02:00
|
|
|
|
*
|
|
|
|
|
* All classes derived from `GOutputStream` *should* implement synchronous
|
|
|
|
|
* writing, splicing, flushing and closing streams, but *may* implement
|
|
|
|
|
* asynchronous versions.
|
2007-11-27 15:00:13 +01:00
|
|
|
|
**/
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
struct _GOutputStreamPrivate {
|
|
|
|
|
guint closed : 1;
|
|
|
|
|
guint pending : 1;
|
2009-11-23 15:02:50 +01:00
|
|
|
|
guint closing : 1;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
GAsyncReadyCallback outstanding_callback;
|
|
|
|
|
};
|
|
|
|
|
|
2013-06-11 01:29:58 +02:00
|
|
|
|
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GOutputStream, g_output_stream, G_TYPE_OBJECT)
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
static gssize g_output_stream_real_splice (GOutputStream *stream,
|
|
|
|
|
GInputStream *source,
|
|
|
|
|
GOutputStreamSpliceFlags flags,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error);
|
|
|
|
|
static void g_output_stream_real_write_async (GOutputStream *stream,
|
|
|
|
|
const void *buffer,
|
|
|
|
|
gsize count,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer data);
|
|
|
|
|
static gssize g_output_stream_real_write_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error);
|
2018-09-13 12:10:36 +02:00
|
|
|
|
static gboolean g_output_stream_real_writev (GOutputStream *stream,
|
|
|
|
|
const GOutputVector *vectors,
|
|
|
|
|
gsize n_vectors,
|
|
|
|
|
gsize *bytes_written,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error);
|
|
|
|
|
static void g_output_stream_real_writev_async (GOutputStream *stream,
|
|
|
|
|
const GOutputVector *vectors,
|
|
|
|
|
gsize n_vectors,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer data);
|
|
|
|
|
static gboolean g_output_stream_real_writev_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
gsize *bytes_written,
|
|
|
|
|
GError **error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
static void g_output_stream_real_splice_async (GOutputStream *stream,
|
|
|
|
|
GInputStream *source,
|
|
|
|
|
GOutputStreamSpliceFlags flags,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer data);
|
|
|
|
|
static gssize g_output_stream_real_splice_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error);
|
|
|
|
|
static void g_output_stream_real_flush_async (GOutputStream *stream,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer data);
|
|
|
|
|
static gboolean g_output_stream_real_flush_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error);
|
|
|
|
|
static void g_output_stream_real_close_async (GOutputStream *stream,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer data);
|
|
|
|
|
static gboolean g_output_stream_real_close_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error);
|
2013-02-24 16:09:05 +01:00
|
|
|
|
static gboolean g_output_stream_internal_close (GOutputStream *stream,
|
2011-09-19 10:13:52 +02:00
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error);
|
2013-02-18 14:58:58 +01:00
|
|
|
|
static void g_output_stream_internal_close_async (GOutputStream *stream,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer data);
|
|
|
|
|
static gboolean g_output_stream_internal_close_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_output_stream_dispose (GObject *object)
|
|
|
|
|
{
|
|
|
|
|
GOutputStream *stream;
|
|
|
|
|
|
|
|
|
|
stream = G_OUTPUT_STREAM (object);
|
|
|
|
|
|
|
|
|
|
if (!stream->priv->closed)
|
|
|
|
|
g_output_stream_close (stream, NULL, NULL);
|
2008-06-16 11:54:04 +02:00
|
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_output_stream_class_init (GOutputStreamClass *klass)
|
|
|
|
|
{
|
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
2013-06-11 01:29:58 +02:00
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
gobject_class->dispose = g_output_stream_dispose;
|
|
|
|
|
|
|
|
|
|
klass->splice = g_output_stream_real_splice;
|
|
|
|
|
|
|
|
|
|
klass->write_async = g_output_stream_real_write_async;
|
|
|
|
|
klass->write_finish = g_output_stream_real_write_finish;
|
2018-09-13 12:10:36 +02:00
|
|
|
|
klass->writev_fn = g_output_stream_real_writev;
|
|
|
|
|
klass->writev_async = g_output_stream_real_writev_async;
|
|
|
|
|
klass->writev_finish = g_output_stream_real_writev_finish;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
klass->splice_async = g_output_stream_real_splice_async;
|
|
|
|
|
klass->splice_finish = g_output_stream_real_splice_finish;
|
|
|
|
|
klass->flush_async = g_output_stream_real_flush_async;
|
|
|
|
|
klass->flush_finish = g_output_stream_real_flush_finish;
|
|
|
|
|
klass->close_async = g_output_stream_real_close_async;
|
|
|
|
|
klass->close_finish = g_output_stream_real_close_finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_output_stream_init (GOutputStream *stream)
|
|
|
|
|
{
|
2013-06-24 16:43:04 +02:00
|
|
|
|
stream->priv = g_output_stream_get_instance_private (stream);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-10-15 16:25:40 +02:00
|
|
|
|
* g_output_stream_write: (virtual write_fn)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* @stream: a #GOutputStream.
|
2010-09-24 23:24:41 +02:00
|
|
|
|
* @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* @count: the number of bytes to write
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional cancellable object
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
|
|
|
|
* Tries to write @count bytes from @buffer into the stream. Will block
|
|
|
|
|
* during the operation.
|
|
|
|
|
*
|
2010-09-17 16:21:57 +02:00
|
|
|
|
* If count is 0, returns 0 and does nothing. A value of @count
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
|
|
|
|
|
*
|
|
|
|
|
* On success, the number of bytes written to the stream is returned.
|
|
|
|
|
* It is not an error if this is not the same as the requested size, as it
|
2010-09-17 16:21:57 +02:00
|
|
|
|
* can happen e.g. on a partial I/O error, or if there is not enough
|
|
|
|
|
* storage in the stream. All writes block until at least one byte
|
|
|
|
|
* is written or an error occurs; 0 is never returned (unless
|
|
|
|
|
* @count is 0).
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2012-04-04 21:22:15 +02:00
|
|
|
|
* If @cancellable is not %NULL, then the operation can be cancelled by
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* triggering the cancellable object from another thread. If the operation
|
2010-09-17 16:21:57 +02:00
|
|
|
|
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* operation was partially finished when the operation was cancelled the
|
|
|
|
|
* partial result will be returned, without an error.
|
|
|
|
|
*
|
|
|
|
|
* On error -1 is returned and @error is set accordingly.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: Number of bytes written, or -1 on error
|
2007-11-26 17:13:05 +01:00
|
|
|
|
**/
|
|
|
|
|
gssize
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_write (GOutputStream *stream,
|
|
|
|
|
const void *buffer,
|
|
|
|
|
gsize count,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
gssize res;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
|
2024-06-26 20:18:41 +02:00
|
|
|
|
g_return_val_if_fail (buffer != NULL || count == 0, 0);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (((gssize) count) < 0)
|
|
|
|
|
{
|
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
|
2008-02-10 05:41:25 +01:00
|
|
|
|
_("Too large count value passed to %s"), G_STRFUNC);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
2007-12-05 11:38:03 +01:00
|
|
|
|
if (class->write_fn == NULL)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2008-06-16 18:53:58 +02:00
|
|
|
|
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
2016-09-30 05:47:15 +02:00
|
|
|
|
_("Output stream doesn’t implement write"));
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-04 09:57:04 +01:00
|
|
|
|
if (!g_output_stream_set_pending (stream, error))
|
|
|
|
|
return -1;
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
if (cancellable)
|
2007-12-13 17:48:06 +01:00
|
|
|
|
g_cancellable_push_current (cancellable);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2007-12-05 11:38:03 +01:00
|
|
|
|
res = class->write_fn (stream, buffer, count, cancellable, error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
if (cancellable)
|
2007-12-13 17:48:06 +01:00
|
|
|
|
g_cancellable_pop_current (cancellable);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2007-12-04 09:57:04 +01:00
|
|
|
|
g_output_stream_clear_pending (stream);
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_write_all:
|
|
|
|
|
* @stream: a #GOutputStream.
|
2010-09-24 23:24:41 +02:00
|
|
|
|
* @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* @count: the number of bytes to write
|
2017-09-27 11:31:34 +02:00
|
|
|
|
* @bytes_written: (out) (optional): location to store the number of bytes that was
|
2007-11-30 06:11:25 +01:00
|
|
|
|
* written to the stream
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
|
|
|
|
* Tries to write @count bytes from @buffer into the stream. Will block
|
|
|
|
|
* during the operation.
|
|
|
|
|
*
|
|
|
|
|
* This function is similar to g_output_stream_write(), except it tries to
|
|
|
|
|
* write as many bytes as requested, only stopping on an error.
|
|
|
|
|
*
|
|
|
|
|
* On a successful write of @count bytes, %TRUE is returned, and @bytes_written
|
|
|
|
|
* is set to @count.
|
|
|
|
|
*
|
2012-04-04 21:22:15 +02:00
|
|
|
|
* If there is an error during the operation %FALSE is returned and @error
|
2014-09-29 17:50:05 +02:00
|
|
|
|
* is set to indicate the error status.
|
|
|
|
|
*
|
|
|
|
|
* As a special exception to the normal conventions for functions that
|
|
|
|
|
* use #GError, if this function returns %FALSE (and sets @error) then
|
|
|
|
|
* @bytes_written will be set to the number of bytes that were
|
|
|
|
|
* successfully written before the error was encountered. This
|
|
|
|
|
* functionality is only available from C. If you need it from another
|
|
|
|
|
* language then you must write your own loop around
|
|
|
|
|
* g_output_stream_write().
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: %TRUE on success, %FALSE if there was an error
|
2007-11-26 17:13:05 +01:00
|
|
|
|
**/
|
|
|
|
|
gboolean
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_write_all (GOutputStream *stream,
|
|
|
|
|
const void *buffer,
|
|
|
|
|
gsize count,
|
|
|
|
|
gsize *bytes_written,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
gsize _bytes_written;
|
|
|
|
|
gssize res;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
2021-09-21 12:40:14 +02:00
|
|
|
|
g_return_val_if_fail (buffer != NULL || count == 0, FALSE);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
_bytes_written = 0;
|
|
|
|
|
while (_bytes_written < count)
|
|
|
|
|
{
|
|
|
|
|
res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
|
|
|
|
|
cancellable, error);
|
|
|
|
|
if (res == -1)
|
|
|
|
|
{
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
*bytes_written = _bytes_written;
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2019-01-04 12:00:34 +01:00
|
|
|
|
g_return_val_if_fail (res > 0, FALSE);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
_bytes_written += res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
*bytes_written = _bytes_written;
|
2007-11-30 06:11:25 +01:00
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:10:36 +02:00
|
|
|
|
/**
|
2023-10-15 16:25:40 +02:00
|
|
|
|
* g_output_stream_writev: (virtual writev_fn)
|
2018-09-13 12:10:36 +02:00
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
* @vectors: (array length=n_vectors): the buffer containing the #GOutputVectors to write.
|
|
|
|
|
* @n_vectors: the number of vectors to write
|
|
|
|
|
* @bytes_written: (out) (optional): location to store the number of bytes that were
|
|
|
|
|
* written to the stream
|
|
|
|
|
* @cancellable: (nullable): optional cancellable object
|
|
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
|
|
|
|
*
|
|
|
|
|
* Tries to write the bytes contained in the @n_vectors @vectors into the
|
|
|
|
|
* stream. Will block during the operation.
|
|
|
|
|
*
|
|
|
|
|
* If @n_vectors is 0 or the sum of all bytes in @vectors is 0, returns 0 and
|
|
|
|
|
* does nothing.
|
|
|
|
|
*
|
|
|
|
|
* On success, the number of bytes written to the stream is returned.
|
|
|
|
|
* It is not an error if this is not the same as the requested size, as it
|
|
|
|
|
* can happen e.g. on a partial I/O error, or if there is not enough
|
|
|
|
|
* storage in the stream. All writes block until at least one byte
|
|
|
|
|
* is written or an error occurs; 0 is never returned (unless
|
|
|
|
|
* @n_vectors is 0 or the sum of all bytes in @vectors is 0).
|
|
|
|
|
*
|
|
|
|
|
* If @cancellable is not %NULL, then the operation can be cancelled by
|
|
|
|
|
* triggering the cancellable object from another thread. If the operation
|
|
|
|
|
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
|
|
|
|
|
* operation was partially finished when the operation was cancelled the
|
|
|
|
|
* partial result will be returned, without an error.
|
|
|
|
|
*
|
|
|
|
|
* Some implementations of g_output_stream_writev() may have limitations on the
|
|
|
|
|
* aggregate buffer size, and will return %G_IO_ERROR_INVALID_ARGUMENT if these
|
|
|
|
|
* are exceeded. For example, when writing to a local file on UNIX platforms,
|
|
|
|
|
* the aggregate buffer size must not exceed %G_MAXSSIZE bytes.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE on success, %FALSE if there was an error
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.60
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_writev (GOutputStream *stream,
|
|
|
|
|
const GOutputVector *vectors,
|
|
|
|
|
gsize n_vectors,
|
|
|
|
|
gsize *bytes_written,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
gboolean res;
|
|
|
|
|
gsize _bytes_written = 0;
|
|
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
*bytes_written = 0;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (vectors != NULL || n_vectors == 0, FALSE);
|
|
|
|
|
g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
|
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
if (n_vectors == 0)
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (class->writev_fn != NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
if (!g_output_stream_set_pending (stream, error))
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
if (cancellable)
|
|
|
|
|
g_cancellable_push_current (cancellable);
|
|
|
|
|
|
|
|
|
|
res = class->writev_fn (stream, vectors, n_vectors, &_bytes_written, cancellable, error);
|
|
|
|
|
|
|
|
|
|
g_warn_if_fail (res || _bytes_written == 0);
|
|
|
|
|
g_warn_if_fail (res || (error == NULL || *error != NULL));
|
|
|
|
|
|
|
|
|
|
if (cancellable)
|
|
|
|
|
g_cancellable_pop_current (cancellable);
|
|
|
|
|
|
|
|
|
|
g_output_stream_clear_pending (stream);
|
|
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
*bytes_written = _bytes_written;
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_writev_all:
|
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
* @vectors: (array length=n_vectors): the buffer containing the #GOutputVectors to write.
|
|
|
|
|
* @n_vectors: the number of vectors to write
|
|
|
|
|
* @bytes_written: (out) (optional): location to store the number of bytes that were
|
|
|
|
|
* written to the stream
|
|
|
|
|
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
|
|
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
|
|
|
|
*
|
|
|
|
|
* Tries to write the bytes contained in the @n_vectors @vectors into the
|
|
|
|
|
* stream. Will block during the operation.
|
|
|
|
|
*
|
|
|
|
|
* This function is similar to g_output_stream_writev(), except it tries to
|
|
|
|
|
* write as many bytes as requested, only stopping on an error.
|
|
|
|
|
*
|
|
|
|
|
* On a successful write of all @n_vectors vectors, %TRUE is returned, and
|
|
|
|
|
* @bytes_written is set to the sum of all the sizes of @vectors.
|
|
|
|
|
*
|
|
|
|
|
* If there is an error during the operation %FALSE is returned and @error
|
|
|
|
|
* is set to indicate the error status.
|
|
|
|
|
*
|
|
|
|
|
* As a special exception to the normal conventions for functions that
|
|
|
|
|
* use #GError, if this function returns %FALSE (and sets @error) then
|
|
|
|
|
* @bytes_written will be set to the number of bytes that were
|
|
|
|
|
* successfully written before the error was encountered. This
|
|
|
|
|
* functionality is only available from C. If you need it from another
|
|
|
|
|
* language then you must write your own loop around
|
|
|
|
|
* g_output_stream_write().
|
|
|
|
|
*
|
|
|
|
|
* The content of the individual elements of @vectors might be changed by this
|
|
|
|
|
* function.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE on success, %FALSE if there was an error
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.60
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_writev_all (GOutputStream *stream,
|
|
|
|
|
GOutputVector *vectors,
|
|
|
|
|
gsize n_vectors,
|
|
|
|
|
gsize *bytes_written,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
gsize _bytes_written = 0;
|
|
|
|
|
gsize i, to_be_written = 0;
|
|
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
*bytes_written = 0;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (vectors != NULL || n_vectors == 0, FALSE);
|
|
|
|
|
g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
|
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
/* We can't write more than G_MAXSIZE bytes overall, otherwise we
|
|
|
|
|
* would overflow the bytes_written counter */
|
|
|
|
|
for (i = 0; i < n_vectors; i++)
|
|
|
|
|
{
|
|
|
|
|
if (to_be_written > G_MAXSIZE - vectors[i].size)
|
|
|
|
|
{
|
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
|
|
|
|
|
_("Sum of vectors passed to %s too large"), G_STRFUNC);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
to_be_written += vectors[i].size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_bytes_written = 0;
|
|
|
|
|
while (n_vectors > 0 && to_be_written > 0)
|
|
|
|
|
{
|
|
|
|
|
gsize n_written = 0;
|
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
|
|
res = g_output_stream_writev (stream, vectors, n_vectors, &n_written, cancellable, error);
|
|
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
|
{
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
*bytes_written = _bytes_written;
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-04 12:00:34 +01:00
|
|
|
|
g_return_val_if_fail (n_written > 0, FALSE);
|
2018-09-13 12:10:36 +02:00
|
|
|
|
_bytes_written += n_written;
|
|
|
|
|
|
|
|
|
|
/* skip vectors that have been written in full */
|
|
|
|
|
while (n_vectors > 0 && n_written >= vectors[0].size)
|
|
|
|
|
{
|
|
|
|
|
n_written -= vectors[0].size;
|
|
|
|
|
++vectors;
|
|
|
|
|
--n_vectors;
|
|
|
|
|
}
|
|
|
|
|
/* skip partially written vector data */
|
|
|
|
|
if (n_written > 0 && n_vectors > 0)
|
|
|
|
|
{
|
|
|
|
|
vectors[0].size -= n_written;
|
|
|
|
|
vectors[0].buffer = ((guint8 *) vectors[0].buffer) + n_written;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
*bytes_written = _bytes_written;
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-25 23:26:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_printf:
|
|
|
|
|
* @stream: a #GOutputStream.
|
2017-09-27 11:31:34 +02:00
|
|
|
|
* @bytes_written: (out) (optional): location to store the number of bytes that was
|
2013-11-25 23:26:21 +01:00
|
|
|
|
* written to the stream
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
|
2013-11-25 23:26:21 +01:00
|
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
|
|
|
|
* @format: the format string. See the printf() documentation
|
|
|
|
|
* @...: the parameters to insert into the format string
|
|
|
|
|
*
|
|
|
|
|
* This is a utility function around g_output_stream_write_all(). It
|
|
|
|
|
* uses g_strdup_vprintf() to turn @format and @... into a string that
|
|
|
|
|
* is then written to @stream.
|
|
|
|
|
*
|
|
|
|
|
* See the documentation of g_output_stream_write_all() about the
|
|
|
|
|
* behavior of the actual write operation.
|
|
|
|
|
*
|
|
|
|
|
* Note that partial writes cannot be properly checked with this
|
|
|
|
|
* function due to the variable length of the written string, if you
|
|
|
|
|
* need precise control over partial write failures, you need to
|
|
|
|
|
* create you own printf()-like wrapper around g_output_stream_write()
|
|
|
|
|
* or g_output_stream_write_all().
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.40
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: %TRUE on success, %FALSE if there was an error
|
2013-11-25 23:26:21 +01:00
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_printf (GOutputStream *stream,
|
|
|
|
|
gsize *bytes_written,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error,
|
|
|
|
|
const gchar *format,
|
|
|
|
|
...)
|
|
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
gboolean success;
|
|
|
|
|
|
|
|
|
|
va_start (args, format);
|
|
|
|
|
success = g_output_stream_vprintf (stream, bytes_written, cancellable,
|
|
|
|
|
error, format, args);
|
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_vprintf:
|
|
|
|
|
* @stream: a #GOutputStream.
|
2017-09-27 11:31:34 +02:00
|
|
|
|
* @bytes_written: (out) (optional): location to store the number of bytes that was
|
2013-11-25 23:26:21 +01:00
|
|
|
|
* written to the stream
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
|
2013-11-25 23:26:21 +01:00
|
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
|
|
|
|
* @format: the format string. See the printf() documentation
|
|
|
|
|
* @args: the parameters to insert into the format string
|
|
|
|
|
*
|
|
|
|
|
* This is a utility function around g_output_stream_write_all(). It
|
|
|
|
|
* uses g_strdup_vprintf() to turn @format and @args into a string that
|
|
|
|
|
* is then written to @stream.
|
|
|
|
|
*
|
|
|
|
|
* See the documentation of g_output_stream_write_all() about the
|
|
|
|
|
* behavior of the actual write operation.
|
|
|
|
|
*
|
|
|
|
|
* Note that partial writes cannot be properly checked with this
|
|
|
|
|
* function due to the variable length of the written string, if you
|
|
|
|
|
* need precise control over partial write failures, you need to
|
|
|
|
|
* create you own printf()-like wrapper around g_output_stream_write()
|
|
|
|
|
* or g_output_stream_write_all().
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.40
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: %TRUE on success, %FALSE if there was an error
|
2013-11-25 23:26:21 +01:00
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_vprintf (GOutputStream *stream,
|
|
|
|
|
gsize *bytes_written,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error,
|
|
|
|
|
const gchar *format,
|
|
|
|
|
va_list args)
|
|
|
|
|
{
|
|
|
|
|
gchar *text;
|
|
|
|
|
gboolean success;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
2017-12-01 11:02:21 +01:00
|
|
|
|
g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
|
2013-11-25 23:26:21 +01:00
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (format != NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
text = g_strdup_vprintf (format, args);
|
|
|
|
|
success = g_output_stream_write_all (stream,
|
|
|
|
|
text, strlen (text),
|
|
|
|
|
bytes_written, cancellable, error);
|
|
|
|
|
g_free (text);
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-05 15:19:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_write_bytes:
|
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
* @bytes: the #GBytes to write
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional cancellable object
|
2012-04-05 15:19:17 +02:00
|
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
|
|
|
|
*
|
2013-10-02 16:32:03 +02:00
|
|
|
|
* A wrapper function for g_output_stream_write() which takes a
|
|
|
|
|
* #GBytes as input. This can be more convenient for use by language
|
|
|
|
|
* bindings or in other cases where the refcounted nature of #GBytes
|
|
|
|
|
* is helpful over a bare pointer interface.
|
|
|
|
|
*
|
2014-02-01 02:34:33 +01:00
|
|
|
|
* However, note that this function may still perform partial writes,
|
|
|
|
|
* just like g_output_stream_write(). If that occurs, to continue
|
|
|
|
|
* writing, you will need to create a new #GBytes containing just the
|
|
|
|
|
* remaining bytes, using g_bytes_new_from_bytes(). Passing the same
|
|
|
|
|
* #GBytes instance multiple times potentially can result in duplicated
|
|
|
|
|
* data in the output stream.
|
2012-04-05 15:19:17 +02:00
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: Number of bytes written, or -1 on error
|
2012-04-05 15:19:17 +02:00
|
|
|
|
**/
|
|
|
|
|
gssize
|
|
|
|
|
g_output_stream_write_bytes (GOutputStream *stream,
|
|
|
|
|
GBytes *bytes,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
gsize size;
|
|
|
|
|
gconstpointer data;
|
|
|
|
|
|
|
|
|
|
data = g_bytes_get_data (bytes, &size);
|
|
|
|
|
|
|
|
|
|
return g_output_stream_write (stream,
|
|
|
|
|
data, size,
|
|
|
|
|
cancellable,
|
|
|
|
|
error);
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_flush:
|
|
|
|
|
* @stream: a #GOutputStream.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional cancellable object
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2011-12-05 22:40:18 +01:00
|
|
|
|
* Forces a write of all user-space buffered data for the given
|
|
|
|
|
* @stream. Will block during the operation. Closing the stream will
|
|
|
|
|
* implicitly cause a flush.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
|
|
|
|
* This function is optional for inherited classes.
|
|
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
|
* If @cancellable is not %NULL, then the operation can be cancelled by
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* triggering the cancellable object from another thread. If the operation
|
2007-11-27 15:00:13 +01:00
|
|
|
|
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: %TRUE on success, %FALSE on error
|
2007-11-26 17:13:05 +01:00
|
|
|
|
**/
|
|
|
|
|
gboolean
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_flush (GOutputStream *stream,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
|
2007-12-04 09:57:04 +01:00
|
|
|
|
if (!g_output_stream_set_pending (stream, error))
|
|
|
|
|
return FALSE;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
|
|
|
|
res = TRUE;
|
|
|
|
|
if (class->flush)
|
|
|
|
|
{
|
|
|
|
|
if (cancellable)
|
2007-12-13 17:48:06 +01:00
|
|
|
|
g_cancellable_push_current (cancellable);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
res = class->flush (stream, cancellable, error);
|
|
|
|
|
|
|
|
|
|
if (cancellable)
|
2007-12-13 17:48:06 +01:00
|
|
|
|
g_cancellable_pop_current (cancellable);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2007-12-04 09:57:04 +01:00
|
|
|
|
g_output_stream_clear_pending (stream);
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_splice:
|
2007-11-27 15:00:13 +01:00
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
* @source: a #GInputStream.
|
|
|
|
|
* @flags: a set of #GOutputStreamSpliceFlags.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* ignore.
|
|
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
|
* Splices an input stream into an output stream.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2009-09-09 06:18:23 +02:00
|
|
|
|
* Returns: a #gssize containing the size of the data spliced, or
|
2011-06-07 23:15:17 +02:00
|
|
|
|
* -1 if an error occurred. Note that if the number of bytes
|
|
|
|
|
* spliced is greater than %G_MAXSSIZE, then that will be
|
|
|
|
|
* returned, and there is no way to determine the actual number
|
|
|
|
|
* of bytes spliced.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
**/
|
|
|
|
|
gssize
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_splice (GOutputStream *stream,
|
|
|
|
|
GInputStream *source,
|
|
|
|
|
GOutputStreamSpliceFlags flags,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
2009-09-09 06:18:23 +02:00
|
|
|
|
gssize bytes_copied;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
|
|
|
|
|
g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
|
|
|
|
|
|
|
|
|
|
if (g_input_stream_is_closed (source))
|
|
|
|
|
{
|
2008-06-16 18:53:58 +02:00
|
|
|
|
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
|
|
|
|
|
_("Source stream is already closed"));
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-04 09:57:04 +01:00
|
|
|
|
if (!g_output_stream_set_pending (stream, error))
|
|
|
|
|
return -1;
|
2009-09-09 06:18:23 +02:00
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
|
|
|
|
if (cancellable)
|
2007-12-13 17:48:06 +01:00
|
|
|
|
g_cancellable_push_current (cancellable);
|
2009-09-09 06:18:23 +02:00
|
|
|
|
|
|
|
|
|
bytes_copied = class->splice (stream, source, flags, cancellable, error);
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
if (cancellable)
|
2007-12-13 17:48:06 +01:00
|
|
|
|
g_cancellable_pop_current (cancellable);
|
2009-09-09 06:18:23 +02:00
|
|
|
|
|
2007-12-04 09:57:04 +01:00
|
|
|
|
g_output_stream_clear_pending (stream);
|
|
|
|
|
|
2009-09-09 06:18:23 +02:00
|
|
|
|
return bytes_copied;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gssize
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_real_splice (GOutputStream *stream,
|
|
|
|
|
GInputStream *source,
|
|
|
|
|
GOutputStreamSpliceFlags flags,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2007-12-04 09:55:51 +01:00
|
|
|
|
GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
gssize n_read, n_written;
|
2011-06-07 23:15:17 +02:00
|
|
|
|
gsize bytes_copied;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
char buffer[8192], *p;
|
|
|
|
|
gboolean res;
|
|
|
|
|
|
2007-12-05 11:38:03 +01:00
|
|
|
|
bytes_copied = 0;
|
2009-09-09 06:18:23 +02:00
|
|
|
|
if (class->write_fn == NULL)
|
2007-12-04 09:55:51 +01:00
|
|
|
|
{
|
2008-06-16 18:53:58 +02:00
|
|
|
|
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
2016-09-30 05:47:15 +02:00
|
|
|
|
_("Output stream doesn’t implement write"));
|
2007-12-04 09:55:51 +01:00
|
|
|
|
res = FALSE;
|
|
|
|
|
goto notsupported;
|
|
|
|
|
}
|
2009-09-09 06:18:23 +02:00
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
res = TRUE;
|
2009-09-09 06:18:23 +02:00
|
|
|
|
do
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
|
|
|
|
|
if (n_read == -1)
|
|
|
|
|
{
|
|
|
|
|
res = FALSE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-09-09 06:18:23 +02:00
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
if (n_read == 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
|
while (n_read > 0)
|
|
|
|
|
{
|
2007-12-05 11:38:03 +01:00
|
|
|
|
n_written = class->write_fn (stream, p, n_read, cancellable, error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
if (n_written == -1)
|
|
|
|
|
{
|
|
|
|
|
res = FALSE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p += n_written;
|
|
|
|
|
n_read -= n_written;
|
|
|
|
|
bytes_copied += n_written;
|
|
|
|
|
}
|
2011-06-07 23:15:17 +02:00
|
|
|
|
|
|
|
|
|
if (bytes_copied > G_MAXSSIZE)
|
|
|
|
|
bytes_copied = G_MAXSSIZE;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
while (res);
|
|
|
|
|
|
2007-12-04 09:55:51 +01:00
|
|
|
|
notsupported:
|
2007-11-26 17:13:05 +01:00
|
|
|
|
if (!res)
|
|
|
|
|
error = NULL; /* Ignore further errors */
|
|
|
|
|
|
2007-12-14 12:07:31 +01:00
|
|
|
|
if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
/* Don't care about errors in source here */
|
|
|
|
|
g_input_stream_close (source, cancellable, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-14 12:07:31 +01:00
|
|
|
|
if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
/* But write errors on close are bad! */
|
2015-10-08 02:34:17 +02:00
|
|
|
|
if (!g_output_stream_internal_close (stream, cancellable, error))
|
|
|
|
|
res = FALSE;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (res)
|
|
|
|
|
return bytes_copied;
|
2009-05-20 13:37:55 +02:00
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-19 10:13:52 +02:00
|
|
|
|
/* Must always be called inside
|
|
|
|
|
* g_output_stream_set_pending()/g_output_stream_clear_pending(). */
|
|
|
|
|
static gboolean
|
2013-02-24 16:09:05 +01:00
|
|
|
|
g_output_stream_internal_close (GOutputStream *stream,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2011-09-19 10:13:52 +02:00
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
|
|
if (stream->priv->closed)
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
|
|
|
|
stream->priv->closing = TRUE;
|
|
|
|
|
|
|
|
|
|
if (cancellable)
|
|
|
|
|
g_cancellable_push_current (cancellable);
|
|
|
|
|
|
|
|
|
|
if (class->flush)
|
|
|
|
|
res = class->flush (stream, cancellable, error);
|
|
|
|
|
else
|
|
|
|
|
res = TRUE;
|
|
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
|
{
|
|
|
|
|
/* flushing caused the error that we want to return,
|
|
|
|
|
* but we still want to close the underlying stream if possible
|
|
|
|
|
*/
|
|
|
|
|
if (class->close_fn)
|
|
|
|
|
class->close_fn (stream, cancellable, NULL);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
res = TRUE;
|
|
|
|
|
if (class->close_fn)
|
|
|
|
|
res = class->close_fn (stream, cancellable, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cancellable)
|
|
|
|
|
g_cancellable_pop_current (cancellable);
|
|
|
|
|
|
|
|
|
|
stream->priv->closing = FALSE;
|
|
|
|
|
stream->priv->closed = TRUE;
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_close:
|
|
|
|
|
* @stream: A #GOutputStream.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional cancellable object
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
|
|
|
|
* Closes the stream, releasing resources related to it.
|
|
|
|
|
*
|
|
|
|
|
* Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
|
|
|
|
|
* Closing a stream multiple times will not return an error.
|
|
|
|
|
*
|
|
|
|
|
* Closing a stream will automatically flush any outstanding buffers in the
|
|
|
|
|
* stream.
|
|
|
|
|
*
|
|
|
|
|
* Streams will be automatically closed when the last reference
|
2008-01-21 14:08:23 +01:00
|
|
|
|
* is dropped, but you might want to call this function to make sure
|
|
|
|
|
* resources are released as early as possible.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
|
|
|
|
* Some streams might keep the backing store of the stream (e.g. a file descriptor)
|
|
|
|
|
* open after the stream is closed. See the documentation for the individual
|
|
|
|
|
* stream for details.
|
|
|
|
|
*
|
|
|
|
|
* On failure the first error that happened will be reported, but the close
|
|
|
|
|
* operation will finish as much as possible. A stream that failed to
|
2008-03-04 14:15:57 +01:00
|
|
|
|
* close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* is important to check and report the error to the user, otherwise
|
|
|
|
|
* there might be a loss of data as all data might not be written.
|
|
|
|
|
*
|
2012-04-04 21:22:15 +02:00
|
|
|
|
* If @cancellable is not %NULL, then the operation can be cancelled by
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* triggering the cancellable object from another thread. If the operation
|
|
|
|
|
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
|
|
|
|
|
* Cancelling a close will still leave the stream closed, but there some streams
|
|
|
|
|
* can use a faster close that doesn't block to e.g. check errors. On
|
|
|
|
|
* cancellation (as with any error) there is no guarantee that all written
|
|
|
|
|
* data will reach the target.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: %TRUE on success, %FALSE on failure
|
2007-11-26 17:13:05 +01:00
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_close (GOutputStream *stream,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
|
|
|
|
|
if (stream->priv->closed)
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
2007-12-04 09:57:04 +01:00
|
|
|
|
if (!g_output_stream_set_pending (stream, error))
|
|
|
|
|
return FALSE;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2013-02-24 16:09:05 +01:00
|
|
|
|
res = g_output_stream_internal_close (stream, cancellable, error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2007-12-04 09:57:04 +01:00
|
|
|
|
g_output_stream_clear_pending (stream);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2012-08-02 21:49:59 +02:00
|
|
|
|
async_ready_write_callback_wrapper (GObject *source_object,
|
2007-11-30 06:11:25 +01:00
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
2010-05-10 23:49:41 +02:00
|
|
|
|
{
|
|
|
|
|
GOutputStream *stream = G_OUTPUT_STREAM (source_object);
|
|
|
|
|
GOutputStreamClass *class;
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
gssize nwrote;
|
|
|
|
|
GError *error = NULL;
|
2010-05-10 23:49:41 +02:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_output_stream_clear_pending (stream);
|
|
|
|
|
|
|
|
|
|
if (g_async_result_legacy_propagate_error (res, &error))
|
|
|
|
|
nwrote = -1;
|
|
|
|
|
else
|
2010-05-10 23:49:41 +02:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
nwrote = class->write_finish (stream, res, &error);
|
2010-05-10 23:49:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
if (nwrote >= 0)
|
|
|
|
|
g_task_return_int (task, nwrote);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_write_async:
|
|
|
|
|
* @stream: A #GOutputStream.
|
2010-09-24 23:24:41 +02:00
|
|
|
|
* @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* @count: the number of bytes to write
|
2007-11-30 06:11:25 +01:00
|
|
|
|
* @io_priority: the io priority of the request.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
|
2023-02-20 21:43:02 +01:00
|
|
|
|
* @callback: (scope async): a #GAsyncReadyCallback
|
|
|
|
|
* to call when the request is satisfied
|
|
|
|
|
* @user_data: the data to pass to callback function
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2007-11-30 06:11:25 +01:00
|
|
|
|
* Request an asynchronous write of @count bytes from @buffer into
|
2008-01-21 14:08:23 +01:00
|
|
|
|
* the stream. When the operation is finished @callback will be called.
|
|
|
|
|
* You can then call g_output_stream_write_finish() to get the result of the
|
|
|
|
|
* operation.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2007-11-30 06:11:25 +01:00
|
|
|
|
* During an async request no other sync and async calls are allowed,
|
|
|
|
|
* and will result in %G_IO_ERROR_PENDING errors.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2007-11-30 06:11:25 +01:00
|
|
|
|
* A value of @count larger than %G_MAXSSIZE will cause a
|
|
|
|
|
* %G_IO_ERROR_INVALID_ARGUMENT error.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
|
|
|
|
* On success, the number of bytes written will be passed to the
|
2007-11-30 06:11:25 +01:00
|
|
|
|
* @callback. It is not an error if this is not the same as the
|
|
|
|
|
* requested size, as it can happen e.g. on a partial I/O error,
|
|
|
|
|
* but generally we try to write as many bytes as requested.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2010-08-16 21:37:01 +02:00
|
|
|
|
* You are guaranteed that this method will never fail with
|
|
|
|
|
* %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
|
|
|
|
|
* method will just wait until this changes.
|
|
|
|
|
*
|
2007-11-30 06:11:25 +01:00
|
|
|
|
* Any outstanding I/O request with higher priority (lower numerical
|
|
|
|
|
* value) will be executed before an outstanding request with lower
|
|
|
|
|
* priority. Default priority is %G_PRIORITY_DEFAULT.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2017-01-19 09:21:16 +01:00
|
|
|
|
* The asynchronous methods have a default fallback that uses threads
|
2007-11-30 06:11:25 +01:00
|
|
|
|
* to implement asynchronicity, so they are optional for inheriting
|
|
|
|
|
* classes. However, if you override one you must override all.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2007-11-30 06:11:25 +01:00
|
|
|
|
* For the synchronous, blocking version of this function, see
|
|
|
|
|
* g_output_stream_write().
|
2013-12-02 20:37:23 +01:00
|
|
|
|
*
|
2014-01-31 23:38:27 +01:00
|
|
|
|
* Note that no copy of @buffer will be made, so it must stay valid
|
|
|
|
|
* until @callback is called. See g_output_stream_write_bytes_async()
|
|
|
|
|
* for a #GBytes version that will automatically hold a reference to
|
|
|
|
|
* the contents (without copying) for the duration of the call.
|
|
|
|
|
*/
|
2007-11-26 17:13:05 +01:00
|
|
|
|
void
|
|
|
|
|
g_output_stream_write_async (GOutputStream *stream,
|
|
|
|
|
const void *buffer,
|
|
|
|
|
gsize count,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
2007-12-04 09:57:04 +01:00
|
|
|
|
GError *error = NULL;
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
|
|
|
|
|
g_return_if_fail (buffer != NULL);
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
|
|
|
|
g_task_set_source_tag (task, g_output_stream_write_async);
|
|
|
|
|
g_task_set_priority (task, io_priority);
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
if (count == 0)
|
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_return_int (task, 0);
|
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (((gssize) count) < 0)
|
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
|
|
|
|
|
_("Too large count value passed to %s"),
|
|
|
|
|
G_STRFUNC);
|
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-04 09:57:04 +01:00
|
|
|
|
if (!g_output_stream_set_pending (stream, &error))
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
|
|
|
|
class->write_async (stream, buffer, count, io_priority, cancellable,
|
2012-08-02 21:49:59 +02:00
|
|
|
|
async_ready_write_callback_wrapper, task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_write_finish:
|
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
* @result: a #GAsyncResult.
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* ignore.
|
|
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
|
* Finishes a stream write operation.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
|
* Returns: a #gssize containing the number of bytes written to the stream.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
**/
|
|
|
|
|
gssize
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_write_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_write_async), FALSE);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
/* @result is always the GTask created by g_output_stream_write_async();
|
|
|
|
|
* we called class->write_finish() from async_ready_write_callback_wrapper.
|
|
|
|
|
*/
|
|
|
|
|
return g_task_propagate_int (G_TASK (result), error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 17:44:09 +02:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
const guint8 *buffer;
|
|
|
|
|
gsize to_write;
|
|
|
|
|
gsize bytes_written;
|
|
|
|
|
} AsyncWriteAll;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
free_async_write_all (gpointer data)
|
|
|
|
|
{
|
|
|
|
|
g_slice_free (AsyncWriteAll, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
write_all_callback (GObject *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
AsyncWriteAll *data = g_task_get_task_data (task);
|
|
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gssize nwritten;
|
|
|
|
|
|
|
|
|
|
nwritten = g_output_stream_write_finish (G_OUTPUT_STREAM (stream), result, &error);
|
|
|
|
|
|
|
|
|
|
if (nwritten == -1)
|
|
|
|
|
{
|
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_assert_cmpint (nwritten, <=, data->to_write);
|
|
|
|
|
g_warn_if_fail (nwritten > 0);
|
|
|
|
|
|
|
|
|
|
data->to_write -= nwritten;
|
|
|
|
|
data->bytes_written += nwritten;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data->to_write == 0)
|
|
|
|
|
{
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
g_output_stream_write_async (G_OUTPUT_STREAM (stream),
|
|
|
|
|
data->buffer + data->bytes_written,
|
|
|
|
|
data->to_write,
|
|
|
|
|
g_task_get_priority (task),
|
|
|
|
|
g_task_get_cancellable (task),
|
|
|
|
|
write_all_callback, task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
write_all_async_thread (GTask *task,
|
|
|
|
|
gpointer source_object,
|
|
|
|
|
gpointer task_data,
|
|
|
|
|
GCancellable *cancellable)
|
|
|
|
|
{
|
|
|
|
|
GOutputStream *stream = source_object;
|
|
|
|
|
AsyncWriteAll *data = task_data;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
|
|
if (g_output_stream_write_all (stream, data->buffer, data->to_write, &data->bytes_written,
|
|
|
|
|
g_task_get_cancellable (task), &error))
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_write_all_async:
|
|
|
|
|
* @stream: A #GOutputStream
|
|
|
|
|
* @buffer: (array length=count) (element-type guint8): the buffer containing the data to write
|
|
|
|
|
* @count: the number of bytes to write
|
|
|
|
|
* @io_priority: the io priority of the request
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
|
2023-02-20 21:43:02 +01:00
|
|
|
|
* @callback: (scope async): a #GAsyncReadyCallback
|
|
|
|
|
* to call when the request is satisfied
|
|
|
|
|
* @user_data: the data to pass to callback function
|
2014-09-29 17:44:09 +02:00
|
|
|
|
*
|
|
|
|
|
* Request an asynchronous write of @count bytes from @buffer into
|
|
|
|
|
* the stream. When the operation is finished @callback will be called.
|
|
|
|
|
* You can then call g_output_stream_write_all_finish() to get the result of the
|
|
|
|
|
* operation.
|
|
|
|
|
*
|
|
|
|
|
* This is the asynchronous version of g_output_stream_write_all().
|
|
|
|
|
*
|
|
|
|
|
* Call g_output_stream_write_all_finish() to collect the result.
|
|
|
|
|
*
|
|
|
|
|
* Any outstanding I/O request with higher priority (lower numerical
|
|
|
|
|
* value) will be executed before an outstanding request with lower
|
|
|
|
|
* priority. Default priority is %G_PRIORITY_DEFAULT.
|
|
|
|
|
*
|
|
|
|
|
* Note that no copy of @buffer will be made, so it must stay valid
|
|
|
|
|
* until @callback is called.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.44
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_output_stream_write_all_async (GOutputStream *stream,
|
|
|
|
|
const void *buffer,
|
|
|
|
|
gsize count,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
AsyncWriteAll *data;
|
|
|
|
|
GTask *task;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
|
|
|
|
|
g_return_if_fail (buffer != NULL || count == 0);
|
|
|
|
|
|
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
|
|
|
|
data = g_slice_new0 (AsyncWriteAll);
|
|
|
|
|
data->buffer = buffer;
|
|
|
|
|
data->to_write = count;
|
|
|
|
|
|
2016-06-17 01:39:38 +02:00
|
|
|
|
g_task_set_source_tag (task, g_output_stream_write_all_async);
|
2014-09-29 17:44:09 +02:00
|
|
|
|
g_task_set_task_data (task, data, free_async_write_all);
|
|
|
|
|
g_task_set_priority (task, io_priority);
|
|
|
|
|
|
|
|
|
|
/* If async writes are going to be handled via the threadpool anyway
|
|
|
|
|
* then we may as well do it with a single dispatch instead of
|
|
|
|
|
* bouncing in and out.
|
|
|
|
|
*/
|
|
|
|
|
if (g_output_stream_async_write_is_via_threads (stream))
|
|
|
|
|
{
|
|
|
|
|
g_task_run_in_thread (task, write_all_async_thread);
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
write_all_callback (G_OBJECT (stream), NULL, task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_write_all_finish:
|
|
|
|
|
* @stream: a #GOutputStream
|
|
|
|
|
* @result: a #GAsyncResult
|
2017-09-27 11:31:34 +02:00
|
|
|
|
* @bytes_written: (out) (optional): location to store the number of bytes that was written to the stream
|
2014-09-29 17:44:09 +02:00
|
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to ignore.
|
|
|
|
|
*
|
|
|
|
|
* Finishes an asynchronous stream write operation started with
|
|
|
|
|
* g_output_stream_write_all_async().
|
|
|
|
|
*
|
|
|
|
|
* As a special exception to the normal conventions for functions that
|
|
|
|
|
* use #GError, if this function returns %FALSE (and sets @error) then
|
|
|
|
|
* @bytes_written will be set to the number of bytes that were
|
|
|
|
|
* successfully written before the error was encountered. This
|
|
|
|
|
* functionality is only available from C. If you need it from another
|
|
|
|
|
* language then you must write your own loop around
|
|
|
|
|
* g_output_stream_write_async().
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE on success, %FALSE if there was an error
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.44
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_write_all_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
gsize *bytes_written,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
GTask *task;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
|
|
|
|
|
|
|
|
|
task = G_TASK (result);
|
|
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
{
|
|
|
|
|
AsyncWriteAll *data = (AsyncWriteAll *)g_task_get_task_data (task);
|
|
|
|
|
|
|
|
|
|
*bytes_written = data->bytes_written;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return g_task_propagate_boolean (task, error);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:10:36 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_writev_async:
|
|
|
|
|
* @stream: A #GOutputStream.
|
|
|
|
|
* @vectors: (array length=n_vectors): the buffer containing the #GOutputVectors to write.
|
|
|
|
|
* @n_vectors: the number of vectors to write
|
|
|
|
|
* @io_priority: the I/O priority of the request.
|
|
|
|
|
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
|
2023-02-20 21:43:02 +01:00
|
|
|
|
* @callback: (scope async): a #GAsyncReadyCallback
|
|
|
|
|
* to call when the request is satisfied
|
|
|
|
|
* @user_data: the data to pass to callback function
|
2018-09-13 12:10:36 +02:00
|
|
|
|
*
|
|
|
|
|
* Request an asynchronous write of the bytes contained in @n_vectors @vectors into
|
|
|
|
|
* the stream. When the operation is finished @callback will be called.
|
|
|
|
|
* You can then call g_output_stream_writev_finish() to get the result of the
|
|
|
|
|
* operation.
|
|
|
|
|
*
|
|
|
|
|
* During an async request no other sync and async calls are allowed,
|
|
|
|
|
* and will result in %G_IO_ERROR_PENDING errors.
|
|
|
|
|
*
|
|
|
|
|
* On success, the number of bytes written will be passed to the
|
|
|
|
|
* @callback. It is not an error if this is not the same as the
|
|
|
|
|
* requested size, as it can happen e.g. on a partial I/O error,
|
|
|
|
|
* but generally we try to write as many bytes as requested.
|
|
|
|
|
*
|
|
|
|
|
* You are guaranteed that this method will never fail with
|
|
|
|
|
* %G_IO_ERROR_WOULD_BLOCK — if @stream can't accept more data, the
|
|
|
|
|
* method will just wait until this changes.
|
|
|
|
|
*
|
|
|
|
|
* Any outstanding I/O request with higher priority (lower numerical
|
|
|
|
|
* value) will be executed before an outstanding request with lower
|
|
|
|
|
* priority. Default priority is %G_PRIORITY_DEFAULT.
|
|
|
|
|
*
|
|
|
|
|
* The asynchronous methods have a default fallback that uses threads
|
|
|
|
|
* to implement asynchronicity, so they are optional for inheriting
|
|
|
|
|
* classes. However, if you override one you must override all.
|
|
|
|
|
*
|
|
|
|
|
* For the synchronous, blocking version of this function, see
|
|
|
|
|
* g_output_stream_writev().
|
|
|
|
|
*
|
|
|
|
|
* Note that no copy of @vectors will be made, so it must stay valid
|
|
|
|
|
* until @callback is called.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.60
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_output_stream_writev_async (GOutputStream *stream,
|
|
|
|
|
const GOutputVector *vectors,
|
|
|
|
|
gsize n_vectors,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
|
|
|
|
|
g_return_if_fail (vectors != NULL || n_vectors == 0);
|
|
|
|
|
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
g_return_if_fail (class->writev_async != NULL);
|
|
|
|
|
|
|
|
|
|
class->writev_async (stream, vectors, n_vectors, io_priority, cancellable,
|
|
|
|
|
callback, user_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_writev_finish:
|
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
* @result: a #GAsyncResult.
|
|
|
|
|
* @bytes_written: (out) (optional): location to store the number of bytes that were written to the stream
|
|
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
|
|
|
|
* ignore.
|
|
|
|
|
*
|
|
|
|
|
* Finishes a stream writev operation.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE on success, %FALSE if there was an error
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.60
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_writev_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
gsize *bytes_written,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
gboolean res;
|
|
|
|
|
gsize _bytes_written = 0;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
|
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
g_return_val_if_fail (class->writev_finish != NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
res = class->writev_finish (stream, result, &_bytes_written, error);
|
|
|
|
|
|
|
|
|
|
g_warn_if_fail (res || _bytes_written == 0);
|
|
|
|
|
g_warn_if_fail (res || (error == NULL || *error != NULL));
|
|
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
*bytes_written = _bytes_written;
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
GOutputVector *vectors;
|
|
|
|
|
gsize n_vectors; /* (unowned) */
|
|
|
|
|
gsize bytes_written;
|
|
|
|
|
} AsyncWritevAll;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
free_async_writev_all (gpointer data)
|
|
|
|
|
{
|
|
|
|
|
g_slice_free (AsyncWritevAll, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
writev_all_callback (GObject *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
AsyncWritevAll *data = g_task_get_task_data (task);
|
|
|
|
|
gint priority = g_task_get_priority (task);
|
|
|
|
|
GCancellable *cancellable = g_task_get_cancellable (task);
|
|
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gboolean res;
|
|
|
|
|
gsize n_written = 0;
|
|
|
|
|
|
|
|
|
|
res = g_output_stream_writev_finish (G_OUTPUT_STREAM (stream), result, &n_written, &error);
|
|
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
|
{
|
|
|
|
|
g_task_return_error (task, g_steal_pointer (&error));
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_warn_if_fail (n_written > 0);
|
|
|
|
|
data->bytes_written += n_written;
|
|
|
|
|
|
|
|
|
|
/* skip vectors that have been written in full */
|
|
|
|
|
while (data->n_vectors > 0 && n_written >= data->vectors[0].size)
|
|
|
|
|
{
|
|
|
|
|
n_written -= data->vectors[0].size;
|
|
|
|
|
++data->vectors;
|
|
|
|
|
--data->n_vectors;
|
|
|
|
|
}
|
|
|
|
|
/* skip partially written vector data */
|
|
|
|
|
if (n_written > 0 && data->n_vectors > 0)
|
|
|
|
|
{
|
|
|
|
|
data->vectors[0].size -= n_written;
|
|
|
|
|
data->vectors[0].buffer = ((guint8 *) data->vectors[0].buffer) + n_written;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data->n_vectors == 0)
|
|
|
|
|
{
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
g_output_stream_writev_async (G_OUTPUT_STREAM (stream),
|
|
|
|
|
data->vectors,
|
|
|
|
|
data->n_vectors,
|
|
|
|
|
priority,
|
|
|
|
|
cancellable,
|
|
|
|
|
writev_all_callback, g_steal_pointer (&task));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
writev_all_async_thread (GTask *task,
|
|
|
|
|
gpointer source_object,
|
|
|
|
|
gpointer task_data,
|
|
|
|
|
GCancellable *cancellable)
|
|
|
|
|
{
|
|
|
|
|
GOutputStream *stream = G_OUTPUT_STREAM (source_object);
|
|
|
|
|
AsyncWritevAll *data = task_data;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
|
|
if (g_output_stream_writev_all (stream, data->vectors, data->n_vectors, &data->bytes_written,
|
|
|
|
|
g_task_get_cancellable (task), &error))
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_error (task, g_steal_pointer (&error));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_writev_all_async:
|
|
|
|
|
* @stream: A #GOutputStream
|
|
|
|
|
* @vectors: (array length=n_vectors): the buffer containing the #GOutputVectors to write.
|
|
|
|
|
* @n_vectors: the number of vectors to write
|
|
|
|
|
* @io_priority: the I/O priority of the request
|
|
|
|
|
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
|
2023-02-20 21:43:02 +01:00
|
|
|
|
* @callback: (scope async): a #GAsyncReadyCallback
|
|
|
|
|
* to call when the request is satisfied
|
|
|
|
|
* @user_data: the data to pass to callback function
|
2018-09-13 12:10:36 +02:00
|
|
|
|
*
|
|
|
|
|
* Request an asynchronous write of the bytes contained in the @n_vectors @vectors into
|
|
|
|
|
* the stream. When the operation is finished @callback will be called.
|
|
|
|
|
* You can then call g_output_stream_writev_all_finish() to get the result of the
|
|
|
|
|
* operation.
|
|
|
|
|
*
|
|
|
|
|
* This is the asynchronous version of g_output_stream_writev_all().
|
|
|
|
|
*
|
|
|
|
|
* Call g_output_stream_writev_all_finish() to collect the result.
|
|
|
|
|
*
|
|
|
|
|
* Any outstanding I/O request with higher priority (lower numerical
|
|
|
|
|
* value) will be executed before an outstanding request with lower
|
|
|
|
|
* priority. Default priority is %G_PRIORITY_DEFAULT.
|
|
|
|
|
*
|
|
|
|
|
* Note that no copy of @vectors will be made, so it must stay valid
|
|
|
|
|
* until @callback is called. The content of the individual elements
|
|
|
|
|
* of @vectors might be changed by this function.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.60
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_output_stream_writev_all_async (GOutputStream *stream,
|
|
|
|
|
GOutputVector *vectors,
|
|
|
|
|
gsize n_vectors,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
AsyncWritevAll *data;
|
|
|
|
|
GTask *task;
|
|
|
|
|
gsize i, to_be_written = 0;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
|
|
|
|
|
g_return_if_fail (vectors != NULL || n_vectors == 0);
|
|
|
|
|
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
|
|
|
|
|
|
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
|
|
|
|
data = g_slice_new0 (AsyncWritevAll);
|
|
|
|
|
data->vectors = vectors;
|
|
|
|
|
data->n_vectors = n_vectors;
|
|
|
|
|
|
|
|
|
|
g_task_set_source_tag (task, g_output_stream_writev_all_async);
|
|
|
|
|
g_task_set_task_data (task, data, free_async_writev_all);
|
|
|
|
|
g_task_set_priority (task, io_priority);
|
|
|
|
|
|
|
|
|
|
/* We can't write more than G_MAXSIZE bytes overall, otherwise we
|
|
|
|
|
* would overflow the bytes_written counter */
|
|
|
|
|
for (i = 0; i < n_vectors; i++)
|
|
|
|
|
{
|
|
|
|
|
if (to_be_written > G_MAXSIZE - vectors[i].size)
|
|
|
|
|
{
|
|
|
|
|
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
|
|
|
|
|
_("Sum of vectors passed to %s too large"),
|
|
|
|
|
G_STRFUNC);
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
to_be_written += vectors[i].size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If async writes are going to be handled via the threadpool anyway
|
|
|
|
|
* then we may as well do it with a single dispatch instead of
|
|
|
|
|
* bouncing in and out.
|
|
|
|
|
*/
|
|
|
|
|
if (g_output_stream_async_writev_is_via_threads (stream))
|
|
|
|
|
{
|
|
|
|
|
g_task_run_in_thread (task, writev_all_async_thread);
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
writev_all_callback (G_OBJECT (stream), NULL, g_steal_pointer (&task));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_writev_all_finish:
|
|
|
|
|
* @stream: a #GOutputStream
|
|
|
|
|
* @result: a #GAsyncResult
|
|
|
|
|
* @bytes_written: (out) (optional): location to store the number of bytes that were written to the stream
|
|
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to ignore.
|
|
|
|
|
*
|
|
|
|
|
* Finishes an asynchronous stream write operation started with
|
|
|
|
|
* g_output_stream_writev_all_async().
|
|
|
|
|
*
|
|
|
|
|
* As a special exception to the normal conventions for functions that
|
|
|
|
|
* use #GError, if this function returns %FALSE (and sets @error) then
|
|
|
|
|
* @bytes_written will be set to the number of bytes that were
|
|
|
|
|
* successfully written before the error was encountered. This
|
|
|
|
|
* functionality is only available from C. If you need it from another
|
|
|
|
|
* language then you must write your own loop around
|
|
|
|
|
* g_output_stream_writev_async().
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE on success, %FALSE if there was an error
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.60
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_writev_all_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
gsize *bytes_written,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
GTask *task;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
task = G_TASK (result);
|
|
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
{
|
|
|
|
|
AsyncWritevAll *data = (AsyncWritevAll *)g_task_get_task_data (task);
|
|
|
|
|
|
|
|
|
|
*bytes_written = data->bytes_written;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return g_task_propagate_boolean (task, error);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-05 15:19:17 +02:00
|
|
|
|
static void
|
|
|
|
|
write_bytes_callback (GObject *stream,
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
gpointer user_data)
|
2012-04-05 15:19:17 +02:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task = user_data;
|
2012-04-05 15:19:17 +02:00
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gssize nwrote;
|
|
|
|
|
|
|
|
|
|
nwrote = g_output_stream_write_finish (G_OUTPUT_STREAM (stream),
|
2012-08-02 21:49:59 +02:00
|
|
|
|
result, &error);
|
2012-04-05 15:19:17 +02:00
|
|
|
|
if (nwrote == -1)
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_return_error (task, error);
|
2012-04-05 15:19:17 +02:00
|
|
|
|
else
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_return_int (task, nwrote);
|
|
|
|
|
g_object_unref (task);
|
2012-04-05 15:19:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_write_bytes_async:
|
|
|
|
|
* @stream: A #GOutputStream.
|
|
|
|
|
* @bytes: The bytes to write
|
|
|
|
|
* @io_priority: the io priority of the request.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
|
2023-02-20 21:43:02 +01:00
|
|
|
|
* @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
|
|
|
|
|
* to call when the request is satisfied
|
|
|
|
|
* @user_data: the data to pass to callback function
|
2012-04-05 15:19:17 +02:00
|
|
|
|
*
|
2013-10-02 16:32:03 +02:00
|
|
|
|
* This function is similar to g_output_stream_write_async(), but
|
|
|
|
|
* takes a #GBytes as input. Due to the refcounted nature of #GBytes,
|
|
|
|
|
* this allows the stream to avoid taking a copy of the data.
|
|
|
|
|
*
|
2014-02-01 02:34:33 +01:00
|
|
|
|
* However, note that this function may still perform partial writes,
|
|
|
|
|
* just like g_output_stream_write_async(). If that occurs, to continue
|
|
|
|
|
* writing, you will need to create a new #GBytes containing just the
|
|
|
|
|
* remaining bytes, using g_bytes_new_from_bytes(). Passing the same
|
|
|
|
|
* #GBytes instance multiple times potentially can result in duplicated
|
|
|
|
|
* data in the output stream.
|
2012-04-05 15:19:17 +02:00
|
|
|
|
*
|
|
|
|
|
* For the synchronous, blocking version of this function, see
|
|
|
|
|
* g_output_stream_write_bytes().
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
g_output_stream_write_bytes_async (GOutputStream *stream,
|
|
|
|
|
GBytes *bytes,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task;
|
2012-04-05 15:19:17 +02:00
|
|
|
|
gsize size;
|
|
|
|
|
gconstpointer data;
|
|
|
|
|
|
|
|
|
|
data = g_bytes_get_data (bytes, &size);
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
2016-06-17 01:39:38 +02:00
|
|
|
|
g_task_set_source_tag (task, g_output_stream_write_bytes_async);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_set_task_data (task, g_bytes_ref (bytes),
|
|
|
|
|
(GDestroyNotify) g_bytes_unref);
|
2012-04-05 15:19:17 +02:00
|
|
|
|
|
|
|
|
|
g_output_stream_write_async (stream,
|
|
|
|
|
data, size,
|
|
|
|
|
io_priority,
|
|
|
|
|
cancellable,
|
|
|
|
|
write_bytes_callback,
|
2012-08-02 21:49:59 +02:00
|
|
|
|
task);
|
2012-04-05 15:19:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_write_bytes_finish:
|
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
* @result: a #GAsyncResult.
|
|
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
|
|
|
|
* ignore.
|
|
|
|
|
*
|
|
|
|
|
* Finishes a stream write-from-#GBytes operation.
|
|
|
|
|
*
|
|
|
|
|
* Returns: a #gssize containing the number of bytes written to the stream.
|
|
|
|
|
**/
|
|
|
|
|
gssize
|
|
|
|
|
g_output_stream_write_bytes_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), -1);
|
2012-04-05 15:19:17 +02:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
return g_task_propagate_int (G_TASK (result), error);
|
2012-04-05 15:19:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
static void
|
2007-11-30 06:11:25 +01:00
|
|
|
|
async_ready_splice_callback_wrapper (GObject *source_object,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer _data)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
GOutputStream *stream = G_OUTPUT_STREAM (source_object);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
GTask *task = _data;
|
|
|
|
|
gssize nspliced;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
2007-12-04 09:57:04 +01:00
|
|
|
|
g_output_stream_clear_pending (stream);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
if (g_async_result_legacy_propagate_error (res, &error))
|
|
|
|
|
nspliced = -1;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
nspliced = class->splice_finish (stream, res, &error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nspliced >= 0)
|
|
|
|
|
g_task_return_int (task, nspliced);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_splice_async:
|
2007-11-27 15:00:13 +01:00
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
* @source: a #GInputStream.
|
|
|
|
|
* @flags: a set of #GOutputStreamSpliceFlags.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* @io_priority: the io priority of the request.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
|
2023-02-20 21:43:02 +01:00
|
|
|
|
* @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
|
|
|
|
|
* to call when the request is satisfied
|
|
|
|
|
* @user_data: the data to pass to callback function
|
2007-11-27 15:00:13 +01:00
|
|
|
|
*
|
|
|
|
|
* Splices a stream asynchronously.
|
2008-01-21 14:08:23 +01:00
|
|
|
|
* When the operation is finished @callback will be called.
|
|
|
|
|
* You can then call g_output_stream_splice_finish() to get the
|
|
|
|
|
* result of the operation.
|
|
|
|
|
*
|
|
|
|
|
* For the synchronous, blocking version of this function, see
|
|
|
|
|
* g_output_stream_splice().
|
2007-11-26 17:13:05 +01:00
|
|
|
|
**/
|
|
|
|
|
void
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_splice_async (GOutputStream *stream,
|
|
|
|
|
GInputStream *source,
|
|
|
|
|
GOutputStreamSpliceFlags flags,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task;
|
2007-12-04 09:57:04 +01:00
|
|
|
|
GError *error = NULL;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
|
|
|
|
|
g_return_if_fail (G_IS_INPUT_STREAM (source));
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
|
|
|
|
g_task_set_source_tag (task, g_output_stream_splice_async);
|
|
|
|
|
g_task_set_priority (task, io_priority);
|
|
|
|
|
g_task_set_task_data (task, g_object_ref (source), g_object_unref);
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
if (g_input_stream_is_closed (source))
|
|
|
|
|
{
|
2023-12-19 21:03:28 +01:00
|
|
|
|
g_task_return_new_error_literal (task,
|
|
|
|
|
G_IO_ERROR, G_IO_ERROR_CLOSED,
|
|
|
|
|
_("Source stream is already closed"));
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-04 09:57:04 +01:00
|
|
|
|
if (!g_output_stream_set_pending (stream, &error))
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
|
|
|
|
class->splice_async (stream, source, flags, io_priority, cancellable,
|
2012-08-02 21:49:59 +02:00
|
|
|
|
async_ready_splice_callback_wrapper, task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_splice_finish:
|
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
* @result: a #GAsyncResult.
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* ignore.
|
|
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
|
* Finishes an asynchronous stream splice operation.
|
|
|
|
|
*
|
2011-06-07 23:15:17 +02:00
|
|
|
|
* Returns: a #gssize of the number of bytes spliced. Note that if the
|
|
|
|
|
* number of bytes spliced is greater than %G_MAXSSIZE, then that
|
|
|
|
|
* will be returned, and there is no way to determine the actual
|
|
|
|
|
* number of bytes spliced.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
**/
|
|
|
|
|
gssize
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_splice_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_splice_async), FALSE);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
/* @result is always the GTask created by g_output_stream_splice_async();
|
|
|
|
|
* we called class->splice_finish() from async_ready_splice_callback_wrapper.
|
|
|
|
|
*/
|
|
|
|
|
return g_task_propagate_int (G_TASK (result), error);
|
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
static void
|
|
|
|
|
async_ready_flush_callback_wrapper (GObject *source_object,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GOutputStream *stream = G_OUTPUT_STREAM (source_object);
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
gboolean flushed;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
|
|
g_output_stream_clear_pending (stream);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
if (g_async_result_legacy_propagate_error (res, &error))
|
|
|
|
|
flushed = FALSE;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
flushed = class->flush_finish (stream, res, &error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flushed)
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_flush_async:
|
2007-11-27 15:00:13 +01:00
|
|
|
|
* @stream: a #GOutputStream.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* @io_priority: the io priority of the request.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
|
2023-02-20 21:43:02 +01:00
|
|
|
|
* @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
|
|
|
|
|
* to call when the request is satisfied
|
|
|
|
|
* @user_data: the data to pass to callback function
|
2007-11-27 15:00:13 +01:00
|
|
|
|
*
|
2011-12-05 22:40:18 +01:00
|
|
|
|
* Forces an asynchronous write of all user-space buffered data for
|
|
|
|
|
* the given @stream.
|
2008-01-21 14:08:23 +01:00
|
|
|
|
* For behaviour details see g_output_stream_flush().
|
|
|
|
|
*
|
|
|
|
|
* When the operation is finished @callback will be
|
|
|
|
|
* called. You can then call g_output_stream_flush_finish() to get the
|
|
|
|
|
* result of the operation.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
g_output_stream_flush_async (GOutputStream *stream,
|
2007-11-30 06:11:25 +01:00
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task;
|
2007-12-04 09:57:04 +01:00
|
|
|
|
GError *error = NULL;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
|
|
|
|
g_task_set_source_tag (task, g_output_stream_flush_async);
|
|
|
|
|
g_task_set_priority (task, io_priority);
|
|
|
|
|
|
2007-12-04 09:57:04 +01:00
|
|
|
|
if (!g_output_stream_set_pending (stream, &error))
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
|
|
|
|
if (class->flush_async == NULL)
|
|
|
|
|
{
|
2018-02-13 00:57:35 +01:00
|
|
|
|
g_output_stream_clear_pending (stream);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class->flush_async (stream, io_priority, cancellable,
|
2012-08-02 21:49:59 +02:00
|
|
|
|
async_ready_flush_callback_wrapper, task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_flush_finish:
|
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
* @result: a GAsyncResult.
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* ignore.
|
2007-11-27 15:00:13 +01:00
|
|
|
|
*
|
|
|
|
|
* Finishes flushing an output stream.
|
|
|
|
|
*
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* Returns: %TRUE if flush operation succeeded, %FALSE otherwise.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
**/
|
|
|
|
|
gboolean
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_flush_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_flush_async), FALSE);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
/* @result is always the GTask created by g_output_stream_flush_async();
|
|
|
|
|
* we called class->flush_finish() from async_ready_flush_callback_wrapper.
|
|
|
|
|
*/
|
|
|
|
|
return g_task_propagate_boolean (G_TASK (result), error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
async_ready_close_callback_wrapper (GObject *source_object,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GOutputStream *stream = G_OUTPUT_STREAM (source_object);
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
GError *error = g_task_get_task_data (task);
|
|
|
|
|
|
|
|
|
|
stream->priv->closing = FALSE;
|
|
|
|
|
stream->priv->closed = TRUE;
|
|
|
|
|
|
|
|
|
|
if (!error && !g_async_result_legacy_propagate_error (res, &error))
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
|
|
|
|
class->close_finish (stream, res,
|
|
|
|
|
error ? NULL : &error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
if (error != NULL)
|
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
static void
|
|
|
|
|
async_ready_close_flushed_callback_wrapper (GObject *source_object,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GOutputStream *stream = G_OUTPUT_STREAM (source_object);
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
2012-12-15 15:55:39 +01:00
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
if (!g_async_result_legacy_propagate_error (res, &error))
|
|
|
|
|
{
|
|
|
|
|
class->flush_finish (stream, res, &error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* propagate the possible error */
|
|
|
|
|
if (error)
|
|
|
|
|
g_task_set_task_data (task, error, NULL);
|
|
|
|
|
|
|
|
|
|
/* we still close, even if there was a flush error */
|
|
|
|
|
class->close_async (stream,
|
|
|
|
|
g_task_get_priority (task),
|
|
|
|
|
g_task_get_cancellable (task),
|
|
|
|
|
async_ready_close_callback_wrapper, task);
|
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2013-02-18 14:58:58 +01:00
|
|
|
|
static void
|
|
|
|
|
real_close_async_cb (GObject *source_object,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GOutputStream *stream = G_OUTPUT_STREAM (source_object);
|
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
|
|
g_output_stream_clear_pending (stream);
|
|
|
|
|
|
|
|
|
|
ret = g_output_stream_internal_close_finish (stream, res, &error);
|
|
|
|
|
|
|
|
|
|
if (error != NULL)
|
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_boolean (task, ret);
|
|
|
|
|
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_close_async:
|
|
|
|
|
* @stream: A #GOutputStream.
|
2007-11-28 07:01:13 +01:00
|
|
|
|
* @io_priority: the io priority of the request.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): optional cancellable object
|
2023-02-20 21:43:02 +01:00
|
|
|
|
* @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
|
|
|
|
|
* to call when the request is satisfied
|
|
|
|
|
* @user_data: the data to pass to callback function
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2007-11-30 06:11:25 +01:00
|
|
|
|
* Requests an asynchronous close of the stream, releasing resources
|
|
|
|
|
* related to it. When the operation is finished @callback will be
|
2008-01-21 14:08:23 +01:00
|
|
|
|
* called. You can then call g_output_stream_close_finish() to get
|
|
|
|
|
* the result of the operation.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
|
|
|
|
* For behaviour details see g_output_stream_close().
|
|
|
|
|
*
|
2017-01-19 09:21:16 +01:00
|
|
|
|
* The asynchronous methods have a default fallback that uses threads
|
2007-11-30 06:11:25 +01:00
|
|
|
|
* to implement asynchronicity, so they are optional for inheriting
|
|
|
|
|
* classes. However, if you override one you must override all.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
**/
|
|
|
|
|
void
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_close_async (GOutputStream *stream,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task;
|
2007-12-04 09:57:04 +01:00
|
|
|
|
GError *error = NULL;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
|
|
|
|
g_task_set_source_tag (task, g_output_stream_close_async);
|
|
|
|
|
g_task_set_priority (task, io_priority);
|
|
|
|
|
|
2013-02-18 14:58:58 +01:00
|
|
|
|
if (!g_output_stream_set_pending (stream, &error))
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2013-02-18 14:58:58 +01:00
|
|
|
|
g_task_return_error (task, error);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 14:58:58 +01:00
|
|
|
|
g_output_stream_internal_close_async (stream, io_priority, cancellable,
|
|
|
|
|
real_close_async_cb, task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Must always be called inside
|
|
|
|
|
* g_output_stream_set_pending()/g_output_stream_clear_pending().
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_output_stream_internal_close_async (GOutputStream *stream,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
GTask *task;
|
|
|
|
|
|
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
|
|
|
|
g_task_set_source_tag (task, g_output_stream_internal_close_async);
|
|
|
|
|
g_task_set_priority (task, io_priority);
|
|
|
|
|
|
|
|
|
|
if (stream->priv->closed)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2013-02-18 14:58:58 +01:00
|
|
|
|
g_task_return_boolean (task, TRUE);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2013-02-18 14:58:58 +01:00
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
2009-11-23 15:02:50 +01:00
|
|
|
|
stream->priv->closing = TRUE;
|
2010-05-10 23:49:41 +02:00
|
|
|
|
|
|
|
|
|
/* Call close_async directly if there is no need to flush, or if the flush
|
|
|
|
|
can be done sync (in the output stream async close thread) */
|
|
|
|
|
if (class->flush_async == NULL ||
|
|
|
|
|
(class->flush_async == g_output_stream_real_flush_async &&
|
|
|
|
|
(class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
|
|
|
|
|
{
|
|
|
|
|
class->close_async (stream, io_priority, cancellable,
|
2012-08-02 21:49:59 +02:00
|
|
|
|
async_ready_close_callback_wrapper, task);
|
2010-05-10 23:49:41 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* First do an async flush, then do the async close in the callback
|
|
|
|
|
wrapper (see async_ready_close_flushed_callback_wrapper) */
|
|
|
|
|
class->flush_async (stream, io_priority, cancellable,
|
2012-08-02 21:49:59 +02:00
|
|
|
|
async_ready_close_flushed_callback_wrapper, task);
|
2010-05-10 23:49:41 +02:00
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 14:58:58 +01:00
|
|
|
|
static gboolean
|
|
|
|
|
g_output_stream_internal_close_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_internal_close_async), FALSE);
|
|
|
|
|
|
|
|
|
|
return g_task_propagate_boolean (G_TASK (result), error);
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_close_finish:
|
2007-11-27 15:00:13 +01:00
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
* @result: a #GAsyncResult.
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2007-11-26 17:13:05 +01:00
|
|
|
|
* ignore.
|
2007-11-27 15:00:13 +01:00
|
|
|
|
*
|
|
|
|
|
* Closes an output stream.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
**/
|
|
|
|
|
gboolean
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_close_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_close_async), FALSE);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
/* @result is always the GTask created by g_output_stream_close_async();
|
|
|
|
|
* we called class->close_finish() from async_ready_close_callback_wrapper.
|
|
|
|
|
*/
|
|
|
|
|
return g_task_propagate_boolean (G_TASK (result), error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_is_closed:
|
2007-11-27 15:00:13 +01:00
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
*
|
|
|
|
|
* Checks if an output stream has already been closed.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if @stream is closed. %FALSE otherwise.
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_is_closed (GOutputStream *stream)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
|
|
|
|
|
|
|
|
|
|
return stream->priv->closed;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-23 15:02:50 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_is_closing:
|
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
*
|
|
|
|
|
* Checks if an output stream is being closed. This can be
|
|
|
|
|
* used inside e.g. a flush implementation to see if the
|
|
|
|
|
* flush (or other i/o operation) is called from within
|
|
|
|
|
* the closing operation.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if @stream is being closed. %FALSE otherwise.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.24
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_is_closing (GOutputStream *stream)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
|
|
|
|
|
|
|
|
|
|
return stream->priv->closing;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_has_pending:
|
2007-11-27 15:00:13 +01:00
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
*
|
2017-01-19 09:21:16 +01:00
|
|
|
|
* Checks if an output stream has pending actions.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if @stream has pending actions.
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_has_pending (GOutputStream *stream)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
|
|
|
|
|
return stream->priv->pending;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_set_pending:
|
2007-11-27 15:00:13 +01:00
|
|
|
|
* @stream: a #GOutputStream.
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2007-12-04 09:57:04 +01:00
|
|
|
|
* ignore.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
*
|
2007-12-04 09:57:04 +01:00
|
|
|
|
* Sets @stream to have actions pending. If the pending flag is
|
|
|
|
|
* already set or @stream is closed, it will return %FALSE and set
|
|
|
|
|
* @error.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: %TRUE if pending was previously unset and is now set.
|
2007-11-26 17:13:05 +01:00
|
|
|
|
**/
|
2007-12-04 09:57:04 +01:00
|
|
|
|
gboolean
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_set_pending (GOutputStream *stream,
|
2007-12-04 09:57:04 +01:00
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
|
|
|
|
|
if (stream->priv->closed)
|
|
|
|
|
{
|
2008-06-16 18:53:58 +02:00
|
|
|
|
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
|
|
|
|
|
_("Stream is already closed"));
|
2007-12-04 09:57:04 +01:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stream->priv->pending)
|
|
|
|
|
{
|
2008-06-16 18:53:58 +02:00
|
|
|
|
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
|
|
|
|
|
/* Translators: This is an error you get if there is
|
|
|
|
|
* already an operation running against this stream when
|
|
|
|
|
* you try to start one */
|
|
|
|
|
_("Stream has outstanding operation"));
|
2007-12-04 09:57:04 +01:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream->priv->pending = TRUE;
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_output_stream_clear_pending:
|
|
|
|
|
* @stream: output stream
|
|
|
|
|
*
|
|
|
|
|
* Clears the pending flag on @stream.
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
g_output_stream_clear_pending (GOutputStream *stream)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
|
|
|
|
|
|
2007-12-04 09:57:04 +01:00
|
|
|
|
stream->priv->pending = FALSE;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-20 18:54:09 +01:00
|
|
|
|
/*< internal >
|
2013-02-24 00:42:49 +01:00
|
|
|
|
* g_output_stream_async_write_is_via_threads:
|
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
*
|
2017-01-19 09:21:16 +01:00
|
|
|
|
* Checks if an output stream's write_async function uses threads.
|
2013-02-24 00:42:49 +01:00
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if @stream's write_async function uses threads.
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_async_write_is_via_threads (GOutputStream *stream)
|
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
|
|
|
|
return (class->write_async == g_output_stream_real_write_async &&
|
|
|
|
|
!(G_IS_POLLABLE_OUTPUT_STREAM (stream) &&
|
|
|
|
|
g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (stream))));
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:10:36 +02:00
|
|
|
|
/*< internal >
|
|
|
|
|
* g_output_stream_async_writev_is_via_threads:
|
|
|
|
|
* @stream: a #GOutputStream.
|
|
|
|
|
*
|
|
|
|
|
* Checks if an output stream's writev_async function uses threads.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if @stream's writev_async function uses threads.
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_async_writev_is_via_threads (GOutputStream *stream)
|
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
|
|
|
|
return (class->writev_async == g_output_stream_real_writev_async &&
|
|
|
|
|
!(G_IS_POLLABLE_OUTPUT_STREAM (stream) &&
|
|
|
|
|
g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (stream))));
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-20 14:09:33 +01:00
|
|
|
|
/*< internal >
|
|
|
|
|
* g_output_stream_async_close_is_via_threads:
|
|
|
|
|
* @stream: output stream
|
|
|
|
|
*
|
|
|
|
|
* Checks if an output stream's close_async function uses threads.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if @stream's close_async function uses threads.
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
g_output_stream_async_close_is_via_threads (GOutputStream *stream)
|
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
|
|
|
|
return class->close_async == g_output_stream_real_close_async;
|
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2018-09-13 12:10:36 +02:00
|
|
|
|
/********************************************
|
|
|
|
|
* Default implementation of sync ops *
|
|
|
|
|
********************************************/
|
|
|
|
|
static gboolean
|
|
|
|
|
g_output_stream_real_writev (GOutputStream *stream,
|
|
|
|
|
const GOutputVector *vectors,
|
|
|
|
|
gsize n_vectors,
|
|
|
|
|
gsize *bytes_written,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
gsize _bytes_written = 0;
|
|
|
|
|
gsize i;
|
|
|
|
|
GError *err = NULL;
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
*bytes_written = 0;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < n_vectors; i++)
|
|
|
|
|
{
|
|
|
|
|
gssize res = 0;
|
|
|
|
|
|
|
|
|
|
/* Would we overflow here? In that case simply return and let the caller
|
|
|
|
|
* handle this like a short write */
|
|
|
|
|
if (_bytes_written > G_MAXSIZE - vectors[i].size)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
res = class->write_fn (stream, vectors[i].buffer, vectors[i].size, cancellable, &err);
|
|
|
|
|
|
|
|
|
|
if (res == -1)
|
|
|
|
|
{
|
|
|
|
|
/* If we already wrote something we handle this like a short write
|
|
|
|
|
* and assume that on the next call the same error happens again, or
|
|
|
|
|
* everything finishes successfully without data loss then
|
|
|
|
|
*/
|
|
|
|
|
if (_bytes_written > 0)
|
|
|
|
|
{
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
*bytes_written = _bytes_written;
|
|
|
|
|
|
|
|
|
|
g_clear_error (&err);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_propagate_error (error, err);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_bytes_written += res;
|
|
|
|
|
/* if we had a short write break the loop here */
|
2020-11-16 23:51:33 +01:00
|
|
|
|
if ((gsize) res < vectors[i].size)
|
2018-09-13 12:10:36 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
*bytes_written = _bytes_written;
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
/********************************************
|
|
|
|
|
* Default implementation of async ops *
|
|
|
|
|
********************************************/
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
const void *buffer;
|
|
|
|
|
gsize count_requested;
|
|
|
|
|
gssize count_written;
|
|
|
|
|
} WriteData;
|
|
|
|
|
|
2012-04-27 15:27:38 +02:00
|
|
|
|
static void
|
|
|
|
|
free_write_data (WriteData *op)
|
|
|
|
|
{
|
|
|
|
|
g_slice_free (WriteData, op);
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
static void
|
2012-08-02 21:49:59 +02:00
|
|
|
|
write_async_thread (GTask *task,
|
|
|
|
|
gpointer source_object,
|
|
|
|
|
gpointer task_data,
|
|
|
|
|
GCancellable *cancellable)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GOutputStream *stream = source_object;
|
|
|
|
|
WriteData *op = task_data;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
GError *error = NULL;
|
2012-08-02 21:49:59 +02:00
|
|
|
|
gssize count_written;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
count_written = class->write_fn (stream, op->buffer, op->count_requested,
|
|
|
|
|
cancellable, &error);
|
|
|
|
|
if (count_written == -1)
|
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_int (task, count_written);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-04 22:46:29 +01:00
|
|
|
|
static void write_async_pollable (GPollableOutputStream *stream,
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task);
|
2012-02-04 22:46:29 +01:00
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
write_async_pollable_ready (GPollableOutputStream *stream,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task = user_data;
|
2012-02-04 22:46:29 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
write_async_pollable (stream, task);
|
2012-02-04 22:46:29 +01:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
write_async_pollable (GPollableOutputStream *stream,
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task)
|
2012-02-04 22:46:29 +01:00
|
|
|
|
{
|
|
|
|
|
GError *error = NULL;
|
2012-08-02 21:49:59 +02:00
|
|
|
|
WriteData *op = g_task_get_task_data (task);
|
|
|
|
|
gssize count_written;
|
2012-02-04 22:46:29 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
if (g_task_return_error_if_cancelled (task))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
count_written = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
|
|
|
|
|
write_nonblocking (stream, op->buffer, op->count_requested, &error);
|
2012-02-04 22:46:29 +01:00
|
|
|
|
|
|
|
|
|
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
|
|
|
|
|
{
|
|
|
|
|
GSource *source;
|
|
|
|
|
|
|
|
|
|
g_error_free (error);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
|
|
|
|
|
source = g_pollable_output_stream_create_source (stream,
|
|
|
|
|
g_task_get_cancellable (task));
|
|
|
|
|
g_task_attach_source (task, source,
|
|
|
|
|
(GSourceFunc) write_async_pollable_ready);
|
2012-02-04 22:46:29 +01:00
|
|
|
|
g_source_unref (source);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
if (count_written == -1)
|
|
|
|
|
g_task_return_error (task, error);
|
2012-02-04 22:46:29 +01:00
|
|
|
|
else
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_return_int (task, count_written);
|
2012-02-04 22:46:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
static void
|
|
|
|
|
g_output_stream_real_write_async (GOutputStream *stream,
|
2007-11-30 06:11:25 +01:00
|
|
|
|
const void *buffer,
|
|
|
|
|
gsize count,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
WriteData *op;
|
|
|
|
|
|
2012-04-27 15:27:38 +02:00
|
|
|
|
op = g_slice_new0 (WriteData);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
|
|
|
|
g_task_set_check_cancellable (task, FALSE);
|
|
|
|
|
g_task_set_task_data (task, op, (GDestroyNotify) free_write_data);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
op->buffer = buffer;
|
|
|
|
|
op->count_requested = count;
|
2012-08-02 21:49:59 +02:00
|
|
|
|
|
2013-02-24 00:42:49 +01:00
|
|
|
|
if (!g_output_stream_async_write_is_via_threads (stream))
|
2012-08-02 21:49:59 +02:00
|
|
|
|
write_async_pollable (G_POLLABLE_OUTPUT_STREAM (stream), task);
|
2012-02-04 22:46:29 +01:00
|
|
|
|
else
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_run_in_thread (task, write_async_thread);
|
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gssize
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_real_write_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
2012-06-11 19:44:19 +02:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
return g_task_propagate_int (G_TASK (result), error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:10:36 +02:00
|
|
|
|
typedef struct {
|
|
|
|
|
const GOutputVector *vectors;
|
|
|
|
|
gsize n_vectors; /* (unowned) */
|
|
|
|
|
gsize bytes_written;
|
|
|
|
|
} WritevData;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
free_writev_data (WritevData *op)
|
|
|
|
|
{
|
|
|
|
|
g_slice_free (WritevData, op);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
writev_async_thread (GTask *task,
|
|
|
|
|
gpointer source_object,
|
|
|
|
|
gpointer task_data,
|
|
|
|
|
GCancellable *cancellable)
|
|
|
|
|
{
|
|
|
|
|
GOutputStream *stream = source_object;
|
|
|
|
|
WritevData *op = task_data;
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
|
|
|
|
res = class->writev_fn (stream, op->vectors, op->n_vectors,
|
|
|
|
|
&op->bytes_written, cancellable, &error);
|
|
|
|
|
|
|
|
|
|
g_warn_if_fail (res || op->bytes_written == 0);
|
|
|
|
|
g_warn_if_fail (res || error != NULL);
|
|
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
|
g_task_return_error (task, g_steal_pointer (&error));
|
|
|
|
|
else
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void writev_async_pollable (GPollableOutputStream *stream,
|
|
|
|
|
GTask *task);
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
writev_async_pollable_ready (GPollableOutputStream *stream,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
|
|
|
|
|
writev_async_pollable (stream, task);
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
writev_async_pollable (GPollableOutputStream *stream,
|
|
|
|
|
GTask *task)
|
|
|
|
|
{
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
WritevData *op = g_task_get_task_data (task);
|
|
|
|
|
GPollableReturn res;
|
|
|
|
|
gsize bytes_written = 0;
|
|
|
|
|
|
|
|
|
|
if (g_task_return_error_if_cancelled (task))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
res = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
|
|
|
|
|
writev_nonblocking (stream, op->vectors, op->n_vectors, &bytes_written, &error);
|
|
|
|
|
|
|
|
|
|
switch (res)
|
|
|
|
|
{
|
|
|
|
|
case G_POLLABLE_RETURN_WOULD_BLOCK:
|
|
|
|
|
{
|
|
|
|
|
GSource *source;
|
|
|
|
|
|
|
|
|
|
g_warn_if_fail (error == NULL);
|
|
|
|
|
g_warn_if_fail (bytes_written == 0);
|
|
|
|
|
|
|
|
|
|
source = g_pollable_output_stream_create_source (stream,
|
|
|
|
|
g_task_get_cancellable (task));
|
|
|
|
|
g_task_attach_source (task, source,
|
|
|
|
|
(GSourceFunc) writev_async_pollable_ready);
|
|
|
|
|
g_source_unref (source);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case G_POLLABLE_RETURN_OK:
|
|
|
|
|
g_warn_if_fail (error == NULL);
|
|
|
|
|
op->bytes_written = bytes_written;
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
break;
|
|
|
|
|
case G_POLLABLE_RETURN_FAILED:
|
|
|
|
|
g_warn_if_fail (bytes_written == 0);
|
|
|
|
|
g_warn_if_fail (error != NULL);
|
|
|
|
|
g_task_return_error (task, g_steal_pointer (&error));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_output_stream_real_writev_async (GOutputStream *stream,
|
|
|
|
|
const GOutputVector *vectors,
|
|
|
|
|
gsize n_vectors,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GTask *task;
|
|
|
|
|
WritevData *op;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
|
|
op = g_slice_new0 (WritevData);
|
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
|
|
|
|
op->vectors = vectors;
|
|
|
|
|
op->n_vectors = n_vectors;
|
|
|
|
|
|
|
|
|
|
g_task_set_check_cancellable (task, FALSE);
|
|
|
|
|
g_task_set_source_tag (task, g_output_stream_writev_async);
|
|
|
|
|
g_task_set_priority (task, io_priority);
|
|
|
|
|
g_task_set_task_data (task, op, (GDestroyNotify) free_writev_data);
|
|
|
|
|
|
|
|
|
|
if (n_vectors == 0)
|
|
|
|
|
{
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!g_output_stream_set_pending (stream, &error))
|
|
|
|
|
{
|
|
|
|
|
g_task_return_error (task, g_steal_pointer (&error));
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!g_output_stream_async_writev_is_via_threads (stream))
|
|
|
|
|
writev_async_pollable (G_POLLABLE_OUTPUT_STREAM (stream), task);
|
|
|
|
|
else
|
|
|
|
|
g_task_run_in_thread (task, writev_async_thread);
|
|
|
|
|
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
g_output_stream_real_writev_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
gsize *bytes_written,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
GTask *task;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
|
|
|
|
g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_writev_async), FALSE);
|
|
|
|
|
|
|
|
|
|
g_output_stream_clear_pending (stream);
|
|
|
|
|
|
|
|
|
|
task = G_TASK (result);
|
|
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
|
{
|
|
|
|
|
WritevData *op = g_task_get_task_data (task);
|
|
|
|
|
|
|
|
|
|
*bytes_written = op->bytes_written;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return g_task_propagate_boolean (task, error);
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
|
typedef struct {
|
|
|
|
|
GInputStream *source;
|
|
|
|
|
GOutputStreamSpliceFlags flags;
|
goutputstream: Check individual close operations after splice
After a splice operation is finished, it attempts to 1) close input/output
streams, as per the given flags, and 2) return the operation result (maybe
an error, too).
However, if the operation gets cancelled early and the streams indirectly
closed, the splice operation will try to close both descriptors and return
on the task when both are already closed. The catch here is that getting the
streams closed under its feet is possible, so the completion callback would
find both streams closed after returning on the first close operation and
return the error, but then the second operation could be able to trigger
a second error which would be returned as well.
What happens here is up to further race conditions, if the task didn't
return yet, the returned error will be simply replaced (but the old one not
freed...), if it did already return, it'll result in:
GLib-GIO-FATAL-CRITICAL: g_task_return_error: assertion '!task->ever_returned' failed
Fix this by flagging the close_async() callbacks, and checking that both
close operations did return, instead of checking that both streams are
closed by who knows.
This error triggers a semi-frequent CI failure in tracker, see the summary at
https://gitlab.gnome.org/GNOME/tracker/-/issues/240
2020-08-24 12:00:44 +02:00
|
|
|
|
guint istream_closed : 1;
|
|
|
|
|
guint ostream_closed : 1;
|
2013-02-18 15:12:50 +01:00
|
|
|
|
gssize n_read;
|
|
|
|
|
gssize n_written;
|
|
|
|
|
gsize bytes_copied;
|
|
|
|
|
GError *error;
|
|
|
|
|
guint8 *buffer;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
} SpliceData;
|
|
|
|
|
|
|
|
|
|
static void
|
2012-08-02 21:49:59 +02:00
|
|
|
|
free_splice_data (SpliceData *op)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2013-02-18 15:12:50 +01:00
|
|
|
|
g_clear_pointer (&op->buffer, g_free);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_object_unref (op->source);
|
2013-02-18 15:12:50 +01:00
|
|
|
|
g_clear_error (&op->error);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_free (op);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 15:12:50 +01:00
|
|
|
|
static void
|
|
|
|
|
real_splice_async_complete_cb (GTask *task)
|
|
|
|
|
{
|
|
|
|
|
SpliceData *op = g_task_get_task_data (task);
|
|
|
|
|
|
|
|
|
|
if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE &&
|
goutputstream: Check individual close operations after splice
After a splice operation is finished, it attempts to 1) close input/output
streams, as per the given flags, and 2) return the operation result (maybe
an error, too).
However, if the operation gets cancelled early and the streams indirectly
closed, the splice operation will try to close both descriptors and return
on the task when both are already closed. The catch here is that getting the
streams closed under its feet is possible, so the completion callback would
find both streams closed after returning on the first close operation and
return the error, but then the second operation could be able to trigger
a second error which would be returned as well.
What happens here is up to further race conditions, if the task didn't
return yet, the returned error will be simply replaced (but the old one not
freed...), if it did already return, it'll result in:
GLib-GIO-FATAL-CRITICAL: g_task_return_error: assertion '!task->ever_returned' failed
Fix this by flagging the close_async() callbacks, and checking that both
close operations did return, instead of checking that both streams are
closed by who knows.
This error triggers a semi-frequent CI failure in tracker, see the summary at
https://gitlab.gnome.org/GNOME/tracker/-/issues/240
2020-08-24 12:00:44 +02:00
|
|
|
|
!op->istream_closed)
|
2013-02-18 15:12:50 +01:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET &&
|
goutputstream: Check individual close operations after splice
After a splice operation is finished, it attempts to 1) close input/output
streams, as per the given flags, and 2) return the operation result (maybe
an error, too).
However, if the operation gets cancelled early and the streams indirectly
closed, the splice operation will try to close both descriptors and return
on the task when both are already closed. The catch here is that getting the
streams closed under its feet is possible, so the completion callback would
find both streams closed after returning on the first close operation and
return the error, but then the second operation could be able to trigger
a second error which would be returned as well.
What happens here is up to further race conditions, if the task didn't
return yet, the returned error will be simply replaced (but the old one not
freed...), if it did already return, it'll result in:
GLib-GIO-FATAL-CRITICAL: g_task_return_error: assertion '!task->ever_returned' failed
Fix this by flagging the close_async() callbacks, and checking that both
close operations did return, instead of checking that both streams are
closed by who knows.
This error triggers a semi-frequent CI failure in tracker, see the summary at
https://gitlab.gnome.org/GNOME/tracker/-/issues/240
2020-08-24 12:00:44 +02:00
|
|
|
|
!op->ostream_closed)
|
2013-02-18 15:12:50 +01:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (op->error != NULL)
|
|
|
|
|
{
|
|
|
|
|
g_task_return_error (task, op->error);
|
|
|
|
|
op->error = NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_task_return_int (task, op->bytes_copied);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
real_splice_async_close_input_cb (GObject *source,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GTask *task = user_data;
|
goutputstream: Check individual close operations after splice
After a splice operation is finished, it attempts to 1) close input/output
streams, as per the given flags, and 2) return the operation result (maybe
an error, too).
However, if the operation gets cancelled early and the streams indirectly
closed, the splice operation will try to close both descriptors and return
on the task when both are already closed. The catch here is that getting the
streams closed under its feet is possible, so the completion callback would
find both streams closed after returning on the first close operation and
return the error, but then the second operation could be able to trigger
a second error which would be returned as well.
What happens here is up to further race conditions, if the task didn't
return yet, the returned error will be simply replaced (but the old one not
freed...), if it did already return, it'll result in:
GLib-GIO-FATAL-CRITICAL: g_task_return_error: assertion '!task->ever_returned' failed
Fix this by flagging the close_async() callbacks, and checking that both
close operations did return, instead of checking that both streams are
closed by who knows.
This error triggers a semi-frequent CI failure in tracker, see the summary at
https://gitlab.gnome.org/GNOME/tracker/-/issues/240
2020-08-24 12:00:44 +02:00
|
|
|
|
SpliceData *op = g_task_get_task_data (task);
|
2013-02-18 15:12:50 +01:00
|
|
|
|
|
|
|
|
|
g_input_stream_close_finish (G_INPUT_STREAM (source), res, NULL);
|
goutputstream: Check individual close operations after splice
After a splice operation is finished, it attempts to 1) close input/output
streams, as per the given flags, and 2) return the operation result (maybe
an error, too).
However, if the operation gets cancelled early and the streams indirectly
closed, the splice operation will try to close both descriptors and return
on the task when both are already closed. The catch here is that getting the
streams closed under its feet is possible, so the completion callback would
find both streams closed after returning on the first close operation and
return the error, but then the second operation could be able to trigger
a second error which would be returned as well.
What happens here is up to further race conditions, if the task didn't
return yet, the returned error will be simply replaced (but the old one not
freed...), if it did already return, it'll result in:
GLib-GIO-FATAL-CRITICAL: g_task_return_error: assertion '!task->ever_returned' failed
Fix this by flagging the close_async() callbacks, and checking that both
close operations did return, instead of checking that both streams are
closed by who knows.
This error triggers a semi-frequent CI failure in tracker, see the summary at
https://gitlab.gnome.org/GNOME/tracker/-/issues/240
2020-08-24 12:00:44 +02:00
|
|
|
|
op->istream_closed = TRUE;
|
2013-02-18 15:12:50 +01:00
|
|
|
|
|
|
|
|
|
real_splice_async_complete_cb (task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
real_splice_async_close_output_cb (GObject *source,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GTask *task = G_TASK (user_data);
|
|
|
|
|
SpliceData *op = g_task_get_task_data (task);
|
|
|
|
|
GError **error = (op->error == NULL) ? &op->error : NULL;
|
|
|
|
|
|
|
|
|
|
g_output_stream_internal_close_finish (G_OUTPUT_STREAM (source), res, error);
|
goutputstream: Check individual close operations after splice
After a splice operation is finished, it attempts to 1) close input/output
streams, as per the given flags, and 2) return the operation result (maybe
an error, too).
However, if the operation gets cancelled early and the streams indirectly
closed, the splice operation will try to close both descriptors and return
on the task when both are already closed. The catch here is that getting the
streams closed under its feet is possible, so the completion callback would
find both streams closed after returning on the first close operation and
return the error, but then the second operation could be able to trigger
a second error which would be returned as well.
What happens here is up to further race conditions, if the task didn't
return yet, the returned error will be simply replaced (but the old one not
freed...), if it did already return, it'll result in:
GLib-GIO-FATAL-CRITICAL: g_task_return_error: assertion '!task->ever_returned' failed
Fix this by flagging the close_async() callbacks, and checking that both
close operations did return, instead of checking that both streams are
closed by who knows.
This error triggers a semi-frequent CI failure in tracker, see the summary at
https://gitlab.gnome.org/GNOME/tracker/-/issues/240
2020-08-24 12:00:44 +02:00
|
|
|
|
op->ostream_closed = TRUE;
|
2013-02-18 15:12:50 +01:00
|
|
|
|
|
|
|
|
|
real_splice_async_complete_cb (task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
real_splice_async_complete (GTask *task)
|
|
|
|
|
{
|
|
|
|
|
SpliceData *op = g_task_get_task_data (task);
|
|
|
|
|
gboolean done = TRUE;
|
|
|
|
|
|
|
|
|
|
if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
|
|
|
|
|
{
|
|
|
|
|
done = FALSE;
|
|
|
|
|
g_input_stream_close_async (op->source, g_task_get_priority (task),
|
|
|
|
|
g_task_get_cancellable (task),
|
|
|
|
|
real_splice_async_close_input_cb, task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
|
|
|
|
|
{
|
|
|
|
|
done = FALSE;
|
|
|
|
|
g_output_stream_internal_close_async (g_task_get_source_object (task),
|
|
|
|
|
g_task_get_priority (task),
|
|
|
|
|
g_task_get_cancellable (task),
|
|
|
|
|
real_splice_async_close_output_cb,
|
|
|
|
|
task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
|
real_splice_async_complete_cb (task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void real_splice_async_read_cb (GObject *source,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data);
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
real_splice_async_write_cb (GObject *source,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
GTask *task = G_TASK (user_data);
|
|
|
|
|
SpliceData *op = g_task_get_task_data (task);
|
|
|
|
|
gssize ret;
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
|
|
|
|
|
|
|
|
|
|
ret = class->write_finish (G_OUTPUT_STREAM (source), res, &op->error);
|
|
|
|
|
|
|
|
|
|
if (ret == -1)
|
|
|
|
|
{
|
|
|
|
|
real_splice_async_complete (task);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
op->n_written += ret;
|
|
|
|
|
op->bytes_copied += ret;
|
|
|
|
|
if (op->bytes_copied > G_MAXSSIZE)
|
|
|
|
|
op->bytes_copied = G_MAXSSIZE;
|
|
|
|
|
|
|
|
|
|
if (op->n_written < op->n_read)
|
|
|
|
|
{
|
|
|
|
|
class->write_async (g_task_get_source_object (task),
|
|
|
|
|
op->buffer + op->n_written,
|
|
|
|
|
op->n_read - op->n_written,
|
|
|
|
|
g_task_get_priority (task),
|
|
|
|
|
g_task_get_cancellable (task),
|
|
|
|
|
real_splice_async_write_cb, task);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_input_stream_read_async (op->source, op->buffer, 8192,
|
|
|
|
|
g_task_get_priority (task),
|
|
|
|
|
g_task_get_cancellable (task),
|
|
|
|
|
real_splice_async_read_cb, task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
real_splice_async_read_cb (GObject *source,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
GTask *task = G_TASK (user_data);
|
|
|
|
|
SpliceData *op = g_task_get_task_data (task);
|
|
|
|
|
gssize ret;
|
|
|
|
|
|
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
|
|
|
|
|
|
|
|
|
|
ret = g_input_stream_read_finish (op->source, res, &op->error);
|
|
|
|
|
if (ret == -1 || ret == 0)
|
|
|
|
|
{
|
|
|
|
|
real_splice_async_complete (task);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
op->n_read = ret;
|
|
|
|
|
op->n_written = 0;
|
|
|
|
|
|
|
|
|
|
class->write_async (g_task_get_source_object (task), op->buffer,
|
|
|
|
|
op->n_read, g_task_get_priority (task),
|
|
|
|
|
g_task_get_cancellable (task),
|
|
|
|
|
real_splice_async_write_cb, task);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
static void
|
|
|
|
|
splice_async_thread (GTask *task,
|
|
|
|
|
gpointer source_object,
|
|
|
|
|
gpointer task_data,
|
|
|
|
|
GCancellable *cancellable)
|
|
|
|
|
{
|
|
|
|
|
GOutputStream *stream = source_object;
|
|
|
|
|
SpliceData *op = task_data;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
GError *error = NULL;
|
2012-08-02 21:49:59 +02:00
|
|
|
|
gssize bytes_copied;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
bytes_copied = class->splice (stream,
|
|
|
|
|
op->source,
|
|
|
|
|
op->flags,
|
|
|
|
|
cancellable,
|
|
|
|
|
&error);
|
|
|
|
|
if (bytes_copied == -1)
|
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_int (task, bytes_copied);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_real_splice_async (GOutputStream *stream,
|
|
|
|
|
GInputStream *source,
|
|
|
|
|
GOutputStreamSpliceFlags flags,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
SpliceData *op;
|
|
|
|
|
|
|
|
|
|
op = g_new0 (SpliceData, 1);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
|
|
|
|
g_task_set_task_data (task, op, (GDestroyNotify)free_splice_data);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
op->flags = flags;
|
2012-08-02 21:49:59 +02:00
|
|
|
|
op->source = g_object_ref (source);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2013-02-18 15:12:50 +01:00
|
|
|
|
if (g_input_stream_async_read_is_via_threads (source) &&
|
|
|
|
|
g_output_stream_async_write_is_via_threads (stream))
|
|
|
|
|
{
|
|
|
|
|
g_task_run_in_thread (task, splice_async_thread);
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
op->buffer = g_malloc (8192);
|
|
|
|
|
g_input_stream_read_async (op->source, op->buffer, 8192,
|
|
|
|
|
g_task_get_priority (task),
|
|
|
|
|
g_task_get_cancellable (task),
|
|
|
|
|
real_splice_async_read_cb, task);
|
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gssize
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_real_splice_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
2012-06-11 19:44:19 +02:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
return g_task_propagate_int (G_TASK (result), error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2012-08-02 21:49:59 +02:00
|
|
|
|
flush_async_thread (GTask *task,
|
|
|
|
|
gpointer source_object,
|
|
|
|
|
gpointer task_data,
|
|
|
|
|
GCancellable *cancellable)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GOutputStream *stream = source_object;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
gboolean result;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
result = TRUE;
|
|
|
|
|
if (class->flush)
|
2012-08-02 21:49:59 +02:00
|
|
|
|
result = class->flush (stream, cancellable, &error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
if (result)
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_error (task, error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_output_stream_real_flush_async (GOutputStream *stream,
|
2007-11-30 06:11:25 +01:00
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
|
|
|
|
g_task_set_priority (task, io_priority);
|
|
|
|
|
g_task_run_in_thread (task, flush_async_thread);
|
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_real_flush_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
2012-06-11 19:44:19 +02:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
return g_task_propagate_boolean (G_TASK (result), error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2012-08-02 21:49:59 +02:00
|
|
|
|
close_async_thread (GTask *task,
|
|
|
|
|
gpointer source_object,
|
|
|
|
|
gpointer task_data,
|
|
|
|
|
GCancellable *cancellable)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GOutputStream *stream = source_object;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
GOutputStreamClass *class;
|
|
|
|
|
GError *error = NULL;
|
2010-05-10 23:49:41 +02:00
|
|
|
|
gboolean result = TRUE;
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
2010-05-10 23:49:41 +02:00
|
|
|
|
|
|
|
|
|
/* Do a flush here if there is a flush function, and we did not have to do
|
2012-08-02 21:49:59 +02:00
|
|
|
|
* an async flush before (see g_output_stream_close_async)
|
|
|
|
|
*/
|
2010-05-10 23:49:41 +02:00
|
|
|
|
if (class->flush != NULL &&
|
|
|
|
|
(class->flush_async == NULL ||
|
|
|
|
|
class->flush_async == g_output_stream_real_flush_async))
|
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
result = class->flush (stream, cancellable, &error);
|
2010-05-10 23:49:41 +02:00
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2020-06-12 15:02:30 +02:00
|
|
|
|
/* Auto handling of cancellation disabled, and ignore
|
2007-11-26 17:13:05 +01:00
|
|
|
|
cancellation, since we want to close things anyway, although
|
|
|
|
|
possibly in a quick-n-dirty way. At least we never want to leak
|
|
|
|
|
open handles */
|
2012-08-02 21:49:59 +02:00
|
|
|
|
|
2009-05-20 13:37:55 +02:00
|
|
|
|
if (class->close_fn)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2010-05-10 23:49:41 +02:00
|
|
|
|
/* Make sure to close, even if the flush failed (see sync close) */
|
|
|
|
|
if (!result)
|
2012-08-02 21:49:59 +02:00
|
|
|
|
class->close_fn (stream, cancellable, NULL);
|
2010-05-10 23:49:41 +02:00
|
|
|
|
else
|
2012-08-02 21:49:59 +02:00
|
|
|
|
result = class->close_fn (stream, cancellable, &error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
2012-08-02 21:49:59 +02:00
|
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_error (task, error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_real_close_async (GOutputStream *stream,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
|
|
|
|
g_task_set_priority (task, io_priority);
|
|
|
|
|
g_task_run_in_thread (task, close_async_thread);
|
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2007-11-30 06:11:25 +01:00
|
|
|
|
g_output_stream_real_close_finish (GOutputStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
2012-06-11 19:44:19 +02:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
return g_task_propagate_boolean (G_TASK (result), error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
}
|