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.
54 G_DEFINE_TYPE (GUnixOutputStream, g_unix_output_stream, G_TYPE_OUTPUT_STREAM);
57 struct _GUnixOutputStreamPrivate {
59 gboolean close_fd_at_close;
62 static gssize g_unix_output_stream_write (GOutputStream *stream,
65 GCancellable *cancellable,
67 static gboolean g_unix_output_stream_close (GOutputStream *stream,
68 GCancellable *cancellable,
70 static void g_unix_output_stream_write_async (GOutputStream *stream,
74 GCancellable *cancellable,
75 GAsyncReadyCallback callback,
77 static gssize g_unix_output_stream_write_finish (GOutputStream *stream,
80 static void g_unix_output_stream_close_async (GOutputStream *stream,
82 GCancellable *cancellable,
83 GAsyncReadyCallback callback,
85 static gboolean g_unix_output_stream_close_finish (GOutputStream *stream,
91 g_unix_output_stream_finalize (GObject *object)
93 GUnixOutputStream *stream;
95 stream = G_UNIX_OUTPUT_STREAM (object);
97 G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize (object);
101 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
103 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
104 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
106 g_type_class_add_private (klass, sizeof (GUnixOutputStreamPrivate));
108 gobject_class->finalize = g_unix_output_stream_finalize;
110 stream_class->write_fn = g_unix_output_stream_write;
111 stream_class->close_fn = g_unix_output_stream_close;
112 stream_class->write_async = g_unix_output_stream_write_async;
113 stream_class->write_finish = g_unix_output_stream_write_finish;
114 stream_class->close_async = g_unix_output_stream_close_async;
115 stream_class->close_finish = g_unix_output_stream_close_finish;
119 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
121 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
122 G_TYPE_UNIX_OUTPUT_STREAM,
123 GUnixOutputStreamPrivate);
128 * g_unix_output_stream_new:
129 * @fd: unix's file descriptor.
130 * @close_fd_at_close: a #gboolean.
132 * Creates a new unix output stream for @fd. If @close_fd_at_close
133 * is %TRUE, the fd will be closed when the output stream is destroyed.
135 * Returns: #GOutputStream. If @close_fd_at_close is %TRUE, then
136 * @fd will be closed when the #GOutputStream is closed.
139 g_unix_output_stream_new (int fd,
140 gboolean close_fd_at_close)
142 GUnixOutputStream *stream;
144 g_return_val_if_fail (fd != -1, NULL);
146 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM, NULL);
148 stream->priv->fd = fd;
149 stream->priv->close_fd_at_close = close_fd_at_close;
151 return G_OUTPUT_STREAM (stream);
155 g_unix_output_stream_write (GOutputStream *stream,
158 GCancellable *cancellable,
161 GUnixOutputStream *unix_stream;
166 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
170 poll_fds[0].fd = unix_stream->priv->fd;
171 poll_fds[0].events = G_IO_OUT;
172 g_cancellable_make_pollfd (cancellable, &poll_fds[1]);
174 poll_ret = g_poll (poll_fds, 2, -1);
175 while (poll_ret == -1 && errno == EINTR);
181 g_set_error (error, G_IO_ERROR,
182 g_io_error_from_errno (errsv),
183 _("Error writing to unix: %s"),
191 if (g_cancellable_set_error_if_cancelled (cancellable, error))
194 res = write (unix_stream->priv->fd, buffer, count);
202 g_set_error (error, G_IO_ERROR,
203 g_io_error_from_errno (errsv),
204 _("Error writing to unix: %s"),
215 g_unix_output_stream_close (GOutputStream *stream,
216 GCancellable *cancellable,
219 GUnixOutputStream *unix_stream;
222 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
224 if (!unix_stream->priv->close_fd_at_close)
229 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
230 res = close (unix_stream->priv->fd);
235 g_set_error (error, G_IO_ERROR,
236 g_io_error_from_errno (errsv),
237 _("Error closing unix: %s"),
249 GAsyncReadyCallback callback;
251 GCancellable *cancellable;
252 GUnixOutputStream *stream;
256 write_async_cb (WriteAsyncData *data,
257 GIOCondition condition,
260 GSimpleAsyncResult *simple;
261 GError *error = NULL;
262 gssize count_written;
266 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
272 count_written = write (data->stream->priv->fd, data->buffer, data->count);
273 if (count_written == -1)
280 g_set_error (&error, G_IO_ERROR,
281 g_io_error_from_errno (errsv),
282 _("Error reading from unix: %s"),
288 simple = g_simple_async_result_new (G_OBJECT (data->stream),
291 g_unix_output_stream_write_async);
293 g_simple_async_result_set_op_res_gssize (simple, count_written);
295 if (count_written == -1)
297 g_simple_async_result_set_from_error (simple, error);
298 g_error_free (error);
301 /* Complete immediately, not in idle, since we're already in a mainloop callout */
302 g_simple_async_result_complete (simple);
303 g_object_unref (simple);
309 g_unix_output_stream_write_async (GOutputStream *stream,
313 GCancellable *cancellable,
314 GAsyncReadyCallback callback,
318 GUnixOutputStream *unix_stream;
319 WriteAsyncData *data;
321 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
323 data = g_new0 (WriteAsyncData, 1);
325 data->buffer = buffer;
326 data->callback = callback;
327 data->user_data = user_data;
328 data->cancellable = cancellable;
329 data->stream = unix_stream;
331 source = _g_fd_source_new (unix_stream->priv->fd,
335 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
336 g_source_attach (source, NULL);
338 g_source_unref (source);
342 g_unix_output_stream_write_finish (GOutputStream *stream,
343 GAsyncResult *result,
346 GSimpleAsyncResult *simple;
349 simple = G_SIMPLE_ASYNC_RESULT (result);
350 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
352 nwritten = g_simple_async_result_get_op_res_gssize (simple);
357 GOutputStream *stream;
358 GAsyncReadyCallback callback;
363 close_async_cb (CloseAsyncData *data)
365 GUnixOutputStream *unix_stream;
366 GSimpleAsyncResult *simple;
367 GError *error = NULL;
371 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
373 if (!unix_stream->priv->close_fd_at_close)
381 res = close (unix_stream->priv->fd);
386 g_set_error (&error, G_IO_ERROR,
387 g_io_error_from_errno (errsv),
388 _("Error closing unix: %s"),
397 simple = g_simple_async_result_new (G_OBJECT (data->stream),
400 g_unix_output_stream_close_async);
404 g_simple_async_result_set_from_error (simple, error);
405 g_error_free (error);
408 /* Complete immediately, not in idle, since we're already in a mainloop callout */
409 g_simple_async_result_complete (simple);
410 g_object_unref (simple);
416 g_unix_output_stream_close_async (GOutputStream *stream,
418 GCancellable *cancellable,
419 GAsyncReadyCallback callback,
423 CloseAsyncData *data;
425 data = g_new0 (CloseAsyncData, 1);
427 data->stream = stream;
428 data->callback = callback;
429 data->user_data = user_data;
431 idle = g_idle_source_new ();
432 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
433 g_source_attach (idle, NULL);
434 g_source_unref (idle);
438 g_unix_output_stream_close_finish (GOutputStream *stream,
439 GAsyncResult *result,
442 /* Failures handled in generic close_finish code */
446 #define __G_UNIX_OUTPUT_STREAM_C__
447 #include "gioaliasdef.c"