gbufferedoutputstream.c 24.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Christian Kellner <gicmo@gnome.org> 
 */

23
#include "config.h"
24 25
#include "gbufferedoutputstream.h"
#include "goutputstream.h"
26
#include "gseekable.h"
27
#include "gtask.h"
28
#include "string.h"
29
#include "gioerror.h"
30 31
#include "glibintl.h"

32 33 34
/**
 * SECTION:gbufferedoutputstream
 * @short_description: Buffered Output Stream
Matthias Clasen's avatar
Matthias Clasen committed
35
 * @include: gio/gio.h
36
 * @see_also: #GFilterOutputStream, #GOutputStream
37 38 39 40 41 42
 * 
 * Buffered output stream implements #GFilterOutputStream and provides 
 * for buffered writes. 
 * 
 * By default, #GBufferedOutputStream's buffer size is set at 4 kilobytes.
 * 
Matthias Clasen's avatar
Matthias Clasen committed
43 44 45
 * To create a buffered output stream, use g_buffered_output_stream_new(), 
 * or g_buffered_output_stream_new_sized() to specify the buffer's size 
 * at construction.
46 47 48
 * 
 * To get the size of a buffer within a buffered input stream, use 
 * g_buffered_output_stream_get_buffer_size(). To change the size of a 
Matthias Clasen's avatar
Matthias Clasen committed
49 50 51
 * buffered output stream's buffer, use 
 * g_buffered_output_stream_set_buffer_size(). Note that the buffer's 
 * size cannot be reduced below the size of the data within the buffer.
52 53
 **/

54 55 56 57 58 59 60 61 62 63 64
#define DEFAULT_BUFFER_SIZE 4096

struct _GBufferedOutputStreamPrivate {
  guint8 *buffer; 
  gsize   len;
  goffset pos;
  gboolean auto_grow;
};

enum {
  PROP_0,
Matthias Clasen's avatar
Matthias Clasen committed
65 66
  PROP_BUFSIZE,
  PROP_AUTO_GROW
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
};

static void     g_buffered_output_stream_set_property (GObject      *object,
                                                       guint         prop_id,
                                                       const GValue *value,
                                                       GParamSpec   *pspec);

static void     g_buffered_output_stream_get_property (GObject    *object,
                                                       guint       prop_id,
                                                       GValue     *value,
                                                       GParamSpec *pspec);
static void     g_buffered_output_stream_finalize     (GObject *object);


static gssize   g_buffered_output_stream_write        (GOutputStream *stream,
                                                       const void    *buffer,
                                                       gsize          count,
                                                       GCancellable  *cancellable,
                                                       GError       **error);
static gboolean g_buffered_output_stream_flush        (GOutputStream    *stream,
                                                       GCancellable  *cancellable,
                                                       GError          **error);
static gboolean g_buffered_output_stream_close        (GOutputStream  *stream,
                                                       GCancellable   *cancellable,
                                                       GError        **error);

static void     g_buffered_output_stream_flush_async  (GOutputStream        *stream,
                                                       int                   io_priority,
                                                       GCancellable         *cancellable,
                                                       GAsyncReadyCallback   callback,
                                                       gpointer              data);
static gboolean g_buffered_output_stream_flush_finish (GOutputStream        *stream,
                                                       GAsyncResult         *result,
                                                       GError              **error);
static void     g_buffered_output_stream_close_async  (GOutputStream        *stream,
                                                       int                   io_priority,
                                                       GCancellable         *cancellable,
                                                       GAsyncReadyCallback   callback,
                                                       gpointer              data);
static gboolean g_buffered_output_stream_close_finish (GOutputStream        *stream,
                                                       GAsyncResult         *result,
                                                       GError              **error);

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
static void     g_buffered_output_stream_seekable_iface_init (GSeekableIface  *iface);
static goffset  g_buffered_output_stream_tell                (GSeekable       *seekable);
static gboolean g_buffered_output_stream_can_seek            (GSeekable       *seekable);
static gboolean g_buffered_output_stream_seek                (GSeekable       *seekable,
							      goffset          offset,
							      GSeekType        type,
							      GCancellable    *cancellable,
							      GError         **error);
static gboolean g_buffered_output_stream_can_truncate        (GSeekable       *seekable);
static gboolean g_buffered_output_stream_truncate            (GSeekable       *seekable,
							      goffset          offset,
							      GCancellable    *cancellable,
							      GError         **error);

G_DEFINE_TYPE_WITH_CODE (GBufferedOutputStream,
			 g_buffered_output_stream,
			 G_TYPE_FILTER_OUTPUT_STREAM,
127
                         G_ADD_PRIVATE (GBufferedOutputStream)
128 129
			 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
						g_buffered_output_stream_seekable_iface_init))
130 131 132 133 134 135 136 137 138 139 140 141 142 143


static void
g_buffered_output_stream_class_init (GBufferedOutputStreamClass *klass)
{
  GObjectClass *object_class;
  GOutputStreamClass *ostream_class;

  object_class = G_OBJECT_CLASS (klass);
  object_class->get_property = g_buffered_output_stream_get_property;
  object_class->set_property = g_buffered_output_stream_set_property;
  object_class->finalize     = g_buffered_output_stream_finalize;

  ostream_class = G_OUTPUT_STREAM_CLASS (klass);
144
  ostream_class->write_fn = g_buffered_output_stream_write;
145
  ostream_class->flush = g_buffered_output_stream_flush;
146
  ostream_class->close_fn = g_buffered_output_stream_close;
147 148 149 150 151 152 153 154 155 156 157 158 159
  ostream_class->flush_async  = g_buffered_output_stream_flush_async;
  ostream_class->flush_finish = g_buffered_output_stream_flush_finish;
  ostream_class->close_async  = g_buffered_output_stream_close_async;
  ostream_class->close_finish = g_buffered_output_stream_close_finish;

  g_object_class_install_property (object_class,
                                   PROP_BUFSIZE,
                                   g_param_spec_uint ("buffer-size",
                                                      P_("Buffer Size"),
                                                      P_("The size of the backend buffer"),
                                                      1,
                                                      G_MAXUINT,
                                                      DEFAULT_BUFFER_SIZE,
Matthias Clasen's avatar
Matthias Clasen committed
160
                                                      G_PARAM_READWRITE|G_PARAM_CONSTRUCT|
161 162
                                                      G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));

Matthias Clasen's avatar
Matthias Clasen committed
163 164 165 166 167 168 169 170 171
  g_object_class_install_property (object_class,
                                   PROP_AUTO_GROW,
                                   g_param_spec_boolean ("auto-grow",
                                                         P_("Auto-grow"),
                                                         P_("Whether the buffer should automatically grow"),
                                                         FALSE,
                                                         G_PARAM_READWRITE|
                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));

172 173 174 175 176 177
}

/**
 * g_buffered_output_stream_get_buffer_size:
 * @stream: a #GBufferedOutputStream.
 * 
178 179
 * Gets the size of the buffer in the @stream.
 * 
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
 * Returns: the current size of the buffer.
 **/
gsize
g_buffered_output_stream_get_buffer_size (GBufferedOutputStream *stream)
{
  g_return_val_if_fail (G_IS_BUFFERED_OUTPUT_STREAM (stream), -1);

  return stream->priv->len;
}

/**
 * g_buffered_output_stream_set_buffer_size:
 * @stream: a #GBufferedOutputStream.
 * @size: a #gsize.
 *
 * Sets the size of the internal buffer to @size.
 **/    
void
g_buffered_output_stream_set_buffer_size (GBufferedOutputStream *stream,
Matthias Clasen's avatar
Matthias Clasen committed
199
                                          gsize                  size)
200 201 202 203 204 205 206 207
{
  GBufferedOutputStreamPrivate *priv;
  guint8 *buffer;
  
  g_return_if_fail (G_IS_BUFFERED_OUTPUT_STREAM (stream));

  priv = stream->priv;
  
Matthias Clasen's avatar
Matthias Clasen committed
208 209 210
  if (size == priv->len)
    return;

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  if (priv->buffer)
    {
      size = MAX (size, priv->pos);

      buffer = g_malloc (size);
      memcpy (buffer, priv->buffer, priv->pos);
      g_free (priv->buffer);
      priv->buffer = buffer;
      priv->len = size;
      /* Keep old pos */
    }
  else
    {
      priv->buffer = g_malloc (size);
      priv->len = size;
      priv->pos = 0;
    }
Matthias Clasen's avatar
Matthias Clasen committed
228 229

  g_object_notify (G_OBJECT (stream), "buffer-size");
230 231 232 233 234 235
}

/**
 * g_buffered_output_stream_get_auto_grow:
 * @stream: a #GBufferedOutputStream.
 * 
236 237
 * Checks if the buffer automatically grows as data is added.
 * 
238 239 240 241 242 243 244 245 246 247 248 249 250 251
 * Returns: %TRUE if the @stream's buffer automatically grows,
 * %FALSE otherwise.
 **/  
gboolean
g_buffered_output_stream_get_auto_grow (GBufferedOutputStream *stream)
{
  g_return_val_if_fail (G_IS_BUFFERED_OUTPUT_STREAM (stream), FALSE);

  return stream->priv->auto_grow;
}

/**
 * g_buffered_output_stream_set_auto_grow:
 * @stream: a #GBufferedOutputStream.
252
 * @auto_grow: a #gboolean.
253 254
 *
 * Sets whether or not the @stream's buffer should automatically grow.
255 256 257
 * If @auto_grow is true, then each write will just make the buffer
 * larger, and you must manually flush the buffer to actually write out
 * the data to the underlying stream.
258 259 260
 **/
void
g_buffered_output_stream_set_auto_grow (GBufferedOutputStream *stream,
Matthias Clasen's avatar
Matthias Clasen committed
261
                                        gboolean               auto_grow)
262
{
Matthias Clasen's avatar
Matthias Clasen committed
263
  GBufferedOutputStreamPrivate *priv;
264
  g_return_if_fail (G_IS_BUFFERED_OUTPUT_STREAM (stream));
Matthias Clasen's avatar
Matthias Clasen committed
265 266 267 268 269 270 271
  priv = stream->priv;
  auto_grow = auto_grow != FALSE;
  if (priv->auto_grow != auto_grow)
    {
      priv->auto_grow = auto_grow;
      g_object_notify (G_OBJECT (stream), "auto-grow");
    }
272 273 274
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
275 276 277 278
g_buffered_output_stream_set_property (GObject      *object,
                                       guint         prop_id,
                                       const GValue *value,
                                       GParamSpec   *pspec)
279
{
Matthias Clasen's avatar
Matthias Clasen committed
280
  GBufferedOutputStream *stream;
281

Matthias Clasen's avatar
Matthias Clasen committed
282
  stream = G_BUFFERED_OUTPUT_STREAM (object);
283 284 285 286

  switch (prop_id) 
    {
    case PROP_BUFSIZE:
Matthias Clasen's avatar
Matthias Clasen committed
287
      g_buffered_output_stream_set_buffer_size (stream, g_value_get_uint (value));
288 289
      break;    

Matthias Clasen's avatar
Matthias Clasen committed
290 291 292 293
    case PROP_AUTO_GROW:
      g_buffered_output_stream_set_auto_grow (stream, g_value_get_boolean (value));
      break;

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }

}

static void
g_buffered_output_stream_get_property (GObject    *object,
                                       guint       prop_id,
                                       GValue     *value,
                                       GParamSpec *pspec)
{
  GBufferedOutputStream *buffered_stream;
  GBufferedOutputStreamPrivate *priv;

  buffered_stream = G_BUFFERED_OUTPUT_STREAM (object);
  priv = buffered_stream->priv;

  switch (prop_id)
    {
    case PROP_BUFSIZE:
      g_value_set_uint (value, priv->len);
      break;

Matthias Clasen's avatar
Matthias Clasen committed
319 320 321 322
    case PROP_AUTO_GROW:
      g_value_set_boolean (value, priv->auto_grow);
      break;

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }

}

static void
g_buffered_output_stream_finalize (GObject *object)
{
  GBufferedOutputStream *stream;
  GBufferedOutputStreamPrivate *priv;

  stream = G_BUFFERED_OUTPUT_STREAM (object);
  priv = stream->priv;

  g_free (priv->buffer);

341
  G_OBJECT_CLASS (g_buffered_output_stream_parent_class)->finalize (object);
342 343 344 345 346
}

static void
g_buffered_output_stream_init (GBufferedOutputStream *stream)
{
347
  stream->priv = g_buffered_output_stream_get_private (stream);
348 349
}

350 351 352 353 354 355 356 357 358 359
static void
g_buffered_output_stream_seekable_iface_init (GSeekableIface *iface)
{
  iface->tell         = g_buffered_output_stream_tell;
  iface->can_seek     = g_buffered_output_stream_can_seek;
  iface->seek         = g_buffered_output_stream_seek;
  iface->can_truncate = g_buffered_output_stream_can_truncate;
  iface->truncate_fn  = g_buffered_output_stream_truncate;
}

360 361 362 363
/**
 * g_buffered_output_stream_new:
 * @base_stream: a #GOutputStream.
 * 
364 365
 * Creates a new buffered output stream for a base stream.
 * 
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
 * Returns: a #GOutputStream for the given @base_stream.
 **/  
GOutputStream *
g_buffered_output_stream_new (GOutputStream *base_stream)
{
  GOutputStream *stream;

  g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);

  stream = g_object_new (G_TYPE_BUFFERED_OUTPUT_STREAM,
                         "base-stream", base_stream,
                         NULL);

  return stream;
}

/**
 * g_buffered_output_stream_new_sized:
 * @base_stream: a #GOutputStream.
 * @size: a #gsize.
 * 
387 388
 * Creates a new buffered output stream with a given buffer size.
 * 
389 390 391 392
 * Returns: a #GOutputStream with an internal buffer set to @size.
 **/  
GOutputStream *
g_buffered_output_stream_new_sized (GOutputStream *base_stream,
393
                                    gsize          size)
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
{
  GOutputStream *stream;

  g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);

  stream = g_object_new (G_TYPE_BUFFERED_OUTPUT_STREAM,
                         "base-stream", base_stream,
                         "buffer-size", size,
                         NULL);

  return stream;
}

static gboolean
flush_buffer (GBufferedOutputStream  *stream,
              GCancellable           *cancellable,
              GError                 **error)
{
  GBufferedOutputStreamPrivate *priv;
  GOutputStream                *base_stream;
  gboolean                      res;
  gsize                         bytes_written;
  gsize                         count;

  priv = stream->priv;
  bytes_written = 0;
  base_stream = G_FILTER_OUTPUT_STREAM (stream)->base_stream;

  g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), FALSE);

  res = g_output_stream_write_all (base_stream,
                                   priv->buffer,
                                   priv->pos,
                                   &bytes_written,
                                   cancellable,
                                   error);

  count = priv->pos - bytes_written;

  if (count > 0)
    g_memmove (priv->buffer, priv->buffer + bytes_written, count);
  
  priv->pos -= bytes_written;

  return res;
}

static gssize
g_buffered_output_stream_write  (GOutputStream *stream,
                                 const void    *buffer,
                                 gsize          count,
                                 GCancellable  *cancellable,
                                 GError       **error)
{
  GBufferedOutputStream        *bstream;
  GBufferedOutputStreamPrivate *priv;
  gboolean res;
  gsize    n;
  gsize new_size;

  bstream = G_BUFFERED_OUTPUT_STREAM (stream);
  priv = bstream->priv;

  n = priv->len - priv->pos;

  if (priv->auto_grow && n < count)
    {
      new_size = MAX (priv->len * 2, priv->len + count);
      g_buffered_output_stream_set_buffer_size (bstream, new_size);
    }
  else if (n == 0)
    {
      res = flush_buffer (bstream, cancellable, error);
      
      if (res == FALSE)
	return -1;
    }

  n = priv->len - priv->pos;
  
  count = MIN (count, n);
  memcpy (priv->buffer + priv->pos, buffer, count);
  priv->pos += count;

  return count;
}

static gboolean
g_buffered_output_stream_flush (GOutputStream  *stream,
                                GCancellable   *cancellable,
                                GError        **error)
{
  GBufferedOutputStream *bstream;
  GOutputStream                *base_stream;
  gboolean res;

  bstream = G_BUFFERED_OUTPUT_STREAM (stream);
  base_stream = G_FILTER_OUTPUT_STREAM (stream)->base_stream;

  res = flush_buffer (bstream, cancellable, error);

Matthias Clasen's avatar
Matthias Clasen committed
495
  if (res == FALSE)
496 497
    return FALSE;

Matthias Clasen's avatar
Matthias Clasen committed
498 499
  res = g_output_stream_flush (base_stream, cancellable, error);

500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
  return res;
}

static gboolean
g_buffered_output_stream_close (GOutputStream  *stream,
                                GCancellable   *cancellable,
                                GError        **error)
{
  GBufferedOutputStream        *bstream;
  GOutputStream                *base_stream;
  gboolean                      res;

  bstream = G_BUFFERED_OUTPUT_STREAM (stream);
  base_stream = G_FILTER_OUTPUT_STREAM (bstream)->base_stream;
  res = flush_buffer (bstream, cancellable, error);

516 517 518 519
  if (g_filter_output_stream_get_close_base_stream (G_FILTER_OUTPUT_STREAM (stream)))
    {
      /* report the first error but still close the stream */
      if (res)
520
        res = g_output_stream_close (base_stream, cancellable, error);
521
      else
522
        g_output_stream_close (base_stream, cancellable, NULL);
523
    }
524 525 526 527

  return res;
}

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
static goffset
g_buffered_output_stream_tell (GSeekable *seekable)
{
  GBufferedOutputStream        *bstream;
  GBufferedOutputStreamPrivate *priv;
  GOutputStream *base_stream;
  GSeekable    *base_stream_seekable;
  goffset base_offset;
  
  bstream = G_BUFFERED_OUTPUT_STREAM (seekable);
  priv = bstream->priv;

  base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
  if (!G_IS_SEEKABLE (base_stream))
    return 0;

  base_stream_seekable = G_SEEKABLE (base_stream);
  
  base_offset = g_seekable_tell (base_stream_seekable);
  return base_offset + priv->pos;
}

static gboolean
g_buffered_output_stream_can_seek (GSeekable *seekable)
{
  GOutputStream *base_stream;
  
  base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
  return G_IS_SEEKABLE (base_stream) && g_seekable_can_seek (G_SEEKABLE (base_stream));
}

static gboolean
g_buffered_output_stream_seek (GSeekable     *seekable,
			       goffset        offset,
			       GSeekType      type,
			       GCancellable  *cancellable,
			       GError       **error)
{
  GBufferedOutputStream *bstream;
  GOutputStream *base_stream;
  GSeekable *base_stream_seekable;
  gboolean flushed;

  bstream = G_BUFFERED_OUTPUT_STREAM (seekable);

  base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
  if (!G_IS_SEEKABLE (base_stream))
    {
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
                           _("Seek not supported on base stream"));
      return FALSE;
    }

  base_stream_seekable = G_SEEKABLE (base_stream);
  flushed = flush_buffer (bstream, cancellable, error);
  if (!flushed)
    return FALSE;

  return g_seekable_seek (base_stream_seekable, offset, type, cancellable, error);
}

static gboolean
g_buffered_output_stream_can_truncate (GSeekable *seekable)
{
  GOutputStream *base_stream;
  
  base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
  return G_IS_SEEKABLE (base_stream) && g_seekable_can_truncate (G_SEEKABLE (base_stream));
}

static gboolean
g_buffered_output_stream_truncate (GSeekable     *seekable,
				   goffset        offset,
				   GCancellable  *cancellable,
				   GError       **error)
{
  GBufferedOutputStream        *bstream;
  GOutputStream *base_stream;
  GSeekable *base_stream_seekable;
  gboolean flushed;

  bstream = G_BUFFERED_OUTPUT_STREAM (seekable);
  base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
  if (!G_IS_SEEKABLE (base_stream))
    {
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
                           _("Truncate not supported on base stream"));
      return FALSE;
    }

  base_stream_seekable = G_SEEKABLE (base_stream);

  flushed = flush_buffer (bstream, cancellable, error);
  if (!flushed)
    return FALSE;
  return g_seekable_truncate (base_stream_seekable, offset, cancellable, error);
}

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
/* ************************** */
/* Async stuff implementation */
/* ************************** */

/* TODO: This should be using the base class async ops, not threads */

typedef struct {

  guint flush_stream : 1;
  guint close_stream : 1;

} FlushData;

static void
free_flush_data (gpointer data)
{
  g_slice_free (FlushData, data);
}

/* This function is used by all three (i.e. 
 * _write, _flush, _close) functions since
 * all of them will need to flush the buffer
 * and so closing and writing is just a special
 * case of flushing + some addition stuff */
static void
651 652 653 654
flush_buffer_thread (GTask        *task,
                     gpointer      object,
                     gpointer      task_data,
                     GCancellable *cancellable)
655 656 657 658 659 660 661 662
{
  GBufferedOutputStream *stream;
  GOutputStream *base_stream;
  FlushData     *fdata;
  gboolean       res;
  GError        *error = NULL;

  stream = G_BUFFERED_OUTPUT_STREAM (object);
663
  fdata = task_data;
664 665 666 667 668 669 670
  base_stream = G_FILTER_OUTPUT_STREAM (stream)->base_stream;

  res = flush_buffer (stream, cancellable, &error);

  /* if flushing the buffer didn't work don't even bother
   * to flush the stream but just report that error */
  if (res && fdata->flush_stream)
Matthias Clasen's avatar
Matthias Clasen committed
671
    res = g_output_stream_flush (base_stream, cancellable, &error);
672 673 674 675 676 677 678

  if (fdata->close_stream) 
    {
     
      /* if flushing the buffer or the stream returned 
       * an error report that first error but still try 
       * close the stream */
679 680 681 682 683 684 685
      if (g_filter_output_stream_get_close_base_stream (G_FILTER_OUTPUT_STREAM (stream)))
        {
          if (res == FALSE)
            g_output_stream_close (base_stream, cancellable, NULL);
          else 
            res = g_output_stream_close (base_stream, cancellable, &error);
        }
686 687 688
    }

  if (res == FALSE)
689 690 691
    g_task_return_error (task, error);
  else
    g_task_return_boolean (task, TRUE);
692 693 694 695 696 697 698 699 700
}

static void
g_buffered_output_stream_flush_async (GOutputStream        *stream,
                                      int                   io_priority,
                                      GCancellable         *cancellable,
                                      GAsyncReadyCallback   callback,
                                      gpointer              data)
{
701 702
  GTask *task;
  FlushData *fdata;
703 704 705 706 707

  fdata = g_slice_new (FlushData);
  fdata->flush_stream = TRUE;
  fdata->close_stream = FALSE;

708 709 710
  task = g_task_new (stream, cancellable, callback, data);
  g_task_set_task_data (task, fdata, free_flush_data);
  g_task_set_priority (task, io_priority);
711

712 713
  g_task_run_in_thread (task, flush_buffer_thread);
  g_object_unref (task);
714 715 716 717 718 719 720
}

static gboolean
g_buffered_output_stream_flush_finish (GOutputStream        *stream,
                                       GAsyncResult         *result,
                                       GError              **error)
{
721
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
722

723
  return g_task_propagate_boolean (G_TASK (result), error);
724 725 726 727 728 729 730 731 732
}

static void
g_buffered_output_stream_close_async (GOutputStream        *stream,
                                      int                   io_priority,
                                      GCancellable         *cancellable,
                                      GAsyncReadyCallback   callback,
                                      gpointer              data)
{
733 734
  GTask *task;
  FlushData *fdata;
735 736 737 738

  fdata = g_slice_new (FlushData);
  fdata->close_stream = TRUE;

739 740 741
  task = g_task_new (stream, cancellable, callback, data);
  g_task_set_task_data (task, fdata, free_flush_data);
  g_task_set_priority (task, io_priority);
742

743 744
  g_task_run_in_thread (task, flush_buffer_thread);
  g_object_unref (task);
745 746 747 748 749 750 751
}

static gboolean
g_buffered_output_stream_close_finish (GOutputStream        *stream,
                                       GAsyncResult         *result,
                                       GError              **error)
{
752
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
753

754
  return g_task_propagate_boolean (G_TASK (result), error);
755
}