421
411
if (!unix_stream->priv->close_fd)
414
/* This might block during the close. Doesn't seem to be a way to avoid it though. */
415
res = close (unix_stream->priv->fd);
426
/* This might block during the close. Doesn't seem to be a way to avoid it though. */
427
res = close (unix_stream->priv->fd);
432
g_set_error (error, G_IO_ERROR,
433
g_io_error_from_errno (errsv),
434
_("Error closing file descriptor: %s"),
420
g_set_error (error, G_IO_ERROR,
421
g_io_error_from_errno (errsv),
422
_("Error closing file descriptor: %s"),
440
426
return res != -1;
446
GAsyncReadyCallback callback;
448
GCancellable *cancellable;
449
GUnixOutputStream *stream;
453
write_async_cb (int fd,
454
GIOCondition condition,
455
WriteAsyncData *data)
457
GSimpleAsyncResult *simple;
458
GError *error = NULL;
459
gssize count_written;
463
if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
469
count_written = write (data->stream->priv->fd, data->buffer, data->count);
470
if (count_written == -1)
474
if (errsv == EINTR || errsv == EAGAIN)
477
g_set_error (&error, G_IO_ERROR,
478
g_io_error_from_errno (errsv),
479
_("Error writing to file descriptor: %s"),
485
simple = g_simple_async_result_new (G_OBJECT (data->stream),
488
g_unix_output_stream_write_async);
490
g_simple_async_result_set_op_res_gssize (simple, count_written);
492
if (count_written == -1)
493
g_simple_async_result_take_error (simple, error);
495
/* Complete immediately, not in idle, since we're already in a mainloop callout */
496
g_simple_async_result_complete (simple);
497
g_object_unref (simple);
503
g_unix_output_stream_write_async (GOutputStream *stream,
507
GCancellable *cancellable,
508
GAsyncReadyCallback callback,
512
GUnixOutputStream *unix_stream;
513
WriteAsyncData *data;
515
unix_stream = G_UNIX_OUTPUT_STREAM (stream);
517
if (!unix_stream->priv->is_pipe_or_socket)
519
G_OUTPUT_STREAM_CLASS (g_unix_output_stream_parent_class)->
520
write_async (stream, buffer, count, io_priority,
521
cancellable, callback, user_data);
525
data = g_new0 (WriteAsyncData, 1);
527
data->buffer = buffer;
528
data->callback = callback;
529
data->user_data = user_data;
530
data->cancellable = cancellable;
531
data->stream = unix_stream;
533
source = _g_fd_source_new (unix_stream->priv->fd,
536
g_source_set_name (source, "GUnixOutputStream");
538
g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
539
g_source_attach (source, g_main_context_get_thread_default ());
541
g_source_unref (source);
545
g_unix_output_stream_write_finish (GOutputStream *stream,
546
GAsyncResult *result,
549
GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
550
GSimpleAsyncResult *simple;
553
if (!unix_stream->priv->is_pipe_or_socket)
555
return G_OUTPUT_STREAM_CLASS (g_unix_output_stream_parent_class)->
556
write_finish (stream, result, error);
559
simple = G_SIMPLE_ASYNC_RESULT (result);
560
g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
562
nwritten = g_simple_async_result_get_op_res_gssize (simple);
567
GOutputStream *stream;
568
GAsyncReadyCallback callback;
573
close_async_cb (CloseAsyncData *data)
575
GUnixOutputStream *unix_stream;
576
GSimpleAsyncResult *simple;
577
GError *error = NULL;
581
unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
583
if (!unix_stream->priv->close_fd)
591
res = close (unix_stream->priv->fd);
596
g_set_error (&error, G_IO_ERROR,
597
g_io_error_from_errno (errsv),
598
_("Error closing file descriptor: %s"),
607
simple = g_simple_async_result_new (G_OBJECT (data->stream),
610
g_unix_output_stream_close_async);
613
g_simple_async_result_take_error (simple, error);
615
/* Complete immediately, not in idle, since we're already in a mainloop callout */
616
g_simple_async_result_complete (simple);
617
g_object_unref (simple);
623
g_unix_output_stream_close_async (GOutputStream *stream,
625
GCancellable *cancellable,
626
GAsyncReadyCallback callback,
630
CloseAsyncData *data;
632
data = g_new0 (CloseAsyncData, 1);
634
data->stream = stream;
635
data->callback = callback;
636
data->user_data = user_data;
638
idle = g_idle_source_new ();
639
g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
640
g_source_attach (idle, g_main_context_get_thread_default ());
641
g_source_unref (idle);
430
g_unix_output_stream_close_async (GOutputStream *stream,
432
GCancellable *cancellable,
433
GAsyncReadyCallback callback,
437
GError *error = NULL;
439
task = g_task_new (stream, cancellable, callback, user_data);
440
g_task_set_priority (task, io_priority);
442
if (g_unix_output_stream_close (stream, cancellable, &error))
443
g_task_return_boolean (task, TRUE);
445
g_task_return_error (task, error);
446
g_object_unref (task);