Add a quick utility to test datetime formatting
[platform/upstream/glib.git] / gio / goutputstream.c
index c1bb8b2..cac5c92 100644 (file)
  * Author: Alexander Larsson <alexl@redhat.com>
  */
 
-#include <config.h>
+#include "config.h"
 #include "goutputstream.h"
+#include "gcancellable.h"
+#include "gasyncresult.h"
 #include "gsimpleasyncresult.h"
+#include "ginputstream.h"
+#include "gioerror.h"
 #include "glibintl.h"
 
-#include "gioalias.h"
 
 /**
  * SECTION:goutputstream
  * @short_description: Base class for implementing streaming output
- * @include: gio.h
+ * @include: gio/gio.h
  *
+ * GOutputStream has functions to write to a stream (g_output_stream_write()),
+ * to close a stream (g_output_stream_close()) and to flush pending writes
+ * (g_output_stream_flush()). 
  *
+ * To copy the content of an input stream to an output stream without 
+ * manually handling the reads and writes, use g_output_stream_splice(). 
+ *
+ * All of these functions have async variants too.
  **/
 
-G_DEFINE_TYPE (GOutputStream, g_output_stream, G_TYPE_OBJECT);
+G_DEFINE_ABSTRACT_TYPE (GOutputStream, g_output_stream, G_TYPE_OBJECT);
 
 struct _GOutputStreamPrivate {
   guint closed : 1;
   guint pending : 1;
-  guint cancelled : 1;
+  guint closing : 1;
   GAsyncReadyCallback outstanding_callback;
 };
 
@@ -89,12 +99,7 @@ static gboolean g_output_stream_real_close_finish  (GOutputStream             *s
 static void
 g_output_stream_finalize (GObject *object)
 {
-  GOutputStream *stream;
-
-  stream = G_OUTPUT_STREAM (object);
-  
-  if (G_OBJECT_CLASS (g_output_stream_parent_class)->finalize)
-    (*G_OBJECT_CLASS (g_output_stream_parent_class)->finalize) (object);
+  G_OBJECT_CLASS (g_output_stream_parent_class)->finalize (object);
 }
 
 static void
@@ -106,9 +111,8 @@ g_output_stream_dispose (GObject *object)
   
   if (!stream->priv->closed)
     g_output_stream_close (stream, NULL, NULL);
-  
-  if (G_OBJECT_CLASS (g_output_stream_parent_class)->dispose)
-    (*G_OBJECT_CLASS (g_output_stream_parent_class)->dispose) (object);
+
+  G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
 }
 
 static void
@@ -144,26 +148,27 @@ g_output_stream_init (GOutputStream *stream)
 /**
  * g_output_stream_write:
  * @stream: a #GOutputStream.
- * @buffer: the buffer containing the data to write. 
+ * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write. 
  * @count: the number of bytes to write
- * @cancellable: optional cancellable object
+ * @cancellable: (allow-none): optional cancellable object
  * @error: location to store the error occuring, or %NULL to ignore
  *
  * Tries to write @count bytes from @buffer into the stream. Will block
  * during the operation.
  * 
- * If count is zero returns zero and does nothing. A value of @count
+ * If count is 0, returns 0 and does nothing. A value of @count
  * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
  *
  * On success, the number of bytes written to the stream is returned.
  * It is not an error if this is not the same as the requested size, as it
- * can happen e.g. on a partial i/o error, or if the there is not enough
- * storage in the stream. All writes either block until at least one byte
- * is written, so zero is never returned (unless @count is zero).
+ * can happen e.g. on a partial I/O error, or if there is not enough
+ * storage in the stream. All writes block until at least one byte
+ * is written or an error occurs; 0 is never returned (unless
+ * @count is 0).
  * 
  * If @cancellable is not NULL, then the operation can be cancelled by
  * triggering the cancellable object from another thread. If the operation
- * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
+ * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
  * operation was partially finished when the operation was cancelled the
  * partial result will be returned, without an error.
  *
@@ -190,8 +195,7 @@ g_output_stream_write (GOutputStream  *stream,
   if (((gssize) count) < 0)
     {
       g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
-                  _("Too large count value passed to %s"),
-                  G_GNUC_PRETTY_FUNCTION);
+                  _("Too large count value passed to %s"), G_STRFUNC);
       return -1;
     }
 
@@ -199,8 +203,8 @@ g_output_stream_write (GOutputStream  *stream,
 
   if (class->write_fn == NULL) 
     {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
-                  _("Output stream doesn't implement write"));
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
+                           _("Output stream doesn't implement write"));
       return -1;
     }
   
@@ -223,11 +227,11 @@ g_output_stream_write (GOutputStream  *stream,
 /**
  * g_output_stream_write_all:
  * @stream: a #GOutputStream.
- * @buffer: the buffer containing the data to write. 
+ * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write. 
  * @count: the number of bytes to write
- * @bytes_written: location to store the number of bytes that was 
+ * @bytes_written: (out): location to store the number of bytes that was 
  *     written to the stream
- * @cancellable: optional #GCancellable object, %NULL to ignore.
+ * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
  * @error: location to store the error occuring, or %NULL to ignore
  *
  * Tries to write @count bytes from @buffer into the stream. Will block
@@ -286,7 +290,7 @@ g_output_stream_write_all (GOutputStream  *stream,
 /**
  * g_output_stream_flush:
  * @stream: a #GOutputStream.
- * @cancellable: optional cancellable object
+ * @cancellable: (allow-none): optional cancellable object
  * @error: location to store the error occuring, or %NULL to ignore
  *
  * Flushed any outstanding buffers in the stream. Will block during 
@@ -337,13 +341,17 @@ g_output_stream_flush (GOutputStream  *stream,
  * @stream: a #GOutputStream.
  * @source: a #GInputStream.
  * @flags: a set of #GOutputStreamSpliceFlags.
- * @cancellable: optional #GCancellable object, %NULL to ignore. 
- * @error: a #GError location to store the error occuring, or %NULL to 
+ * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
+ * @error: a #GError location to store the error occuring, or %NULL to
  * ignore.
  *
  * Splices an input stream into an output stream.
  *
- * Returns: a #gssize containing the size of the data spliced.
+ * Returns: a #gssize containing the size of the data spliced, or
+ *     -1 if an error occurred. Note that if the number of bytes
+ *     spliced is greater than %G_MAXSSIZE, then that will be
+ *     returned, and there is no way to determine the actual number
+ *     of bytes spliced.
  **/
 gssize
 g_output_stream_splice (GOutputStream             *stream,
@@ -353,35 +361,34 @@ g_output_stream_splice (GOutputStream             *stream,
                        GError                   **error)
 {
   GOutputStreamClass *class;
-  gboolean res;
+  gssize bytes_copied;
 
   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
   g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
 
   if (g_input_stream_is_closed (source))
     {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
-                  _("Source stream is already closed"));
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
+                           _("Source stream is already closed"));
       return -1;
     }
 
   if (!g_output_stream_set_pending (stream, error))
     return -1;
-  
+
   class = G_OUTPUT_STREAM_GET_CLASS (stream);
 
-  res = TRUE;
   if (cancellable)
     g_cancellable_push_current (cancellable);
-      
-  res = class->splice (stream, source, flags, cancellable, error);
-      
+
+  bytes_copied = class->splice (stream, source, flags, cancellable, error);
+
   if (cancellable)
     g_cancellable_pop_current (cancellable);
-  
+
   g_output_stream_clear_pending (stream);
 
-  return res;
+  return bytes_copied;
 }
 
 static gssize
@@ -393,21 +400,21 @@ g_output_stream_real_splice (GOutputStream             *stream,
 {
   GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
   gssize n_read, n_written;
-  gssize bytes_copied;
+  gsize bytes_copied;
   char buffer[8192], *p;
   gboolean res;
 
   bytes_copied = 0;
-  if (class->write_fn == NULL) 
+  if (class->write_fn == NULL)
     {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
-                  _("Output stream doesn't implement write"));
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
+                           _("Output stream doesn't implement write"));
       res = FALSE;
       goto notsupported;
     }
-  
+
   res = TRUE;
-  do 
+  do
     {
       n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
       if (n_read == -1)
@@ -415,7 +422,7 @@ g_output_stream_real_splice (GOutputStream             *stream,
          res = FALSE;
          break;
        }
-       
+
       if (n_read == 0)
        break;
 
@@ -433,6 +440,9 @@ g_output_stream_real_splice (GOutputStream             *stream,
          n_read -= n_written;
          bytes_copied += n_written;
        }
+
+      if (bytes_copied > G_MAXSSIZE)
+       bytes_copied = G_MAXSSIZE;
     }
   while (res);
 
@@ -449,13 +459,14 @@ g_output_stream_real_splice (GOutputStream             *stream,
   if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
     {
       /* But write errors on close are bad! */
-      if (!class->close_fn (stream, cancellable, error))
+      if (class->close_fn &&
+         !class->close_fn (stream, cancellable, error))
        res = FALSE;
     }
 
   if (res)
     return bytes_copied;
-  
+
   return -1;
 }
 
@@ -463,7 +474,7 @@ g_output_stream_real_splice (GOutputStream             *stream,
 /**
  * g_output_stream_close:
  * @stream: A #GOutputStream.
- * @cancellable: optional cancellable object
+ * @cancellable: (allow-none): optional cancellable object
  * @error: location to store the error occuring, or %NULL to ignore
  *
  * Closes the stream, releasing resources related to it.
@@ -484,7 +495,7 @@ g_output_stream_real_splice (GOutputStream             *stream,
  *
  * On failure the first error that happened will be reported, but the close
  * operation will finish as much as possible. A stream that failed to
- * close will still return %G_IO_ERROR_CLOSED all operations. Still, it
+ * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
  * is important to check and report the error to the user, otherwise
  * there might be a loss of data as all data might not be written.
  * 
@@ -516,6 +527,8 @@ g_output_stream_close (GOutputStream  *stream,
   if (!g_output_stream_set_pending (stream, error))
     return FALSE;
 
+  stream->priv->closing = TRUE;
+
   if (cancellable)
     g_cancellable_push_current (cancellable);
 
@@ -541,7 +554,8 @@ g_output_stream_close (GOutputStream  *stream,
   
   if (cancellable)
     g_cancellable_pop_current (cancellable);
-  
+
+  stream->priv->closing = FALSE;
   stream->priv->closed = TRUE;
   g_output_stream_clear_pending (stream);
   
@@ -561,29 +575,95 @@ async_ready_callback_wrapper (GObject      *source_object,
   g_object_unref (stream);
 }
 
+typedef struct {
+  gint io_priority;
+  GCancellable *cancellable;
+  GError *flush_error;
+  gpointer user_data;
+} CloseUserData;
+
 static void
 async_ready_close_callback_wrapper (GObject      *source_object,
                                     GAsyncResult *res,
                                     gpointer      user_data)
 {
   GOutputStream *stream = G_OUTPUT_STREAM (source_object);
+  CloseUserData *data = user_data;
 
+  stream->priv->closing = FALSE;
   stream->priv->closed = TRUE;
+
   g_output_stream_clear_pending (stream);
+
   if (stream->priv->outstanding_callback)
-    (*stream->priv->outstanding_callback) (source_object, res, user_data);
+    {
+      if (data->flush_error != NULL)
+        {
+          GSimpleAsyncResult *err;
+
+          err = g_simple_async_result_new_take_error (source_object,
+                                                      stream->priv->outstanding_callback,
+                                                      data->user_data,
+                                                      data->flush_error);
+          data->flush_error = NULL;
+
+          (*stream->priv->outstanding_callback) (source_object,
+                                                 G_ASYNC_RESULT (err),
+                                                 data->user_data);
+          g_object_unref (err);
+        }
+      else
+        {
+          (*stream->priv->outstanding_callback) (source_object,
+                                                 res,
+                                                 data->user_data);
+        }
+    }
+
   g_object_unref (stream);
+
+  if (data->cancellable)
+    g_object_unref (data->cancellable);
+
+  if (data->flush_error)
+    g_error_free (data->flush_error);
+
+  g_slice_free (CloseUserData, data);
+}
+
+static void
+async_ready_close_flushed_callback_wrapper (GObject      *source_object,
+                                            GAsyncResult *res,
+                                            gpointer      user_data)
+{
+  GOutputStream *stream = G_OUTPUT_STREAM (source_object);
+  GOutputStreamClass *class;
+  CloseUserData *data = user_data;
+  GSimpleAsyncResult *simple;
+
+  /* propagate the possible error */
+  if (G_IS_SIMPLE_ASYNC_RESULT (res))
+    {
+      simple = G_SIMPLE_ASYNC_RESULT (res);
+      g_simple_async_result_propagate_error (simple, &data->flush_error);
+    }
+
+  class = G_OUTPUT_STREAM_GET_CLASS (stream);
+
+  /* we still close, even if there was a flush error */
+  class->close_async (stream, data->io_priority, data->cancellable,
+                     async_ready_close_callback_wrapper, user_data);
 }
 
 /**
  * g_output_stream_write_async:
  * @stream: A #GOutputStream.
- * @buffer: the buffer containing the data to write. 
+ * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write. 
  * @count: the number of bytes to write
  * @io_priority: the io priority of the request.
- * @cancellable: optional #GCancellable object, %NULL to ignore.
- * @callback: callback to call when the request is satisfied
- * @user_data: the data to pass to callback function
+ * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
+ * @callback: (scope async): callback to call when the request is satisfied
+ * @user_data: (closure): the data to pass to callback function
  *
  * Request an asynchronous write of @count bytes from @buffer into 
  * the stream. When the operation is finished @callback will be called.
@@ -601,6 +681,10 @@ async_ready_close_callback_wrapper (GObject      *source_object,
  * requested size, as it can happen e.g. on a partial I/O error, 
  * but generally we try to write as many bytes as requested. 
  *
+ * You are guaranteed that this method will never fail with
+ * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
+ * method will just wait until this changes.
+ *
  * Any outstanding I/O request with higher priority (lower numerical 
  * value) will be executed before an outstanding request with lower 
  * priority. Default priority is %G_PRIORITY_DEFAULT.
@@ -646,17 +730,16 @@ g_output_stream_write_async (GOutputStream       *stream,
                                           user_data,
                                           G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
                                           _("Too large count value passed to %s"),
-                                          G_GNUC_PRETTY_FUNCTION);
+                                          G_STRFUNC);
       return;
     }
 
   if (!g_output_stream_set_pending (stream, &error))
     {
-      g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+      g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
                                            callback,
                                            user_data,
                                            error);
-      g_error_free (error);
       return;
     }
   
@@ -735,9 +818,9 @@ async_ready_splice_callback_wrapper (GObject      *source_object,
  * @source: a #GInputStream. 
  * @flags: a set of #GOutputStreamSpliceFlags.
  * @io_priority: the io priority of the request.
- * @cancellable: optional #GCancellable object, %NULL to ignore. 
- * @callback: a #GAsyncReadyCallback. 
- * @user_data: user data passed to @callback.
+ * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. 
+ * @callback: (scope async): a #GAsyncReadyCallback. 
+ * @user_data: (closure): user data passed to @callback.
  * 
  * Splices a stream asynchronously.
  * When the operation is finished @callback will be called.
@@ -775,11 +858,10 @@ g_output_stream_splice_async (GOutputStream            *stream,
   
   if (!g_output_stream_set_pending (stream, &error))
     {
-      g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+      g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
                                            callback,
                                            user_data,
                                            error);
-      g_error_free (error);
       return;
     }
 
@@ -804,7 +886,10 @@ g_output_stream_splice_async (GOutputStream            *stream,
  *
  * Finishes an asynchronous stream splice operation.
  * 
- * Returns: a #gssize of the number of bytes spliced.
+ * Returns: a #gssize of the number of bytes spliced. Note that if the
+ *     number of bytes spliced is greater than %G_MAXSSIZE, then that
+ *     will be returned, and there is no way to determine the actual
+ *     number of bytes spliced.
  **/
 gssize
 g_output_stream_splice_finish (GOutputStream  *stream,
@@ -832,9 +917,9 @@ g_output_stream_splice_finish (GOutputStream  *stream,
  * g_output_stream_flush_async:
  * @stream: a #GOutputStream.
  * @io_priority: the io priority of the request.
- * @cancellable: optional #GCancellable object, %NULL to ignore.
- * @callback: a #GAsyncReadyCallback to call when the request is satisfied
- * @user_data: the data to pass to callback function
+ * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
+ * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
+ * @user_data: (closure): the data to pass to callback function
  * 
  * Flushes a stream asynchronously.
  * For behaviour details see g_output_stream_flush().
@@ -858,11 +943,10 @@ g_output_stream_flush_async (GOutputStream       *stream,
 
   if (!g_output_stream_set_pending (stream, &error))
     {
-      g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+      g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
                                            callback,
                                            user_data,
                                            error);
-      g_error_free (error);
       return;
     }
 
@@ -928,9 +1012,9 @@ g_output_stream_flush_finish (GOutputStream  *stream,
  * g_output_stream_close_async:
  * @stream: A #GOutputStream.
  * @io_priority: the io priority of the request.
- * @callback: callback to call when the request is satisfied
- * @user_data: the data to pass to callback function
- * @cancellable: optional cancellable object
+ * @cancellable: (allow-none): optional cancellable object
+ * @callback: (scope async): callback to call when the request is satisfied
+ * @user_data: (closure): the data to pass to callback function
  *
  * Requests an asynchronous close of the stream, releasing resources 
  * related to it. When the operation is finished @callback will be 
@@ -953,6 +1037,7 @@ g_output_stream_close_async (GOutputStream       *stream,
   GOutputStreamClass *class;
   GSimpleAsyncResult *simple;
   GError *error = NULL;
+  CloseUserData *data;
 
   g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
   
@@ -969,19 +1054,42 @@ g_output_stream_close_async (GOutputStream       *stream,
 
   if (!g_output_stream_set_pending (stream, &error))
     {
-      g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+      g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
                                            callback,
                                            user_data,
                                            error);
-      g_error_free (error);
       return;
     }
   
   class = G_OUTPUT_STREAM_GET_CLASS (stream);
+  stream->priv->closing = TRUE;
   stream->priv->outstanding_callback = callback;
   g_object_ref (stream);
-  class->close_async (stream, io_priority, cancellable,
-                     async_ready_close_callback_wrapper, user_data);
+
+  data = g_slice_new0 (CloseUserData);
+
+  if (cancellable != NULL)
+    data->cancellable = g_object_ref (cancellable);
+
+  data->io_priority = io_priority;
+  data->user_data = user_data;
+
+  /* Call close_async directly if there is no need to flush, or if the flush
+     can be done sync (in the output stream async close thread) */
+  if (class->flush_async == NULL ||
+      (class->flush_async == g_output_stream_real_flush_async &&
+       (class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
+    {
+      class->close_async (stream, io_priority, cancellable,
+                          async_ready_close_callback_wrapper, data);
+    }
+  else
+    {
+      /* First do an async flush, then do the async close in the callback
+         wrapper (see async_ready_close_flushed_callback_wrapper) */
+      class->flush_async (stream, io_priority, cancellable,
+                          async_ready_close_flushed_callback_wrapper, data);
+    }
 }
 
 /**
@@ -1038,6 +1146,27 @@ g_output_stream_is_closed (GOutputStream *stream)
 }
 
 /**
+ * g_output_stream_is_closing:
+ * @stream: a #GOutputStream.
+ *
+ * Checks if an output stream is being closed. This can be
+ * used inside e.g. a flush implementation to see if the
+ * flush (or other i/o operation) is called from within
+ * the closing operation.
+ *
+ * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
+ *
+ * Since: 2.24
+ **/
+gboolean
+g_output_stream_is_closing (GOutputStream *stream)
+{
+  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
+
+  return stream->priv->closing;
+}
+
+/**
  * g_output_stream_has_pending:
  * @stream: a #GOutputStream.
  * 
@@ -1073,15 +1202,18 @@ g_output_stream_set_pending (GOutputStream *stream,
   
   if (stream->priv->closed)
     {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
-                  _("Stream is already closed"));
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
+                           _("Stream is already closed"));
       return FALSE;
     }
   
   if (stream->priv->pending)
     {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
-                  _("Stream has outstanding operation"));
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
+                           /* Translators: This is an error you get if there is
+                            * already an operation running against this stream when
+                            * you try to start one */
+                           _("Stream has outstanding operation"));
       return FALSE;
     }
   
@@ -1128,10 +1260,7 @@ write_async_thread (GSimpleAsyncResult *res,
   op->count_written = class->write_fn (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
                                       cancellable, &error);
   if (op->count_written == -1)
-    {
-      g_simple_async_result_set_from_error (res, error);
-      g_error_free (error);
-    }
+    g_simple_async_result_take_error (res, error);
 }
 
 static void
@@ -1195,10 +1324,7 @@ splice_async_thread (GSimpleAsyncResult *result,
                                    cancellable,
                                    &error);
   if (op->bytes_copied == -1)
-    {
-      g_simple_async_result_set_from_error (result, error);
-      g_error_free (error);
-    }
+    g_simple_async_result_take_error (result, error);
 }
 
 static void
@@ -1255,10 +1381,7 @@ flush_async_thread (GSimpleAsyncResult *res,
     result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
 
   if (!result)
-    {
-      g_simple_async_result_set_from_error (res, error);
-      g_error_free (error);
-    }
+    g_simple_async_result_take_error (res, error);
 }
 
 static void
@@ -1291,19 +1414,34 @@ close_async_thread (GSimpleAsyncResult *res,
 {
   GOutputStreamClass *class;
   GError *error = NULL;
-  gboolean result;
+  gboolean result = TRUE;
+
+  class = G_OUTPUT_STREAM_GET_CLASS (object);
+
+  /* Do a flush here if there is a flush function, and we did not have to do
+     an async flush before (see g_output_stream_close_async) */
+  if (class->flush != NULL &&
+      (class->flush_async == NULL ||
+       class->flush_async == g_output_stream_real_flush_async))
+    {
+      result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
+    }
 
   /* Auto handling of cancelation disabled, and ignore
      cancellation, since we want to close things anyway, although
      possibly in a quick-n-dirty way. At least we never want to leak
      open handles */
   
-  class = G_OUTPUT_STREAM_GET_CLASS (object);
-  result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
-  if (!result)
+  if (class->close_fn)
     {
-      g_simple_async_result_set_from_error (res, error);
-      g_error_free (error);
+      /* Make sure to close, even if the flush failed (see sync close) */
+      if (!result)
+        class->close_fn (G_OUTPUT_STREAM (object), cancellable, NULL);
+      else
+        result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
+
+      if (!result)
+        g_simple_async_result_take_error (res, error);
     }
 }
 
@@ -1333,6 +1471,3 @@ g_output_stream_real_close_finish (GOutputStream  *stream,
   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);
   return TRUE;
 }
-
-#define __G_OUTPUT_STREAM_C__
-#include "gioaliasdef.c"