2007-11-30 Dan Winship <danw@gnome.org>
+ * ginputstream.c (g_input_stream_set_pending): Make this take a
+ GError and return a gboolean, and do the "outstanding operation"
+ check (and the "stream is already closed" check) itself.
+ (g_input_stream_clear_pending): Formerly set_pending(FALSE).
+
+ * goutputstream.c (g_output_stream_set_pending)
+ (g_output_stream_clear_pending): Likewise
+
+ * gbufferedinputstream.c:
+ * gfileinputstream.c:
+ * gfileoutputstream.c: Update for that
+
+ * gsimpleasyncresult.c (g_simple_async_report_gerror_in_idle):
+ Like g_simple_async_report_error_in_idle, but takes a GError
+ rather than building one.
+
+2007-11-30 Dan Winship <danw@gnome.org>
+
* goutputstream.c: Don't cheat and unset the "pending" flag around
inner calls. Instead, call the class method directly rather than
the wrapper function that checks "pending"
input_stream = G_INPUT_STREAM (stream);
- if (g_input_stream_is_closed (input_stream))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return -1;
- }
-
- if (g_input_stream_has_pending (input_stream))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return -1;
- }
-
- g_input_stream_set_pending (input_stream, TRUE);
+ if (!g_input_stream_set_pending (input_stream, error))
+ return -1;
if (cancellable)
g_push_current_cancellable (cancellable);
if (cancellable)
g_pop_current_cancellable (cancellable);
- g_input_stream_set_pending (input_stream, FALSE);
+ g_input_stream_clear_pending (input_stream);
return res;
}
{
GBufferedInputStream *stream = G_BUFFERED_INPUT_STREAM (source_object);
- g_input_stream_set_pending (G_INPUT_STREAM (stream), FALSE);
+ g_input_stream_clear_pending (G_INPUT_STREAM (stream));
(*stream->priv->outstanding_callback) (source_object, res, user_data);
g_object_unref (stream);
}
{
GBufferedInputStreamClass *class;
GSimpleAsyncResult *simple;
+ GError *error = NULL;
g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
return;
}
- if (g_input_stream_is_closed (G_INPUT_STREAM (stream)))
+ if (!g_input_stream_set_pending (G_INPUT_STREAM (stream), &error))
{
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
+ g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+ callback,
+ user_data,
+ error);
+ g_error_free (error);
return;
}
- if (g_input_stream_has_pending (G_INPUT_STREAM (stream)))
- {
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return;
- }
-
class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
- g_input_stream_set_pending (G_INPUT_STREAM (stream), TRUE);
stream->priv->outstanding_callback = callback;
g_object_ref (stream);
class->fill_async (stream, count, io_priority, cancellable,
return -1;
}
- if (g_input_stream_has_pending (input_stream))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return -1;
- }
+ if (!g_input_stream_set_pending (input_stream, error))
+ return -1;
available = priv->end - priv->pos;
if (available < 1)
- return priv->buffer[priv->pos++];
+ {
+ g_input_stream_clear_pending (input_stream);
+ return priv->buffer[priv->pos++];
+ }
/* Byte not available, request refill for more */
- g_input_stream_set_pending (input_stream, TRUE);
-
if (cancellable)
g_push_current_cancellable (cancellable);
if (cancellable)
g_pop_current_cancellable (cancellable);
- g_input_stream_set_pending (input_stream, FALSE);
+ g_input_stream_clear_pending (input_stream);
if (nread <= 0)
return -1; /* error or end of stream */
input_stream = G_INPUT_STREAM (stream);
- if (g_input_stream_is_closed (input_stream))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return NULL;
- }
-
- if (g_input_stream_has_pending (input_stream))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return NULL;
- }
+ if (!g_input_stream_set_pending (input_stream, error))
+ return NULL;
info = NULL;
- g_input_stream_set_pending (input_stream, TRUE);
-
if (cancellable)
g_push_current_cancellable (cancellable);
if (cancellable)
g_pop_current_cancellable (cancellable);
- g_input_stream_set_pending (input_stream, FALSE);
+ g_input_stream_clear_pending (input_stream);
return info;
}
{
GFileInputStream *stream = G_FILE_INPUT_STREAM (source_object);
- g_input_stream_set_pending (G_INPUT_STREAM (stream), FALSE);
+ g_input_stream_clear_pending (G_INPUT_STREAM (stream));
if (stream->priv->outstanding_callback)
(*stream->priv->outstanding_callback) (source_object, res, user_data);
g_object_unref (stream);
{
GFileInputStreamClass *klass;
GInputStream *input_stream;
+ GError *error = NULL;
g_return_if_fail (G_IS_FILE_INPUT_STREAM (stream));
input_stream = G_INPUT_STREAM (stream);
-
- if (g_input_stream_is_closed (input_stream))
- {
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return;
- }
- if (g_input_stream_has_pending (input_stream))
+ if (!g_input_stream_set_pending (input_stream, &error))
{
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
+ g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+ callback,
+ user_data,
+ error);
+ g_error_free (error);
return;
}
klass = G_FILE_INPUT_STREAM_GET_CLASS (stream);
- g_input_stream_set_pending (input_stream, TRUE);
stream->priv->outstanding_callback = callback;
g_object_ref (stream);
klass->query_info_async (stream, attributes, io_priority, cancellable,
input_stream = G_INPUT_STREAM (stream);
class = G_FILE_INPUT_STREAM_GET_CLASS (stream);
- if (g_input_stream_is_closed (input_stream))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return FALSE;
- }
-
- if (g_input_stream_has_pending (input_stream))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return FALSE;
- }
-
if (!class->seek)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
return FALSE;
}
- g_input_stream_set_pending (input_stream, TRUE);
+ if (!g_input_stream_set_pending (input_stream, error))
+ return FALSE;
if (cancellable)
g_push_current_cancellable (cancellable);
if (cancellable)
g_pop_current_cancellable (cancellable);
- g_input_stream_set_pending (input_stream, FALSE);
+ g_input_stream_clear_pending (input_stream);
return res;
}
output_stream = G_OUTPUT_STREAM (stream);
- if (g_output_stream_is_closed (output_stream))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return NULL;
- }
-
- if (g_output_stream_has_pending (output_stream))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return NULL;
- }
+ if (!g_output_stream_set_pending (output_stream, error))
+ return NULL;
info = NULL;
- g_output_stream_set_pending (output_stream, TRUE);
-
if (cancellable)
g_push_current_cancellable (cancellable);
if (cancellable)
g_pop_current_cancellable (cancellable);
- g_output_stream_set_pending (output_stream, FALSE);
+ g_output_stream_clear_pending (output_stream);
return info;
}
{
GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
- g_output_stream_set_pending (G_OUTPUT_STREAM (stream), FALSE);
+ g_output_stream_clear_pending (G_OUTPUT_STREAM (stream));
if (stream->priv->outstanding_callback)
(*stream->priv->outstanding_callback) (source_object, res, user_data);
g_object_unref (stream);
{
GFileOutputStreamClass *klass;
GOutputStream *output_stream;
+ GError *error = NULL;
g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
output_stream = G_OUTPUT_STREAM (stream);
- if (g_output_stream_is_closed (output_stream))
+ if (!g_output_stream_set_pending (output_stream, &error))
{
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return;
- }
-
- if (g_output_stream_has_pending (output_stream))
- {
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
+ g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+ callback,
+ user_data,
+ error);
+ g_error_free (error);
return;
}
klass = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
- g_output_stream_set_pending (output_stream, TRUE);
stream->priv->outstanding_callback = callback;
g_object_ref (stream);
klass->query_info_async (stream, attributes, io_priority, cancellable,
output_stream = G_OUTPUT_STREAM (stream);
class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
- if (g_output_stream_is_closed (output_stream))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return FALSE;
- }
-
- if (g_output_stream_has_pending (output_stream))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return FALSE;
- }
-
if (!class->seek)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
return FALSE;
}
- g_output_stream_set_pending (output_stream, TRUE);
+ if (!g_output_stream_set_pending (output_stream, error))
+ return FALSE;
if (cancellable)
g_push_current_cancellable (cancellable);
if (cancellable)
g_pop_current_cancellable (cancellable);
- g_output_stream_set_pending (output_stream, FALSE);
+ g_output_stream_clear_pending (output_stream);
return res;
}
output_stream = G_OUTPUT_STREAM (stream);
class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
- if (g_output_stream_is_closed (output_stream))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return FALSE;
- }
-
- if (g_output_stream_has_pending (output_stream))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return FALSE;
- }
-
if (!class->truncate)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
return FALSE;
}
- g_output_stream_set_pending (output_stream, TRUE);
+ if (!g_output_stream_set_pending (output_stream, error))
+ return FALSE;
if (cancellable)
g_push_current_cancellable (cancellable);
if (cancellable)
g_pop_current_cancellable (cancellable);
- g_output_stream_set_pending (output_stream, FALSE);
+ g_output_stream_clear_pending (output_stream);
return res;
}
return -1;
}
- if (stream->priv->closed)
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return -1;
- }
-
- if (stream->priv->pending)
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return -1;
- }
-
class = G_INPUT_STREAM_GET_CLASS (stream);
if (class->read == NULL)
return -1;
}
+ if (!g_input_stream_set_pending (stream, error))
+ return -1;
+
if (cancellable)
g_push_current_cancellable (cancellable);
- stream->priv->pending = TRUE;
res = class->read (stream, buffer, count, cancellable, error);
- stream->priv->pending = FALSE;
if (cancellable)
g_pop_current_cancellable (cancellable);
+ g_input_stream_clear_pending (stream);
+
return res;
}
return -1;
}
- if (stream->priv->closed)
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return -1;
- }
-
- if (stream->priv->pending)
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return -1;
- }
-
class = G_INPUT_STREAM_GET_CLASS (stream);
+ if (!g_input_stream_set_pending (stream, error))
+ return -1;
+
if (cancellable)
g_push_current_cancellable (cancellable);
- stream->priv->pending = TRUE;
res = class->skip (stream, count, cancellable, error);
- stream->priv->pending = FALSE;
if (cancellable)
g_pop_current_cancellable (cancellable);
+ g_input_stream_clear_pending (stream);
+
return res;
}
if (stream->priv->closed)
return TRUE;
- if (stream->priv->pending)
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return FALSE;
- }
-
res = TRUE;
- stream->priv->pending = TRUE;
+ if (!g_input_stream_set_pending (stream, error))
+ return FALSE;
if (cancellable)
g_push_current_cancellable (cancellable);
if (cancellable)
g_pop_current_cancellable (cancellable);
+
+ g_input_stream_clear_pending (stream);
stream->priv->closed = TRUE;
- stream->priv->pending = FALSE;
-
return res;
}
{
GInputStream *stream = G_INPUT_STREAM (source_object);
- stream->priv->pending = FALSE;
+ g_input_stream_clear_pending (stream);
if (stream->priv->outstanding_callback)
(*stream->priv->outstanding_callback) (source_object, res, user_data);
g_object_unref (stream);
{
GInputStream *stream = G_INPUT_STREAM (source_object);
- stream->priv->pending = FALSE;
+ g_input_stream_clear_pending (stream);
stream->priv->closed = TRUE;
if (stream->priv->outstanding_callback)
(*stream->priv->outstanding_callback) (source_object, res, user_data);
{
GInputStreamClass *class;
GSimpleAsyncResult *simple;
+ GError *error = NULL;
g_return_if_fail (G_IS_INPUT_STREAM (stream));
g_return_if_fail (buffer != NULL);
return;
}
- if (stream->priv->closed)
- {
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return;
- }
-
- if (stream->priv->pending)
+ if (!g_input_stream_set_pending (stream, &error))
{
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
+ g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+ callback,
+ user_data,
+ error);
+ g_error_free (error);
return;
}
class = G_INPUT_STREAM_GET_CLASS (stream);
-
- stream->priv->pending = TRUE;
stream->priv->outstanding_callback = callback;
g_object_ref (stream);
class->read_async (stream, buffer, count, io_priority, cancellable,
{
GInputStreamClass *class;
GSimpleAsyncResult *simple;
+ GError *error = NULL;
g_return_if_fail (G_IS_INPUT_STREAM (stream));
return;
}
- if (stream->priv->closed)
+ if (!g_input_stream_set_pending (stream, &error))
{
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return;
- }
-
- if (stream->priv->pending)
- {
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
+ g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+ callback,
+ user_data,
+ error);
+ g_error_free (error);
return;
}
class = G_INPUT_STREAM_GET_CLASS (stream);
- stream->priv->pending = TRUE;
stream->priv->outstanding_callback = callback;
g_object_ref (stream);
class->skip_async (stream, count, io_priority, cancellable,
{
GInputStreamClass *class;
GSimpleAsyncResult *simple;
+ GError *error = NULL;
g_return_if_fail (G_IS_INPUT_STREAM (stream));
return;
}
- if (stream->priv->pending)
+ if (!g_input_stream_set_pending (stream, &error))
{
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
+ g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+ callback,
+ user_data,
+ error);
+ g_error_free (error);
return;
}
class = G_INPUT_STREAM_GET_CLASS (stream);
- stream->priv->pending = TRUE;
stream->priv->outstanding_callback = callback;
g_object_ref (stream);
class->close_async (stream, io_priority, cancellable,
/**
* g_input_stream_set_pending:
* @stream: input stream
- * @pending: boolean.
+ * @error: a #GError location to store the error occuring, or %NULL to
+ * ignore.
+ *
+ * Sets @stream to have actions pending. If the pending flag is
+ * already set or @stream is closed, it will return %FALSE and set
+ * @error.
+ *
+ * Return value: %TRUE if pending was previously unset and is now set.
+ **/
+gboolean
+g_input_stream_set_pending (GInputStream *stream, GError **error)
+{
+ g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
+
+ if (stream->priv->closed)
+ {
+ g_set_error (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"));
+ return FALSE;
+ }
+
+ stream->priv->pending = TRUE;
+ return TRUE;
+}
+
+/**
+ * g_input_stream_clear_pending:
+ * @stream: input stream
*
- * Sets @stream has actions pending.
+ * Clears the pending flag on @stream.
**/
void
-g_input_stream_set_pending (GInputStream *stream,
- gboolean pending)
+g_input_stream_clear_pending (GInputStream *stream)
{
g_return_if_fail (G_IS_INPUT_STREAM (stream));
- stream->priv->pending = pending;
+ stream->priv->pending = FALSE;
}
/********************************************
GType g_input_stream_get_type (void) G_GNUC_CONST;
-gssize g_input_stream_read (GInputStream *stream,
- void *buffer,
- gsize count,
- GCancellable *cancellable,
- GError **error);
-gboolean g_input_stream_read_all (GInputStream *stream,
- void *buffer,
- gsize count,
- gsize *bytes_read,
- GCancellable *cancellable,
- GError **error);
-gssize g_input_stream_skip (GInputStream *stream,
- gsize count,
- GCancellable *cancellable,
- GError **error);
-gboolean g_input_stream_close (GInputStream *stream,
- GCancellable *cancellable,
- GError **error);
-void g_input_stream_read_async (GInputStream *stream,
- void *buffer,
- gsize count,
- int io_priority,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data);
-gssize g_input_stream_read_finish (GInputStream *stream,
- GAsyncResult *result,
- GError **error);
-void g_input_stream_skip_async (GInputStream *stream,
- gsize count,
- int io_priority,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data);
-gssize g_input_stream_skip_finish (GInputStream *stream,
- GAsyncResult *result,
- GError **error);
-void g_input_stream_close_async (GInputStream *stream,
- int io_priority,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data);
-gboolean g_input_stream_close_finish (GInputStream *stream,
- GAsyncResult *result,
- GError **error);
+gssize g_input_stream_read (GInputStream *stream,
+ void *buffer,
+ gsize count,
+ GCancellable *cancellable,
+ GError **error);
+gboolean g_input_stream_read_all (GInputStream *stream,
+ void *buffer,
+ gsize count,
+ gsize *bytes_read,
+ GCancellable *cancellable,
+ GError **error);
+gssize g_input_stream_skip (GInputStream *stream,
+ gsize count,
+ GCancellable *cancellable,
+ GError **error);
+gboolean g_input_stream_close (GInputStream *stream,
+ GCancellable *cancellable,
+ GError **error);
+void g_input_stream_read_async (GInputStream *stream,
+ void *buffer,
+ gsize count,
+ int io_priority,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+gssize g_input_stream_read_finish (GInputStream *stream,
+ GAsyncResult *result,
+ GError **error);
+void g_input_stream_skip_async (GInputStream *stream,
+ gsize count,
+ int io_priority,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+gssize g_input_stream_skip_finish (GInputStream *stream,
+ GAsyncResult *result,
+ GError **error);
+void g_input_stream_close_async (GInputStream *stream,
+ int io_priority,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+gboolean g_input_stream_close_finish (GInputStream *stream,
+ GAsyncResult *result,
+ GError **error);
/* For implementations: */
-gboolean g_input_stream_is_closed (GInputStream *stream);
-gboolean g_input_stream_has_pending (GInputStream *stream);
-void g_input_stream_set_pending (GInputStream *stream,
- gboolean pending);
+gboolean g_input_stream_is_closed (GInputStream *stream);
+gboolean g_input_stream_has_pending (GInputStream *stream);
+gboolean g_input_stream_set_pending (GInputStream *stream,
+ GError **error);
+void g_input_stream_clear_pending (GInputStream *stream);
G_END_DECLS
return -1;
}
- if (stream->priv->closed)
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return -1;
- }
-
- if (stream->priv->pending)
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return -1;
- }
-
class = G_OUTPUT_STREAM_GET_CLASS (stream);
if (class->write == NULL)
return -1;
}
+ if (!g_output_stream_set_pending (stream, error))
+ return -1;
+
if (cancellable)
g_push_current_cancellable (cancellable);
- stream->priv->pending = TRUE;
res = class->write (stream, buffer, count, cancellable, error);
- stream->priv->pending = FALSE;
if (cancellable)
g_pop_current_cancellable (cancellable);
+ g_output_stream_clear_pending (stream);
+
return res;
}
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
- if (stream->priv->closed)
- {
- g_set_error (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"));
- return FALSE;
- }
+ if (!g_output_stream_set_pending (stream, error))
+ return FALSE;
class = G_OUTPUT_STREAM_GET_CLASS (stream);
if (cancellable)
g_push_current_cancellable (cancellable);
- stream->priv->pending = TRUE;
res = class->flush (stream, cancellable, error);
- stream->priv->pending = FALSE;
if (cancellable)
g_pop_current_cancellable (cancellable);
}
+ g_output_stream_clear_pending (stream);
+
return res;
}
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
- if (stream->priv->closed)
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Target stream is already closed"));
- return -1;
- }
-
if (g_input_stream_is_closed (source))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
return -1;
}
- if (stream->priv->pending)
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return -1;
- }
+ if (!g_output_stream_set_pending (stream, error))
+ return -1;
class = G_OUTPUT_STREAM_GET_CLASS (stream);
if (cancellable)
g_push_current_cancellable (cancellable);
- stream->priv->pending = TRUE;
res = class->splice (stream, source, flags, cancellable, error);
- stream->priv->pending = FALSE;
if (cancellable)
g_pop_current_cancellable (cancellable);
+ g_output_stream_clear_pending (stream);
+
return res;
}
if (stream->priv->closed)
return TRUE;
- if (stream->priv->pending)
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return FALSE;
- }
+ if (!g_output_stream_set_pending (stream, error))
+ return FALSE;
- stream->priv->pending = TRUE;
-
if (cancellable)
g_push_current_cancellable (cancellable);
res = class->flush (stream, cancellable, error);
-
+
if (!res)
{
/* flushing caused the error that we want to return,
g_pop_current_cancellable (cancellable);
stream->priv->closed = TRUE;
- stream->priv->pending = FALSE;
+ g_output_stream_clear_pending (stream);
return res;
}
{
GOutputStream *stream = G_OUTPUT_STREAM (source_object);
- stream->priv->pending = FALSE;
+ g_output_stream_clear_pending (stream);
if (stream->priv->outstanding_callback)
(*stream->priv->outstanding_callback) (source_object, res, user_data);
g_object_unref (stream);
{
GOutputStream *stream = G_OUTPUT_STREAM (source_object);
- stream->priv->pending = 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);
g_object_unref (stream);
{
GOutputStreamClass *class;
GSimpleAsyncResult *simple;
+ GError *error = NULL;
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
g_return_if_fail (buffer != NULL);
return;
}
- if (stream->priv->closed)
+ if (!g_output_stream_set_pending (stream, &error))
{
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
+ g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+ callback,
+ user_data,
+ error);
+ g_error_free (error);
return;
}
- if (stream->priv->pending)
- {
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
- return;
- }
-
class = G_OUTPUT_STREAM_GET_CLASS (stream);
- stream->priv->pending = TRUE;
stream->priv->outstanding_callback = callback;
g_object_ref (stream);
class->write_async (stream, buffer, count, io_priority, cancellable,
GOutputStream *stream = G_OUTPUT_STREAM (source_object);
SpliceUserData *data = _data;
- stream->priv->pending = FALSE;
+ g_output_stream_clear_pending (stream);
if (data->callback)
(*data->callback) (source_object, res, data->user_data);
{
GOutputStreamClass *class;
SpliceUserData *data;
+ GError *error = NULL;
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
g_return_if_fail (G_IS_INPUT_STREAM (source));
- if (stream->priv->closed)
- {
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Target stream is already closed"));
- return;
- }
-
if (g_input_stream_is_closed (source))
{
g_simple_async_report_error_in_idle (G_OBJECT (stream),
return;
}
- if (stream->priv->pending)
+ if (!g_output_stream_set_pending (stream, &error))
{
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
+ g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+ callback,
+ user_data,
+ error);
+ g_error_free (error);
return;
}
class = G_OUTPUT_STREAM_GET_CLASS (stream);
- stream->priv->pending = TRUE;
-
data = g_new0 (SpliceUserData, 1);
data->callback = callback;
data->user_data = user_data;
{
GOutputStreamClass *class;
GSimpleAsyncResult *simple;
+ GError *error = NULL;
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
- if (stream->priv->closed)
+ if (!g_output_stream_set_pending (stream, &error))
{
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_CLOSED,
- _("Stream is already closed"));
- return;
- }
-
- if (stream->priv->pending)
- {
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
+ g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+ callback,
+ user_data,
+ error);
+ g_error_free (error);
return;
}
+ stream->priv->outstanding_callback = callback;
+ g_object_ref (stream);
+
class = G_OUTPUT_STREAM_GET_CLASS (stream);
if (class->flush_async == NULL)
{
simple = g_simple_async_result_new (G_OBJECT (stream),
- callback,
+ async_ready_callback_wrapper,
user_data,
g_output_stream_flush_async);
g_simple_async_result_complete_in_idle (simple);
return;
}
- stream->priv->pending = TRUE;
- stream->priv->outstanding_callback = callback;
- g_object_ref (stream);
class->flush_async (stream, io_priority, cancellable,
async_ready_callback_wrapper, user_data);
}
{
GOutputStreamClass *class;
GSimpleAsyncResult *simple;
+ GError *error = NULL;
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
return;
}
- if (stream->priv->pending)
+ if (!g_output_stream_set_pending (stream, &error))
{
- g_simple_async_report_error_in_idle (G_OBJECT (stream),
- callback,
- user_data,
- G_IO_ERROR, G_IO_ERROR_PENDING,
- _("Stream has outstanding operation"));
+ g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+ callback,
+ user_data,
+ error);
+ g_error_free (error);
return;
}
class = G_OUTPUT_STREAM_GET_CLASS (stream);
- stream->priv->pending = TRUE;
stream->priv->outstanding_callback = callback;
g_object_ref (stream);
class->close_async (stream, io_priority, cancellable,
/**
* g_output_stream_set_pending:
* @stream: a #GOutputStream.
- * @pending: a #gboolean.
+ * @error: a #GError location to store the error occuring, or %NULL to
+ * ignore.
*
- * Sets the @stream as having pending actions if @pending is %TRUE.
+ * Sets @stream to have actions pending. If the pending flag is
+ * already set or @stream is closed, it will return %FALSE and set
+ * @error.
+ *
+ * Return value: %TRUE if pending was previously unset and is now set.
**/
-void
+gboolean
g_output_stream_set_pending (GOutputStream *stream,
- gboolean pending)
+ GError **error)
+{
+ g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
+
+ if (stream->priv->closed)
+ {
+ g_set_error (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"));
+ return FALSE;
+ }
+
+ stream->priv->pending = TRUE;
+ return TRUE;
+}
+
+/**
+ * g_output_stream_clear_pending:
+ * @stream: output stream
+ *
+ * Clears the pending flag on @stream.
+ **/
+void
+g_output_stream_clear_pending (GOutputStream *stream)
{
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
- stream->priv->pending = pending;
+ stream->priv->pending = FALSE;
}
op->flags,
cancellable,
&error);
-
if (op->bytes_copied == -1)
{
g_simple_async_result_set_from_error (result, error);
gboolean g_output_stream_is_closed (GOutputStream *stream);
gboolean g_output_stream_has_pending (GOutputStream *stream);
-void g_output_stream_set_pending (GOutputStream *stream,
- gboolean pending);
+gboolean g_output_stream_set_pending (GOutputStream *stream,
+ GError **error);
+void g_output_stream_clear_pending (GOutputStream *stream);
G_END_DECLS
g_object_unref (simple);
}
+/**
+ * g_simple_async_report_error_in_idle:
+ * @object: a #GObject.
+ * @callback: a #GAsyncReadyCallback.
+ * @user_data: user data passed to @callback.
+ * @error: the #GError to report
+ *
+ * Reports an error in an idle function.
+ **/
+void
+g_simple_async_report_gerror_in_idle (GObject *object,
+ GAsyncReadyCallback callback,
+ gpointer user_data,
+ GError *error)
+{
+ GSimpleAsyncResult *simple;
+
+ g_return_if_fail (G_IS_OBJECT (object));
+ g_return_if_fail (error != NULL);
+
+ simple = g_simple_async_result_new_from_error (object,
+ callback,
+ user_data,
+ error);
+ g_simple_async_result_complete_in_idle (simple);
+ g_object_unref (simple);
+}
+
#define __G_SIMPLE_ASYNC_RESULT_C__
#include "gioaliasdef.c"
const char *format,
va_list args);
-void g_simple_async_report_error_in_idle (GObject *object,
- GAsyncReadyCallback callback,
- gpointer user_data,
- GQuark domain,
- gint code,
- const char *format,
- ...);
+void g_simple_async_report_error_in_idle (GObject *object,
+ GAsyncReadyCallback callback,
+ gpointer user_data,
+ GQuark domain,
+ gint code,
+ const char *format,
+ ...);
+void g_simple_async_report_gerror_in_idle (GObject *object,
+ GAsyncReadyCallback callback,
+ gpointer user_data,
+ GError *error);
G_END_DECLS