Coverage Report

Created: 2026-09-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib/gio/giostream.c
Line
Count
Source
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright © 2008 codethink
4
 * Copyright © 2009 Red Hat, Inc.
5
 *
6
 * SPDX-License-Identifier: LGPL-2.1-or-later
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General
19
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20
 *
21
 * Authors: Ryan Lortie <desrt@desrt.ca>
22
 *          Alexander Larsson <alexl@redhat.com>
23
 */
24
25
#include "config.h"
26
#include <glib.h>
27
#include "glibintl.h"
28
29
#include "giostream.h"
30
#include "gasyncresult.h"
31
#include "gioprivate.h"
32
#include "gtask.h"
33
34
/**
35
 * GIOStream:
36
 *
37
 * `GIOStream` represents an object that has both read and write streams.
38
 * Generally the two streams act as separate input and output streams,
39
 * but they share some common resources and state. For instance, for
40
 * seekable streams, both streams may use the same position.
41
 *
42
 * Examples of `GIOStream` objects are [class@Gio.SocketConnection], which represents
43
 * a two-way network connection; and [class@Gio.FileIOStream], which represents a
44
 * file handle opened in read-write mode.
45
 *
46
 * To do the actual reading and writing you need to get the substreams
47
 * with [method@Gio.IOStream.get_input_stream] and
48
 * [method@Gio.IOStream.get_output_stream].
49
 *
50
 * The `GIOStream` object owns the input and the output streams, not the other
51
 * way around, so keeping the substreams alive will not keep the `GIOStream`
52
 * object alive. If the `GIOStream` object is freed it will be closed, thus
53
 * closing the substreams, so even if the substreams stay alive they will
54
 * always return `G_IO_ERROR_CLOSED` for all operations.
55
 *
56
 * To close a stream use [method@Gio.IOStream.close] which will close the common
57
 * stream object and also the individual substreams. You can also close
58
 * the substreams themselves. In most cases this only marks the
59
 * substream as closed, so further I/O on it fails but common state in the
60
 * `GIOStream` may still be open. However, some streams may support
61
 * ‘half-closed’ states where one direction of the stream is actually shut down.
62
 *
63
 * Operations on `GIOStream`s cannot be started while another operation on the
64
 * `GIOStream` or its substreams is in progress. Specifically, an application can
65
 * read from the [class@Gio.InputStream] and write to the
66
 * [class@Gio.OutputStream] simultaneously (either in separate threads, or as
67
 * asynchronous operations in the same thread), but an application cannot start
68
 * any `GIOStream` operation while there is a `GIOStream`, `GInputStream` or
69
 * `GOutputStream` operation in progress, and an application can’t start any
70
 * `GInputStream` or `GOutputStream` operation while there is a `GIOStream`
71
 * operation in progress.
72
 *
73
 * This is a product of individual stream operations being associated with a
74
 * given [type@GLib.MainContext] (the thread-default context at the time the
75
 * operation was started), rather than entire streams being associated with a
76
 * single `GMainContext`.
77
 *
78
 * GIO may run operations on `GIOStream`s from other (worker) threads, and this
79
 * may be exposed to application code in the behaviour of wrapper streams, such
80
 * as [class@Gio.BufferedInputStream] or [class@Gio.TlsConnection]. With such
81
 * wrapper APIs, application code may only run operations on the base (wrapped)
82
 * stream when the wrapper stream is idle. Note that the semantics of such
83
 * operations may not be well-defined due to the state the wrapper stream leaves
84
 * the base stream in (though they are guaranteed not to crash).
85
 *
86
 * Since: 2.22
87
 */
88
89
enum
90
{
91
  PROP_0,
92
  PROP_INPUT_STREAM,
93
  PROP_OUTPUT_STREAM,
94
  PROP_CLOSED
95
};
96
97
struct _GIOStreamPrivate {
98
  guint closed : 1;
99
  guint pending : 1;
100
};
101
102
static gboolean g_io_stream_real_close        (GIOStream            *stream,
103
                 GCancellable         *cancellable,
104
                 GError              **error);
105
static void     g_io_stream_real_close_async  (GIOStream            *stream,
106
                 int                   io_priority,
107
                 GCancellable         *cancellable,
108
                 GAsyncReadyCallback   callback,
109
                 gpointer              user_data);
110
static gboolean g_io_stream_real_close_finish (GIOStream            *stream,
111
                 GAsyncResult         *result,
112
                 GError              **error);
113
114
0
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GIOStream, g_io_stream, G_TYPE_OBJECT)
115
0
116
0
static void
117
0
g_io_stream_dispose (GObject *object)
118
0
{
119
0
  GIOStream *stream;
120
121
0
  stream = G_IO_STREAM (object);
122
123
0
  if (!stream->priv->closed)
124
0
    g_io_stream_close (stream, NULL, NULL);
125
126
0
  G_OBJECT_CLASS (g_io_stream_parent_class)->dispose (object);
127
0
}
128
129
static void
130
g_io_stream_init (GIOStream *stream)
131
0
{
132
0
  stream->priv = g_io_stream_get_instance_private (stream);
133
0
}
134
135
static void
136
g_io_stream_get_property (GObject    *object,
137
        guint       prop_id,
138
        GValue     *value,
139
        GParamSpec *pspec)
140
0
{
141
0
  GIOStream *stream = G_IO_STREAM (object);
142
143
0
  switch (prop_id)
144
0
    {
145
0
      case PROP_CLOSED:
146
0
        g_value_set_boolean (value, stream->priv->closed);
147
0
        break;
148
149
0
      case PROP_INPUT_STREAM:
150
0
        g_value_set_object (value, g_io_stream_get_input_stream (stream));
151
0
        break;
152
153
0
      case PROP_OUTPUT_STREAM:
154
0
        g_value_set_object (value, g_io_stream_get_output_stream (stream));
155
0
        break;
156
157
0
      default:
158
0
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159
0
    }
160
0
}
161
162
static void
163
g_io_stream_class_init (GIOStreamClass *klass)
164
0
{
165
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
166
167
0
  gobject_class->dispose = g_io_stream_dispose;
168
0
  gobject_class->get_property = g_io_stream_get_property;
169
170
0
  klass->close_fn = g_io_stream_real_close;
171
0
  klass->close_async = g_io_stream_real_close_async;
172
0
  klass->close_finish = g_io_stream_real_close_finish;
173
174
  /**
175
   * GIOStream:closed:
176
   *
177
   * Whether the stream is closed.
178
   *
179
   * Since: 2.22
180
   */
181
0
  g_object_class_install_property (gobject_class, PROP_CLOSED,
182
0
                                   g_param_spec_boolean ("closed", NULL, NULL,
183
0
                                                         FALSE,
184
0
                                                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
185
186
  /**
187
   * GIOStream:input-stream:
188
   *
189
   * The [class@Gio.InputStream] to read from.
190
   *
191
   * Since: 2.22
192
   */
193
0
  g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
194
0
           g_param_spec_object ("input-stream", NULL, NULL,
195
0
              G_TYPE_INPUT_STREAM,
196
0
              G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
197
198
  /**
199
   * GIOStream:output-stream:
200
   *
201
   * The [class@Gio.OutputStream] to write to.
202
   *
203
   * Since: 2.22
204
   */
205
0
  g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
206
0
           g_param_spec_object ("output-stream", NULL, NULL,
207
0
              G_TYPE_OUTPUT_STREAM,
208
0
              G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
209
0
}
210
211
/**
212
 * g_io_stream_is_closed:
213
 * @stream: a #GIOStream
214
 *
215
 * Checks if a stream has been closed.
216
 *
217
 * This only indicates whether the I/O stream has been closed at the top level
218
 * by calling [method@Gio.IOStream.close]. If the underlying input and output
219
 * streams have been closed separately, this method will still return false.
220
 *
221
 * Returns: true if the stream has been closed; false otherwise
222
 *
223
 * Since: 2.22
224
 */
225
gboolean
226
g_io_stream_is_closed (GIOStream *stream)
227
0
{
228
0
  g_return_val_if_fail (G_IS_IO_STREAM (stream), TRUE);
229
230
0
  return stream->priv->closed;
231
0
}
232
233
/**
234
 * g_io_stream_get_input_stream:
235
 * @stream: a #GIOStream
236
 *
237
 * Gets the input stream for this object. This is used
238
 * for reading.
239
 *
240
 * Returns: (transfer none): a #GInputStream, owned by the #GIOStream.
241
 * Do not free.
242
 *
243
 * Since: 2.22
244
 */
245
GInputStream *
246
g_io_stream_get_input_stream (GIOStream *stream)
247
0
{
248
0
  GIOStreamClass *klass;
249
250
0
  klass = G_IO_STREAM_GET_CLASS (stream);
251
252
0
  g_assert (klass->get_input_stream != NULL);
253
254
0
  return klass->get_input_stream (stream);
255
0
}
256
257
/**
258
 * g_io_stream_get_output_stream:
259
 * @stream: a #GIOStream
260
 *
261
 * Gets the output stream for this object. This is used for
262
 * writing.
263
 *
264
 * Returns: (transfer none): a #GOutputStream, owned by the #GIOStream.
265
 * Do not free.
266
 *
267
 * Since: 2.22
268
 */
269
GOutputStream *
270
g_io_stream_get_output_stream (GIOStream *stream)
271
0
{
272
0
  GIOStreamClass *klass;
273
274
0
  klass = G_IO_STREAM_GET_CLASS (stream);
275
276
0
  g_assert (klass->get_output_stream != NULL);
277
0
  return klass->get_output_stream (stream);
278
0
}
279
280
/**
281
 * g_io_stream_has_pending:
282
 * @stream: a #GIOStream
283
 *
284
 * Checks if a stream has pending actions.
285
 *
286
 * Returns: %TRUE if @stream has pending actions.
287
 *
288
 * Since: 2.22
289
 **/
290
gboolean
291
g_io_stream_has_pending (GIOStream *stream)
292
0
{
293
0
  g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
294
295
0
  return stream->priv->pending;
296
0
}
297
298
/**
299
 * g_io_stream_set_pending:
300
 * @stream: a #GIOStream
301
 * @error: a #GError location to store the error occurring, or %NULL to
302
 *     ignore
303
 *
304
 * Sets @stream to have actions pending. If the pending flag is
305
 * already set or @stream is closed, it will return %FALSE and set
306
 * @error.
307
 *
308
 * Returns: %TRUE if pending was previously unset and is now set.
309
 *
310
 * Since: 2.22
311
 */
312
gboolean
313
g_io_stream_set_pending (GIOStream  *stream,
314
       GError    **error)
315
0
{
316
0
  g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
317
318
0
  if (stream->priv->closed)
319
0
    {
320
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
321
0
                           _("Stream is already closed"));
322
0
      return FALSE;
323
0
    }
324
325
0
  if (stream->priv->pending)
326
0
    {
327
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
328
                           /* Translators: This is an error you get if there is
329
                            * already an operation running against this stream when
330
                            * you try to start one */
331
0
                           _("Stream has outstanding operation"));
332
0
      return FALSE;
333
0
    }
334
335
0
  stream->priv->pending = TRUE;
336
0
  return TRUE;
337
0
}
338
339
/**
340
 * g_io_stream_clear_pending:
341
 * @stream: a #GIOStream
342
 *
343
 * Clears the pending flag on @stream.
344
 *
345
 * Since: 2.22
346
 */
347
void
348
g_io_stream_clear_pending (GIOStream *stream)
349
0
{
350
0
  g_return_if_fail (G_IS_IO_STREAM (stream));
351
352
0
  stream->priv->pending = FALSE;
353
0
}
354
355
static gboolean
356
g_io_stream_real_close (GIOStream     *stream,
357
      GCancellable  *cancellable,
358
      GError       **error)
359
0
{
360
0
  gboolean res;
361
362
0
  res = g_output_stream_close (g_io_stream_get_output_stream (stream),
363
0
             cancellable, error);
364
365
  /* If this errored out, unset error so that we don't report
366
     further errors, but still do the following ops */
367
0
  if (error != NULL && *error != NULL)
368
0
    error = NULL;
369
370
0
  res &= g_input_stream_close (g_io_stream_get_input_stream (stream),
371
0
             cancellable, error);
372
373
0
  return res;
374
0
}
375
376
/**
377
 * g_io_stream_close:
378
 * @stream: a #GIOStream
379
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
380
 * @error: location to store the error occurring, or %NULL to ignore
381
 *
382
 * Closes the stream, releasing resources related to it. This will also
383
 * close the individual input and output streams, if they are not already
384
 * closed.
385
 *
386
 * Once the stream is closed, all other operations will return
387
 * %G_IO_ERROR_CLOSED. Closing a stream multiple times will not
388
 * return an error.
389
 *
390
 * Closing a stream will automatically flush any outstanding buffers
391
 * in the stream.
392
 *
393
 * Streams will be automatically closed when the last reference
394
 * is dropped, but you might want to call this function to make sure
395
 * resources are released as early as possible.
396
 *
397
 * Some streams might keep the backing store of the stream (e.g. a file
398
 * descriptor) open after the stream is closed. See the documentation for
399
 * the individual stream for details.
400
 *
401
 * On failure the first error that happened will be reported, but the
402
 * close operation will finish as much as possible. A stream that failed
403
 * to close will still return %G_IO_ERROR_CLOSED for all operations.
404
 * Still, it is important to check and report the error to the user,
405
 * otherwise there might be a loss of data as all data might not be written.
406
 *
407
 * If @cancellable is not NULL, then the operation can be cancelled by
408
 * triggering the cancellable object from another thread. If the operation
409
 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
410
 * Cancelling a close will still leave the stream closed, but some streams
411
 * can use a faster close that doesn't block to e.g. check errors.
412
 *
413
 * The default implementation of this method just calls close on the
414
 * individual input/output streams.
415
 *
416
 * Returns: %TRUE on success, %FALSE on failure
417
 *
418
 * Since: 2.22
419
 */
420
gboolean
421
g_io_stream_close (GIOStream     *stream,
422
       GCancellable  *cancellable,
423
       GError       **error)
424
0
{
425
0
  GIOStreamClass *class;
426
0
  gboolean res;
427
428
0
  g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
429
430
0
  class = G_IO_STREAM_GET_CLASS (stream);
431
432
0
  if (stream->priv->closed)
433
0
    return TRUE;
434
435
0
  if (!g_io_stream_set_pending (stream, error))
436
0
    return FALSE;
437
438
0
  if (cancellable)
439
0
    g_cancellable_push_current (cancellable);
440
441
0
  res = TRUE;
442
0
  if (class->close_fn)
443
0
    res = class->close_fn (stream, cancellable, error);
444
445
0
  if (cancellable)
446
0
    g_cancellable_pop_current (cancellable);
447
448
0
  stream->priv->closed = TRUE;
449
0
  g_io_stream_clear_pending (stream);
450
451
0
  return res;
452
0
}
453
454
static void
455
async_ready_close_callback_wrapper (GObject      *source_object,
456
                                    GAsyncResult *res,
457
                                    gpointer      user_data)
458
0
{
459
0
  GIOStream *stream = G_IO_STREAM (source_object);
460
0
  GIOStreamClass *klass = G_IO_STREAM_GET_CLASS (stream);
461
0
  GTask *task = user_data;
462
0
  GError *error = NULL;
463
0
  gboolean success;
464
465
0
  stream->priv->closed = TRUE;
466
0
  g_io_stream_clear_pending (stream);
467
468
0
  if (g_async_result_legacy_propagate_error (res, &error))
469
0
    success = FALSE;
470
0
  else
471
0
    success = klass->close_finish (stream, res, &error);
472
473
0
  if (error)
474
0
    g_task_return_error (task, error);
475
0
  else
476
0
    g_task_return_boolean (task, success);
477
478
0
  g_object_unref (task);
479
0
}
480
481
/**
482
 * g_io_stream_close_async:
483
 * @stream: a #GIOStream
484
 * @io_priority: the io priority of the request
485
 * @cancellable: (nullable): optional cancellable object
486
 * @callback: (scope async): a #GAsyncReadyCallback
487
 *   to call when the request is satisfied
488
 * @user_data: the data to pass to callback function
489
 *
490
 * Requests an asynchronous close of the stream, releasing resources
491
 * related to it. When the operation is finished @callback will be
492
 * called. You can then call g_io_stream_close_finish() to get
493
 * the result of the operation.
494
 *
495
 * For behaviour details see g_io_stream_close().
496
 *
497
 * The asynchronous methods have a default fallback that uses threads
498
 * to implement asynchronicity, so they are optional for inheriting
499
 * classes. However, if you override one you must override all.
500
 *
501
 * Since: 2.22
502
 */
503
void
504
g_io_stream_close_async (GIOStream           *stream,
505
       int                  io_priority,
506
       GCancellable        *cancellable,
507
       GAsyncReadyCallback  callback,
508
       gpointer             user_data)
509
0
{
510
0
  GIOStreamClass *class;
511
0
  GError *error = NULL;
512
0
  GTask *task;
513
514
0
  g_return_if_fail (G_IS_IO_STREAM (stream));
515
516
0
  task = g_task_new (stream, cancellable, callback, user_data);
517
0
  g_task_set_source_tag (task, g_io_stream_close_async);
518
519
0
  if (stream->priv->closed)
520
0
    {
521
0
      g_task_return_boolean (task, TRUE);
522
0
      g_object_unref (task);
523
0
      return;
524
0
    }
525
526
0
  if (!g_io_stream_set_pending (stream, &error))
527
0
    {
528
0
      g_task_return_error (task, error);
529
0
      g_object_unref (task);
530
0
      return;
531
0
    }
532
533
0
  class = G_IO_STREAM_GET_CLASS (stream);
534
535
0
  class->close_async (stream, io_priority, cancellable,
536
0
          async_ready_close_callback_wrapper, task);
537
0
}
538
539
/**
540
 * g_io_stream_close_finish:
541
 * @stream: a #GIOStream
542
 * @result: a #GAsyncResult
543
 * @error: a #GError location to store the error occurring, or %NULL to
544
 *    ignore
545
 *
546
 * Closes a stream.
547
 *
548
 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
549
 *
550
 * Since: 2.22
551
 */
552
gboolean
553
g_io_stream_close_finish (GIOStream     *stream,
554
        GAsyncResult  *result,
555
        GError       **error)
556
0
{
557
0
  g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
558
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
559
560
0
  return g_task_propagate_boolean (G_TASK (result), error);
561
0
}
562
563
564
static void
565
close_async_thread (GTask        *task,
566
                    gpointer      source_object,
567
                    gpointer      task_data,
568
                    GCancellable *cancellable)
569
0
{
570
0
  GIOStream *stream = source_object;
571
0
  GIOStreamClass *class;
572
0
  GError *error = NULL;
573
0
  gboolean result;
574
575
0
  class = G_IO_STREAM_GET_CLASS (stream);
576
0
  if (class->close_fn)
577
0
    {
578
0
      result = class->close_fn (stream,
579
0
                                g_task_get_cancellable (task),
580
0
                                &error);
581
0
      if (!result)
582
0
        {
583
0
          g_task_return_error (task, error);
584
0
          return;
585
0
        }
586
0
    }
587
588
0
  g_task_return_boolean (task, TRUE);
589
0
}
590
591
typedef struct
592
{
593
  GError *error;
594
  gint pending;
595
} CloseAsyncData;
596
597
static void
598
stream_close_complete (GObject      *source,
599
                       GAsyncResult *result,
600
                       gpointer      user_data)
601
0
{
602
0
  GTask *task = user_data;
603
0
  CloseAsyncData *data;
604
605
0
  data = g_task_get_task_data (task);
606
0
  data->pending--;
607
608
0
  if (G_IS_OUTPUT_STREAM (source))
609
0
    {
610
0
      GError *error = NULL;
611
612
      /* Match behaviour with the sync route and give precedent to the
613
       * error returned from closing the output stream.
614
       */
615
0
      g_output_stream_close_finish (G_OUTPUT_STREAM (source), result, &error);
616
0
      if (error)
617
0
        {
618
0
          if (data->error)
619
0
            g_error_free (data->error);
620
0
          data->error = error;
621
0
        }
622
0
    }
623
0
  else
624
0
    g_input_stream_close_finish (G_INPUT_STREAM (source), result, data->error ? NULL : &data->error);
625
626
0
  if (data->pending == 0)
627
0
    {
628
0
      if (data->error)
629
0
        g_task_return_error (task, data->error);
630
0
      else
631
0
        g_task_return_boolean (task, TRUE);
632
633
0
      g_slice_free (CloseAsyncData, data);
634
0
      g_object_unref (task);
635
0
    }
636
0
}
637
638
static void
639
g_io_stream_real_close_async (GIOStream           *stream,
640
            int                  io_priority,
641
            GCancellable        *cancellable,
642
            GAsyncReadyCallback  callback,
643
            gpointer             user_data)
644
0
{
645
0
  GInputStream *input;
646
0
  GOutputStream *output;
647
0
  GTask *task;
648
649
0
  task = g_task_new (stream, cancellable, callback, user_data);
650
0
  g_task_set_source_tag (task, g_io_stream_real_close_async);
651
0
  g_task_set_check_cancellable (task, FALSE);
652
0
  g_task_set_priority (task, io_priority);
653
654
0
  input = g_io_stream_get_input_stream (stream);
655
0
  output = g_io_stream_get_output_stream (stream);
656
657
0
  if (g_input_stream_async_close_is_via_threads (input) && g_output_stream_async_close_is_via_threads (output))
658
0
    {
659
      /* No sense in dispatching to the thread twice -- just do it all
660
       * in one go.
661
       */
662
0
      g_task_run_in_thread (task, close_async_thread);
663
0
      g_object_unref (task);
664
0
    }
665
0
  else
666
0
    {
667
0
      CloseAsyncData *data;
668
669
      /* We should avoid dispatching to another thread in case either
670
       * object that would not do it for itself because it may not be
671
       * threadsafe.
672
       */
673
0
      data = g_slice_new (CloseAsyncData);
674
0
      data->error = NULL;
675
0
      data->pending = 2;
676
677
0
      g_task_set_task_data (task, data, NULL);
678
0
      g_input_stream_close_async (input, io_priority, cancellable, stream_close_complete, task);
679
0
      g_output_stream_close_async (output, io_priority, cancellable, stream_close_complete, task);
680
0
    }
681
0
}
682
683
static gboolean
684
g_io_stream_real_close_finish (GIOStream     *stream,
685
             GAsyncResult  *result,
686
             GError       **error)
687
0
{
688
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
689
690
0
  return g_task_propagate_boolean (G_TASK (result), error);
691
0
}
692
693
typedef struct
694
{
695
  GIOStream *stream1;
696
  GIOStream *stream2;
697
  GIOStreamSpliceFlags flags;
698
  gint io_priority;
699
  GCancellable *cancellable;
700
  gulong cancelled_id;
701
  GCancellable *op1_cancellable;
702
  GCancellable *op2_cancellable;
703
  guint completed;
704
  GError *error;
705
} SpliceContext;
706
707
static void
708
splice_context_free (SpliceContext *ctx)
709
0
{
710
0
  g_object_unref (ctx->stream1);
711
0
  g_object_unref (ctx->stream2);
712
0
  if (ctx->cancellable != NULL)
713
0
    g_object_unref (ctx->cancellable);
714
0
  g_object_unref (ctx->op1_cancellable);
715
0
  g_object_unref (ctx->op2_cancellable);
716
0
  g_clear_error (&ctx->error);
717
0
  g_slice_free (SpliceContext, ctx);
718
0
}
719
720
static void
721
splice_complete (GTask         *task,
722
                 SpliceContext *ctx)
723
0
{
724
0
  if (ctx->cancelled_id != 0)
725
0
    g_cancellable_disconnect (ctx->cancellable, ctx->cancelled_id);
726
0
  ctx->cancelled_id = 0;
727
728
0
  if (ctx->error != NULL)
729
0
    {
730
0
      g_task_return_error (task, ctx->error);
731
0
      ctx->error = NULL;
732
0
    }
733
0
  else
734
0
    g_task_return_boolean (task, TRUE);
735
0
}
736
737
static void
738
splice_close_cb (GObject      *iostream,
739
                 GAsyncResult *res,
740
                 gpointer      user_data)
741
0
{
742
0
  GTask *task = user_data;
743
0
  SpliceContext *ctx = g_task_get_task_data (task);
744
0
  GError *error = NULL;
745
746
0
  g_io_stream_close_finish (G_IO_STREAM (iostream), res, &error);
747
748
0
  ctx->completed++;
749
750
  /* Keep the first error that occurred */
751
0
  if (error != NULL && ctx->error == NULL)
752
0
    ctx->error = error;
753
0
  else
754
0
    g_clear_error (&error);
755
756
  /* If all operations are done, complete now */
757
0
  if (ctx->completed == 4)
758
0
    splice_complete (task, ctx);
759
760
0
  g_object_unref (task);
761
0
}
762
763
static void
764
splice_cb (GObject      *ostream,
765
           GAsyncResult *res,
766
           gpointer      user_data)
767
0
{
768
0
  GTask *task = user_data;
769
0
  SpliceContext *ctx = g_task_get_task_data (task);
770
0
  GError *error = NULL;
771
772
0
  g_output_stream_splice_finish (G_OUTPUT_STREAM (ostream), res, &error);
773
774
0
  ctx->completed++;
775
776
  /* ignore cancellation error if it was not requested by the user */
777
0
  if (error != NULL &&
778
0
      g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
779
0
      (ctx->cancellable == NULL ||
780
0
       !g_cancellable_is_cancelled (ctx->cancellable)))
781
0
    g_clear_error (&error);
782
783
  /* Keep the first error that occurred */
784
0
  if (error != NULL && ctx->error == NULL)
785
0
    ctx->error = error;
786
0
  else
787
0
    g_clear_error (&error);
788
789
0
   if (ctx->completed == 1 &&
790
0
       (ctx->flags & G_IO_STREAM_SPLICE_WAIT_FOR_BOTH) == 0)
791
0
    {
792
      /* We don't want to wait for the 2nd operation to finish, cancel it */
793
0
      g_cancellable_cancel (ctx->op1_cancellable);
794
0
      g_cancellable_cancel (ctx->op2_cancellable);
795
0
    }
796
0
  else if (ctx->completed == 2)
797
0
    {
798
0
      if (ctx->cancellable == NULL ||
799
0
          !g_cancellable_is_cancelled (ctx->cancellable))
800
0
        {
801
0
          g_cancellable_reset (ctx->op1_cancellable);
802
0
          g_cancellable_reset (ctx->op2_cancellable);
803
0
        }
804
805
      /* Close the IO streams if needed */
806
0
      if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM1) != 0)
807
0
  {
808
0
    g_io_stream_close_async (ctx->stream1,
809
0
                                   g_task_get_priority (task),
810
0
                                   ctx->op1_cancellable,
811
0
                                   splice_close_cb, g_object_ref (task));
812
0
  }
813
0
      else
814
0
        ctx->completed++;
815
816
0
      if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM2) != 0)
817
0
  {
818
0
    g_io_stream_close_async (ctx->stream2,
819
0
                                   g_task_get_priority (task),
820
0
                                   ctx->op2_cancellable,
821
0
                                   splice_close_cb, g_object_ref (task));
822
0
  }
823
0
      else
824
0
        ctx->completed++;
825
826
      /* If all operations are done, complete now */
827
0
      if (ctx->completed == 4)
828
0
        splice_complete (task, ctx);
829
0
    }
830
831
0
  g_object_unref (task);
832
0
}
833
834
static void
835
splice_cancelled_cb (GCancellable *cancellable,
836
                     GTask        *task)
837
0
{
838
0
  SpliceContext *ctx;
839
840
0
  ctx = g_task_get_task_data (task);
841
0
  g_cancellable_cancel (ctx->op1_cancellable);
842
0
  g_cancellable_cancel (ctx->op2_cancellable);
843
0
}
844
845
/**
846
 * g_io_stream_splice_async: (finish-func splice_finish):
847
 * @stream1: a #GIOStream.
848
 * @stream2: a #GIOStream.
849
 * @flags: a set of #GIOStreamSpliceFlags.
850
 * @io_priority: the io priority of the request.
851
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
852
 * @callback: (scope async): a #GAsyncReadyCallback
853
 *   to call when the request is satisfied
854
 * @user_data: the data to pass to callback function
855
 *
856
 * Asynchronously splice the output stream of @stream1 to the input stream of
857
 * @stream2, and splice the output stream of @stream2 to the input stream of
858
 * @stream1.
859
 *
860
 * When the operation is finished @callback will be called.
861
 * You can then call g_io_stream_splice_finish() to get the
862
 * result of the operation.
863
 *
864
 * Since: 2.28
865
 **/
866
void
867
g_io_stream_splice_async (GIOStream            *stream1,
868
                          GIOStream            *stream2,
869
                          GIOStreamSpliceFlags  flags,
870
                          gint                  io_priority,
871
                          GCancellable         *cancellable,
872
                          GAsyncReadyCallback   callback,
873
                          gpointer              user_data)
874
0
{
875
0
  GTask *task;
876
0
  SpliceContext *ctx;
877
0
  GInputStream *istream;
878
0
  GOutputStream *ostream;
879
880
0
  if (cancellable != NULL && g_cancellable_is_cancelled (cancellable))
881
0
    {
882
0
      g_task_report_new_error (NULL, callback, user_data,
883
0
                               g_io_stream_splice_async,
884
0
                               G_IO_ERROR, G_IO_ERROR_CANCELLED,
885
0
                               "Operation has been cancelled");
886
0
      return;
887
0
    }
888
889
0
  ctx = g_slice_new0 (SpliceContext);
890
0
  ctx->stream1 = g_object_ref (stream1);
891
0
  ctx->stream2 = g_object_ref (stream2);
892
0
  ctx->flags = flags;
893
0
  ctx->op1_cancellable = g_cancellable_new ();
894
0
  ctx->op2_cancellable = g_cancellable_new ();
895
0
  ctx->completed = 0;
896
897
0
  task = g_task_new (NULL, cancellable, callback, user_data);
898
0
  g_task_set_source_tag (task, g_io_stream_splice_async);
899
0
  g_task_set_task_data (task, ctx, (GDestroyNotify) splice_context_free);
900
901
0
  if (cancellable != NULL)
902
0
    {
903
0
      ctx->cancellable = g_object_ref (cancellable);
904
0
      ctx->cancelled_id = g_cancellable_connect (cancellable,
905
0
          G_CALLBACK (splice_cancelled_cb), g_object_ref (task),
906
0
          g_object_unref);
907
0
    }
908
909
0
  istream = g_io_stream_get_input_stream (stream1);
910
0
  ostream = g_io_stream_get_output_stream (stream2);
911
0
  g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
912
0
      io_priority, ctx->op1_cancellable, splice_cb,
913
0
      g_object_ref (task));
914
915
0
  istream = g_io_stream_get_input_stream (stream2);
916
0
  ostream = g_io_stream_get_output_stream (stream1);
917
0
  g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
918
0
      io_priority, ctx->op2_cancellable, splice_cb,
919
0
      g_object_ref (task));
920
921
0
  g_object_unref (task);
922
0
}
923
924
/**
925
 * g_io_stream_splice_finish:
926
 * @result: a #GAsyncResult.
927
 * @error: a #GError location to store the error occurring, or %NULL to
928
 * ignore.
929
 *
930
 * Finishes an asynchronous io stream splice operation.
931
 *
932
 * Returns: %TRUE on success, %FALSE otherwise.
933
 *
934
 * Since: 2.28
935
 **/
936
gboolean
937
g_io_stream_splice_finish (GAsyncResult  *result,
938
                           GError       **error)
939
0
{
940
0
  g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);
941
942
0
  return g_task_propagate_boolean (G_TASK (result), error);
943
0
}