Coverage Report

Created: 2026-05-23 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/irssi/subprojects/glib-2.74.7/gio/goutputstream.c
Line
Count
Source
1
/* GIO - GLib Input, Output and Streaming Library
2
 * 
3
 * Copyright (C) 2006-2007 Red Hat, Inc.
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General
18
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * Author: Alexander Larsson <alexl@redhat.com>
21
 */
22
23
#include "config.h"
24
#include <string.h>
25
#include "goutputstream.h"
26
#include "gcancellable.h"
27
#include "gasyncresult.h"
28
#include "gtask.h"
29
#include "ginputstream.h"
30
#include "gioerror.h"
31
#include "gioprivate.h"
32
#include "glibintl.h"
33
#include "gpollableoutputstream.h"
34
35
/**
36
 * SECTION:goutputstream
37
 * @short_description: Base class for implementing streaming output
38
 * @include: gio/gio.h
39
 *
40
 * #GOutputStream has functions to write to a stream (g_output_stream_write()),
41
 * to close a stream (g_output_stream_close()) and to flush pending writes
42
 * (g_output_stream_flush()). 
43
 *
44
 * To copy the content of an input stream to an output stream without 
45
 * manually handling the reads and writes, use g_output_stream_splice().
46
 *
47
 * See the documentation for #GIOStream for details of thread safety of
48
 * streaming APIs.
49
 *
50
 * All of these functions have async variants too.
51
 **/
52
53
struct _GOutputStreamPrivate {
54
  guint closed : 1;
55
  guint pending : 1;
56
  guint closing : 1;
57
  GAsyncReadyCallback outstanding_callback;
58
};
59
60
0
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GOutputStream, g_output_stream, G_TYPE_OBJECT)
61
0
62
0
static gssize   g_output_stream_real_splice        (GOutputStream             *stream,
63
0
                GInputStream              *source,
64
0
                GOutputStreamSpliceFlags   flags,
65
0
                GCancellable              *cancellable,
66
0
                GError                   **error);
67
0
static void     g_output_stream_real_write_async   (GOutputStream             *stream,
68
0
                const void                *buffer,
69
0
                gsize                      count,
70
0
                int                        io_priority,
71
0
                GCancellable              *cancellable,
72
0
                GAsyncReadyCallback        callback,
73
0
                gpointer                   data);
74
0
static gssize   g_output_stream_real_write_finish  (GOutputStream             *stream,
75
0
                GAsyncResult              *result,
76
0
                GError                   **error);
77
0
static gboolean g_output_stream_real_writev        (GOutputStream             *stream,
78
0
                const GOutputVector       *vectors,
79
0
                gsize                      n_vectors,
80
0
                gsize                     *bytes_written,
81
0
                GCancellable              *cancellable,
82
0
                GError                   **error);
83
0
static void     g_output_stream_real_writev_async  (GOutputStream             *stream,
84
0
                const GOutputVector       *vectors,
85
0
                gsize                      n_vectors,
86
0
                int                        io_priority,
87
0
                GCancellable              *cancellable,
88
0
                GAsyncReadyCallback        callback,
89
0
                gpointer                   data);
90
0
static gboolean g_output_stream_real_writev_finish (GOutputStream             *stream,
91
0
                GAsyncResult              *result,
92
0
                gsize                     *bytes_written,
93
0
                GError                   **error);
94
0
static void     g_output_stream_real_splice_async  (GOutputStream             *stream,
95
0
                GInputStream              *source,
96
0
                GOutputStreamSpliceFlags   flags,
97
0
                int                        io_priority,
98
0
                GCancellable              *cancellable,
99
0
                GAsyncReadyCallback        callback,
100
0
                gpointer                   data);
101
0
static gssize   g_output_stream_real_splice_finish (GOutputStream             *stream,
102
0
                GAsyncResult              *result,
103
0
                GError                   **error);
104
0
static void     g_output_stream_real_flush_async   (GOutputStream             *stream,
105
0
                int                        io_priority,
106
0
                GCancellable              *cancellable,
107
0
                GAsyncReadyCallback        callback,
108
0
                gpointer                   data);
109
0
static gboolean g_output_stream_real_flush_finish  (GOutputStream             *stream,
110
0
                GAsyncResult              *result,
111
0
                GError                   **error);
112
0
static void     g_output_stream_real_close_async   (GOutputStream             *stream,
113
0
                int                        io_priority,
114
0
                GCancellable              *cancellable,
115
0
                GAsyncReadyCallback        callback,
116
0
                gpointer                   data);
117
0
static gboolean g_output_stream_real_close_finish  (GOutputStream             *stream,
118
0
                GAsyncResult              *result,
119
0
                GError                   **error);
120
0
static gboolean g_output_stream_internal_close     (GOutputStream             *stream,
121
0
                                                    GCancellable              *cancellable,
122
0
                                                    GError                   **error);
123
0
static void     g_output_stream_internal_close_async (GOutputStream           *stream,
124
0
                                                      int                      io_priority,
125
0
                                                      GCancellable            *cancellable,
126
0
                                                      GAsyncReadyCallback      callback,
127
0
                                                      gpointer                 data);
128
0
static gboolean g_output_stream_internal_close_finish (GOutputStream          *stream,
129
0
                                                       GAsyncResult           *result,
130
0
                                                       GError                **error);
131
0
132
0
static void
133
0
g_output_stream_dispose (GObject *object)
134
0
{
135
0
  GOutputStream *stream;
136
137
0
  stream = G_OUTPUT_STREAM (object);
138
  
139
0
  if (!stream->priv->closed)
140
0
    g_output_stream_close (stream, NULL, NULL);
141
142
0
  G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
143
0
}
144
145
static void
146
g_output_stream_class_init (GOutputStreamClass *klass)
147
0
{
148
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
149
150
0
  gobject_class->dispose = g_output_stream_dispose;
151
152
0
  klass->splice = g_output_stream_real_splice;
153
  
154
0
  klass->write_async = g_output_stream_real_write_async;
155
0
  klass->write_finish = g_output_stream_real_write_finish;
156
0
  klass->writev_fn = g_output_stream_real_writev;
157
0
  klass->writev_async = g_output_stream_real_writev_async;
158
0
  klass->writev_finish = g_output_stream_real_writev_finish;
159
0
  klass->splice_async = g_output_stream_real_splice_async;
160
0
  klass->splice_finish = g_output_stream_real_splice_finish;
161
0
  klass->flush_async = g_output_stream_real_flush_async;
162
0
  klass->flush_finish = g_output_stream_real_flush_finish;
163
0
  klass->close_async = g_output_stream_real_close_async;
164
0
  klass->close_finish = g_output_stream_real_close_finish;
165
0
}
166
167
static void
168
g_output_stream_init (GOutputStream *stream)
169
0
{
170
0
  stream->priv = g_output_stream_get_instance_private (stream);
171
0
}
172
173
/**
174
 * g_output_stream_write:
175
 * @stream: a #GOutputStream.
176
 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write. 
177
 * @count: the number of bytes to write
178
 * @cancellable: (nullable): optional cancellable object
179
 * @error: location to store the error occurring, or %NULL to ignore
180
 *
181
 * Tries to write @count bytes from @buffer into the stream. Will block
182
 * during the operation.
183
 * 
184
 * If count is 0, returns 0 and does nothing. A value of @count
185
 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
186
 *
187
 * On success, the number of bytes written to the stream is returned.
188
 * It is not an error if this is not the same as the requested size, as it
189
 * can happen e.g. on a partial I/O error, or if there is not enough
190
 * storage in the stream. All writes block until at least one byte
191
 * is written or an error occurs; 0 is never returned (unless
192
 * @count is 0).
193
 * 
194
 * If @cancellable is not %NULL, then the operation can be cancelled by
195
 * triggering the cancellable object from another thread. If the operation
196
 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
197
 * operation was partially finished when the operation was cancelled the
198
 * partial result will be returned, without an error.
199
 *
200
 * On error -1 is returned and @error is set accordingly.
201
 * 
202
 * Virtual: write_fn
203
 *
204
 * Returns: Number of bytes written, or -1 on error
205
 **/
206
gssize
207
g_output_stream_write (GOutputStream  *stream,
208
           const void     *buffer,
209
           gsize           count,
210
           GCancellable   *cancellable,
211
           GError        **error)
212
0
{
213
0
  GOutputStreamClass *class;
214
0
  gssize res;
215
216
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
217
0
  g_return_val_if_fail (buffer != NULL, 0);
218
219
0
  if (count == 0)
220
0
    return 0;
221
  
222
0
  if (((gssize) count) < 0)
223
0
    {
224
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
225
0
       _("Too large count value passed to %s"), G_STRFUNC);
226
0
      return -1;
227
0
    }
228
229
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
230
231
0
  if (class->write_fn == NULL) 
232
0
    {
233
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
234
0
                           _("Output stream doesn’t implement write"));
235
0
      return -1;
236
0
    }
237
  
238
0
  if (!g_output_stream_set_pending (stream, error))
239
0
    return -1;
240
  
241
0
  if (cancellable)
242
0
    g_cancellable_push_current (cancellable);
243
  
244
0
  res = class->write_fn (stream, buffer, count, cancellable, error);
245
  
246
0
  if (cancellable)
247
0
    g_cancellable_pop_current (cancellable);
248
  
249
0
  g_output_stream_clear_pending (stream);
250
251
0
  return res; 
252
0
}
253
254
/**
255
 * g_output_stream_write_all:
256
 * @stream: a #GOutputStream.
257
 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write. 
258
 * @count: the number of bytes to write
259
 * @bytes_written: (out) (optional): location to store the number of bytes that was
260
 *     written to the stream
261
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
262
 * @error: location to store the error occurring, or %NULL to ignore
263
 *
264
 * Tries to write @count bytes from @buffer into the stream. Will block
265
 * during the operation.
266
 * 
267
 * This function is similar to g_output_stream_write(), except it tries to
268
 * write as many bytes as requested, only stopping on an error.
269
 *
270
 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
271
 * is set to @count.
272
 * 
273
 * If there is an error during the operation %FALSE is returned and @error
274
 * is set to indicate the error status.
275
 *
276
 * As a special exception to the normal conventions for functions that
277
 * use #GError, if this function returns %FALSE (and sets @error) then
278
 * @bytes_written will be set to the number of bytes that were
279
 * successfully written before the error was encountered.  This
280
 * functionality is only available from C.  If you need it from another
281
 * language then you must write your own loop around
282
 * g_output_stream_write().
283
 *
284
 * Returns: %TRUE on success, %FALSE if there was an error
285
 **/
286
gboolean
287
g_output_stream_write_all (GOutputStream  *stream,
288
         const void     *buffer,
289
         gsize           count,
290
         gsize          *bytes_written,
291
         GCancellable   *cancellable,
292
         GError        **error)
293
0
{
294
0
  gsize _bytes_written;
295
0
  gssize res;
296
297
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
298
0
  g_return_val_if_fail (buffer != NULL || count == 0, FALSE);
299
300
0
  _bytes_written = 0;
301
0
  while (_bytes_written < count)
302
0
    {
303
0
      res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
304
0
           cancellable, error);
305
0
      if (res == -1)
306
0
  {
307
0
    if (bytes_written)
308
0
      *bytes_written = _bytes_written;
309
0
    return FALSE;
310
0
  }
311
0
      g_return_val_if_fail (res > 0, FALSE);
312
313
0
      _bytes_written += res;
314
0
    }
315
  
316
0
  if (bytes_written)
317
0
    *bytes_written = _bytes_written;
318
319
0
  return TRUE;
320
0
}
321
322
/**
323
 * g_output_stream_writev:
324
 * @stream: a #GOutputStream.
325
 * @vectors: (array length=n_vectors): the buffer containing the #GOutputVectors to write.
326
 * @n_vectors: the number of vectors to write
327
 * @bytes_written: (out) (optional): location to store the number of bytes that were
328
 *     written to the stream
329
 * @cancellable: (nullable): optional cancellable object
330
 * @error: location to store the error occurring, or %NULL to ignore
331
 *
332
 * Tries to write the bytes contained in the @n_vectors @vectors into the
333
 * stream. Will block during the operation.
334
 *
335
 * If @n_vectors is 0 or the sum of all bytes in @vectors is 0, returns 0 and
336
 * does nothing.
337
 *
338
 * On success, the number of bytes written to the stream is returned.
339
 * It is not an error if this is not the same as the requested size, as it
340
 * can happen e.g. on a partial I/O error, or if there is not enough
341
 * storage in the stream. All writes block until at least one byte
342
 * is written or an error occurs; 0 is never returned (unless
343
 * @n_vectors is 0 or the sum of all bytes in @vectors is 0).
344
 *
345
 * If @cancellable is not %NULL, then the operation can be cancelled by
346
 * triggering the cancellable object from another thread. If the operation
347
 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
348
 * operation was partially finished when the operation was cancelled the
349
 * partial result will be returned, without an error.
350
 *
351
 * Some implementations of g_output_stream_writev() may have limitations on the
352
 * aggregate buffer size, and will return %G_IO_ERROR_INVALID_ARGUMENT if these
353
 * are exceeded. For example, when writing to a local file on UNIX platforms,
354
 * the aggregate buffer size must not exceed %G_MAXSSIZE bytes.
355
 *
356
 * Virtual: writev_fn
357
 *
358
 * Returns: %TRUE on success, %FALSE if there was an error
359
 *
360
 * Since: 2.60
361
 */
362
gboolean
363
g_output_stream_writev (GOutputStream        *stream,
364
            const GOutputVector  *vectors,
365
            gsize                 n_vectors,
366
            gsize                *bytes_written,
367
            GCancellable         *cancellable,
368
            GError              **error)
369
0
{
370
0
  GOutputStreamClass *class;
371
0
  gboolean res;
372
0
  gsize _bytes_written = 0;
373
374
0
  if (bytes_written)
375
0
    *bytes_written = 0;
376
377
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
378
0
  g_return_val_if_fail (vectors != NULL || n_vectors == 0, FALSE);
379
0
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
380
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
381
382
0
  if (n_vectors == 0)
383
0
    return TRUE;
384
385
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
386
387
0
  g_return_val_if_fail (class->writev_fn != NULL, FALSE);
388
389
0
  if (!g_output_stream_set_pending (stream, error))
390
0
    return FALSE;
391
392
0
  if (cancellable)
393
0
    g_cancellable_push_current (cancellable);
394
395
0
  res = class->writev_fn (stream, vectors, n_vectors, &_bytes_written, cancellable, error);
396
397
0
  g_warn_if_fail (res || _bytes_written == 0);
398
0
  g_warn_if_fail (res || (error == NULL || *error != NULL));
399
400
0
  if (cancellable)
401
0
    g_cancellable_pop_current (cancellable);
402
403
0
  g_output_stream_clear_pending (stream);
404
405
0
  if (bytes_written)
406
0
    *bytes_written = _bytes_written;
407
408
0
  return res;
409
0
}
410
411
/**
412
 * g_output_stream_writev_all:
413
 * @stream: a #GOutputStream.
414
 * @vectors: (array length=n_vectors): the buffer containing the #GOutputVectors to write.
415
 * @n_vectors: the number of vectors to write
416
 * @bytes_written: (out) (optional): location to store the number of bytes that were
417
 *     written to the stream
418
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
419
 * @error: location to store the error occurring, or %NULL to ignore
420
 *
421
 * Tries to write the bytes contained in the @n_vectors @vectors into the
422
 * stream. Will block during the operation.
423
 *
424
 * This function is similar to g_output_stream_writev(), except it tries to
425
 * write as many bytes as requested, only stopping on an error.
426
 *
427
 * On a successful write of all @n_vectors vectors, %TRUE is returned, and
428
 * @bytes_written is set to the sum of all the sizes of @vectors.
429
 *
430
 * If there is an error during the operation %FALSE is returned and @error
431
 * is set to indicate the error status.
432
 *
433
 * As a special exception to the normal conventions for functions that
434
 * use #GError, if this function returns %FALSE (and sets @error) then
435
 * @bytes_written will be set to the number of bytes that were
436
 * successfully written before the error was encountered.  This
437
 * functionality is only available from C. If you need it from another
438
 * language then you must write your own loop around
439
 * g_output_stream_write().
440
 *
441
 * The content of the individual elements of @vectors might be changed by this
442
 * function.
443
 *
444
 * Returns: %TRUE on success, %FALSE if there was an error
445
 *
446
 * Since: 2.60
447
 */
448
gboolean
449
g_output_stream_writev_all (GOutputStream  *stream,
450
          GOutputVector  *vectors,
451
          gsize           n_vectors,
452
          gsize          *bytes_written,
453
          GCancellable   *cancellable,
454
          GError        **error)
455
0
{
456
0
  gsize _bytes_written = 0;
457
0
  gsize i, to_be_written = 0;
458
459
0
  if (bytes_written)
460
0
    *bytes_written = 0;
461
462
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
463
0
  g_return_val_if_fail (vectors != NULL || n_vectors == 0, FALSE);
464
0
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
465
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
466
467
  /* We can't write more than G_MAXSIZE bytes overall, otherwise we
468
   * would overflow the bytes_written counter */
469
0
  for (i = 0; i < n_vectors; i++)
470
0
    {
471
0
       if (to_be_written > G_MAXSIZE - vectors[i].size)
472
0
         {
473
0
           g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
474
0
                        _("Sum of vectors passed to %s too large"), G_STRFUNC);
475
0
           return FALSE;
476
0
         }
477
0
       to_be_written += vectors[i].size;
478
0
    }
479
480
0
  _bytes_written = 0;
481
0
  while (n_vectors > 0 && to_be_written > 0)
482
0
    {
483
0
      gsize n_written = 0;
484
0
      gboolean res;
485
486
0
      res = g_output_stream_writev (stream, vectors, n_vectors, &n_written, cancellable, error);
487
488
0
      if (!res)
489
0
        {
490
0
          if (bytes_written)
491
0
            *bytes_written = _bytes_written;
492
0
          return FALSE;
493
0
        }
494
495
0
      g_return_val_if_fail (n_written > 0, FALSE);
496
0
      _bytes_written += n_written;
497
498
      /* skip vectors that have been written in full */
499
0
      while (n_vectors > 0 && n_written >= vectors[0].size)
500
0
        {
501
0
          n_written -= vectors[0].size;
502
0
          ++vectors;
503
0
          --n_vectors;
504
0
        }
505
      /* skip partially written vector data */
506
0
      if (n_written > 0 && n_vectors > 0)
507
0
        {
508
0
          vectors[0].size -= n_written;
509
0
          vectors[0].buffer = ((guint8 *) vectors[0].buffer) + n_written;
510
0
        }
511
0
    }
512
513
0
  if (bytes_written)
514
0
    *bytes_written = _bytes_written;
515
516
0
  return TRUE;
517
0
}
518
519
/**
520
 * g_output_stream_printf:
521
 * @stream: a #GOutputStream.
522
 * @bytes_written: (out) (optional): location to store the number of bytes that was
523
 *     written to the stream
524
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
525
 * @error: location to store the error occurring, or %NULL to ignore
526
 * @format: the format string. See the printf() documentation
527
 * @...: the parameters to insert into the format string
528
 *
529
 * This is a utility function around g_output_stream_write_all(). It
530
 * uses g_strdup_vprintf() to turn @format and @... into a string that
531
 * is then written to @stream.
532
 *
533
 * See the documentation of g_output_stream_write_all() about the
534
 * behavior of the actual write operation.
535
 *
536
 * Note that partial writes cannot be properly checked with this
537
 * function due to the variable length of the written string, if you
538
 * need precise control over partial write failures, you need to
539
 * create you own printf()-like wrapper around g_output_stream_write()
540
 * or g_output_stream_write_all().
541
 *
542
 * Since: 2.40
543
 *
544
 * Returns: %TRUE on success, %FALSE if there was an error
545
 **/
546
gboolean
547
g_output_stream_printf (GOutputStream  *stream,
548
                        gsize          *bytes_written,
549
                        GCancellable   *cancellable,
550
                        GError        **error,
551
                        const gchar    *format,
552
                        ...)
553
0
{
554
0
  va_list  args;
555
0
  gboolean success;
556
557
0
  va_start (args, format);
558
0
  success = g_output_stream_vprintf (stream, bytes_written, cancellable,
559
0
                                     error, format, args);
560
0
  va_end (args);
561
562
0
  return success;
563
0
}
564
565
/**
566
 * g_output_stream_vprintf:
567
 * @stream: a #GOutputStream.
568
 * @bytes_written: (out) (optional): location to store the number of bytes that was
569
 *     written to the stream
570
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
571
 * @error: location to store the error occurring, or %NULL to ignore
572
 * @format: the format string. See the printf() documentation
573
 * @args: the parameters to insert into the format string
574
 *
575
 * This is a utility function around g_output_stream_write_all(). It
576
 * uses g_strdup_vprintf() to turn @format and @args into a string that
577
 * is then written to @stream.
578
 *
579
 * See the documentation of g_output_stream_write_all() about the
580
 * behavior of the actual write operation.
581
 *
582
 * Note that partial writes cannot be properly checked with this
583
 * function due to the variable length of the written string, if you
584
 * need precise control over partial write failures, you need to
585
 * create you own printf()-like wrapper around g_output_stream_write()
586
 * or g_output_stream_write_all().
587
 *
588
 * Since: 2.40
589
 *
590
 * Returns: %TRUE on success, %FALSE if there was an error
591
 **/
592
gboolean
593
g_output_stream_vprintf (GOutputStream  *stream,
594
                         gsize          *bytes_written,
595
                         GCancellable   *cancellable,
596
                         GError        **error,
597
                         const gchar    *format,
598
                         va_list         args)
599
0
{
600
0
  gchar    *text;
601
0
  gboolean  success;
602
603
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
604
0
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
605
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
606
0
  g_return_val_if_fail (format != NULL, FALSE);
607
608
0
  text = g_strdup_vprintf (format, args);
609
0
  success = g_output_stream_write_all (stream,
610
0
                                       text, strlen (text),
611
0
                                       bytes_written, cancellable, error);
612
0
  g_free (text);
613
614
0
  return success;
615
0
}
616
617
/**
618
 * g_output_stream_write_bytes:
619
 * @stream: a #GOutputStream.
620
 * @bytes: the #GBytes to write
621
 * @cancellable: (nullable): optional cancellable object
622
 * @error: location to store the error occurring, or %NULL to ignore
623
 *
624
 * A wrapper function for g_output_stream_write() which takes a
625
 * #GBytes as input.  This can be more convenient for use by language
626
 * bindings or in other cases where the refcounted nature of #GBytes
627
 * is helpful over a bare pointer interface.
628
 *
629
 * However, note that this function may still perform partial writes,
630
 * just like g_output_stream_write().  If that occurs, to continue
631
 * writing, you will need to create a new #GBytes containing just the
632
 * remaining bytes, using g_bytes_new_from_bytes(). Passing the same
633
 * #GBytes instance multiple times potentially can result in duplicated
634
 * data in the output stream.
635
 *
636
 * Returns: Number of bytes written, or -1 on error
637
 **/
638
gssize
639
g_output_stream_write_bytes (GOutputStream  *stream,
640
           GBytes         *bytes,
641
           GCancellable   *cancellable,
642
           GError        **error)
643
0
{
644
0
  gsize size;
645
0
  gconstpointer data;
646
647
0
  data = g_bytes_get_data (bytes, &size);
648
649
0
  return g_output_stream_write (stream,
650
0
                                data, size,
651
0
        cancellable,
652
0
        error);
653
0
}
654
655
/**
656
 * g_output_stream_flush:
657
 * @stream: a #GOutputStream.
658
 * @cancellable: (nullable): optional cancellable object
659
 * @error: location to store the error occurring, or %NULL to ignore
660
 *
661
 * Forces a write of all user-space buffered data for the given
662
 * @stream. Will block during the operation. Closing the stream will
663
 * implicitly cause a flush.
664
 *
665
 * This function is optional for inherited classes.
666
 * 
667
 * If @cancellable is not %NULL, then the operation can be cancelled by
668
 * triggering the cancellable object from another thread. If the operation
669
 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
670
 *
671
 * Returns: %TRUE on success, %FALSE on error
672
 **/
673
gboolean
674
g_output_stream_flush (GOutputStream  *stream,
675
                       GCancellable   *cancellable,
676
                       GError        **error)
677
0
{
678
0
  GOutputStreamClass *class;
679
0
  gboolean res;
680
681
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
682
683
0
  if (!g_output_stream_set_pending (stream, error))
684
0
    return FALSE;
685
  
686
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
687
688
0
  res = TRUE;
689
0
  if (class->flush)
690
0
    {
691
0
      if (cancellable)
692
0
  g_cancellable_push_current (cancellable);
693
      
694
0
      res = class->flush (stream, cancellable, error);
695
      
696
0
      if (cancellable)
697
0
  g_cancellable_pop_current (cancellable);
698
0
    }
699
  
700
0
  g_output_stream_clear_pending (stream);
701
702
0
  return res;
703
0
}
704
705
/**
706
 * g_output_stream_splice:
707
 * @stream: a #GOutputStream.
708
 * @source: a #GInputStream.
709
 * @flags: a set of #GOutputStreamSpliceFlags.
710
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
711
 * @error: a #GError location to store the error occurring, or %NULL to
712
 * ignore.
713
 *
714
 * Splices an input stream into an output stream.
715
 *
716
 * Returns: a #gssize containing the size of the data spliced, or
717
 *     -1 if an error occurred. Note that if the number of bytes
718
 *     spliced is greater than %G_MAXSSIZE, then that will be
719
 *     returned, and there is no way to determine the actual number
720
 *     of bytes spliced.
721
 **/
722
gssize
723
g_output_stream_splice (GOutputStream             *stream,
724
      GInputStream              *source,
725
      GOutputStreamSpliceFlags   flags,
726
      GCancellable              *cancellable,
727
      GError                   **error)
728
0
{
729
0
  GOutputStreamClass *class;
730
0
  gssize bytes_copied;
731
732
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
733
0
  g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
734
735
0
  if (g_input_stream_is_closed (source))
736
0
    {
737
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
738
0
                           _("Source stream is already closed"));
739
0
      return -1;
740
0
    }
741
742
0
  if (!g_output_stream_set_pending (stream, error))
743
0
    return -1;
744
745
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
746
747
0
  if (cancellable)
748
0
    g_cancellable_push_current (cancellable);
749
750
0
  bytes_copied = class->splice (stream, source, flags, cancellable, error);
751
752
0
  if (cancellable)
753
0
    g_cancellable_pop_current (cancellable);
754
755
0
  g_output_stream_clear_pending (stream);
756
757
0
  return bytes_copied;
758
0
}
759
760
static gssize
761
g_output_stream_real_splice (GOutputStream             *stream,
762
                             GInputStream              *source,
763
                             GOutputStreamSpliceFlags   flags,
764
                             GCancellable              *cancellable,
765
                             GError                   **error)
766
0
{
767
0
  GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
768
0
  gssize n_read, n_written;
769
0
  gsize bytes_copied;
770
0
  char buffer[8192], *p;
771
0
  gboolean res;
772
773
0
  bytes_copied = 0;
774
0
  if (class->write_fn == NULL)
775
0
    {
776
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
777
0
                           _("Output stream doesn’t implement write"));
778
0
      res = FALSE;
779
0
      goto notsupported;
780
0
    }
781
782
0
  res = TRUE;
783
0
  do
784
0
    {
785
0
      n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
786
0
      if (n_read == -1)
787
0
  {
788
0
    res = FALSE;
789
0
    break;
790
0
  }
791
792
0
      if (n_read == 0)
793
0
  break;
794
795
0
      p = buffer;
796
0
      while (n_read > 0)
797
0
  {
798
0
    n_written = class->write_fn (stream, p, n_read, cancellable, error);
799
0
    if (n_written == -1)
800
0
      {
801
0
        res = FALSE;
802
0
        break;
803
0
      }
804
805
0
    p += n_written;
806
0
    n_read -= n_written;
807
0
    bytes_copied += n_written;
808
0
  }
809
810
0
      if (bytes_copied > G_MAXSSIZE)
811
0
  bytes_copied = G_MAXSSIZE;
812
0
    }
813
0
  while (res);
814
815
0
 notsupported:
816
0
  if (!res)
817
0
    error = NULL; /* Ignore further errors */
818
819
0
  if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
820
0
    {
821
      /* Don't care about errors in source here */
822
0
      g_input_stream_close (source, cancellable, NULL);
823
0
    }
824
825
0
  if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
826
0
    {
827
      /* But write errors on close are bad! */
828
0
      if (!g_output_stream_internal_close (stream, cancellable, error))
829
0
        res = FALSE;
830
0
    }
831
832
0
  if (res)
833
0
    return bytes_copied;
834
835
0
  return -1;
836
0
}
837
838
/* Must always be called inside
839
 * g_output_stream_set_pending()/g_output_stream_clear_pending(). */
840
static gboolean
841
g_output_stream_internal_close (GOutputStream  *stream,
842
                                GCancellable   *cancellable,
843
                                GError        **error)
844
0
{
845
0
  GOutputStreamClass *class;
846
0
  gboolean res;
847
848
0
  if (stream->priv->closed)
849
0
    return TRUE;
850
851
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
852
853
0
  stream->priv->closing = TRUE;
854
855
0
  if (cancellable)
856
0
    g_cancellable_push_current (cancellable);
857
858
0
  if (class->flush)
859
0
    res = class->flush (stream, cancellable, error);
860
0
  else
861
0
    res = TRUE;
862
863
0
  if (!res)
864
0
    {
865
      /* flushing caused the error that we want to return,
866
       * but we still want to close the underlying stream if possible
867
       */
868
0
      if (class->close_fn)
869
0
        class->close_fn (stream, cancellable, NULL);
870
0
    }
871
0
  else
872
0
    {
873
0
      res = TRUE;
874
0
      if (class->close_fn)
875
0
        res = class->close_fn (stream, cancellable, error);
876
0
    }
877
878
0
  if (cancellable)
879
0
    g_cancellable_pop_current (cancellable);
880
881
0
  stream->priv->closing = FALSE;
882
0
  stream->priv->closed = TRUE;
883
884
0
  return res;
885
0
}
886
887
/**
888
 * g_output_stream_close:
889
 * @stream: A #GOutputStream.
890
 * @cancellable: (nullable): optional cancellable object
891
 * @error: location to store the error occurring, or %NULL to ignore
892
 *
893
 * Closes the stream, releasing resources related to it.
894
 *
895
 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
896
 * Closing a stream multiple times will not return an error.
897
 *
898
 * Closing a stream will automatically flush any outstanding buffers in the
899
 * stream.
900
 *
901
 * Streams will be automatically closed when the last reference
902
 * is dropped, but you might want to call this function to make sure 
903
 * resources are released as early as possible.
904
 *
905
 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
906
 * open after the stream is closed. See the documentation for the individual
907
 * stream for details.
908
 *
909
 * On failure the first error that happened will be reported, but the close
910
 * operation will finish as much as possible. A stream that failed to
911
 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
912
 * is important to check and report the error to the user, otherwise
913
 * there might be a loss of data as all data might not be written.
914
 * 
915
 * If @cancellable is not %NULL, then the operation can be cancelled by
916
 * triggering the cancellable object from another thread. If the operation
917
 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
918
 * Cancelling a close will still leave the stream closed, but there some streams
919
 * can use a faster close that doesn't block to e.g. check errors. On
920
 * cancellation (as with any error) there is no guarantee that all written
921
 * data will reach the target. 
922
 *
923
 * Returns: %TRUE on success, %FALSE on failure
924
 **/
925
gboolean
926
g_output_stream_close (GOutputStream  *stream,
927
           GCancellable   *cancellable,
928
           GError        **error)
929
0
{
930
0
  gboolean res;
931
932
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
933
934
0
  if (stream->priv->closed)
935
0
    return TRUE;
936
937
0
  if (!g_output_stream_set_pending (stream, error))
938
0
    return FALSE;
939
940
0
  res = g_output_stream_internal_close (stream, cancellable, error);
941
942
0
  g_output_stream_clear_pending (stream);
943
  
944
0
  return res;
945
0
}
946
947
static void
948
async_ready_write_callback_wrapper (GObject      *source_object,
949
                                    GAsyncResult *res,
950
                                    gpointer      user_data)
951
0
{
952
0
  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
953
0
  GOutputStreamClass *class;
954
0
  GTask *task = user_data;
955
0
  gssize nwrote;
956
0
  GError *error = NULL;
957
958
0
  g_output_stream_clear_pending (stream);
959
  
960
0
  if (g_async_result_legacy_propagate_error (res, &error))
961
0
    nwrote = -1;
962
0
  else
963
0
    {
964
0
      class = G_OUTPUT_STREAM_GET_CLASS (stream);
965
0
      nwrote = class->write_finish (stream, res, &error);
966
0
    }
967
968
0
  if (nwrote >= 0)
969
0
    g_task_return_int (task, nwrote);
970
0
  else
971
0
    g_task_return_error (task, error);
972
0
  g_object_unref (task);
973
0
}
974
975
/**
976
 * g_output_stream_write_async:
977
 * @stream: A #GOutputStream.
978
 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write. 
979
 * @count: the number of bytes to write
980
 * @io_priority: the io priority of the request.
981
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
982
 * @callback: (scope async): callback to call when the request is satisfied
983
 * @user_data: (closure): the data to pass to callback function
984
 *
985
 * Request an asynchronous write of @count bytes from @buffer into 
986
 * the stream. When the operation is finished @callback will be called.
987
 * You can then call g_output_stream_write_finish() to get the result of the 
988
 * operation.
989
 *
990
 * During an async request no other sync and async calls are allowed, 
991
 * and will result in %G_IO_ERROR_PENDING errors. 
992
 *
993
 * A value of @count larger than %G_MAXSSIZE will cause a 
994
 * %G_IO_ERROR_INVALID_ARGUMENT error.
995
 *
996
 * On success, the number of bytes written will be passed to the
997
 * @callback. It is not an error if this is not the same as the 
998
 * requested size, as it can happen e.g. on a partial I/O error, 
999
 * but generally we try to write as many bytes as requested. 
1000
 *
1001
 * You are guaranteed that this method will never fail with
1002
 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
1003
 * method will just wait until this changes.
1004
 *
1005
 * Any outstanding I/O request with higher priority (lower numerical 
1006
 * value) will be executed before an outstanding request with lower 
1007
 * priority. Default priority is %G_PRIORITY_DEFAULT.
1008
 *
1009
 * The asynchronous methods have a default fallback that uses threads
1010
 * to implement asynchronicity, so they are optional for inheriting 
1011
 * classes. However, if you override one you must override all.
1012
 *
1013
 * For the synchronous, blocking version of this function, see 
1014
 * g_output_stream_write().
1015
 *
1016
 * Note that no copy of @buffer will be made, so it must stay valid
1017
 * until @callback is called. See g_output_stream_write_bytes_async()
1018
 * for a #GBytes version that will automatically hold a reference to
1019
 * the contents (without copying) for the duration of the call.
1020
 */
1021
void
1022
g_output_stream_write_async (GOutputStream       *stream,
1023
           const void          *buffer,
1024
           gsize                count,
1025
           int                  io_priority,
1026
           GCancellable        *cancellable,
1027
           GAsyncReadyCallback  callback,
1028
           gpointer             user_data)
1029
0
{
1030
0
  GOutputStreamClass *class;
1031
0
  GError *error = NULL;
1032
0
  GTask *task;
1033
1034
0
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1035
0
  g_return_if_fail (buffer != NULL);
1036
1037
0
  task = g_task_new (stream, cancellable, callback, user_data);
1038
0
  g_task_set_source_tag (task, g_output_stream_write_async);
1039
0
  g_task_set_priority (task, io_priority);
1040
1041
0
  if (count == 0)
1042
0
    {
1043
0
      g_task_return_int (task, 0);
1044
0
      g_object_unref (task);
1045
0
      return;
1046
0
    }
1047
1048
0
  if (((gssize) count) < 0)
1049
0
    {
1050
0
      g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1051
0
                               _("Too large count value passed to %s"),
1052
0
                               G_STRFUNC);
1053
0
      g_object_unref (task);
1054
0
      return;
1055
0
    }
1056
1057
0
  if (!g_output_stream_set_pending (stream, &error))
1058
0
    {
1059
0
      g_task_return_error (task, error);
1060
0
      g_object_unref (task);
1061
0
      return;
1062
0
    }
1063
  
1064
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
1065
1066
0
  class->write_async (stream, buffer, count, io_priority, cancellable,
1067
0
                      async_ready_write_callback_wrapper, task);
1068
0
}
1069
1070
/**
1071
 * g_output_stream_write_finish:
1072
 * @stream: a #GOutputStream.
1073
 * @result: a #GAsyncResult.
1074
 * @error: a #GError location to store the error occurring, or %NULL to 
1075
 * ignore.
1076
 * 
1077
 * Finishes a stream write operation.
1078
 * 
1079
 * Returns: a #gssize containing the number of bytes written to the stream.
1080
 **/
1081
gssize
1082
g_output_stream_write_finish (GOutputStream  *stream,
1083
                              GAsyncResult   *result,
1084
                              GError        **error)
1085
0
{
1086
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1087
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1088
0
  g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_write_async), FALSE);
1089
1090
  /* @result is always the GTask created by g_output_stream_write_async();
1091
   * we called class->write_finish() from async_ready_write_callback_wrapper.
1092
   */
1093
0
  return g_task_propagate_int (G_TASK (result), error);
1094
0
}
1095
1096
typedef struct
1097
{
1098
  const guint8 *buffer;
1099
  gsize to_write;
1100
  gsize bytes_written;
1101
} AsyncWriteAll;
1102
1103
static void
1104
free_async_write_all (gpointer data)
1105
0
{
1106
0
  g_slice_free (AsyncWriteAll, data);
1107
0
}
1108
1109
static void
1110
write_all_callback (GObject      *stream,
1111
                    GAsyncResult *result,
1112
                    gpointer      user_data)
1113
0
{
1114
0
  GTask *task = user_data;
1115
0
  AsyncWriteAll *data = g_task_get_task_data (task);
1116
1117
0
  if (result)
1118
0
    {
1119
0
      GError *error = NULL;
1120
0
      gssize nwritten;
1121
1122
0
      nwritten = g_output_stream_write_finish (G_OUTPUT_STREAM (stream), result, &error);
1123
1124
0
      if (nwritten == -1)
1125
0
        {
1126
0
          g_task_return_error (task, error);
1127
0
          g_object_unref (task);
1128
0
          return;
1129
0
        }
1130
1131
0
      g_assert_cmpint (nwritten, <=, data->to_write);
1132
0
      g_warn_if_fail (nwritten > 0);
1133
1134
0
      data->to_write -= nwritten;
1135
0
      data->bytes_written += nwritten;
1136
0
    }
1137
1138
0
  if (data->to_write == 0)
1139
0
    {
1140
0
      g_task_return_boolean (task, TRUE);
1141
0
      g_object_unref (task);
1142
0
    }
1143
0
  else
1144
0
    g_output_stream_write_async (G_OUTPUT_STREAM (stream),
1145
0
                                 data->buffer + data->bytes_written,
1146
0
                                 data->to_write,
1147
0
                                 g_task_get_priority (task),
1148
0
                                 g_task_get_cancellable (task),
1149
0
                                 write_all_callback, task);
1150
0
}
1151
1152
static void
1153
write_all_async_thread (GTask        *task,
1154
                        gpointer      source_object,
1155
                        gpointer      task_data,
1156
                        GCancellable *cancellable)
1157
0
{
1158
0
  GOutputStream *stream = source_object;
1159
0
  AsyncWriteAll *data = task_data;
1160
0
  GError *error = NULL;
1161
1162
0
  if (g_output_stream_write_all (stream, data->buffer, data->to_write, &data->bytes_written,
1163
0
                                 g_task_get_cancellable (task), &error))
1164
0
    g_task_return_boolean (task, TRUE);
1165
0
  else
1166
0
    g_task_return_error (task, error);
1167
0
}
1168
1169
/**
1170
 * g_output_stream_write_all_async:
1171
 * @stream: A #GOutputStream
1172
 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write
1173
 * @count: the number of bytes to write
1174
 * @io_priority: the io priority of the request
1175
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
1176
 * @callback: (scope async): callback to call when the request is satisfied
1177
 * @user_data: (closure): the data to pass to callback function
1178
 *
1179
 * Request an asynchronous write of @count bytes from @buffer into
1180
 * the stream. When the operation is finished @callback will be called.
1181
 * You can then call g_output_stream_write_all_finish() to get the result of the
1182
 * operation.
1183
 *
1184
 * This is the asynchronous version of g_output_stream_write_all().
1185
 *
1186
 * Call g_output_stream_write_all_finish() to collect the result.
1187
 *
1188
 * Any outstanding I/O request with higher priority (lower numerical
1189
 * value) will be executed before an outstanding request with lower
1190
 * priority. Default priority is %G_PRIORITY_DEFAULT.
1191
 *
1192
 * Note that no copy of @buffer will be made, so it must stay valid
1193
 * until @callback is called.
1194
 *
1195
 * Since: 2.44
1196
 */
1197
void
1198
g_output_stream_write_all_async (GOutputStream       *stream,
1199
                                 const void          *buffer,
1200
                                 gsize                count,
1201
                                 int                  io_priority,
1202
                                 GCancellable        *cancellable,
1203
                                 GAsyncReadyCallback  callback,
1204
                                 gpointer             user_data)
1205
0
{
1206
0
  AsyncWriteAll *data;
1207
0
  GTask *task;
1208
1209
0
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1210
0
  g_return_if_fail (buffer != NULL || count == 0);
1211
1212
0
  task = g_task_new (stream, cancellable, callback, user_data);
1213
0
  data = g_slice_new0 (AsyncWriteAll);
1214
0
  data->buffer = buffer;
1215
0
  data->to_write = count;
1216
1217
0
  g_task_set_source_tag (task, g_output_stream_write_all_async);
1218
0
  g_task_set_task_data (task, data, free_async_write_all);
1219
0
  g_task_set_priority (task, io_priority);
1220
1221
  /* If async writes are going to be handled via the threadpool anyway
1222
   * then we may as well do it with a single dispatch instead of
1223
   * bouncing in and out.
1224
   */
1225
0
  if (g_output_stream_async_write_is_via_threads (stream))
1226
0
    {
1227
0
      g_task_run_in_thread (task, write_all_async_thread);
1228
0
      g_object_unref (task);
1229
0
    }
1230
0
  else
1231
0
    write_all_callback (G_OBJECT (stream), NULL, task);
1232
0
}
1233
1234
/**
1235
 * g_output_stream_write_all_finish:
1236
 * @stream: a #GOutputStream
1237
 * @result: a #GAsyncResult
1238
 * @bytes_written: (out) (optional): location to store the number of bytes that was written to the stream
1239
 * @error: a #GError location to store the error occurring, or %NULL to ignore.
1240
 *
1241
 * Finishes an asynchronous stream write operation started with
1242
 * g_output_stream_write_all_async().
1243
 *
1244
 * As a special exception to the normal conventions for functions that
1245
 * use #GError, if this function returns %FALSE (and sets @error) then
1246
 * @bytes_written will be set to the number of bytes that were
1247
 * successfully written before the error was encountered.  This
1248
 * functionality is only available from C.  If you need it from another
1249
 * language then you must write your own loop around
1250
 * g_output_stream_write_async().
1251
 *
1252
 * Returns: %TRUE on success, %FALSE if there was an error
1253
 *
1254
 * Since: 2.44
1255
 **/
1256
gboolean
1257
g_output_stream_write_all_finish (GOutputStream  *stream,
1258
                                  GAsyncResult   *result,
1259
                                  gsize          *bytes_written,
1260
                                  GError        **error)
1261
0
{
1262
0
  GTask *task;
1263
1264
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1265
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1266
1267
0
  task = G_TASK (result);
1268
1269
0
  if (bytes_written)
1270
0
    {
1271
0
      AsyncWriteAll *data = (AsyncWriteAll *)g_task_get_task_data (task);
1272
1273
0
      *bytes_written = data->bytes_written;
1274
0
    }
1275
1276
0
  return g_task_propagate_boolean (task, error);
1277
0
}
1278
1279
/**
1280
 * g_output_stream_writev_async:
1281
 * @stream: A #GOutputStream.
1282
 * @vectors: (array length=n_vectors): the buffer containing the #GOutputVectors to write.
1283
 * @n_vectors: the number of vectors to write
1284
 * @io_priority: the I/O priority of the request.
1285
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
1286
 * @callback: (scope async): callback to call when the request is satisfied
1287
 * @user_data: (closure): the data to pass to callback function
1288
 *
1289
 * Request an asynchronous write of the bytes contained in @n_vectors @vectors into
1290
 * the stream. When the operation is finished @callback will be called.
1291
 * You can then call g_output_stream_writev_finish() to get the result of the
1292
 * operation.
1293
 *
1294
 * During an async request no other sync and async calls are allowed,
1295
 * and will result in %G_IO_ERROR_PENDING errors.
1296
 *
1297
 * On success, the number of bytes written will be passed to the
1298
 * @callback. It is not an error if this is not the same as the
1299
 * requested size, as it can happen e.g. on a partial I/O error,
1300
 * but generally we try to write as many bytes as requested.
1301
 *
1302
 * You are guaranteed that this method will never fail with
1303
 * %G_IO_ERROR_WOULD_BLOCK — if @stream can't accept more data, the
1304
 * method will just wait until this changes.
1305
 *
1306
 * Any outstanding I/O request with higher priority (lower numerical
1307
 * value) will be executed before an outstanding request with lower
1308
 * priority. Default priority is %G_PRIORITY_DEFAULT.
1309
 *
1310
 * The asynchronous methods have a default fallback that uses threads
1311
 * to implement asynchronicity, so they are optional for inheriting
1312
 * classes. However, if you override one you must override all.
1313
 *
1314
 * For the synchronous, blocking version of this function, see
1315
 * g_output_stream_writev().
1316
 *
1317
 * Note that no copy of @vectors will be made, so it must stay valid
1318
 * until @callback is called.
1319
 *
1320
 * Since: 2.60
1321
 */
1322
void
1323
g_output_stream_writev_async (GOutputStream             *stream,
1324
            const GOutputVector       *vectors,
1325
            gsize                      n_vectors,
1326
            int                        io_priority,
1327
            GCancellable              *cancellable,
1328
            GAsyncReadyCallback        callback,
1329
            gpointer                   user_data)
1330
0
{
1331
0
  GOutputStreamClass *class;
1332
1333
0
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1334
0
  g_return_if_fail (vectors != NULL || n_vectors == 0);
1335
0
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1336
1337
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
1338
0
  g_return_if_fail (class->writev_async != NULL);
1339
1340
0
  class->writev_async (stream, vectors, n_vectors, io_priority, cancellable,
1341
0
                       callback, user_data);
1342
0
}
1343
1344
/**
1345
 * g_output_stream_writev_finish:
1346
 * @stream: a #GOutputStream.
1347
 * @result: a #GAsyncResult.
1348
 * @bytes_written: (out) (optional): location to store the number of bytes that were written to the stream
1349
 * @error: a #GError location to store the error occurring, or %NULL to
1350
 * ignore.
1351
 *
1352
 * Finishes a stream writev operation.
1353
 *
1354
 * Returns: %TRUE on success, %FALSE if there was an error
1355
 *
1356
 * Since: 2.60
1357
 */
1358
gboolean
1359
g_output_stream_writev_finish (GOutputStream  *stream,
1360
                               GAsyncResult   *result,
1361
                               gsize          *bytes_written,
1362
                               GError        **error)
1363
0
{
1364
0
  GOutputStreamClass *class;
1365
0
  gboolean res;
1366
0
  gsize _bytes_written = 0;
1367
1368
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1369
0
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1370
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1371
1372
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
1373
0
  g_return_val_if_fail (class->writev_finish != NULL, FALSE);
1374
1375
0
  res = class->writev_finish (stream, result, &_bytes_written, error);
1376
1377
0
  g_warn_if_fail (res || _bytes_written == 0);
1378
0
  g_warn_if_fail (res || (error == NULL || *error != NULL));
1379
1380
0
  if (bytes_written)
1381
0
    *bytes_written = _bytes_written;
1382
1383
0
  return res;
1384
0
}
1385
1386
typedef struct
1387
{
1388
  GOutputVector *vectors;
1389
  gsize n_vectors; /* (unowned) */
1390
  gsize bytes_written;
1391
} AsyncWritevAll;
1392
1393
static void
1394
free_async_writev_all (gpointer data)
1395
0
{
1396
0
  g_slice_free (AsyncWritevAll, data);
1397
0
}
1398
1399
static void
1400
writev_all_callback (GObject      *stream,
1401
                     GAsyncResult *result,
1402
                     gpointer      user_data)
1403
0
{
1404
0
  GTask *task = user_data;
1405
0
  AsyncWritevAll *data = g_task_get_task_data (task);
1406
0
  gint priority = g_task_get_priority (task);
1407
0
  GCancellable *cancellable = g_task_get_cancellable (task);
1408
1409
0
  if (result)
1410
0
    {
1411
0
      GError *error = NULL;
1412
0
      gboolean res;
1413
0
      gsize n_written = 0;
1414
1415
0
      res = g_output_stream_writev_finish (G_OUTPUT_STREAM (stream), result, &n_written, &error);
1416
1417
0
      if (!res)
1418
0
        {
1419
0
          g_task_return_error (task, g_steal_pointer (&error));
1420
0
          g_object_unref (task);
1421
0
          return;
1422
0
        }
1423
1424
0
      g_warn_if_fail (n_written > 0);
1425
0
      data->bytes_written += n_written;
1426
1427
      /* skip vectors that have been written in full */
1428
0
      while (data->n_vectors > 0 && n_written >= data->vectors[0].size)
1429
0
        {
1430
0
          n_written -= data->vectors[0].size;
1431
0
          ++data->vectors;
1432
0
          --data->n_vectors;
1433
0
        }
1434
      /* skip partially written vector data */
1435
0
      if (n_written > 0 && data->n_vectors > 0)
1436
0
        {
1437
0
          data->vectors[0].size -= n_written;
1438
0
          data->vectors[0].buffer = ((guint8 *) data->vectors[0].buffer) + n_written;
1439
0
        }
1440
0
    }
1441
1442
0
  if (data->n_vectors == 0)
1443
0
    {
1444
0
      g_task_return_boolean (task, TRUE);
1445
0
      g_object_unref (task);
1446
0
    }
1447
0
  else
1448
0
    g_output_stream_writev_async (G_OUTPUT_STREAM (stream),
1449
0
                                  data->vectors,
1450
0
                                  data->n_vectors,
1451
0
                                  priority,
1452
0
                                  cancellable,
1453
0
                                  writev_all_callback, g_steal_pointer (&task));
1454
0
}
1455
1456
static void
1457
writev_all_async_thread (GTask        *task,
1458
                         gpointer      source_object,
1459
                         gpointer      task_data,
1460
                         GCancellable *cancellable)
1461
0
{
1462
0
  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1463
0
  AsyncWritevAll *data = task_data;
1464
0
  GError *error = NULL;
1465
1466
0
  if (g_output_stream_writev_all (stream, data->vectors, data->n_vectors, &data->bytes_written,
1467
0
                                  g_task_get_cancellable (task), &error))
1468
0
    g_task_return_boolean (task, TRUE);
1469
0
  else
1470
0
    g_task_return_error (task, g_steal_pointer (&error));
1471
0
}
1472
1473
/**
1474
 * g_output_stream_writev_all_async:
1475
 * @stream: A #GOutputStream
1476
 * @vectors: (array length=n_vectors): the buffer containing the #GOutputVectors to write.
1477
 * @n_vectors: the number of vectors to write
1478
 * @io_priority: the I/O priority of the request
1479
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
1480
 * @callback: (scope async): callback to call when the request is satisfied
1481
 * @user_data: (closure): the data to pass to callback function
1482
 *
1483
 * Request an asynchronous write of the bytes contained in the @n_vectors @vectors into
1484
 * the stream. When the operation is finished @callback will be called.
1485
 * You can then call g_output_stream_writev_all_finish() to get the result of the
1486
 * operation.
1487
 *
1488
 * This is the asynchronous version of g_output_stream_writev_all().
1489
 *
1490
 * Call g_output_stream_writev_all_finish() to collect the result.
1491
 *
1492
 * Any outstanding I/O request with higher priority (lower numerical
1493
 * value) will be executed before an outstanding request with lower
1494
 * priority. Default priority is %G_PRIORITY_DEFAULT.
1495
 *
1496
 * Note that no copy of @vectors will be made, so it must stay valid
1497
 * until @callback is called. The content of the individual elements
1498
 * of @vectors might be changed by this function.
1499
 *
1500
 * Since: 2.60
1501
 */
1502
void
1503
g_output_stream_writev_all_async (GOutputStream       *stream,
1504
                                  GOutputVector       *vectors,
1505
                                  gsize                n_vectors,
1506
                                  int                  io_priority,
1507
                                  GCancellable        *cancellable,
1508
                                  GAsyncReadyCallback  callback,
1509
                                  gpointer             user_data)
1510
0
{
1511
0
  AsyncWritevAll *data;
1512
0
  GTask *task;
1513
0
  gsize i, to_be_written = 0;
1514
1515
0
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1516
0
  g_return_if_fail (vectors != NULL || n_vectors == 0);
1517
0
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1518
1519
0
  task = g_task_new (stream, cancellable, callback, user_data);
1520
0
  data = g_slice_new0 (AsyncWritevAll);
1521
0
  data->vectors = vectors;
1522
0
  data->n_vectors = n_vectors;
1523
1524
0
  g_task_set_source_tag (task, g_output_stream_writev_all_async);
1525
0
  g_task_set_task_data (task, data, free_async_writev_all);
1526
0
  g_task_set_priority (task, io_priority);
1527
1528
  /* We can't write more than G_MAXSIZE bytes overall, otherwise we
1529
   * would overflow the bytes_written counter */
1530
0
  for (i = 0; i < n_vectors; i++)
1531
0
    {
1532
0
       if (to_be_written > G_MAXSIZE - vectors[i].size)
1533
0
         {
1534
0
           g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1535
0
                                    _("Sum of vectors passed to %s too large"),
1536
0
                                    G_STRFUNC);
1537
0
           g_object_unref (task);
1538
0
           return;
1539
0
         }
1540
0
       to_be_written += vectors[i].size;
1541
0
    }
1542
1543
  /* If async writes are going to be handled via the threadpool anyway
1544
   * then we may as well do it with a single dispatch instead of
1545
   * bouncing in and out.
1546
   */
1547
0
  if (g_output_stream_async_writev_is_via_threads (stream))
1548
0
    {
1549
0
      g_task_run_in_thread (task, writev_all_async_thread);
1550
0
      g_object_unref (task);
1551
0
    }
1552
0
  else
1553
0
    writev_all_callback (G_OBJECT (stream), NULL, g_steal_pointer (&task));
1554
0
}
1555
1556
/**
1557
 * g_output_stream_writev_all_finish:
1558
 * @stream: a #GOutputStream
1559
 * @result: a #GAsyncResult
1560
 * @bytes_written: (out) (optional): location to store the number of bytes that were written to the stream
1561
 * @error: a #GError location to store the error occurring, or %NULL to ignore.
1562
 *
1563
 * Finishes an asynchronous stream write operation started with
1564
 * g_output_stream_writev_all_async().
1565
 *
1566
 * As a special exception to the normal conventions for functions that
1567
 * use #GError, if this function returns %FALSE (and sets @error) then
1568
 * @bytes_written will be set to the number of bytes that were
1569
 * successfully written before the error was encountered.  This
1570
 * functionality is only available from C.  If you need it from another
1571
 * language then you must write your own loop around
1572
 * g_output_stream_writev_async().
1573
 *
1574
 * Returns: %TRUE on success, %FALSE if there was an error
1575
 *
1576
 * Since: 2.60
1577
 */
1578
gboolean
1579
g_output_stream_writev_all_finish (GOutputStream  *stream,
1580
                                   GAsyncResult   *result,
1581
                                   gsize          *bytes_written,
1582
                                   GError        **error)
1583
0
{
1584
0
  GTask *task;
1585
1586
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1587
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1588
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1589
1590
0
  task = G_TASK (result);
1591
1592
0
  if (bytes_written)
1593
0
    {
1594
0
      AsyncWritevAll *data = (AsyncWritevAll *)g_task_get_task_data (task);
1595
1596
0
      *bytes_written = data->bytes_written;
1597
0
    }
1598
1599
0
  return g_task_propagate_boolean (task, error);
1600
0
}
1601
1602
static void
1603
write_bytes_callback (GObject      *stream,
1604
                      GAsyncResult *result,
1605
                      gpointer      user_data)
1606
0
{
1607
0
  GTask *task = user_data;
1608
0
  GError *error = NULL;
1609
0
  gssize nwrote;
1610
1611
0
  nwrote = g_output_stream_write_finish (G_OUTPUT_STREAM (stream),
1612
0
                                         result, &error);
1613
0
  if (nwrote == -1)
1614
0
    g_task_return_error (task, error);
1615
0
  else
1616
0
    g_task_return_int (task, nwrote);
1617
0
  g_object_unref (task);
1618
0
}
1619
1620
/**
1621
 * g_output_stream_write_bytes_async:
1622
 * @stream: A #GOutputStream.
1623
 * @bytes: The bytes to write
1624
 * @io_priority: the io priority of the request.
1625
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
1626
 * @callback: (scope async): callback to call when the request is satisfied
1627
 * @user_data: (closure): the data to pass to callback function
1628
 *
1629
 * This function is similar to g_output_stream_write_async(), but
1630
 * takes a #GBytes as input.  Due to the refcounted nature of #GBytes,
1631
 * this allows the stream to avoid taking a copy of the data.
1632
 *
1633
 * However, note that this function may still perform partial writes,
1634
 * just like g_output_stream_write_async(). If that occurs, to continue
1635
 * writing, you will need to create a new #GBytes containing just the
1636
 * remaining bytes, using g_bytes_new_from_bytes(). Passing the same
1637
 * #GBytes instance multiple times potentially can result in duplicated
1638
 * data in the output stream.
1639
 *
1640
 * For the synchronous, blocking version of this function, see
1641
 * g_output_stream_write_bytes().
1642
 **/
1643
void
1644
g_output_stream_write_bytes_async (GOutputStream       *stream,
1645
           GBytes              *bytes,
1646
           int                  io_priority,
1647
           GCancellable        *cancellable,
1648
           GAsyncReadyCallback  callback,
1649
           gpointer             user_data)
1650
0
{
1651
0
  GTask *task;
1652
0
  gsize size;
1653
0
  gconstpointer data;
1654
1655
0
  data = g_bytes_get_data (bytes, &size);
1656
1657
0
  task = g_task_new (stream, cancellable, callback, user_data);
1658
0
  g_task_set_source_tag (task, g_output_stream_write_bytes_async);
1659
0
  g_task_set_task_data (task, g_bytes_ref (bytes),
1660
0
                        (GDestroyNotify) g_bytes_unref);
1661
1662
0
  g_output_stream_write_async (stream,
1663
0
                               data, size,
1664
0
                               io_priority,
1665
0
                               cancellable,
1666
0
                               write_bytes_callback,
1667
0
                               task);
1668
0
}
1669
1670
/**
1671
 * g_output_stream_write_bytes_finish:
1672
 * @stream: a #GOutputStream.
1673
 * @result: a #GAsyncResult.
1674
 * @error: a #GError location to store the error occurring, or %NULL to
1675
 * ignore.
1676
 *
1677
 * Finishes a stream write-from-#GBytes operation.
1678
 *
1679
 * Returns: a #gssize containing the number of bytes written to the stream.
1680
 **/
1681
gssize
1682
g_output_stream_write_bytes_finish (GOutputStream  *stream,
1683
            GAsyncResult   *result,
1684
            GError        **error)
1685
0
{
1686
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
1687
0
  g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1688
1689
0
  return g_task_propagate_int (G_TASK (result), error);
1690
0
}
1691
1692
static void
1693
async_ready_splice_callback_wrapper (GObject      *source_object,
1694
                                     GAsyncResult *res,
1695
                                     gpointer     _data)
1696
0
{
1697
0
  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1698
0
  GOutputStreamClass *class;
1699
0
  GTask *task = _data;
1700
0
  gssize nspliced;
1701
0
  GError *error = NULL;
1702
1703
0
  g_output_stream_clear_pending (stream);
1704
  
1705
0
  if (g_async_result_legacy_propagate_error (res, &error))
1706
0
    nspliced = -1;
1707
0
  else
1708
0
    {
1709
0
      class = G_OUTPUT_STREAM_GET_CLASS (stream);
1710
0
      nspliced = class->splice_finish (stream, res, &error);
1711
0
    }
1712
1713
0
  if (nspliced >= 0)
1714
0
    g_task_return_int (task, nspliced);
1715
0
  else
1716
0
    g_task_return_error (task, error);
1717
0
  g_object_unref (task);
1718
0
}
1719
1720
/**
1721
 * g_output_stream_splice_async:
1722
 * @stream: a #GOutputStream.
1723
 * @source: a #GInputStream. 
1724
 * @flags: a set of #GOutputStreamSpliceFlags.
1725
 * @io_priority: the io priority of the request.
1726
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. 
1727
 * @callback: (scope async): a #GAsyncReadyCallback. 
1728
 * @user_data: (closure): user data passed to @callback.
1729
 * 
1730
 * Splices a stream asynchronously.
1731
 * When the operation is finished @callback will be called.
1732
 * You can then call g_output_stream_splice_finish() to get the 
1733
 * result of the operation.
1734
 *
1735
 * For the synchronous, blocking version of this function, see 
1736
 * g_output_stream_splice().
1737
 **/
1738
void
1739
g_output_stream_splice_async (GOutputStream            *stream,
1740
            GInputStream             *source,
1741
            GOutputStreamSpliceFlags  flags,
1742
            int                       io_priority,
1743
            GCancellable             *cancellable,
1744
            GAsyncReadyCallback       callback,
1745
            gpointer                  user_data)
1746
0
{
1747
0
  GOutputStreamClass *class;
1748
0
  GTask *task;
1749
0
  GError *error = NULL;
1750
1751
0
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1752
0
  g_return_if_fail (G_IS_INPUT_STREAM (source));
1753
1754
0
  task = g_task_new (stream, cancellable, callback, user_data);
1755
0
  g_task_set_source_tag (task, g_output_stream_splice_async);
1756
0
  g_task_set_priority (task, io_priority);
1757
0
  g_task_set_task_data (task, g_object_ref (source), g_object_unref);
1758
1759
0
  if (g_input_stream_is_closed (source))
1760
0
    {
1761
0
      g_task_return_new_error (task,
1762
0
                               G_IO_ERROR, G_IO_ERROR_CLOSED,
1763
0
                               _("Source stream is already closed"));
1764
0
      g_object_unref (task);
1765
0
      return;
1766
0
    }
1767
  
1768
0
  if (!g_output_stream_set_pending (stream, &error))
1769
0
    {
1770
0
      g_task_return_error (task, error);
1771
0
      g_object_unref (task);
1772
0
      return;
1773
0
    }
1774
1775
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
1776
1777
0
  class->splice_async (stream, source, flags, io_priority, cancellable,
1778
0
                       async_ready_splice_callback_wrapper, task);
1779
0
}
1780
1781
/**
1782
 * g_output_stream_splice_finish:
1783
 * @stream: a #GOutputStream.
1784
 * @result: a #GAsyncResult.
1785
 * @error: a #GError location to store the error occurring, or %NULL to 
1786
 * ignore.
1787
 *
1788
 * Finishes an asynchronous stream splice operation.
1789
 * 
1790
 * Returns: a #gssize of the number of bytes spliced. Note that if the
1791
 *     number of bytes spliced is greater than %G_MAXSSIZE, then that
1792
 *     will be returned, and there is no way to determine the actual
1793
 *     number of bytes spliced.
1794
 **/
1795
gssize
1796
g_output_stream_splice_finish (GOutputStream  *stream,
1797
             GAsyncResult   *result,
1798
             GError        **error)
1799
0
{
1800
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1801
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1802
0
  g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_splice_async), FALSE);
1803
1804
  /* @result is always the GTask created by g_output_stream_splice_async();
1805
   * we called class->splice_finish() from async_ready_splice_callback_wrapper.
1806
   */
1807
0
  return g_task_propagate_int (G_TASK (result), error);
1808
0
}
1809
1810
static void
1811
async_ready_flush_callback_wrapper (GObject      *source_object,
1812
                                    GAsyncResult *res,
1813
                                    gpointer      user_data)
1814
0
{
1815
0
  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1816
0
  GOutputStreamClass *class;
1817
0
  GTask *task = user_data;
1818
0
  gboolean flushed;
1819
0
  GError *error = NULL;
1820
1821
0
  g_output_stream_clear_pending (stream);
1822
  
1823
0
  if (g_async_result_legacy_propagate_error (res, &error))
1824
0
    flushed = FALSE;
1825
0
  else
1826
0
    {
1827
0
      class = G_OUTPUT_STREAM_GET_CLASS (stream);
1828
0
      flushed = class->flush_finish (stream, res, &error);
1829
0
    }
1830
1831
0
  if (flushed)
1832
0
    g_task_return_boolean (task, TRUE);
1833
0
  else
1834
0
    g_task_return_error (task, error);
1835
0
  g_object_unref (task);
1836
0
}
1837
1838
/**
1839
 * g_output_stream_flush_async:
1840
 * @stream: a #GOutputStream.
1841
 * @io_priority: the io priority of the request.
1842
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
1843
 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1844
 * @user_data: (closure): the data to pass to callback function
1845
 * 
1846
 * Forces an asynchronous write of all user-space buffered data for
1847
 * the given @stream.
1848
 * For behaviour details see g_output_stream_flush().
1849
 *
1850
 * When the operation is finished @callback will be 
1851
 * called. You can then call g_output_stream_flush_finish() to get the 
1852
 * result of the operation.
1853
 **/
1854
void
1855
g_output_stream_flush_async (GOutputStream       *stream,
1856
                             int                  io_priority,
1857
                             GCancellable        *cancellable,
1858
                             GAsyncReadyCallback  callback,
1859
                             gpointer             user_data)
1860
0
{
1861
0
  GOutputStreamClass *class;
1862
0
  GTask *task;
1863
0
  GError *error = NULL;
1864
1865
0
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1866
1867
0
  task = g_task_new (stream, cancellable, callback, user_data);
1868
0
  g_task_set_source_tag (task, g_output_stream_flush_async);
1869
0
  g_task_set_priority (task, io_priority);
1870
1871
0
  if (!g_output_stream_set_pending (stream, &error))
1872
0
    {
1873
0
      g_task_return_error (task, error);
1874
0
      g_object_unref (task);
1875
0
      return;
1876
0
    }
1877
1878
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
1879
  
1880
0
  if (class->flush_async == NULL)
1881
0
    {
1882
0
      g_output_stream_clear_pending (stream);
1883
0
      g_task_return_boolean (task, TRUE);
1884
0
      g_object_unref (task);
1885
0
      return;
1886
0
    }
1887
      
1888
0
  class->flush_async (stream, io_priority, cancellable,
1889
0
                      async_ready_flush_callback_wrapper, task);
1890
0
}
1891
1892
/**
1893
 * g_output_stream_flush_finish:
1894
 * @stream: a #GOutputStream.
1895
 * @result: a GAsyncResult.
1896
 * @error: a #GError location to store the error occurring, or %NULL to 
1897
 * ignore.
1898
 * 
1899
 * Finishes flushing an output stream.
1900
 * 
1901
 * Returns: %TRUE if flush operation succeeded, %FALSE otherwise.
1902
 **/
1903
gboolean
1904
g_output_stream_flush_finish (GOutputStream  *stream,
1905
                              GAsyncResult   *result,
1906
                              GError        **error)
1907
0
{
1908
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1909
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1910
0
  g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_flush_async), FALSE);
1911
1912
  /* @result is always the GTask created by g_output_stream_flush_async();
1913
   * we called class->flush_finish() from async_ready_flush_callback_wrapper.
1914
   */
1915
0
  return g_task_propagate_boolean (G_TASK (result), error);
1916
0
}
1917
1918
1919
static void
1920
async_ready_close_callback_wrapper (GObject      *source_object,
1921
                                    GAsyncResult *res,
1922
                                    gpointer      user_data)
1923
0
{
1924
0
  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1925
0
  GOutputStreamClass *class;
1926
0
  GTask *task = user_data;
1927
0
  GError *error = g_task_get_task_data (task);
1928
1929
0
  stream->priv->closing = FALSE;
1930
0
  stream->priv->closed = TRUE;
1931
1932
0
  if (!error && !g_async_result_legacy_propagate_error (res, &error))
1933
0
    {
1934
0
      class = G_OUTPUT_STREAM_GET_CLASS (stream);
1935
1936
0
      class->close_finish (stream, res,
1937
0
                           error ? NULL : &error);
1938
0
    }
1939
1940
0
  if (error != NULL)
1941
0
    g_task_return_error (task, error);
1942
0
  else
1943
0
    g_task_return_boolean (task, TRUE);
1944
0
  g_object_unref (task);
1945
0
}
1946
1947
static void
1948
async_ready_close_flushed_callback_wrapper (GObject      *source_object,
1949
                                            GAsyncResult *res,
1950
                                            gpointer      user_data)
1951
0
{
1952
0
  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1953
0
  GOutputStreamClass *class;
1954
0
  GTask *task = user_data;
1955
0
  GError *error = NULL;
1956
1957
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
1958
1959
0
  if (!g_async_result_legacy_propagate_error (res, &error))
1960
0
    {
1961
0
      class->flush_finish (stream, res, &error);
1962
0
    }
1963
1964
  /* propagate the possible error */
1965
0
  if (error)
1966
0
    g_task_set_task_data (task, error, NULL);
1967
1968
  /* we still close, even if there was a flush error */
1969
0
  class->close_async (stream,
1970
0
                      g_task_get_priority (task),
1971
0
                      g_task_get_cancellable (task),
1972
0
                      async_ready_close_callback_wrapper, task);
1973
0
}
1974
1975
static void
1976
real_close_async_cb (GObject      *source_object,
1977
                     GAsyncResult *res,
1978
                     gpointer      user_data)
1979
0
{
1980
0
  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1981
0
  GTask *task = user_data;
1982
0
  GError *error = NULL;
1983
0
  gboolean ret;
1984
1985
0
  g_output_stream_clear_pending (stream);
1986
1987
0
  ret = g_output_stream_internal_close_finish (stream, res, &error);
1988
1989
0
  if (error != NULL)
1990
0
    g_task_return_error (task, error);
1991
0
  else
1992
0
    g_task_return_boolean (task, ret);
1993
1994
0
  g_object_unref (task);
1995
0
}
1996
1997
/**
1998
 * g_output_stream_close_async:
1999
 * @stream: A #GOutputStream.
2000
 * @io_priority: the io priority of the request.
2001
 * @cancellable: (nullable): optional cancellable object
2002
 * @callback: (scope async): callback to call when the request is satisfied
2003
 * @user_data: (closure): the data to pass to callback function
2004
 *
2005
 * Requests an asynchronous close of the stream, releasing resources 
2006
 * related to it. When the operation is finished @callback will be 
2007
 * called. You can then call g_output_stream_close_finish() to get 
2008
 * the result of the operation.
2009
 *
2010
 * For behaviour details see g_output_stream_close().
2011
 *
2012
 * The asynchronous methods have a default fallback that uses threads
2013
 * to implement asynchronicity, so they are optional for inheriting 
2014
 * classes. However, if you override one you must override all.
2015
 **/
2016
void
2017
g_output_stream_close_async (GOutputStream       *stream,
2018
                             int                  io_priority,
2019
                             GCancellable        *cancellable,
2020
                             GAsyncReadyCallback  callback,
2021
                             gpointer             user_data)
2022
0
{
2023
0
  GTask *task;
2024
0
  GError *error = NULL;
2025
2026
0
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
2027
  
2028
0
  task = g_task_new (stream, cancellable, callback, user_data);
2029
0
  g_task_set_source_tag (task, g_output_stream_close_async);
2030
0
  g_task_set_priority (task, io_priority);
2031
2032
0
  if (!g_output_stream_set_pending (stream, &error))
2033
0
    {
2034
0
      g_task_return_error (task, error);
2035
0
      g_object_unref (task);
2036
0
      return;
2037
0
    }
2038
2039
0
  g_output_stream_internal_close_async (stream, io_priority, cancellable,
2040
0
                                        real_close_async_cb, task);
2041
0
}
2042
2043
/* Must always be called inside
2044
 * g_output_stream_set_pending()/g_output_stream_clear_pending().
2045
 */
2046
void
2047
g_output_stream_internal_close_async (GOutputStream       *stream,
2048
                                      int                  io_priority,
2049
                                      GCancellable        *cancellable,
2050
                                      GAsyncReadyCallback  callback,
2051
                                      gpointer             user_data)
2052
0
{
2053
0
  GOutputStreamClass *class;
2054
0
  GTask *task;
2055
2056
0
  task = g_task_new (stream, cancellable, callback, user_data);
2057
0
  g_task_set_source_tag (task, g_output_stream_internal_close_async);
2058
0
  g_task_set_priority (task, io_priority);
2059
2060
0
  if (stream->priv->closed)
2061
0
    {
2062
0
      g_task_return_boolean (task, TRUE);
2063
0
      g_object_unref (task);
2064
0
      return;
2065
0
    }
2066
2067
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
2068
0
  stream->priv->closing = TRUE;
2069
2070
  /* Call close_async directly if there is no need to flush, or if the flush
2071
     can be done sync (in the output stream async close thread) */
2072
0
  if (class->flush_async == NULL ||
2073
0
      (class->flush_async == g_output_stream_real_flush_async &&
2074
0
       (class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
2075
0
    {
2076
0
      class->close_async (stream, io_priority, cancellable,
2077
0
                          async_ready_close_callback_wrapper, task);
2078
0
    }
2079
0
  else
2080
0
    {
2081
      /* First do an async flush, then do the async close in the callback
2082
         wrapper (see async_ready_close_flushed_callback_wrapper) */
2083
0
      class->flush_async (stream, io_priority, cancellable,
2084
0
                          async_ready_close_flushed_callback_wrapper, task);
2085
0
    }
2086
0
}
2087
2088
static gboolean
2089
g_output_stream_internal_close_finish (GOutputStream  *stream,
2090
                                       GAsyncResult   *result,
2091
                                       GError        **error)
2092
0
{
2093
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
2094
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2095
0
  g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_internal_close_async), FALSE);
2096
2097
0
  return g_task_propagate_boolean (G_TASK (result), error);
2098
0
}
2099
2100
/**
2101
 * g_output_stream_close_finish:
2102
 * @stream: a #GOutputStream.
2103
 * @result: a #GAsyncResult.
2104
 * @error: a #GError location to store the error occurring, or %NULL to 
2105
 * ignore.
2106
 * 
2107
 * Closes an output stream.
2108
 * 
2109
 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
2110
 **/
2111
gboolean
2112
g_output_stream_close_finish (GOutputStream  *stream,
2113
                              GAsyncResult   *result,
2114
                              GError        **error)
2115
0
{
2116
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
2117
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2118
0
  g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_close_async), FALSE);
2119
2120
  /* @result is always the GTask created by g_output_stream_close_async();
2121
   * we called class->close_finish() from async_ready_close_callback_wrapper.
2122
   */
2123
0
  return g_task_propagate_boolean (G_TASK (result), error);
2124
0
}
2125
2126
/**
2127
 * g_output_stream_is_closed:
2128
 * @stream: a #GOutputStream.
2129
 * 
2130
 * Checks if an output stream has already been closed.
2131
 * 
2132
 * Returns: %TRUE if @stream is closed. %FALSE otherwise. 
2133
 **/
2134
gboolean
2135
g_output_stream_is_closed (GOutputStream *stream)
2136
0
{
2137
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
2138
  
2139
0
  return stream->priv->closed;
2140
0
}
2141
2142
/**
2143
 * g_output_stream_is_closing:
2144
 * @stream: a #GOutputStream.
2145
 *
2146
 * Checks if an output stream is being closed. This can be
2147
 * used inside e.g. a flush implementation to see if the
2148
 * flush (or other i/o operation) is called from within
2149
 * the closing operation.
2150
 *
2151
 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
2152
 *
2153
 * Since: 2.24
2154
 **/
2155
gboolean
2156
g_output_stream_is_closing (GOutputStream *stream)
2157
0
{
2158
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
2159
2160
0
  return stream->priv->closing;
2161
0
}
2162
2163
/**
2164
 * g_output_stream_has_pending:
2165
 * @stream: a #GOutputStream.
2166
 * 
2167
 * Checks if an output stream has pending actions.
2168
 * 
2169
 * Returns: %TRUE if @stream has pending actions. 
2170
 **/
2171
gboolean
2172
g_output_stream_has_pending (GOutputStream *stream)
2173
0
{
2174
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
2175
  
2176
0
  return stream->priv->pending;
2177
0
}
2178
2179
/**
2180
 * g_output_stream_set_pending:
2181
 * @stream: a #GOutputStream.
2182
 * @error: a #GError location to store the error occurring, or %NULL to 
2183
 * ignore.
2184
 * 
2185
 * Sets @stream to have actions pending. If the pending flag is
2186
 * already set or @stream is closed, it will return %FALSE and set
2187
 * @error.
2188
 *
2189
 * Returns: %TRUE if pending was previously unset and is now set.
2190
 **/
2191
gboolean
2192
g_output_stream_set_pending (GOutputStream *stream,
2193
           GError **error)
2194
0
{
2195
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
2196
  
2197
0
  if (stream->priv->closed)
2198
0
    {
2199
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
2200
0
                           _("Stream is already closed"));
2201
0
      return FALSE;
2202
0
    }
2203
  
2204
0
  if (stream->priv->pending)
2205
0
    {
2206
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
2207
                           /* Translators: This is an error you get if there is
2208
                            * already an operation running against this stream when
2209
                            * you try to start one */
2210
0
                           _("Stream has outstanding operation"));
2211
0
      return FALSE;
2212
0
    }
2213
  
2214
0
  stream->priv->pending = TRUE;
2215
0
  return TRUE;
2216
0
}
2217
2218
/**
2219
 * g_output_stream_clear_pending:
2220
 * @stream: output stream
2221
 * 
2222
 * Clears the pending flag on @stream.
2223
 **/
2224
void
2225
g_output_stream_clear_pending (GOutputStream *stream)
2226
0
{
2227
0
  g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
2228
  
2229
0
  stream->priv->pending = FALSE;
2230
0
}
2231
2232
/*< internal >
2233
 * g_output_stream_async_write_is_via_threads:
2234
 * @stream: a #GOutputStream.
2235
 *
2236
 * Checks if an output stream's write_async function uses threads.
2237
 *
2238
 * Returns: %TRUE if @stream's write_async function uses threads.
2239
 **/
2240
gboolean
2241
g_output_stream_async_write_is_via_threads (GOutputStream *stream)
2242
0
{
2243
0
  GOutputStreamClass *class;
2244
2245
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
2246
2247
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
2248
2249
0
  return (class->write_async == g_output_stream_real_write_async &&
2250
0
      !(G_IS_POLLABLE_OUTPUT_STREAM (stream) &&
2251
0
        g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (stream))));
2252
0
}
2253
2254
/*< internal >
2255
 * g_output_stream_async_writev_is_via_threads:
2256
 * @stream: a #GOutputStream.
2257
 *
2258
 * Checks if an output stream's writev_async function uses threads.
2259
 *
2260
 * Returns: %TRUE if @stream's writev_async function uses threads.
2261
 **/
2262
gboolean
2263
g_output_stream_async_writev_is_via_threads (GOutputStream *stream)
2264
0
{
2265
0
  GOutputStreamClass *class;
2266
2267
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
2268
2269
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
2270
2271
0
  return (class->writev_async == g_output_stream_real_writev_async &&
2272
0
      !(G_IS_POLLABLE_OUTPUT_STREAM (stream) &&
2273
0
        g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (stream))));
2274
0
}
2275
2276
/*< internal >
2277
 * g_output_stream_async_close_is_via_threads:
2278
 * @stream: output stream
2279
 *
2280
 * Checks if an output stream's close_async function uses threads.
2281
 *
2282
 * Returns: %TRUE if @stream's close_async function uses threads.
2283
 **/
2284
gboolean
2285
g_output_stream_async_close_is_via_threads (GOutputStream *stream)
2286
0
{
2287
0
  GOutputStreamClass *class;
2288
2289
0
  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
2290
2291
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
2292
2293
0
  return class->close_async == g_output_stream_real_close_async;
2294
0
}
2295
2296
/********************************************
2297
 *   Default implementation of sync ops    *
2298
 ********************************************/
2299
static gboolean
2300
g_output_stream_real_writev (GOutputStream         *stream,
2301
                             const GOutputVector   *vectors,
2302
                             gsize                  n_vectors,
2303
                             gsize                 *bytes_written,
2304
                             GCancellable          *cancellable,
2305
                             GError               **error)
2306
0
{
2307
0
  GOutputStreamClass *class;
2308
0
  gsize _bytes_written = 0;
2309
0
  gsize i;
2310
0
  GError *err = NULL;
2311
2312
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
2313
2314
0
  if (bytes_written)
2315
0
    *bytes_written = 0;
2316
2317
0
  for (i = 0; i < n_vectors; i++)
2318
0
    {
2319
0
      gssize res = 0;
2320
2321
      /* Would we overflow here? In that case simply return and let the caller
2322
       * handle this like a short write */
2323
0
      if (_bytes_written > G_MAXSIZE - vectors[i].size)
2324
0
        break;
2325
2326
0
      res = class->write_fn (stream, vectors[i].buffer, vectors[i].size, cancellable, &err);
2327
2328
0
      if (res == -1)
2329
0
        {
2330
          /* If we already wrote something  we handle this like a short write
2331
           * and assume that on the next call the same error happens again, or
2332
           * everything finishes successfully without data loss then
2333
           */
2334
0
          if (_bytes_written > 0)
2335
0
            {
2336
0
              if (bytes_written)
2337
0
                *bytes_written = _bytes_written;
2338
2339
0
              g_clear_error (&err);
2340
0
              return TRUE;
2341
0
            }
2342
2343
0
          g_propagate_error (error, err);
2344
0
          return FALSE;
2345
0
        }
2346
2347
0
      _bytes_written += res;
2348
      /* if we had a short write break the loop here */
2349
0
      if ((gsize) res < vectors[i].size)
2350
0
        break;
2351
0
    }
2352
2353
0
  if (bytes_written)
2354
0
    *bytes_written = _bytes_written;
2355
2356
0
  return TRUE;
2357
0
}
2358
2359
/********************************************
2360
 *   Default implementation of async ops    *
2361
 ********************************************/
2362
2363
typedef struct {
2364
  const void         *buffer;
2365
  gsize               count_requested;
2366
  gssize              count_written;
2367
} WriteData;
2368
2369
static void
2370
free_write_data (WriteData *op)
2371
0
{
2372
0
  g_slice_free (WriteData, op);
2373
0
}
2374
2375
static void
2376
write_async_thread (GTask        *task,
2377
                    gpointer      source_object,
2378
                    gpointer      task_data,
2379
                    GCancellable *cancellable)
2380
0
{
2381
0
  GOutputStream *stream = source_object;
2382
0
  WriteData *op = task_data;
2383
0
  GOutputStreamClass *class;
2384
0
  GError *error = NULL;
2385
0
  gssize count_written;
2386
2387
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
2388
0
  count_written = class->write_fn (stream, op->buffer, op->count_requested,
2389
0
                                   cancellable, &error);
2390
0
  if (count_written == -1)
2391
0
    g_task_return_error (task, error);
2392
0
  else
2393
0
    g_task_return_int (task, count_written);
2394
0
}
2395
2396
static void write_async_pollable (GPollableOutputStream *stream,
2397
                                  GTask                 *task);
2398
2399
static gboolean
2400
write_async_pollable_ready (GPollableOutputStream *stream,
2401
          gpointer               user_data)
2402
0
{
2403
0
  GTask *task = user_data;
2404
2405
0
  write_async_pollable (stream, task);
2406
0
  return FALSE;
2407
0
}
2408
2409
static void
2410
write_async_pollable (GPollableOutputStream *stream,
2411
                      GTask                 *task)
2412
0
{
2413
0
  GError *error = NULL;
2414
0
  WriteData *op = g_task_get_task_data (task);
2415
0
  gssize count_written;
2416
2417
0
  if (g_task_return_error_if_cancelled (task))
2418
0
    return;
2419
2420
0
  count_written = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
2421
0
    write_nonblocking (stream, op->buffer, op->count_requested, &error);
2422
2423
0
  if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
2424
0
    {
2425
0
      GSource *source;
2426
2427
0
      g_error_free (error);
2428
2429
0
      source = g_pollable_output_stream_create_source (stream,
2430
0
                                                       g_task_get_cancellable (task));
2431
0
      g_task_attach_source (task, source,
2432
0
                            (GSourceFunc) write_async_pollable_ready);
2433
0
      g_source_unref (source);
2434
0
      return;
2435
0
    }
2436
2437
0
  if (count_written == -1)
2438
0
    g_task_return_error (task, error);
2439
0
  else
2440
0
    g_task_return_int (task, count_written);
2441
0
}
2442
2443
static void
2444
g_output_stream_real_write_async (GOutputStream       *stream,
2445
                                  const void          *buffer,
2446
                                  gsize                count,
2447
                                  int                  io_priority,
2448
                                  GCancellable        *cancellable,
2449
                                  GAsyncReadyCallback  callback,
2450
                                  gpointer             user_data)
2451
0
{
2452
0
  GTask *task;
2453
0
  WriteData *op;
2454
2455
0
  op = g_slice_new0 (WriteData);
2456
0
  task = g_task_new (stream, cancellable, callback, user_data);
2457
0
  g_task_set_check_cancellable (task, FALSE);
2458
0
  g_task_set_task_data (task, op, (GDestroyNotify) free_write_data);
2459
0
  op->buffer = buffer;
2460
0
  op->count_requested = count;
2461
2462
0
  if (!g_output_stream_async_write_is_via_threads (stream))
2463
0
    write_async_pollable (G_POLLABLE_OUTPUT_STREAM (stream), task);
2464
0
  else
2465
0
    g_task_run_in_thread (task, write_async_thread);
2466
0
  g_object_unref (task);
2467
0
}
2468
2469
static gssize
2470
g_output_stream_real_write_finish (GOutputStream  *stream,
2471
                                   GAsyncResult   *result,
2472
                                   GError        **error)
2473
0
{
2474
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2475
2476
0
  return g_task_propagate_int (G_TASK (result), error);
2477
0
}
2478
2479
typedef struct {
2480
  const GOutputVector *vectors;
2481
  gsize                n_vectors; /* (unowned) */
2482
  gsize                bytes_written;
2483
} WritevData;
2484
2485
static void
2486
free_writev_data (WritevData *op)
2487
0
{
2488
0
  g_slice_free (WritevData, op);
2489
0
}
2490
2491
static void
2492
writev_async_thread (GTask        *task,
2493
                     gpointer      source_object,
2494
                     gpointer      task_data,
2495
                     GCancellable *cancellable)
2496
0
{
2497
0
  GOutputStream *stream = source_object;
2498
0
  WritevData *op = task_data;
2499
0
  GOutputStreamClass *class;
2500
0
  GError *error = NULL;
2501
0
  gboolean res;
2502
2503
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
2504
0
  res = class->writev_fn (stream, op->vectors, op->n_vectors,
2505
0
                          &op->bytes_written, cancellable, &error);
2506
2507
0
  g_warn_if_fail (res || op->bytes_written == 0);
2508
0
  g_warn_if_fail (res || error != NULL);
2509
2510
0
  if (!res)
2511
0
    g_task_return_error (task, g_steal_pointer (&error));
2512
0
  else
2513
0
    g_task_return_boolean (task, TRUE);
2514
0
}
2515
2516
static void writev_async_pollable (GPollableOutputStream *stream,
2517
                                   GTask                 *task);
2518
2519
static gboolean
2520
writev_async_pollable_ready (GPollableOutputStream *stream,
2521
           gpointer               user_data)
2522
0
{
2523
0
  GTask *task = user_data;
2524
2525
0
  writev_async_pollable (stream, task);
2526
0
  return G_SOURCE_REMOVE;
2527
0
}
2528
2529
static void
2530
writev_async_pollable (GPollableOutputStream *stream,
2531
                       GTask                 *task)
2532
0
{
2533
0
  GError *error = NULL;
2534
0
  WritevData *op = g_task_get_task_data (task);
2535
0
  GPollableReturn res;
2536
0
  gsize bytes_written = 0;
2537
2538
0
  if (g_task_return_error_if_cancelled (task))
2539
0
    return;
2540
2541
0
  res = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
2542
0
    writev_nonblocking (stream, op->vectors, op->n_vectors, &bytes_written, &error);
2543
2544
0
  switch (res)
2545
0
    {
2546
0
    case G_POLLABLE_RETURN_WOULD_BLOCK:
2547
0
        {
2548
0
          GSource *source;
2549
2550
0
          g_warn_if_fail (error == NULL);
2551
0
          g_warn_if_fail (bytes_written == 0);
2552
2553
0
          source = g_pollable_output_stream_create_source (stream,
2554
0
                                                           g_task_get_cancellable (task));
2555
0
          g_task_attach_source (task, source,
2556
0
                                (GSourceFunc) writev_async_pollable_ready);
2557
0
          g_source_unref (source);
2558
0
        }
2559
0
        break;
2560
0
      case G_POLLABLE_RETURN_OK:
2561
0
        g_warn_if_fail (error == NULL);
2562
0
        op->bytes_written = bytes_written;
2563
0
        g_task_return_boolean (task, TRUE);
2564
0
        break;
2565
0
      case G_POLLABLE_RETURN_FAILED:
2566
0
        g_warn_if_fail (bytes_written == 0);
2567
0
        g_warn_if_fail (error != NULL);
2568
0
        g_task_return_error (task, g_steal_pointer (&error));
2569
0
        break;
2570
0
      default:
2571
0
        g_assert_not_reached ();
2572
0
    }
2573
0
}
2574
2575
static void
2576
g_output_stream_real_writev_async (GOutputStream        *stream,
2577
                                   const GOutputVector  *vectors,
2578
                                   gsize                 n_vectors,
2579
                                   int                   io_priority,
2580
                                   GCancellable         *cancellable,
2581
                                   GAsyncReadyCallback   callback,
2582
                                   gpointer              user_data)
2583
0
{
2584
0
  GTask *task;
2585
0
  WritevData *op;
2586
0
  GError *error = NULL;
2587
2588
0
  op = g_slice_new0 (WritevData);
2589
0
  task = g_task_new (stream, cancellable, callback, user_data);
2590
0
  op->vectors = vectors;
2591
0
  op->n_vectors = n_vectors;
2592
2593
0
  g_task_set_check_cancellable (task, FALSE);
2594
0
  g_task_set_source_tag (task, g_output_stream_writev_async);
2595
0
  g_task_set_priority (task, io_priority);
2596
0
  g_task_set_task_data (task, op, (GDestroyNotify) free_writev_data);
2597
2598
0
  if (n_vectors == 0)
2599
0
    {
2600
0
      g_task_return_boolean (task, TRUE);
2601
0
      g_object_unref (task);
2602
0
      return;
2603
0
    }
2604
2605
0
  if (!g_output_stream_set_pending (stream, &error))
2606
0
    {
2607
0
      g_task_return_error (task, g_steal_pointer (&error));
2608
0
      g_object_unref (task);
2609
0
      return;
2610
0
    }
2611
2612
0
  if (!g_output_stream_async_writev_is_via_threads (stream))
2613
0
    writev_async_pollable (G_POLLABLE_OUTPUT_STREAM (stream), task);
2614
0
  else
2615
0
    g_task_run_in_thread (task, writev_async_thread);
2616
2617
0
  g_object_unref (task);
2618
0
}
2619
2620
static gboolean
2621
g_output_stream_real_writev_finish (GOutputStream   *stream,
2622
                                    GAsyncResult    *result,
2623
                                    gsize           *bytes_written,
2624
                                    GError         **error)
2625
0
{
2626
0
  GTask *task;
2627
2628
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2629
0
  g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_writev_async), FALSE);
2630
2631
0
  g_output_stream_clear_pending (stream);
2632
2633
0
  task = G_TASK (result);
2634
2635
0
  if (bytes_written)
2636
0
    {
2637
0
      WritevData *op = g_task_get_task_data (task);
2638
2639
0
      *bytes_written = op->bytes_written;
2640
0
    }
2641
2642
0
  return g_task_propagate_boolean (task, error);
2643
0
}
2644
2645
typedef struct {
2646
  GInputStream *source;
2647
  GOutputStreamSpliceFlags flags;
2648
  guint istream_closed : 1;
2649
  guint ostream_closed : 1;
2650
  gssize n_read;
2651
  gssize n_written;
2652
  gsize bytes_copied;
2653
  GError *error;
2654
  guint8 *buffer;
2655
} SpliceData;
2656
2657
static void
2658
free_splice_data (SpliceData *op)
2659
0
{
2660
0
  g_clear_pointer (&op->buffer, g_free);
2661
0
  g_object_unref (op->source);
2662
0
  g_clear_error (&op->error);
2663
0
  g_free (op);
2664
0
}
2665
2666
static void
2667
real_splice_async_complete_cb (GTask *task)
2668
0
{
2669
0
  SpliceData *op = g_task_get_task_data (task);
2670
2671
0
  if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE &&
2672
0
      !op->istream_closed)
2673
0
    return;
2674
2675
0
  if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET &&
2676
0
      !op->ostream_closed)
2677
0
    return;
2678
2679
0
  if (op->error != NULL)
2680
0
    {
2681
0
      g_task_return_error (task, op->error);
2682
0
      op->error = NULL;
2683
0
    }
2684
0
  else
2685
0
    {
2686
0
      g_task_return_int (task, op->bytes_copied);
2687
0
    }
2688
2689
0
  g_object_unref (task);
2690
0
}
2691
2692
static void
2693
real_splice_async_close_input_cb (GObject      *source,
2694
                                  GAsyncResult *res,
2695
                                  gpointer      user_data)
2696
0
{
2697
0
  GTask *task = user_data;
2698
0
  SpliceData *op = g_task_get_task_data (task);
2699
2700
0
  g_input_stream_close_finish (G_INPUT_STREAM (source), res, NULL);
2701
0
  op->istream_closed = TRUE;
2702
2703
0
  real_splice_async_complete_cb (task);
2704
0
}
2705
2706
static void
2707
real_splice_async_close_output_cb (GObject      *source,
2708
                                   GAsyncResult *res,
2709
                                   gpointer      user_data)
2710
0
{
2711
0
  GTask *task = G_TASK (user_data);
2712
0
  SpliceData *op = g_task_get_task_data (task);
2713
0
  GError **error = (op->error == NULL) ? &op->error : NULL;
2714
2715
0
  g_output_stream_internal_close_finish (G_OUTPUT_STREAM (source), res, error);
2716
0
  op->ostream_closed = TRUE;
2717
2718
0
  real_splice_async_complete_cb (task);
2719
0
}
2720
2721
static void
2722
real_splice_async_complete (GTask *task)
2723
0
{
2724
0
  SpliceData *op = g_task_get_task_data (task);
2725
0
  gboolean done = TRUE;
2726
2727
0
  if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
2728
0
    {
2729
0
      done = FALSE;
2730
0
      g_input_stream_close_async (op->source, g_task_get_priority (task),
2731
0
                                  g_task_get_cancellable (task),
2732
0
                                  real_splice_async_close_input_cb, task);
2733
0
    }
2734
2735
0
  if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
2736
0
    {
2737
0
      done = FALSE;
2738
0
      g_output_stream_internal_close_async (g_task_get_source_object (task),
2739
0
                                            g_task_get_priority (task),
2740
0
                                            g_task_get_cancellable (task),
2741
0
                                            real_splice_async_close_output_cb,
2742
0
                                            task);
2743
0
    }
2744
2745
0
  if (done)
2746
0
    real_splice_async_complete_cb (task);
2747
0
}
2748
2749
static void real_splice_async_read_cb (GObject      *source,
2750
                                       GAsyncResult *res,
2751
                                       gpointer      user_data);
2752
2753
static void
2754
real_splice_async_write_cb (GObject      *source,
2755
                            GAsyncResult *res,
2756
                            gpointer      user_data)
2757
0
{
2758
0
  GOutputStreamClass *class;
2759
0
  GTask *task = G_TASK (user_data);
2760
0
  SpliceData *op = g_task_get_task_data (task);
2761
0
  gssize ret;
2762
2763
0
  class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
2764
2765
0
  ret = class->write_finish (G_OUTPUT_STREAM (source), res, &op->error);
2766
2767
0
  if (ret == -1)
2768
0
    {
2769
0
      real_splice_async_complete (task);
2770
0
      return;
2771
0
    }
2772
2773
0
  op->n_written += ret;
2774
0
  op->bytes_copied += ret;
2775
0
  if (op->bytes_copied > G_MAXSSIZE)
2776
0
    op->bytes_copied = G_MAXSSIZE;
2777
2778
0
  if (op->n_written < op->n_read)
2779
0
    {
2780
0
      class->write_async (g_task_get_source_object (task),
2781
0
                          op->buffer + op->n_written,
2782
0
                          op->n_read - op->n_written,
2783
0
                          g_task_get_priority (task),
2784
0
                          g_task_get_cancellable (task),
2785
0
                          real_splice_async_write_cb, task);
2786
0
      return;
2787
0
    }
2788
2789
0
  g_input_stream_read_async (op->source, op->buffer, 8192,
2790
0
                             g_task_get_priority (task),
2791
0
                             g_task_get_cancellable (task),
2792
0
                             real_splice_async_read_cb, task);
2793
0
}
2794
2795
static void
2796
real_splice_async_read_cb (GObject      *source,
2797
                           GAsyncResult *res,
2798
                           gpointer      user_data)
2799
0
{
2800
0
  GOutputStreamClass *class;
2801
0
  GTask *task = G_TASK (user_data);
2802
0
  SpliceData *op = g_task_get_task_data (task);
2803
0
  gssize ret;
2804
2805
0
  class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
2806
2807
0
  ret = g_input_stream_read_finish (op->source, res, &op->error);
2808
0
  if (ret == -1 || ret == 0)
2809
0
    {
2810
0
      real_splice_async_complete (task);
2811
0
      return;
2812
0
    }
2813
2814
0
  op->n_read = ret;
2815
0
  op->n_written = 0;
2816
2817
0
  class->write_async (g_task_get_source_object (task), op->buffer,
2818
0
                      op->n_read, g_task_get_priority (task),
2819
0
                      g_task_get_cancellable (task),
2820
0
                      real_splice_async_write_cb, task);
2821
0
}
2822
2823
static void
2824
splice_async_thread (GTask        *task,
2825
                     gpointer      source_object,
2826
                     gpointer      task_data,
2827
                     GCancellable *cancellable)
2828
0
{
2829
0
  GOutputStream *stream = source_object;
2830
0
  SpliceData *op = task_data;
2831
0
  GOutputStreamClass *class;
2832
0
  GError *error = NULL;
2833
0
  gssize bytes_copied;
2834
2835
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
2836
  
2837
0
  bytes_copied = class->splice (stream,
2838
0
                                op->source,
2839
0
                                op->flags,
2840
0
                                cancellable,
2841
0
                                &error);
2842
0
  if (bytes_copied == -1)
2843
0
    g_task_return_error (task, error);
2844
0
  else
2845
0
    g_task_return_int (task, bytes_copied);
2846
0
}
2847
2848
static void
2849
g_output_stream_real_splice_async (GOutputStream             *stream,
2850
                                   GInputStream              *source,
2851
                                   GOutputStreamSpliceFlags   flags,
2852
                                   int                        io_priority,
2853
                                   GCancellable              *cancellable,
2854
                                   GAsyncReadyCallback        callback,
2855
                                   gpointer                   user_data)
2856
0
{
2857
0
  GTask *task;
2858
0
  SpliceData *op;
2859
2860
0
  op = g_new0 (SpliceData, 1);
2861
0
  task = g_task_new (stream, cancellable, callback, user_data);
2862
0
  g_task_set_task_data (task, op, (GDestroyNotify)free_splice_data);
2863
0
  op->flags = flags;
2864
0
  op->source = g_object_ref (source);
2865
2866
0
  if (g_input_stream_async_read_is_via_threads (source) &&
2867
0
      g_output_stream_async_write_is_via_threads (stream))
2868
0
    {
2869
0
      g_task_run_in_thread (task, splice_async_thread);
2870
0
      g_object_unref (task);
2871
0
    }
2872
0
  else
2873
0
    {
2874
0
      op->buffer = g_malloc (8192);
2875
0
      g_input_stream_read_async (op->source, op->buffer, 8192,
2876
0
                                 g_task_get_priority (task),
2877
0
                                 g_task_get_cancellable (task),
2878
0
                                 real_splice_async_read_cb, task);
2879
0
    }
2880
0
}
2881
2882
static gssize
2883
g_output_stream_real_splice_finish (GOutputStream  *stream,
2884
                                    GAsyncResult   *result,
2885
                                    GError        **error)
2886
0
{
2887
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2888
2889
0
  return g_task_propagate_int (G_TASK (result), error);
2890
0
}
2891
2892
2893
static void
2894
flush_async_thread (GTask        *task,
2895
                    gpointer      source_object,
2896
                    gpointer      task_data,
2897
                    GCancellable *cancellable)
2898
0
{
2899
0
  GOutputStream *stream = source_object;
2900
0
  GOutputStreamClass *class;
2901
0
  gboolean result;
2902
0
  GError *error = NULL;
2903
2904
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
2905
0
  result = TRUE;
2906
0
  if (class->flush)
2907
0
    result = class->flush (stream, cancellable, &error);
2908
2909
0
  if (result)
2910
0
    g_task_return_boolean (task, TRUE);
2911
0
  else
2912
0
    g_task_return_error (task, error);
2913
0
}
2914
2915
static void
2916
g_output_stream_real_flush_async (GOutputStream       *stream,
2917
                                  int                  io_priority,
2918
                                  GCancellable        *cancellable,
2919
                                  GAsyncReadyCallback  callback,
2920
                                  gpointer             user_data)
2921
0
{
2922
0
  GTask *task;
2923
2924
0
  task = g_task_new (stream, cancellable, callback, user_data);
2925
0
  g_task_set_priority (task, io_priority);
2926
0
  g_task_run_in_thread (task, flush_async_thread);
2927
0
  g_object_unref (task);
2928
0
}
2929
2930
static gboolean
2931
g_output_stream_real_flush_finish (GOutputStream  *stream,
2932
                                   GAsyncResult   *result,
2933
                                   GError        **error)
2934
0
{
2935
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2936
2937
0
  return g_task_propagate_boolean (G_TASK (result), error);
2938
0
}
2939
2940
static void
2941
close_async_thread (GTask        *task,
2942
                    gpointer      source_object,
2943
                    gpointer      task_data,
2944
                    GCancellable *cancellable)
2945
0
{
2946
0
  GOutputStream *stream = source_object;
2947
0
  GOutputStreamClass *class;
2948
0
  GError *error = NULL;
2949
0
  gboolean result = TRUE;
2950
2951
0
  class = G_OUTPUT_STREAM_GET_CLASS (stream);
2952
2953
  /* Do a flush here if there is a flush function, and we did not have to do
2954
   * an async flush before (see g_output_stream_close_async)
2955
   */
2956
0
  if (class->flush != NULL &&
2957
0
      (class->flush_async == NULL ||
2958
0
       class->flush_async == g_output_stream_real_flush_async))
2959
0
    {
2960
0
      result = class->flush (stream, cancellable, &error);
2961
0
    }
2962
2963
  /* Auto handling of cancellation disabled, and ignore
2964
     cancellation, since we want to close things anyway, although
2965
     possibly in a quick-n-dirty way. At least we never want to leak
2966
     open handles */
2967
2968
0
  if (class->close_fn)
2969
0
    {
2970
      /* Make sure to close, even if the flush failed (see sync close) */
2971
0
      if (!result)
2972
0
        class->close_fn (stream, cancellable, NULL);
2973
0
      else
2974
0
        result = class->close_fn (stream, cancellable, &error);
2975
0
    }
2976
2977
0
  if (result)
2978
0
    g_task_return_boolean (task, TRUE);
2979
0
  else
2980
0
    g_task_return_error (task, error);
2981
0
}
2982
2983
static void
2984
g_output_stream_real_close_async (GOutputStream       *stream,
2985
                                  int                  io_priority,
2986
                                  GCancellable        *cancellable,
2987
                                  GAsyncReadyCallback  callback,
2988
                                  gpointer             user_data)
2989
0
{
2990
0
  GTask *task;
2991
2992
0
  task = g_task_new (stream, cancellable, callback, user_data);
2993
0
  g_task_set_priority (task, io_priority);
2994
0
  g_task_run_in_thread (task, close_async_thread);
2995
0
  g_object_unref (task);
2996
0
}
2997
2998
static gboolean
2999
g_output_stream_real_close_finish (GOutputStream  *stream,
3000
                                   GAsyncResult   *result,
3001
                                   GError        **error)
3002
0
{
3003
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
3004
3005
0
  return g_task_propagate_boolean (G_TASK (result), error);
3006
0
}