2009-05-12 16:30:15 +02:00
|
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2008 codethink
|
|
|
|
|
* Copyright © 2009 Red Hat, Inc.
|
|
|
|
|
*
|
2022-05-18 10:12:45 +02:00
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
|
*
|
2009-05-12 16:30:15 +02: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.
|
2009-05-12 16:30:15 +02: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/>.
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Authors: Ryan Lortie <desrt@desrt.ca>
|
|
|
|
|
* Alexander Larsson <alexl@redhat.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
#include "glibintl.h"
|
|
|
|
|
|
|
|
|
|
#include "giostream.h"
|
2012-08-02 21:49:59 +02:00
|
|
|
|
#include "gasyncresult.h"
|
2015-01-20 14:11:02 +01:00
|
|
|
|
#include "gioprivate.h"
|
2012-08-02 21:49:59 +02:00
|
|
|
|
#include "gtask.h"
|
2009-05-12 16:30:15 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2023-10-23 01:44:20 +02:00
|
|
|
|
* GIOStream:
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
2023-10-23 01:44:20 +02:00
|
|
|
|
* `GIOStream` represents an object that has both read and write streams.
|
2014-09-26 14:45:00 +02:00
|
|
|
|
* Generally the two streams act as separate input and output streams,
|
2009-05-12 16:30:15 +02:00
|
|
|
|
* but they share some common resources and state. For instance, for
|
2014-09-26 14:45:00 +02:00
|
|
|
|
* seekable streams, both streams may use the same position.
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
2023-10-23 01:44:20 +02:00
|
|
|
|
* Examples of `GIOStream` objects are [class@Gio.SocketConnection], which represents
|
|
|
|
|
* a two-way network connection; and [class@Gio.FileIOStream], which represents a
|
2009-05-18 13:02:11 +02:00
|
|
|
|
* file handle opened in read-write mode.
|
|
|
|
|
*
|
2009-05-12 16:30:15 +02:00
|
|
|
|
* To do the actual reading and writing you need to get the substreams
|
2023-10-23 01:44:20 +02:00
|
|
|
|
* with [method@Gio.IOStream.get_input_stream] and
|
|
|
|
|
* [method@Gio.IOStream.get_output_stream].
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
2023-10-23 01:44:20 +02:00
|
|
|
|
* The `GIOStream` object owns the input and the output streams, not the other
|
|
|
|
|
* way around, so keeping the substreams alive will not keep the `GIOStream`
|
|
|
|
|
* object alive. If the `GIOStream` object is freed it will be closed, thus
|
2014-09-26 14:45:00 +02:00
|
|
|
|
* closing the substreams, so even if the substreams stay alive they will
|
2023-10-23 01:44:20 +02:00
|
|
|
|
* always return `G_IO_ERROR_CLOSED` for all operations.
|
2009-05-12 19:31:19 +02:00
|
|
|
|
*
|
2023-10-23 01:44:20 +02:00
|
|
|
|
* To close a stream use [method@Gio.IOStream.close] which will close the common
|
2009-05-12 16:30:15 +02:00
|
|
|
|
* stream object and also the individual substreams. You can also close
|
|
|
|
|
* the substreams themselves. In most cases this only marks the
|
2014-02-13 11:58:21 +01:00
|
|
|
|
* substream as closed, so further I/O on it fails but common state in the
|
2023-10-23 01:44:20 +02:00
|
|
|
|
* `GIOStream` may still be open. However, some streams may support
|
|
|
|
|
* ‘half-closed’ states where one direction of the stream is actually shut down.
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
2023-10-23 01:44:20 +02:00
|
|
|
|
* Operations on `GIOStream`s cannot be started while another operation on the
|
|
|
|
|
* `GIOStream` or its substreams is in progress. Specifically, an application can
|
|
|
|
|
* read from the [class@Gio.InputStream] and write to the
|
|
|
|
|
* [class@Gio.OutputStream] simultaneously (either in separate threads, or as
|
|
|
|
|
* asynchronous operations in the same thread), but an application cannot start
|
|
|
|
|
* any `GIOStream` operation while there is a `GIOStream`, `GInputStream` or
|
|
|
|
|
* `GOutputStream` operation in progress, and an application can’t start any
|
|
|
|
|
* `GInputStream` or `GOutputStream` operation while there is a `GIOStream`
|
|
|
|
|
* operation in progress.
|
2014-09-26 15:01:16 +02:00
|
|
|
|
*
|
|
|
|
|
* This is a product of individual stream operations being associated with a
|
2023-10-23 01:44:20 +02:00
|
|
|
|
* given [type@GLib.MainContext] (the thread-default context at the time the
|
|
|
|
|
* operation was started), rather than entire streams being associated with a
|
|
|
|
|
* single `GMainContext`.
|
2014-09-26 15:01:16 +02:00
|
|
|
|
*
|
2023-10-23 01:44:20 +02:00
|
|
|
|
* GIO may run operations on `GIOStream`s from other (worker) threads, and this
|
2014-09-26 15:01:16 +02:00
|
|
|
|
* may be exposed to application code in the behaviour of wrapper streams, such
|
2023-10-23 01:44:20 +02:00
|
|
|
|
* as [class@Gio.BufferedInputStream] or [class@Gio.TlsConnection]. With such
|
|
|
|
|
* wrapper APIs, application code may only run operations on the base (wrapped)
|
|
|
|
|
* stream when the wrapper stream is idle. Note that the semantics of such
|
|
|
|
|
* operations may not be well-defined due to the state the wrapper stream leaves
|
|
|
|
|
* the base stream in (though they are guaranteed not to crash).
|
2014-09-26 15:01:16 +02:00
|
|
|
|
*
|
2009-05-12 16:30:15 +02:00
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
|
*/
|
2009-05-12 16:30:15 +02:00
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
PROP_0,
|
|
|
|
|
PROP_INPUT_STREAM,
|
|
|
|
|
PROP_OUTPUT_STREAM,
|
|
|
|
|
PROP_CLOSED
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct _GIOStreamPrivate {
|
|
|
|
|
guint closed : 1;
|
|
|
|
|
guint pending : 1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static gboolean g_io_stream_real_close (GIOStream *stream,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error);
|
|
|
|
|
static void g_io_stream_real_close_async (GIOStream *stream,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data);
|
|
|
|
|
static gboolean g_io_stream_real_close_finish (GIOStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error);
|
|
|
|
|
|
2013-06-11 01:29:58 +02:00
|
|
|
|
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GIOStream, g_io_stream, G_TYPE_OBJECT)
|
2009-05-12 16:30:15 +02:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_io_stream_dispose (GObject *object)
|
|
|
|
|
{
|
|
|
|
|
GIOStream *stream;
|
|
|
|
|
|
|
|
|
|
stream = G_IO_STREAM (object);
|
|
|
|
|
|
|
|
|
|
if (!stream->priv->closed)
|
|
|
|
|
g_io_stream_close (stream, NULL, NULL);
|
|
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (g_io_stream_parent_class)->dispose (object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_io_stream_init (GIOStream *stream)
|
|
|
|
|
{
|
2013-06-24 16:43:04 +02:00
|
|
|
|
stream->priv = g_io_stream_get_instance_private (stream);
|
2009-05-12 16:30:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_io_stream_get_property (GObject *object,
|
|
|
|
|
guint prop_id,
|
|
|
|
|
GValue *value,
|
|
|
|
|
GParamSpec *pspec)
|
|
|
|
|
{
|
|
|
|
|
GIOStream *stream = G_IO_STREAM (object);
|
|
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
|
{
|
|
|
|
|
case PROP_CLOSED:
|
|
|
|
|
g_value_set_boolean (value, stream->priv->closed);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PROP_INPUT_STREAM:
|
|
|
|
|
g_value_set_object (value, g_io_stream_get_input_stream (stream));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PROP_OUTPUT_STREAM:
|
|
|
|
|
g_value_set_object (value, g_io_stream_get_output_stream (stream));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_io_stream_class_init (GIOStreamClass *klass)
|
|
|
|
|
{
|
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
|
|
gobject_class->dispose = g_io_stream_dispose;
|
|
|
|
|
gobject_class->get_property = g_io_stream_get_property;
|
|
|
|
|
|
|
|
|
|
klass->close_fn = g_io_stream_real_close;
|
|
|
|
|
klass->close_async = g_io_stream_real_close_async;
|
|
|
|
|
klass->close_finish = g_io_stream_real_close_finish;
|
|
|
|
|
|
2023-11-29 14:23:09 +01:00
|
|
|
|
/**
|
|
|
|
|
* GIOStream:closed:
|
|
|
|
|
*
|
|
|
|
|
* Whether the stream is closed.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
|
|
|
|
*/
|
2009-05-12 16:30:15 +02:00
|
|
|
|
g_object_class_install_property (gobject_class, PROP_CLOSED,
|
2023-04-28 01:59:26 +02:00
|
|
|
|
g_param_spec_boolean ("closed", NULL, NULL,
|
2009-05-12 16:30:15 +02:00
|
|
|
|
FALSE,
|
2011-09-23 14:53:47 +02:00
|
|
|
|
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
|
2009-05-12 16:30:15 +02:00
|
|
|
|
|
2023-11-29 14:23:09 +01:00
|
|
|
|
/**
|
|
|
|
|
* GIOStream:input-stream:
|
|
|
|
|
*
|
|
|
|
|
* The [class@Gio.InputStream] to read from.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
|
|
|
|
*/
|
2009-05-12 16:30:15 +02:00
|
|
|
|
g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
|
2023-04-28 01:59:26 +02:00
|
|
|
|
g_param_spec_object ("input-stream", NULL, NULL,
|
2009-05-12 16:30:15 +02:00
|
|
|
|
G_TYPE_INPUT_STREAM,
|
|
|
|
|
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
|
2023-11-29 14:23:09 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GIOStream:output-stream:
|
|
|
|
|
*
|
|
|
|
|
* The [class@Gio.OutputStream] to write to.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
|
|
|
|
*/
|
2009-05-12 16:30:15 +02:00
|
|
|
|
g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
|
2023-04-28 01:59:26 +02:00
|
|
|
|
g_param_spec_object ("output-stream", NULL, NULL,
|
2009-05-12 16:30:15 +02:00
|
|
|
|
G_TYPE_OUTPUT_STREAM,
|
|
|
|
|
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_io_stream_is_closed:
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* @stream: a #GIOStream
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Checks if a stream is closed.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if the stream is closed.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
|
*/
|
2009-05-12 16:30:15 +02:00
|
|
|
|
gboolean
|
|
|
|
|
g_io_stream_is_closed (GIOStream *stream)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_IO_STREAM (stream), TRUE);
|
|
|
|
|
|
|
|
|
|
return stream->priv->closed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_io_stream_get_input_stream:
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* @stream: a #GIOStream
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Gets the input stream for this object. This is used
|
|
|
|
|
* for reading.
|
|
|
|
|
*
|
2010-07-26 17:21:14 +02:00
|
|
|
|
* Returns: (transfer none): a #GInputStream, owned by the #GIOStream.
|
|
|
|
|
* Do not free.
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
|
*/
|
2009-05-12 16:30:15 +02:00
|
|
|
|
GInputStream *
|
2009-05-28 00:20:08 +02:00
|
|
|
|
g_io_stream_get_input_stream (GIOStream *stream)
|
2009-05-12 16:30:15 +02:00
|
|
|
|
{
|
|
|
|
|
GIOStreamClass *klass;
|
|
|
|
|
|
2009-05-28 00:20:08 +02:00
|
|
|
|
klass = G_IO_STREAM_GET_CLASS (stream);
|
2009-05-12 16:30:15 +02:00
|
|
|
|
|
|
|
|
|
g_assert (klass->get_input_stream != NULL);
|
|
|
|
|
|
2009-05-28 00:20:08 +02:00
|
|
|
|
return klass->get_input_stream (stream);
|
2009-05-12 16:30:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_io_stream_get_output_stream:
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* @stream: a #GIOStream
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Gets the output stream for this object. This is used for
|
|
|
|
|
* writing.
|
|
|
|
|
*
|
2010-07-26 17:21:14 +02:00
|
|
|
|
* Returns: (transfer none): a #GOutputStream, owned by the #GIOStream.
|
|
|
|
|
* Do not free.
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
|
*/
|
2009-05-12 16:30:15 +02:00
|
|
|
|
GOutputStream *
|
2009-05-28 00:20:08 +02:00
|
|
|
|
g_io_stream_get_output_stream (GIOStream *stream)
|
2009-05-12 16:30:15 +02:00
|
|
|
|
{
|
|
|
|
|
GIOStreamClass *klass;
|
|
|
|
|
|
2009-05-28 00:20:08 +02:00
|
|
|
|
klass = G_IO_STREAM_GET_CLASS (stream);
|
2009-05-12 16:30:15 +02:00
|
|
|
|
|
|
|
|
|
g_assert (klass->get_output_stream != NULL);
|
2009-05-28 00:20:08 +02:00
|
|
|
|
return klass->get_output_stream (stream);
|
2009-05-12 16:30:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_io_stream_has_pending:
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* @stream: a #GIOStream
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Checks if a stream has pending actions.
|
|
|
|
|
*
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* Returns: %TRUE if @stream has pending actions.
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
g_io_stream_has_pending (GIOStream *stream)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
|
|
|
|
|
|
|
|
|
|
return stream->priv->pending;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_io_stream_set_pending:
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* @stream: a #GIOStream
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* ignore
|
2009-05-12 16:30:15 +02: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.
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
|
*/
|
2009-05-12 16:30:15 +02:00
|
|
|
|
gboolean
|
2009-05-28 00:20:08 +02:00
|
|
|
|
g_io_stream_set_pending (GIOStream *stream,
|
|
|
|
|
GError **error)
|
2009-05-12 16:30:15 +02:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
|
|
|
|
|
|
|
|
|
|
if (stream->priv->closed)
|
|
|
|
|
{
|
|
|
|
|
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
|
|
|
|
|
_("Stream is already closed"));
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stream->priv->pending)
|
|
|
|
|
{
|
|
|
|
|
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"));
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream->priv->pending = TRUE;
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_io_stream_clear_pending:
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* @stream: a #GIOStream
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Clears the pending flag on @stream.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
|
*/
|
2009-05-12 16:30:15 +02:00
|
|
|
|
void
|
|
|
|
|
g_io_stream_clear_pending (GIOStream *stream)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (G_IS_IO_STREAM (stream));
|
|
|
|
|
|
|
|
|
|
stream->priv->pending = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2009-05-28 00:20:08 +02:00
|
|
|
|
g_io_stream_real_close (GIOStream *stream,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2009-05-12 16:30:15 +02:00
|
|
|
|
{
|
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
|
|
res = g_output_stream_close (g_io_stream_get_output_stream (stream),
|
|
|
|
|
cancellable, error);
|
|
|
|
|
|
|
|
|
|
/* If this errored out, unset error so that we don't report
|
|
|
|
|
further errors, but still do the following ops */
|
|
|
|
|
if (error != NULL && *error != NULL)
|
|
|
|
|
error = NULL;
|
|
|
|
|
|
|
|
|
|
res &= g_input_stream_close (g_io_stream_get_input_stream (stream),
|
|
|
|
|
cancellable, error);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_io_stream_close:
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* @stream: a #GIOStream
|
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
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Closes the stream, releasing resources related to it. This will also
|
2015-09-30 13:57:42 +02:00
|
|
|
|
* close the individual input and output streams, if they are not already
|
2009-05-12 16:30:15 +02:00
|
|
|
|
* closed.
|
|
|
|
|
*
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* Once the stream is closed, all other operations will return
|
|
|
|
|
* %G_IO_ERROR_CLOSED. Closing a stream multiple times will not
|
|
|
|
|
* return an error.
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* Closing a stream will automatically flush any outstanding buffers
|
|
|
|
|
* in the stream.
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Streams will be automatically closed when the last reference
|
|
|
|
|
* is dropped, but you might want to call this function to make sure
|
|
|
|
|
* resources are released as early as possible.
|
|
|
|
|
*
|
2009-05-28 00:20:08 +02: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.
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* 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 close will still return %G_IO_ERROR_CLOSED for all operations.
|
|
|
|
|
* Still, it 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.
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
* Cancelling a close will still leave the stream closed, but some streams
|
|
|
|
|
* can use a faster close that doesn't block to e.g. check errors.
|
|
|
|
|
*
|
|
|
|
|
* The default implementation of this method just calls close on the
|
|
|
|
|
* individual input/output streams.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: %TRUE on success, %FALSE on failure
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
|
*/
|
2009-05-12 16:30:15 +02:00
|
|
|
|
gboolean
|
2009-05-28 00:20:08 +02:00
|
|
|
|
g_io_stream_close (GIOStream *stream,
|
2009-05-12 16:30:15 +02:00
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
GIOStreamClass *class;
|
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
|
|
|
|
|
|
|
|
|
|
class = G_IO_STREAM_GET_CLASS (stream);
|
|
|
|
|
|
|
|
|
|
if (stream->priv->closed)
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
if (!g_io_stream_set_pending (stream, error))
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
if (cancellable)
|
|
|
|
|
g_cancellable_push_current (cancellable);
|
|
|
|
|
|
|
|
|
|
res = TRUE;
|
|
|
|
|
if (class->close_fn)
|
|
|
|
|
res = class->close_fn (stream, cancellable, error);
|
|
|
|
|
|
|
|
|
|
if (cancellable)
|
|
|
|
|
g_cancellable_pop_current (cancellable);
|
|
|
|
|
|
|
|
|
|
stream->priv->closed = TRUE;
|
|
|
|
|
g_io_stream_clear_pending (stream);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
async_ready_close_callback_wrapper (GObject *source_object,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GIOStream *stream = G_IO_STREAM (source_object);
|
2014-01-21 20:12:08 +01:00
|
|
|
|
GIOStreamClass *klass = G_IO_STREAM_GET_CLASS (stream);
|
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gboolean success;
|
2009-05-12 16:30:15 +02:00
|
|
|
|
|
|
|
|
|
stream->priv->closed = TRUE;
|
|
|
|
|
g_io_stream_clear_pending (stream);
|
2014-01-21 20:12:08 +01:00
|
|
|
|
|
|
|
|
|
if (g_async_result_legacy_propagate_error (res, &error))
|
|
|
|
|
success = FALSE;
|
|
|
|
|
else
|
|
|
|
|
success = klass->close_finish (stream, res, &error);
|
|
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_boolean (task, success);
|
|
|
|
|
|
|
|
|
|
g_object_unref (task);
|
2009-05-12 16:30:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_io_stream_close_async:
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* @stream: a #GIOStream
|
|
|
|
|
* @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): a #GAsyncReadyCallback
|
|
|
|
|
* to call when the request is satisfied
|
|
|
|
|
* @user_data: the data to pass to callback function
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Requests an asynchronous close of the stream, releasing resources
|
|
|
|
|
* related to it. When the operation is finished @callback will be
|
|
|
|
|
* called. You can then call g_io_stream_close_finish() to get
|
|
|
|
|
* the result of the operation.
|
|
|
|
|
*
|
|
|
|
|
* For behaviour details see g_io_stream_close().
|
|
|
|
|
*
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* The asynchronous methods have a default fallback that uses threads
|
2009-05-12 16:30:15 +02:00
|
|
|
|
* to implement asynchronicity, so they are optional for inheriting
|
|
|
|
|
* classes. However, if you override one you must override all.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
|
*/
|
2009-05-12 16:30:15 +02:00
|
|
|
|
void
|
|
|
|
|
g_io_stream_close_async (GIOStream *stream,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GIOStreamClass *class;
|
|
|
|
|
GError *error = NULL;
|
2014-01-21 20:12:08 +01:00
|
|
|
|
GTask *task;
|
2009-05-12 16:30:15 +02:00
|
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_IO_STREAM (stream));
|
|
|
|
|
|
2014-01-21 20:12:08 +01: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_io_stream_close_async);
|
2014-01-21 20:12:08 +01:00
|
|
|
|
|
2009-05-12 16:30:15 +02:00
|
|
|
|
if (stream->priv->closed)
|
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
g_object_unref (task);
|
2009-05-12 16:30:15 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!g_io_stream_set_pending (stream, &error))
|
|
|
|
|
{
|
2014-01-21 20:12:08 +01:00
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
g_object_unref (task);
|
2009-05-12 16:30:15 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class = G_IO_STREAM_GET_CLASS (stream);
|
2014-01-21 20:12:08 +01:00
|
|
|
|
|
2009-05-12 16:30:15 +02:00
|
|
|
|
class->close_async (stream, io_priority, cancellable,
|
2014-01-21 20:12:08 +01:00
|
|
|
|
async_ready_close_callback_wrapper, task);
|
2009-05-12 16:30:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_io_stream_close_finish:
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* @stream: a #GIOStream
|
|
|
|
|
* @result: a #GAsyncResult
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2009-05-28 00:20:08 +02:00
|
|
|
|
* ignore
|
2009-05-12 16:30:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Closes a stream.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
|
*/
|
2009-05-12 16:30:15 +02:00
|
|
|
|
gboolean
|
2009-05-28 00:20:08 +02:00
|
|
|
|
g_io_stream_close_finish (GIOStream *stream,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error)
|
2009-05-12 16:30:15 +02:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
|
2014-01-21 20:12:08 +01:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
|
2009-05-12 16:30:15 +02:00
|
|
|
|
|
2014-01-21 20:12:08 +01:00
|
|
|
|
return g_task_propagate_boolean (G_TASK (result), error);
|
2009-05-12 16:30:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2012-08-02 21:49:59 +02:00
|
|
|
|
close_async_thread (GTask *task,
|
|
|
|
|
gpointer source_object,
|
|
|
|
|
gpointer task_data,
|
|
|
|
|
GCancellable *cancellable)
|
2009-05-12 16:30:15 +02:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GIOStream *stream = source_object;
|
2009-05-12 16:30:15 +02:00
|
|
|
|
GIOStreamClass *class;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gboolean result;
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
class = G_IO_STREAM_GET_CLASS (stream);
|
2009-05-20 13:37:55 +02:00
|
|
|
|
if (class->close_fn)
|
2009-05-12 16:30:15 +02:00
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
result = class->close_fn (stream,
|
|
|
|
|
g_task_get_cancellable (task),
|
|
|
|
|
&error);
|
2009-05-20 13:37:55 +02:00
|
|
|
|
if (!result)
|
2012-08-02 21:49:59 +02:00
|
|
|
|
{
|
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2009-05-12 16:30:15 +02:00
|
|
|
|
}
|
2012-08-02 21:49:59 +02:00
|
|
|
|
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
2009-05-12 16:30:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-20 14:11:02 +01:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
GError *error;
|
|
|
|
|
gint pending;
|
|
|
|
|
} CloseAsyncData;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
stream_close_complete (GObject *source,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
CloseAsyncData *data;
|
|
|
|
|
|
|
|
|
|
data = g_task_get_task_data (task);
|
|
|
|
|
data->pending--;
|
|
|
|
|
|
|
|
|
|
if (G_IS_OUTPUT_STREAM (source))
|
|
|
|
|
{
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
|
|
/* Match behaviour with the sync route and give precedent to the
|
|
|
|
|
* error returned from closing the output stream.
|
|
|
|
|
*/
|
|
|
|
|
g_output_stream_close_finish (G_OUTPUT_STREAM (source), result, &error);
|
|
|
|
|
if (error)
|
|
|
|
|
{
|
|
|
|
|
if (data->error)
|
|
|
|
|
g_error_free (data->error);
|
|
|
|
|
data->error = error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
g_input_stream_close_finish (G_INPUT_STREAM (source), result, data->error ? NULL : &data->error);
|
|
|
|
|
|
|
|
|
|
if (data->pending == 0)
|
|
|
|
|
{
|
|
|
|
|
if (data->error)
|
|
|
|
|
g_task_return_error (task, data->error);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
|
|
|
|
|
g_slice_free (CloseAsyncData, data);
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-12 16:30:15 +02:00
|
|
|
|
static void
|
2009-05-28 00:20:08 +02:00
|
|
|
|
g_io_stream_real_close_async (GIOStream *stream,
|
|
|
|
|
int io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2009-05-12 16:30:15 +02:00
|
|
|
|
{
|
2015-01-20 14:11:02 +01:00
|
|
|
|
GInputStream *input;
|
|
|
|
|
GOutputStream *output;
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task;
|
|
|
|
|
|
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
2016-06-17 01:39:38 +02:00
|
|
|
|
g_task_set_source_tag (task, g_io_stream_real_close_async);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_set_check_cancellable (task, FALSE);
|
|
|
|
|
g_task_set_priority (task, io_priority);
|
2015-01-20 14:11:02 +01:00
|
|
|
|
|
|
|
|
|
input = g_io_stream_get_input_stream (stream);
|
|
|
|
|
output = g_io_stream_get_output_stream (stream);
|
|
|
|
|
|
|
|
|
|
if (g_input_stream_async_close_is_via_threads (input) && g_output_stream_async_close_is_via_threads (output))
|
|
|
|
|
{
|
|
|
|
|
/* No sense in dispatching to the thread twice -- just do it all
|
|
|
|
|
* in one go.
|
|
|
|
|
*/
|
|
|
|
|
g_task_run_in_thread (task, close_async_thread);
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CloseAsyncData *data;
|
|
|
|
|
|
|
|
|
|
/* We should avoid dispatching to another thread in case either
|
|
|
|
|
* object that would not do it for itself because it may not be
|
|
|
|
|
* threadsafe.
|
|
|
|
|
*/
|
|
|
|
|
data = g_slice_new (CloseAsyncData);
|
|
|
|
|
data->error = NULL;
|
|
|
|
|
data->pending = 2;
|
|
|
|
|
|
|
|
|
|
g_task_set_task_data (task, data, NULL);
|
|
|
|
|
g_input_stream_close_async (input, io_priority, cancellable, stream_close_complete, task);
|
|
|
|
|
g_output_stream_close_async (output, io_priority, cancellable, stream_close_complete, task);
|
|
|
|
|
}
|
2009-05-12 16:30:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2009-05-28 00:20:08 +02:00
|
|
|
|
g_io_stream_real_close_finish (GIOStream *stream,
|
2009-05-12 16:30:15 +02:00
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
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);
|
2009-05-12 16:30:15 +02:00
|
|
|
|
}
|
2010-08-13 10:10:48 +02:00
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
GIOStream *stream1;
|
|
|
|
|
GIOStream *stream2;
|
|
|
|
|
GIOStreamSpliceFlags flags;
|
|
|
|
|
gint io_priority;
|
|
|
|
|
GCancellable *cancellable;
|
|
|
|
|
gulong cancelled_id;
|
|
|
|
|
GCancellable *op1_cancellable;
|
|
|
|
|
GCancellable *op2_cancellable;
|
|
|
|
|
guint completed;
|
|
|
|
|
GError *error;
|
|
|
|
|
} SpliceContext;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
splice_context_free (SpliceContext *ctx)
|
|
|
|
|
{
|
|
|
|
|
g_object_unref (ctx->stream1);
|
|
|
|
|
g_object_unref (ctx->stream2);
|
|
|
|
|
if (ctx->cancellable != NULL)
|
|
|
|
|
g_object_unref (ctx->cancellable);
|
|
|
|
|
g_object_unref (ctx->op1_cancellable);
|
|
|
|
|
g_object_unref (ctx->op2_cancellable);
|
|
|
|
|
g_clear_error (&ctx->error);
|
|
|
|
|
g_slice_free (SpliceContext, ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2012-08-02 21:49:59 +02:00
|
|
|
|
splice_complete (GTask *task,
|
|
|
|
|
SpliceContext *ctx)
|
2010-08-13 10:10:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (ctx->cancelled_id != 0)
|
|
|
|
|
g_cancellable_disconnect (ctx->cancellable, ctx->cancelled_id);
|
|
|
|
|
ctx->cancelled_id = 0;
|
|
|
|
|
|
|
|
|
|
if (ctx->error != NULL)
|
2012-08-02 21:49:59 +02:00
|
|
|
|
{
|
|
|
|
|
g_task_return_error (task, ctx->error);
|
|
|
|
|
ctx->error = NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
2010-08-13 10:10:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
splice_close_cb (GObject *iostream,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
SpliceContext *ctx = g_task_get_task_data (task);
|
2010-08-13 10:10:48 +02:00
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
|
|
g_io_stream_close_finish (G_IO_STREAM (iostream), res, &error);
|
|
|
|
|
|
|
|
|
|
ctx->completed++;
|
|
|
|
|
|
2011-08-29 20:49:32 +02:00
|
|
|
|
/* Keep the first error that occurred */
|
2010-08-13 10:10:48 +02:00
|
|
|
|
if (error != NULL && ctx->error == NULL)
|
|
|
|
|
ctx->error = error;
|
|
|
|
|
else
|
|
|
|
|
g_clear_error (&error);
|
|
|
|
|
|
|
|
|
|
/* If all operations are done, complete now */
|
|
|
|
|
if (ctx->completed == 4)
|
2012-08-02 21:49:59 +02:00
|
|
|
|
splice_complete (task, ctx);
|
2010-08-13 10:10:48 +02:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_object_unref (task);
|
2010-08-13 10:10:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
splice_cb (GObject *ostream,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
SpliceContext *ctx = g_task_get_task_data (task);
|
2010-08-13 10:10:48 +02:00
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
|
|
g_output_stream_splice_finish (G_OUTPUT_STREAM (ostream), res, &error);
|
|
|
|
|
|
|
|
|
|
ctx->completed++;
|
|
|
|
|
|
|
|
|
|
/* ignore cancellation error if it was not requested by the user */
|
|
|
|
|
if (error != NULL &&
|
|
|
|
|
g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
|
|
|
|
|
(ctx->cancellable == NULL ||
|
|
|
|
|
!g_cancellable_is_cancelled (ctx->cancellable)))
|
|
|
|
|
g_clear_error (&error);
|
|
|
|
|
|
2011-08-29 20:49:32 +02:00
|
|
|
|
/* Keep the first error that occurred */
|
2010-08-13 10:10:48 +02:00
|
|
|
|
if (error != NULL && ctx->error == NULL)
|
|
|
|
|
ctx->error = error;
|
|
|
|
|
else
|
|
|
|
|
g_clear_error (&error);
|
|
|
|
|
|
|
|
|
|
if (ctx->completed == 1 &&
|
|
|
|
|
(ctx->flags & G_IO_STREAM_SPLICE_WAIT_FOR_BOTH) == 0)
|
|
|
|
|
{
|
|
|
|
|
/* We don't want to wait for the 2nd operation to finish, cancel it */
|
|
|
|
|
g_cancellable_cancel (ctx->op1_cancellable);
|
|
|
|
|
g_cancellable_cancel (ctx->op2_cancellable);
|
|
|
|
|
}
|
|
|
|
|
else if (ctx->completed == 2)
|
|
|
|
|
{
|
|
|
|
|
if (ctx->cancellable == NULL ||
|
|
|
|
|
!g_cancellable_is_cancelled (ctx->cancellable))
|
|
|
|
|
{
|
|
|
|
|
g_cancellable_reset (ctx->op1_cancellable);
|
|
|
|
|
g_cancellable_reset (ctx->op2_cancellable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Close the IO streams if needed */
|
|
|
|
|
if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM1) != 0)
|
2012-08-02 21:49:59 +02:00
|
|
|
|
{
|
|
|
|
|
g_io_stream_close_async (ctx->stream1,
|
|
|
|
|
g_task_get_priority (task),
|
|
|
|
|
ctx->op1_cancellable,
|
|
|
|
|
splice_close_cb, g_object_ref (task));
|
|
|
|
|
}
|
2010-08-13 10:10:48 +02:00
|
|
|
|
else
|
|
|
|
|
ctx->completed++;
|
|
|
|
|
|
|
|
|
|
if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM2) != 0)
|
2012-08-02 21:49:59 +02:00
|
|
|
|
{
|
|
|
|
|
g_io_stream_close_async (ctx->stream2,
|
|
|
|
|
g_task_get_priority (task),
|
|
|
|
|
ctx->op2_cancellable,
|
|
|
|
|
splice_close_cb, g_object_ref (task));
|
|
|
|
|
}
|
2010-08-13 10:10:48 +02:00
|
|
|
|
else
|
|
|
|
|
ctx->completed++;
|
|
|
|
|
|
|
|
|
|
/* If all operations are done, complete now */
|
|
|
|
|
if (ctx->completed == 4)
|
2012-08-02 21:49:59 +02:00
|
|
|
|
splice_complete (task, ctx);
|
2010-08-13 10:10:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_object_unref (task);
|
2010-08-13 10:10:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2012-08-02 21:49:59 +02:00
|
|
|
|
splice_cancelled_cb (GCancellable *cancellable,
|
|
|
|
|
GTask *task)
|
2010-08-13 10:10:48 +02:00
|
|
|
|
{
|
|
|
|
|
SpliceContext *ctx;
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
ctx = g_task_get_task_data (task);
|
2010-08-13 10:10:48 +02:00
|
|
|
|
g_cancellable_cancel (ctx->op1_cancellable);
|
|
|
|
|
g_cancellable_cancel (ctx->op2_cancellable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_io_stream_splice_async:
|
|
|
|
|
* @stream1: a #GIOStream.
|
|
|
|
|
* @stream2: a #GIOStream.
|
|
|
|
|
* @flags: a set of #GIOStreamSpliceFlags.
|
|
|
|
|
* @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
|
2010-08-13 10:10:48 +02:00
|
|
|
|
*
|
2020-06-12 15:02:30 +02:00
|
|
|
|
* Asynchronously splice the output stream of @stream1 to the input stream of
|
2010-08-13 10:10:48 +02:00
|
|
|
|
* @stream2, and splice the output stream of @stream2 to the input stream of
|
|
|
|
|
* @stream1.
|
|
|
|
|
*
|
|
|
|
|
* When the operation is finished @callback will be called.
|
|
|
|
|
* You can then call g_io_stream_splice_finish() to get the
|
|
|
|
|
* result of the operation.
|
2010-12-13 17:42:11 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
2010-08-13 10:10:48 +02:00
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
g_io_stream_splice_async (GIOStream *stream1,
|
|
|
|
|
GIOStream *stream2,
|
|
|
|
|
GIOStreamSpliceFlags flags,
|
|
|
|
|
gint io_priority,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
GTask *task;
|
2010-08-13 10:10:48 +02:00
|
|
|
|
SpliceContext *ctx;
|
|
|
|
|
GInputStream *istream;
|
|
|
|
|
GOutputStream *ostream;
|
|
|
|
|
|
|
|
|
|
if (cancellable != NULL && g_cancellable_is_cancelled (cancellable))
|
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_report_new_error (NULL, callback, user_data,
|
|
|
|
|
g_io_stream_splice_async,
|
|
|
|
|
G_IO_ERROR, G_IO_ERROR_CANCELLED,
|
|
|
|
|
"Operation has been cancelled");
|
2010-08-13 10:10:48 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx = g_slice_new0 (SpliceContext);
|
|
|
|
|
ctx->stream1 = g_object_ref (stream1);
|
|
|
|
|
ctx->stream2 = g_object_ref (stream2);
|
|
|
|
|
ctx->flags = flags;
|
|
|
|
|
ctx->op1_cancellable = g_cancellable_new ();
|
|
|
|
|
ctx->op2_cancellable = g_cancellable_new ();
|
|
|
|
|
ctx->completed = 0;
|
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
task = g_task_new (NULL, cancellable, callback, user_data);
|
2016-06-17 01:39:38 +02:00
|
|
|
|
g_task_set_source_tag (task, g_io_stream_splice_async);
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_task_set_task_data (task, ctx, (GDestroyNotify) splice_context_free);
|
2010-08-13 10:10:48 +02:00
|
|
|
|
|
|
|
|
|
if (cancellable != NULL)
|
|
|
|
|
{
|
|
|
|
|
ctx->cancellable = g_object_ref (cancellable);
|
|
|
|
|
ctx->cancelled_id = g_cancellable_connect (cancellable,
|
2012-08-02 21:49:59 +02:00
|
|
|
|
G_CALLBACK (splice_cancelled_cb), g_object_ref (task),
|
2010-08-13 10:10:48 +02:00
|
|
|
|
g_object_unref);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
istream = g_io_stream_get_input_stream (stream1);
|
|
|
|
|
ostream = g_io_stream_get_output_stream (stream2);
|
|
|
|
|
g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
|
|
|
|
|
io_priority, ctx->op1_cancellable, splice_cb,
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_object_ref (task));
|
2010-08-13 10:10:48 +02:00
|
|
|
|
|
|
|
|
|
istream = g_io_stream_get_input_stream (stream2);
|
|
|
|
|
ostream = g_io_stream_get_output_stream (stream1);
|
|
|
|
|
g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
|
|
|
|
|
io_priority, ctx->op2_cancellable, splice_cb,
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_object_ref (task));
|
2010-08-13 10:10:48 +02:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_object_unref (task);
|
2010-08-13 10:10:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_io_stream_splice_finish:
|
|
|
|
|
* @result: a #GAsyncResult.
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2010-08-13 10:10:48 +02:00
|
|
|
|
* ignore.
|
|
|
|
|
*
|
|
|
|
|
* Finishes an asynchronous io stream splice operation.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE on success, %FALSE otherwise.
|
2010-12-13 17:42:11 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
2010-08-13 10:10:48 +02:00
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
g_io_stream_splice_finish (GAsyncResult *result,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);
|
2010-08-13 10:10:48 +02:00
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
|
return g_task_propagate_boolean (G_TASK (result), error);
|
2010-08-13 10:10:48 +02:00
|
|
|
|
}
|