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);
331 poll_fds[0].fd = unix_stream->priv->fd;
332 poll_fds[0].events = G_IO_OUT;
333 g_cancellable_make_pollfd (cancellable, &poll_fds[1]);
335 poll_ret = g_poll (poll_fds, 2, -1);
336 while (poll_ret == -1 && errno == EINTR);
342 g_set_error (error, G_IO_ERROR,
343 g_io_error_from_errno (errsv),
344 _("Error writing to unix: %s"),
352 if (g_cancellable_set_error_if_cancelled (cancellable, error))
355 res = write (unix_stream->priv->fd, buffer, count);
363 g_set_error (error, G_IO_ERROR,
364 g_io_error_from_errno (errsv),
365 _("Error writing to unix: %s"),
376 g_unix_output_stream_close (GOutputStream *stream,
377 GCancellable *cancellable,
380 GUnixOutputStream *unix_stream;
383 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
385 if (!unix_stream->priv->close_fd)
390 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
391 res = close (unix_stream->priv->fd);
396 g_set_error (error, G_IO_ERROR,
397 g_io_error_from_errno (errsv),
398 _("Error closing unix: %s"),
410 GAsyncReadyCallback callback;
412 GCancellable *cancellable;
413 GUnixOutputStream *stream;
417 write_async_cb (WriteAsyncData *data,
418 GIOCondition condition,
421 GSimpleAsyncResult *simple;
422 GError *error = NULL;
423 gssize count_written;
427 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
433 count_written = write (data->stream->priv->fd, data->buffer, data->count);
434 if (count_written == -1)
441 g_set_error (&error, G_IO_ERROR,
442 g_io_error_from_errno (errsv),
443 _("Error reading from unix: %s"),
449 simple = g_simple_async_result_new (G_OBJECT (data->stream),
452 g_unix_output_stream_write_async);
454 g_simple_async_result_set_op_res_gssize (simple, count_written);
456 if (count_written == -1)
458 g_simple_async_result_set_from_error (simple, error);
459 g_error_free (error);
462 /* Complete immediately, not in idle, since we're already in a mainloop callout */
463 g_simple_async_result_complete (simple);
464 g_object_unref (simple);
470 g_unix_output_stream_write_async (GOutputStream *stream,
474 GCancellable *cancellable,
475 GAsyncReadyCallback callback,
479 GUnixOutputStream *unix_stream;
480 WriteAsyncData *data;
482 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
484 data = g_new0 (WriteAsyncData, 1);
486 data->buffer = buffer;
487 data->callback = callback;
488 data->user_data = user_data;
489 data->cancellable = cancellable;
490 data->stream = unix_stream;
492 source = _g_fd_source_new (unix_stream->priv->fd,
496 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
497 g_source_attach (source, g_main_context_get_thread_default ());
499 g_source_unref (source);
503 g_unix_output_stream_write_finish (GOutputStream *stream,
504 GAsyncResult *result,
507 GSimpleAsyncResult *simple;
510 simple = G_SIMPLE_ASYNC_RESULT (result);
511 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
513 nwritten = g_simple_async_result_get_op_res_gssize (simple);
518 GOutputStream *stream;
519 GAsyncReadyCallback callback;
524 close_async_cb (CloseAsyncData *data)
526 GUnixOutputStream *unix_stream;
527 GSimpleAsyncResult *simple;
528 GError *error = NULL;
532 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
534 if (!unix_stream->priv->close_fd)
542 res = close (unix_stream->priv->fd);
547 g_set_error (&error, G_IO_ERROR,
548 g_io_error_from_errno (errsv),
549 _("Error closing unix: %s"),
558 simple = g_simple_async_result_new (G_OBJECT (data->stream),
561 g_unix_output_stream_close_async);
565 g_simple_async_result_set_from_error (simple, error);
566 g_error_free (error);
569 /* Complete immediately, not in idle, since we're already in a mainloop callout */
570 g_simple_async_result_complete (simple);
571 g_object_unref (simple);
577 g_unix_output_stream_close_async (GOutputStream *stream,
579 GCancellable *cancellable,
580 GAsyncReadyCallback callback,
584 CloseAsyncData *data;
586 data = g_new0 (CloseAsyncData, 1);
588 data->stream = stream;
589 data->callback = callback;
590 data->user_data = user_data;
592 idle = g_idle_source_new ();
593 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
594 g_source_attach (idle, g_main_context_get_thread_default ());
595 g_source_unref (idle);
599 g_unix_output_stream_close_finish (GOutputStream *stream,
600 GAsyncResult *result,
603 /* Failures handled in generic close_finish code */
607 #define __G_UNIX_OUTPUT_STREAM_C__
608 #include "gioaliasdef.c"