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"
43 * SECTION:gunixoutputstream
44 * @short_description: Streaming output operations for UNIX file descriptors
45 * @include: gio/gunixoutputstream.h
46 * @see_also: #GOutputStream
48 * #GUnixOutputStream implements #GOutputStream for writing to a
49 * UNIX file descriptor, including asynchronous operations. The file
50 * descriptor must be selectable, so it doesn't work with opened files.
52 * Note that <filename><gio/gunixoutputstream.h></filename> belongs
53 * to the UNIX-specific GIO interfaces, thus you have to use the
54 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
63 G_DEFINE_TYPE (GUnixOutputStream, g_unix_output_stream, G_TYPE_OUTPUT_STREAM);
66 struct _GUnixOutputStreamPrivate {
71 static void g_unix_output_stream_set_property (GObject *object,
75 static void g_unix_output_stream_get_property (GObject *object,
79 static gssize g_unix_output_stream_write (GOutputStream *stream,
82 GCancellable *cancellable,
84 static gboolean g_unix_output_stream_close (GOutputStream *stream,
85 GCancellable *cancellable,
87 static void g_unix_output_stream_write_async (GOutputStream *stream,
91 GCancellable *cancellable,
92 GAsyncReadyCallback callback,
94 static gssize g_unix_output_stream_write_finish (GOutputStream *stream,
97 static void g_unix_output_stream_close_async (GOutputStream *stream,
99 GCancellable *cancellable,
100 GAsyncReadyCallback callback,
102 static gboolean g_unix_output_stream_close_finish (GOutputStream *stream,
103 GAsyncResult *result,
108 g_unix_output_stream_finalize (GObject *object)
110 G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize (object);
114 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
116 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
117 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
119 g_type_class_add_private (klass, sizeof (GUnixOutputStreamPrivate));
121 gobject_class->get_property = g_unix_output_stream_get_property;
122 gobject_class->set_property = g_unix_output_stream_set_property;
123 gobject_class->finalize = g_unix_output_stream_finalize;
125 stream_class->write_fn = g_unix_output_stream_write;
126 stream_class->close_fn = g_unix_output_stream_close;
127 stream_class->write_async = g_unix_output_stream_write_async;
128 stream_class->write_finish = g_unix_output_stream_write_finish;
129 stream_class->close_async = g_unix_output_stream_close_async;
130 stream_class->close_finish = g_unix_output_stream_close_finish;
133 * GUnixOutputStream:fd:
135 * The file descriptor that the stream writes to.
139 g_object_class_install_property (gobject_class,
141 g_param_spec_int ("fd",
142 P_("File descriptor"),
143 P_("The file descriptor to write to"),
144 G_MININT, G_MAXINT, -1,
145 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
148 * GUnixOutputStream:close-fd:
150 * Whether to close the file descriptor when the stream is closed.
154 g_object_class_install_property (gobject_class,
156 g_param_spec_boolean ("close-fd",
157 P_("Close file descriptor"),
158 P_("Whether to close the file descriptor when the stream is closed"),
160 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
164 g_unix_output_stream_set_property (GObject *object,
169 GUnixOutputStream *unix_stream;
171 unix_stream = G_UNIX_OUTPUT_STREAM (object);
176 unix_stream->priv->fd = g_value_get_int (value);
179 unix_stream->priv->close_fd = g_value_get_boolean (value);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 g_unix_output_stream_get_property (GObject *object,
193 GUnixOutputStream *unix_stream;
195 unix_stream = G_UNIX_OUTPUT_STREAM (object);
200 g_value_set_int (value, unix_stream->priv->fd);
203 g_value_set_boolean (value, unix_stream->priv->close_fd);
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
211 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
213 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
214 G_TYPE_UNIX_OUTPUT_STREAM,
215 GUnixOutputStreamPrivate);
217 unix_stream->priv->fd = -1;
218 unix_stream->priv->close_fd = TRUE;
222 * g_unix_output_stream_new:
223 * @fd: a UNIX file descriptor
224 * @close_fd: %TRUE to close the file descriptor when done
226 * Creates a new #GUnixOutputStream for the given @fd.
228 * If @close_fd, is %TRUE, the file descriptor will be closed when
229 * the output stream is destroyed.
231 * Returns: a new #GOutputStream
234 g_unix_output_stream_new (gint fd,
237 GUnixOutputStream *stream;
239 g_return_val_if_fail (fd != -1, NULL);
241 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM,
243 "close-fd", close_fd,
246 return G_OUTPUT_STREAM (stream);
250 * g_unix_output_stream_set_close_fd:
251 * @stream: a #GUnixOutputStream
252 * @close_fd: %TRUE to close the file descriptor when done
254 * Sets whether the file descriptor of @stream shall be closed
255 * when the stream is closed.
260 g_unix_output_stream_set_close_fd (GUnixOutputStream *stream,
263 g_return_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream));
265 close_fd = close_fd != FALSE;
266 if (stream->priv->close_fd != close_fd)
268 stream->priv->close_fd = close_fd;
269 g_object_notify (G_OBJECT (stream), "close-fd");
274 * g_unix_output_stream_get_close_fd:
275 * @stream: a #GUnixOutputStream
277 * Returns whether the file descriptor of @stream will be
278 * closed when the stream is closed.
280 * Return value: %TRUE if the file descriptor is closed when done
285 g_unix_output_stream_get_close_fd (GUnixOutputStream *stream)
287 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), FALSE);
289 return stream->priv->close_fd;
293 * g_unix_output_stream_get_fd:
294 * @stream: a #GUnixOutputStream
296 * Return the UNIX file descriptor that the stream writes to.
298 * Return value: The file descriptor of @stream
303 g_unix_output_stream_get_fd (GUnixOutputStream *stream)
305 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), -1);
307 return stream->priv->fd;
311 g_unix_output_stream_write (GOutputStream *stream,
314 GCancellable *cancellable,
317 GUnixOutputStream *unix_stream;
322 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
324 if (g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
326 poll_fds[0].fd = unix_stream->priv->fd;
327 poll_fds[0].events = G_IO_OUT;
329 poll_ret = g_poll (poll_fds, 2, -1);
330 while (poll_ret == -1 && errno == EINTR);
331 g_cancellable_release_fd (cancellable);
337 g_set_error (error, G_IO_ERROR,
338 g_io_error_from_errno (errsv),
339 _("Error writing to unix: %s"),
347 if (g_cancellable_set_error_if_cancelled (cancellable, error))
350 res = write (unix_stream->priv->fd, buffer, count);
358 g_set_error (error, G_IO_ERROR,
359 g_io_error_from_errno (errsv),
360 _("Error writing to unix: %s"),
371 g_unix_output_stream_close (GOutputStream *stream,
372 GCancellable *cancellable,
375 GUnixOutputStream *unix_stream;
378 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
380 if (!unix_stream->priv->close_fd)
385 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
386 res = close (unix_stream->priv->fd);
391 g_set_error (error, G_IO_ERROR,
392 g_io_error_from_errno (errsv),
393 _("Error closing unix: %s"),
405 GAsyncReadyCallback callback;
407 GCancellable *cancellable;
408 GUnixOutputStream *stream;
412 write_async_cb (int fd,
413 GIOCondition condition,
414 WriteAsyncData *data)
416 GSimpleAsyncResult *simple;
417 GError *error = NULL;
418 gssize count_written;
422 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
428 count_written = write (data->stream->priv->fd, data->buffer, data->count);
429 if (count_written == -1)
436 g_set_error (&error, G_IO_ERROR,
437 g_io_error_from_errno (errsv),
438 _("Error writing to unix: %s"),
444 simple = g_simple_async_result_new (G_OBJECT (data->stream),
447 g_unix_output_stream_write_async);
449 g_simple_async_result_set_op_res_gssize (simple, count_written);
451 if (count_written == -1)
452 g_simple_async_result_take_error (simple, error);
454 /* Complete immediately, not in idle, since we're already in a mainloop callout */
455 g_simple_async_result_complete (simple);
456 g_object_unref (simple);
462 g_unix_output_stream_write_async (GOutputStream *stream,
466 GCancellable *cancellable,
467 GAsyncReadyCallback callback,
471 GUnixOutputStream *unix_stream;
472 WriteAsyncData *data;
474 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
476 data = g_new0 (WriteAsyncData, 1);
478 data->buffer = buffer;
479 data->callback = callback;
480 data->user_data = user_data;
481 data->cancellable = cancellable;
482 data->stream = unix_stream;
484 source = _g_fd_source_new (unix_stream->priv->fd,
487 g_source_set_name (source, "GUnixOutputStream");
489 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
490 g_source_attach (source, g_main_context_get_thread_default ());
492 g_source_unref (source);
496 g_unix_output_stream_write_finish (GOutputStream *stream,
497 GAsyncResult *result,
500 GSimpleAsyncResult *simple;
503 simple = G_SIMPLE_ASYNC_RESULT (result);
504 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
506 nwritten = g_simple_async_result_get_op_res_gssize (simple);
511 GOutputStream *stream;
512 GAsyncReadyCallback callback;
517 close_async_cb (CloseAsyncData *data)
519 GUnixOutputStream *unix_stream;
520 GSimpleAsyncResult *simple;
521 GError *error = NULL;
525 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
527 if (!unix_stream->priv->close_fd)
535 res = close (unix_stream->priv->fd);
540 g_set_error (&error, G_IO_ERROR,
541 g_io_error_from_errno (errsv),
542 _("Error closing unix: %s"),
551 simple = g_simple_async_result_new (G_OBJECT (data->stream),
554 g_unix_output_stream_close_async);
557 g_simple_async_result_take_error (simple, error);
559 /* Complete immediately, not in idle, since we're already in a mainloop callout */
560 g_simple_async_result_complete (simple);
561 g_object_unref (simple);
567 g_unix_output_stream_close_async (GOutputStream *stream,
569 GCancellable *cancellable,
570 GAsyncReadyCallback callback,
574 CloseAsyncData *data;
576 data = g_new0 (CloseAsyncData, 1);
578 data->stream = stream;
579 data->callback = callback;
580 data->user_data = user_data;
582 idle = g_idle_source_new ();
583 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
584 g_source_attach (idle, g_main_context_get_thread_default ());
585 g_source_unref (idle);
589 g_unix_output_stream_close_finish (GOutputStream *stream,
590 GAsyncResult *result,
593 /* Failures handled in generic close_finish code */