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 G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize (object);
115 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
117 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
118 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
120 g_type_class_add_private (klass, sizeof (GUnixOutputStreamPrivate));
122 gobject_class->get_property = g_unix_output_stream_get_property;
123 gobject_class->set_property = g_unix_output_stream_set_property;
124 gobject_class->finalize = g_unix_output_stream_finalize;
126 stream_class->write_fn = g_unix_output_stream_write;
127 stream_class->close_fn = g_unix_output_stream_close;
128 stream_class->write_async = g_unix_output_stream_write_async;
129 stream_class->write_finish = g_unix_output_stream_write_finish;
130 stream_class->close_async = g_unix_output_stream_close_async;
131 stream_class->close_finish = g_unix_output_stream_close_finish;
134 * GUnixOutputStream:fd:
136 * The file descriptor that the stream writes to.
140 g_object_class_install_property (gobject_class,
142 g_param_spec_int ("fd",
143 P_("File descriptor"),
144 P_("The file descriptor to write to"),
145 G_MININT, G_MAXINT, -1,
146 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
149 * GUnixOutputStream:close-fd:
151 * Whether to close the file descriptor when the stream is closed.
155 g_object_class_install_property (gobject_class,
157 g_param_spec_boolean ("close-fd",
158 P_("Close file descriptor"),
159 P_("Whether to close the file descriptor when the stream is closed"),
161 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
165 g_unix_output_stream_set_property (GObject *object,
170 GUnixOutputStream *unix_stream;
172 unix_stream = G_UNIX_OUTPUT_STREAM (object);
177 unix_stream->priv->fd = g_value_get_int (value);
180 unix_stream->priv->close_fd = g_value_get_boolean (value);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 g_unix_output_stream_get_property (GObject *object,
194 GUnixOutputStream *unix_stream;
196 unix_stream = G_UNIX_OUTPUT_STREAM (object);
201 g_value_set_int (value, unix_stream->priv->fd);
204 g_value_set_boolean (value, unix_stream->priv->close_fd);
207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
214 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
215 G_TYPE_UNIX_OUTPUT_STREAM,
216 GUnixOutputStreamPrivate);
218 unix_stream->priv->fd = -1;
219 unix_stream->priv->close_fd = TRUE;
223 * g_unix_output_stream_new:
224 * @fd: a UNIX file descriptor
225 * @close_fd: %TRUE to close the file descriptor when done
227 * Creates a new #GUnixOutputStream for the given @fd.
229 * If @close_fd, is %TRUE, the file descriptor will be closed when
230 * the output stream is destroyed.
232 * Returns: a new #GOutputStream
235 g_unix_output_stream_new (gint fd,
238 GUnixOutputStream *stream;
240 g_return_val_if_fail (fd != -1, NULL);
242 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM,
244 "close-fd", close_fd,
247 return G_OUTPUT_STREAM (stream);
251 * g_unix_output_stream_set_close_fd:
252 * @stream: a #GUnixOutputStream
253 * @close_fd: %TRUE to close the file descriptor when done
255 * Sets whether the file descriptor of @stream shall be closed
256 * when the stream is closed.
261 g_unix_output_stream_set_close_fd (GUnixOutputStream *stream,
264 g_return_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream));
266 close_fd = close_fd != FALSE;
267 if (stream->priv->close_fd != close_fd)
269 stream->priv->close_fd = close_fd;
270 g_object_notify (G_OBJECT (stream), "close-fd");
275 * g_unix_output_stream_get_close_fd:
276 * @stream: a #GUnixOutputStream
278 * Returns whether the file descriptor of @stream will be
279 * closed when the stream is closed.
281 * Return value: %TRUE if the file descriptor is closed when done
286 g_unix_output_stream_get_close_fd (GUnixOutputStream *stream)
288 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), FALSE);
290 return stream->priv->close_fd;
294 * g_unix_output_stream_get_fd:
295 * @stream: a #GUnixOutputStream
297 * Return the UNIX file descriptor that the stream writes to.
299 * Return value: The file descriptor of @stream
304 g_unix_output_stream_get_fd (GUnixOutputStream *stream)
306 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), -1);
308 return stream->priv->fd;
312 g_unix_output_stream_write (GOutputStream *stream,
315 GCancellable *cancellable,
318 GUnixOutputStream *unix_stream;
323 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
325 if (g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
327 poll_fds[0].fd = unix_stream->priv->fd;
328 poll_fds[0].events = G_IO_OUT;
330 poll_ret = g_poll (poll_fds, 2, -1);
331 while (poll_ret == -1 && errno == EINTR);
332 g_cancellable_release_fd (cancellable);
338 g_set_error (error, G_IO_ERROR,
339 g_io_error_from_errno (errsv),
340 _("Error writing to unix: %s"),
348 if (g_cancellable_set_error_if_cancelled (cancellable, error))
351 res = write (unix_stream->priv->fd, buffer, count);
359 g_set_error (error, G_IO_ERROR,
360 g_io_error_from_errno (errsv),
361 _("Error writing to unix: %s"),
372 g_unix_output_stream_close (GOutputStream *stream,
373 GCancellable *cancellable,
376 GUnixOutputStream *unix_stream;
379 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
381 if (!unix_stream->priv->close_fd)
386 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
387 res = close (unix_stream->priv->fd);
392 g_set_error (error, G_IO_ERROR,
393 g_io_error_from_errno (errsv),
394 _("Error closing unix: %s"),
406 GAsyncReadyCallback callback;
408 GCancellable *cancellable;
409 GUnixOutputStream *stream;
413 write_async_cb (WriteAsyncData *data,
414 GIOCondition condition,
417 GSimpleAsyncResult *simple;
418 GError *error = NULL;
419 gssize count_written;
423 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
429 count_written = write (data->stream->priv->fd, data->buffer, data->count);
430 if (count_written == -1)
437 g_set_error (&error, G_IO_ERROR,
438 g_io_error_from_errno (errsv),
439 _("Error reading from unix: %s"),
445 simple = g_simple_async_result_new (G_OBJECT (data->stream),
448 g_unix_output_stream_write_async);
450 g_simple_async_result_set_op_res_gssize (simple, count_written);
452 if (count_written == -1)
454 g_simple_async_result_set_from_error (simple, error);
455 g_error_free (error);
458 /* Complete immediately, not in idle, since we're already in a mainloop callout */
459 g_simple_async_result_complete (simple);
460 g_object_unref (simple);
466 g_unix_output_stream_write_async (GOutputStream *stream,
470 GCancellable *cancellable,
471 GAsyncReadyCallback callback,
475 GUnixOutputStream *unix_stream;
476 WriteAsyncData *data;
478 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
480 data = g_new0 (WriteAsyncData, 1);
482 data->buffer = buffer;
483 data->callback = callback;
484 data->user_data = user_data;
485 data->cancellable = cancellable;
486 data->stream = unix_stream;
488 source = _g_fd_source_new (unix_stream->priv->fd,
492 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
493 g_source_attach (source, g_main_context_get_thread_default ());
495 g_source_unref (source);
499 g_unix_output_stream_write_finish (GOutputStream *stream,
500 GAsyncResult *result,
503 GSimpleAsyncResult *simple;
506 simple = G_SIMPLE_ASYNC_RESULT (result);
507 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
509 nwritten = g_simple_async_result_get_op_res_gssize (simple);
514 GOutputStream *stream;
515 GAsyncReadyCallback callback;
520 close_async_cb (CloseAsyncData *data)
522 GUnixOutputStream *unix_stream;
523 GSimpleAsyncResult *simple;
524 GError *error = NULL;
528 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
530 if (!unix_stream->priv->close_fd)
538 res = close (unix_stream->priv->fd);
543 g_set_error (&error, G_IO_ERROR,
544 g_io_error_from_errno (errsv),
545 _("Error closing unix: %s"),
554 simple = g_simple_async_result_new (G_OBJECT (data->stream),
557 g_unix_output_stream_close_async);
561 g_simple_async_result_set_from_error (simple, error);
562 g_error_free (error);
565 /* Complete immediately, not in idle, since we're already in a mainloop callout */
566 g_simple_async_result_complete (simple);
567 g_object_unref (simple);
573 g_unix_output_stream_close_async (GOutputStream *stream,
575 GCancellable *cancellable,
576 GAsyncReadyCallback callback,
580 CloseAsyncData *data;
582 data = g_new0 (CloseAsyncData, 1);
584 data->stream = stream;
585 data->callback = callback;
586 data->user_data = user_data;
588 idle = g_idle_source_new ();
589 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
590 g_source_attach (idle, g_main_context_get_thread_default ());
591 g_source_unref (idle);
595 g_unix_output_stream_close_finish (GOutputStream *stream,
596 GAsyncResult *result,
599 /* Failures handled in generic close_finish code */
603 #define __G_UNIX_OUTPUT_STREAM_C__
604 #include "gioaliasdef.c"