1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
33 #include <glib/gstdio.h>
35 #include "gunixoutputstream.h"
36 #include "gcancellable.h"
37 #include "gsimpleasyncresult.h"
38 #include "gasynchelper.h"
44 * SECTION:gunixoutputstream
45 * @short_description: Streaming output operations for Unix file descriptors
46 * @include: gio/gunixoutputstream.h
47 * @see_also: #GOutputStream
49 * #GUnixOutputStream implements #GOutputStream for writing to a
50 * unix file descriptor, including asynchronous operations. The file
51 * descriptor must be selectable, so it doesn't work with opened files.
53 * Note that <filename><gio/gunixoutputstream.h></filename> belongs
54 * to the UNIX-specific GIO interfaces, thus you have to use the
55 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
64 G_DEFINE_TYPE (GUnixOutputStream, g_unix_output_stream, G_TYPE_OUTPUT_STREAM);
67 struct _GUnixOutputStreamPrivate {
72 static void g_unix_output_stream_set_property (GObject *object,
76 static void g_unix_output_stream_get_property (GObject *object,
80 static gssize g_unix_output_stream_write (GOutputStream *stream,
83 GCancellable *cancellable,
85 static gboolean g_unix_output_stream_close (GOutputStream *stream,
86 GCancellable *cancellable,
88 static void g_unix_output_stream_write_async (GOutputStream *stream,
92 GCancellable *cancellable,
93 GAsyncReadyCallback callback,
95 static gssize g_unix_output_stream_write_finish (GOutputStream *stream,
98 static void g_unix_output_stream_close_async (GOutputStream *stream,
100 GCancellable *cancellable,
101 GAsyncReadyCallback callback,
103 static gboolean g_unix_output_stream_close_finish (GOutputStream *stream,
104 GAsyncResult *result,
109 g_unix_output_stream_finalize (GObject *object)
111 GUnixOutputStream *stream;
113 stream = G_UNIX_OUTPUT_STREAM (object);
115 G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize (object);
119 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
121 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
122 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
124 g_type_class_add_private (klass, sizeof (GUnixOutputStreamPrivate));
126 gobject_class->get_property = g_unix_output_stream_get_property;
127 gobject_class->set_property = g_unix_output_stream_set_property;
128 gobject_class->finalize = g_unix_output_stream_finalize;
130 stream_class->write_fn = g_unix_output_stream_write;
131 stream_class->close_fn = g_unix_output_stream_close;
132 stream_class->write_async = g_unix_output_stream_write_async;
133 stream_class->write_finish = g_unix_output_stream_write_finish;
134 stream_class->close_async = g_unix_output_stream_close_async;
135 stream_class->close_finish = g_unix_output_stream_close_finish;
138 * GUnixOutputStream:fd:
140 * The file descriptor that the stream writes to.
144 g_object_class_install_property (gobject_class,
146 g_param_spec_int ("fd",
147 P_("File descriptor"),
148 P_("The file descriptor to write to"),
149 G_MININT, G_MAXINT, -1,
150 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
153 * GUnixOutputStream:close-fd:
155 * Whether to close the file descriptor when the stream is closed.
159 g_object_class_install_property (gobject_class,
161 g_param_spec_boolean ("close-fd",
162 P_("Close file descriptor"),
163 P_("Whether to close the file descriptor when the stream is closed"),
165 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
169 g_unix_output_stream_set_property (GObject *object,
174 GUnixOutputStream *unix_stream;
176 unix_stream = G_UNIX_OUTPUT_STREAM (object);
181 unix_stream->priv->fd = g_value_get_int (value);
184 unix_stream->priv->close_fd = g_value_get_boolean (value);
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193 g_unix_output_stream_get_property (GObject *object,
198 GUnixOutputStream *unix_stream;
200 unix_stream = G_UNIX_OUTPUT_STREAM (object);
205 g_value_set_int (value, unix_stream->priv->fd);
208 g_value_set_boolean (value, unix_stream->priv->close_fd);
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
218 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
219 G_TYPE_UNIX_OUTPUT_STREAM,
220 GUnixOutputStreamPrivate);
222 unix_stream->priv->fd = -1;
223 unix_stream->priv->close_fd = TRUE;
227 * g_unix_output_stream_new:
228 * @fd: a UNIX file descriptor
229 * @close_fd: %TRUE to close the file descriptor when done
231 * Creates a new #GUnixOutputStream for the given @fd.
233 * If @close_fd, is %TRUE, the file descriptor will be closed when
234 * the output stream is destroyed.
236 * Returns: a new #GOutputStream
239 g_unix_output_stream_new (gint fd,
242 GUnixOutputStream *stream;
244 g_return_val_if_fail (fd != -1, NULL);
246 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM,
248 "close-fd", close_fd,
251 return G_OUTPUT_STREAM (stream);
255 * g_unix_output_stream_set_close_fd:
256 * @stream: a #GUnixOutputStream
257 * @close_fd: %TRUE to close the file descriptor when done
259 * Sets whether the file descriptor of @stream shall be closed
260 * when the stream is closed.
265 g_unix_output_stream_set_close_fd (GUnixOutputStream *stream,
268 g_return_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream));
270 close_fd = close_fd != FALSE;
271 if (stream->priv->close_fd != close_fd)
273 stream->priv->close_fd = close_fd;
274 g_object_notify (G_OBJECT (stream), "close-fd");
279 * g_unix_output_stream_get_close_fd:
280 * @stream: a #GUnixOutputStream
282 * Returns whether the file descriptor of @stream will be
283 * closed when the stream is closed.
285 * Return value: %TRUE if the file descriptor is closed when done
290 g_unix_output_stream_get_close_fd (GUnixOutputStream *stream)
292 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), FALSE);
294 return stream->priv->close_fd;
298 * g_unix_output_stream_get_fd:
299 * @stream: a #GUnixOutputStream
301 * Return the UNIX file descriptor that the stream writes to.
303 * Return value: The file descriptor of @stream
308 g_unix_output_stream_get_fd (GUnixOutputStream *stream)
310 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), -1);
312 return stream->priv->fd;
316 g_unix_output_stream_write (GOutputStream *stream,
319 GCancellable *cancellable,
322 GUnixOutputStream *unix_stream;
327 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
329 if (g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
331 poll_fds[0].fd = unix_stream->priv->fd;
332 poll_fds[0].events = G_IO_OUT;
334 poll_ret = g_poll (poll_fds, 2, -1);
335 while (poll_ret == -1 && errno == EINTR);
341 g_set_error (error, G_IO_ERROR,
342 g_io_error_from_errno (errsv),
343 _("Error writing to unix: %s"),
351 if (g_cancellable_set_error_if_cancelled (cancellable, error))
354 res = write (unix_stream->priv->fd, buffer, count);
362 g_set_error (error, G_IO_ERROR,
363 g_io_error_from_errno (errsv),
364 _("Error writing to unix: %s"),
375 g_unix_output_stream_close (GOutputStream *stream,
376 GCancellable *cancellable,
379 GUnixOutputStream *unix_stream;
382 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
384 if (!unix_stream->priv->close_fd)
389 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
390 res = close (unix_stream->priv->fd);
395 g_set_error (error, G_IO_ERROR,
396 g_io_error_from_errno (errsv),
397 _("Error closing unix: %s"),
409 GAsyncReadyCallback callback;
411 GCancellable *cancellable;
412 GUnixOutputStream *stream;
416 write_async_cb (WriteAsyncData *data,
417 GIOCondition condition,
420 GSimpleAsyncResult *simple;
421 GError *error = NULL;
422 gssize count_written;
426 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
432 count_written = write (data->stream->priv->fd, data->buffer, data->count);
433 if (count_written == -1)
440 g_set_error (&error, G_IO_ERROR,
441 g_io_error_from_errno (errsv),
442 _("Error reading from unix: %s"),
448 simple = g_simple_async_result_new (G_OBJECT (data->stream),
451 g_unix_output_stream_write_async);
453 g_simple_async_result_set_op_res_gssize (simple, count_written);
455 if (count_written == -1)
457 g_simple_async_result_set_from_error (simple, error);
458 g_error_free (error);
461 /* Complete immediately, not in idle, since we're already in a mainloop callout */
462 g_simple_async_result_complete (simple);
463 g_object_unref (simple);
469 g_unix_output_stream_write_async (GOutputStream *stream,
473 GCancellable *cancellable,
474 GAsyncReadyCallback callback,
478 GUnixOutputStream *unix_stream;
479 WriteAsyncData *data;
481 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
483 data = g_new0 (WriteAsyncData, 1);
485 data->buffer = buffer;
486 data->callback = callback;
487 data->user_data = user_data;
488 data->cancellable = cancellable;
489 data->stream = unix_stream;
491 source = _g_fd_source_new (unix_stream->priv->fd,
495 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
496 g_source_attach (source, g_main_context_get_thread_default ());
498 g_source_unref (source);
502 g_unix_output_stream_write_finish (GOutputStream *stream,
503 GAsyncResult *result,
506 GSimpleAsyncResult *simple;
509 simple = G_SIMPLE_ASYNC_RESULT (result);
510 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
512 nwritten = g_simple_async_result_get_op_res_gssize (simple);
517 GOutputStream *stream;
518 GAsyncReadyCallback callback;
523 close_async_cb (CloseAsyncData *data)
525 GUnixOutputStream *unix_stream;
526 GSimpleAsyncResult *simple;
527 GError *error = NULL;
531 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
533 if (!unix_stream->priv->close_fd)
541 res = close (unix_stream->priv->fd);
546 g_set_error (&error, G_IO_ERROR,
547 g_io_error_from_errno (errsv),
548 _("Error closing unix: %s"),
557 simple = g_simple_async_result_new (G_OBJECT (data->stream),
560 g_unix_output_stream_close_async);
564 g_simple_async_result_set_from_error (simple, error);
565 g_error_free (error);
568 /* Complete immediately, not in idle, since we're already in a mainloop callout */
569 g_simple_async_result_complete (simple);
570 g_object_unref (simple);
576 g_unix_output_stream_close_async (GOutputStream *stream,
578 GCancellable *cancellable,
579 GAsyncReadyCallback callback,
583 CloseAsyncData *data;
585 data = g_new0 (CloseAsyncData, 1);
587 data->stream = stream;
588 data->callback = callback;
589 data->user_data = user_data;
591 idle = g_idle_source_new ();
592 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
593 g_source_attach (idle, g_main_context_get_thread_default ());
594 g_source_unref (idle);
598 g_unix_output_stream_close_finish (GOutputStream *stream,
599 GAsyncResult *result,
602 /* Failures handled in generic close_finish code */
606 #define __G_UNIX_OUTPUT_STREAM_C__
607 #include "gioaliasdef.c"