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"
45 * SECTION:gunixoutputstream
46 * @short_description: Streaming output operations for Unix file descriptors
47 * @see_also: #GOutputStream
49 * #GUnixOutputStream implements #GOutputStream for writing to a a
50 * unix file descriptor, including asynchronous operations. The file
51 * descriptor much 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 if (G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize)
98 (*G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize) (object);
102 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
104 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
105 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
107 g_type_class_add_private (klass, sizeof (GUnixOutputStreamPrivate));
109 gobject_class->finalize = g_unix_output_stream_finalize;
111 stream_class->write_fn = g_unix_output_stream_write;
112 stream_class->close_fn = g_unix_output_stream_close;
113 stream_class->write_async = g_unix_output_stream_write_async;
114 stream_class->write_finish = g_unix_output_stream_write_finish;
115 stream_class->close_async = g_unix_output_stream_close_async;
116 stream_class->close_finish = g_unix_output_stream_close_finish;
120 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
122 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
123 G_TYPE_UNIX_OUTPUT_STREAM,
124 GUnixOutputStreamPrivate);
129 * g_unix_output_stream_new:
130 * @fd: unix's file descriptor.
131 * @close_fd_at_close: a #gboolean.
133 * Creates a new unix output stream for @fd. If @close_fd_at_close
134 * is %TRUE, the fd will be closed when the output stream is destroyed.
136 * Returns: #GOutputStream. If @close_fd_at_close is %TRUE, then
137 * @fd will be closed when the #GOutputStream is closed.
140 g_unix_output_stream_new (int fd,
141 gboolean close_fd_at_close)
143 GUnixOutputStream *stream;
145 g_return_val_if_fail (fd != -1, NULL);
147 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM, NULL);
149 stream->priv->fd = fd;
150 stream->priv->close_fd_at_close = close_fd_at_close;
152 return G_OUTPUT_STREAM (stream);
156 g_unix_output_stream_write (GOutputStream *stream,
159 GCancellable *cancellable,
162 GUnixOutputStream *unix_stream;
164 struct pollfd poll_fds[2];
168 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
170 cancel_fd = g_cancellable_get_fd (cancellable);
175 poll_fds[0].events = POLLOUT;
176 poll_fds[0].fd = unix_stream->priv->fd;
177 poll_fds[1].events = POLLIN;
178 poll_fds[1].fd = cancel_fd;
179 poll_ret = poll (poll_fds, 2, -1);
181 while (poll_ret == -1 && errno == EINTR);
185 g_set_error (error, G_IO_ERROR,
186 g_io_error_from_errno (errno),
187 _("Error writing to unix: %s"),
195 if (g_cancellable_set_error_if_cancelled (cancellable, error))
198 res = write (unix_stream->priv->fd, buffer, count);
204 g_set_error (error, G_IO_ERROR,
205 g_io_error_from_errno (errno),
206 _("Error writing to unix: %s"),
217 g_unix_output_stream_close (GOutputStream *stream,
218 GCancellable *cancellable,
221 GUnixOutputStream *unix_stream;
224 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
226 if (!unix_stream->priv->close_fd_at_close)
231 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
232 res = close (unix_stream->priv->fd);
235 g_set_error (error, G_IO_ERROR,
236 g_io_error_from_errno (errno),
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)
278 g_set_error (&error, G_IO_ERROR,
279 g_io_error_from_errno (errno),
280 _("Error reading from unix: %s"),
286 simple = g_simple_async_result_new (G_OBJECT (data->stream),
289 g_unix_output_stream_write_async);
291 g_simple_async_result_set_op_res_gssize (simple, count_written);
293 if (count_written == -1)
295 g_simple_async_result_set_from_error (simple, error);
296 g_error_free (error);
299 /* Complete immediately, not in idle, since we're already in a mainloop callout */
300 g_simple_async_result_complete (simple);
301 g_object_unref (simple);
307 g_unix_output_stream_write_async (GOutputStream *stream,
311 GCancellable *cancellable,
312 GAsyncReadyCallback callback,
316 GUnixOutputStream *unix_stream;
317 WriteAsyncData *data;
319 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
321 data = g_new0 (WriteAsyncData, 1);
323 data->buffer = buffer;
324 data->callback = callback;
325 data->user_data = user_data;
326 data->cancellable = cancellable;
327 data->stream = unix_stream;
329 source = _g_fd_source_new (unix_stream->priv->fd,
333 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
334 g_source_attach (source, NULL);
336 g_source_unref (source);
340 g_unix_output_stream_write_finish (GOutputStream *stream,
341 GAsyncResult *result,
344 GSimpleAsyncResult *simple;
347 simple = G_SIMPLE_ASYNC_RESULT (result);
348 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
350 nwritten = g_simple_async_result_get_op_res_gssize (simple);
355 GOutputStream *stream;
356 GAsyncReadyCallback callback;
361 close_async_cb (CloseAsyncData *data)
363 GUnixOutputStream *unix_stream;
364 GSimpleAsyncResult *simple;
365 GError *error = NULL;
369 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
371 if (!unix_stream->priv->close_fd_at_close)
379 res = close (unix_stream->priv->fd);
382 g_set_error (&error, G_IO_ERROR,
383 g_io_error_from_errno (errno),
384 _("Error closing unix: %s"),
393 simple = g_simple_async_result_new (G_OBJECT (data->stream),
396 g_unix_output_stream_close_async);
400 g_simple_async_result_set_from_error (simple, error);
401 g_error_free (error);
404 /* Complete immediately, not in idle, since we're already in a mainloop callout */
405 g_simple_async_result_complete (simple);
406 g_object_unref (simple);
412 g_unix_output_stream_close_async (GOutputStream *stream,
414 GCancellable *cancellable,
415 GAsyncReadyCallback callback,
419 CloseAsyncData *data;
421 data = g_new0 (CloseAsyncData, 1);
423 data->stream = stream;
424 data->callback = callback;
425 data->user_data = user_data;
427 idle = g_idle_source_new ();
428 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
429 g_source_attach (idle, NULL);
430 g_source_unref (idle);
434 g_unix_output_stream_close_finish (GOutputStream *stream,
435 GAsyncResult *result,
438 /* Failures handled in generic close_finish code */
442 #define __G_UNIX_OUTPUT_STREAM_C__
443 #include "gioaliasdef.c"