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>
34 #include <glib/gstdio.h>
36 #include "gunixoutputstream.h"
37 #include "gcancellable.h"
38 #include "gsimpleasyncresult.h"
39 #include "gasynchelper.h"
43 * SECTION:gunixoutputstream
44 * @short_description: Unix Output Stream
45 * @see_also: #GOutputStream.
47 * #GUnixOutputStream implements #GOutputStream for writing to a a
48 * unix file descriptor, including asynchronous operations. The file
49 * descriptor much be selectable, so it doesn't work with opened files.
52 G_DEFINE_TYPE (GUnixOutputStream, g_unix_output_stream, G_TYPE_OUTPUT_STREAM);
55 struct _GUnixOutputStreamPrivate {
57 gboolean close_fd_at_close;
60 static gssize g_unix_output_stream_write (GOutputStream *stream,
63 GCancellable *cancellable,
65 static gboolean g_unix_output_stream_close (GOutputStream *stream,
66 GCancellable *cancellable,
68 static void g_unix_output_stream_write_async (GOutputStream *stream,
72 GCancellable *cancellable,
73 GAsyncReadyCallback callback,
75 static gssize g_unix_output_stream_write_finish (GOutputStream *stream,
78 static void g_unix_output_stream_close_async (GOutputStream *stream,
80 GCancellable *cancellable,
81 GAsyncReadyCallback callback,
83 static gboolean g_unix_output_stream_close_finish (GOutputStream *stream,
89 g_unix_output_stream_finalize (GObject *object)
91 GUnixOutputStream *stream;
93 stream = G_UNIX_OUTPUT_STREAM (object);
95 if (G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize)
96 (*G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize) (object);
100 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
102 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
103 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
105 g_type_class_add_private (klass, sizeof (GUnixOutputStreamPrivate));
107 gobject_class->finalize = g_unix_output_stream_finalize;
109 stream_class->write = g_unix_output_stream_write;
110 stream_class->close = g_unix_output_stream_close;
111 stream_class->write_async = g_unix_output_stream_write_async;
112 stream_class->write_finish = g_unix_output_stream_write_finish;
113 stream_class->close_async = g_unix_output_stream_close_async;
114 stream_class->close_finish = g_unix_output_stream_close_finish;
118 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
120 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
121 G_TYPE_UNIX_OUTPUT_STREAM,
122 GUnixOutputStreamPrivate);
127 * g_unix_output_stream_new:
128 * @fd: unix's file descriptor.
129 * @close_fd_at_close: a #gboolean.
131 * Creates a new unix output stream for @fd. If @close_fd_at_close
132 * is %TRUE, the fd will be closed when the output stream is destroyed.
134 * Returns: #GOutputStream. If @close_fd_at_close is %TRUE, then
135 * @fd will be closed when the #GOutputStream is closed.
138 g_unix_output_stream_new (int fd,
139 gboolean close_fd_at_close)
141 GUnixOutputStream *stream;
143 g_return_val_if_fail (fd != -1, NULL);
145 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM, NULL);
147 stream->priv->fd = fd;
148 stream->priv->close_fd_at_close = close_fd_at_close;
150 return G_OUTPUT_STREAM (stream);
154 g_unix_output_stream_write (GOutputStream *stream,
157 GCancellable *cancellable,
160 GUnixOutputStream *unix_stream;
162 struct pollfd poll_fds[2];
166 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
168 cancel_fd = g_cancellable_get_fd (cancellable);
173 poll_fds[0].events = POLLOUT;
174 poll_fds[0].fd = unix_stream->priv->fd;
175 poll_fds[1].events = POLLIN;
176 poll_fds[1].fd = cancel_fd;
177 poll_ret = poll (poll_fds, 2, -1);
179 while (poll_ret == -1 && errno == EINTR);
183 g_set_error (error, G_IO_ERROR,
184 g_io_error_from_errno (errno),
185 _("Error writing to unix: %s"),
193 if (g_cancellable_set_error_if_cancelled (cancellable, error))
196 res = write (unix_stream->priv->fd, buffer, count);
202 g_set_error (error, G_IO_ERROR,
203 g_io_error_from_errno (errno),
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);
233 g_set_error (error, G_IO_ERROR,
234 g_io_error_from_errno (errno),
235 _("Error closing unix: %s"),
247 GAsyncReadyCallback callback;
249 GCancellable *cancellable;
250 GUnixOutputStream *stream;
254 write_async_cb (WriteAsyncData *data,
255 GIOCondition condition,
258 GSimpleAsyncResult *simple;
259 GError *error = NULL;
260 gssize count_written;
264 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
270 count_written = write (data->stream->priv->fd, data->buffer, data->count);
271 if (count_written == -1)
276 g_set_error (&error, G_IO_ERROR,
277 g_io_error_from_errno (errno),
278 _("Error reading from unix: %s"),
284 simple = g_simple_async_result_new (G_OBJECT (data->stream),
287 g_unix_output_stream_write_async);
289 g_simple_async_result_set_op_res_gssize (simple, count_written);
291 if (count_written == -1)
293 g_simple_async_result_set_from_error (simple, error);
294 g_error_free (error);
297 /* Complete immediately, not in idle, since we're already in a mainloop callout */
298 g_simple_async_result_complete (simple);
299 g_object_unref (simple);
305 g_unix_output_stream_write_async (GOutputStream *stream,
309 GCancellable *cancellable,
310 GAsyncReadyCallback callback,
314 GUnixOutputStream *unix_stream;
315 WriteAsyncData *data;
317 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
319 data = g_new0 (WriteAsyncData, 1);
321 data->buffer = buffer;
322 data->callback = callback;
323 data->user_data = user_data;
324 data->cancellable = cancellable;
325 data->stream = unix_stream;
327 source = _g_fd_source_new (unix_stream->priv->fd,
331 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
332 g_source_attach (source, NULL);
334 g_source_unref (source);
338 g_unix_output_stream_write_finish (GOutputStream *stream,
339 GAsyncResult *result,
342 GSimpleAsyncResult *simple;
345 simple = G_SIMPLE_ASYNC_RESULT (result);
346 g_assert (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
348 nwritten = g_simple_async_result_get_op_res_gssize (simple);
353 GOutputStream *stream;
354 GAsyncReadyCallback callback;
359 close_async_cb (CloseAsyncData *data)
361 GUnixOutputStream *unix_stream;
362 GSimpleAsyncResult *simple;
363 GError *error = NULL;
367 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
369 if (!unix_stream->priv->close_fd_at_close)
377 res = close (unix_stream->priv->fd);
380 g_set_error (&error, G_IO_ERROR,
381 g_io_error_from_errno (errno),
382 _("Error closing unix: %s"),
391 simple = g_simple_async_result_new (G_OBJECT (data->stream),
394 g_unix_output_stream_close_async);
398 g_simple_async_result_set_from_error (simple, error);
399 g_error_free (error);
402 /* Complete immediately, not in idle, since we're already in a mainloop callout */
403 g_simple_async_result_complete (simple);
404 g_object_unref (simple);
410 g_unix_output_stream_close_async (GOutputStream *stream,
412 GCancellable *cancellable,
413 GAsyncReadyCallback callback,
417 CloseAsyncData *data;
419 data = g_new0 (CloseAsyncData, 1);
421 data->stream = stream;
422 data->callback = callback;
423 data->user_data = user_data;
425 idle = g_idle_source_new ();
426 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
427 g_source_attach (idle, NULL);
428 g_source_unref (idle);
432 g_unix_output_stream_close_finish (GOutputStream *stream,
433 GAsyncResult *result,
436 /* Failures handled in generic close_finish code */