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 * @include: gio/gunixoutputstream.h
48 * @see_also: #GOutputStream
50 * #GUnixOutputStream implements #GOutputStream for writing to a a
51 * unix file descriptor, including asynchronous operations. The file
52 * descriptor much be selectable, so it doesn't work with opened files.
55 G_DEFINE_TYPE (GUnixOutputStream, g_unix_output_stream, G_TYPE_OUTPUT_STREAM);
58 struct _GUnixOutputStreamPrivate {
60 gboolean close_fd_at_close;
63 static gssize g_unix_output_stream_write (GOutputStream *stream,
66 GCancellable *cancellable,
68 static gboolean g_unix_output_stream_close (GOutputStream *stream,
69 GCancellable *cancellable,
71 static void g_unix_output_stream_write_async (GOutputStream *stream,
75 GCancellable *cancellable,
76 GAsyncReadyCallback callback,
78 static gssize g_unix_output_stream_write_finish (GOutputStream *stream,
81 static void g_unix_output_stream_close_async (GOutputStream *stream,
83 GCancellable *cancellable,
84 GAsyncReadyCallback callback,
86 static gboolean g_unix_output_stream_close_finish (GOutputStream *stream,
92 g_unix_output_stream_finalize (GObject *object)
94 GUnixOutputStream *stream;
96 stream = G_UNIX_OUTPUT_STREAM (object);
98 if (G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize)
99 (*G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize) (object);
103 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
105 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
106 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
108 g_type_class_add_private (klass, sizeof (GUnixOutputStreamPrivate));
110 gobject_class->finalize = g_unix_output_stream_finalize;
112 stream_class->write_fn = g_unix_output_stream_write;
113 stream_class->close_fn = g_unix_output_stream_close;
114 stream_class->write_async = g_unix_output_stream_write_async;
115 stream_class->write_finish = g_unix_output_stream_write_finish;
116 stream_class->close_async = g_unix_output_stream_close_async;
117 stream_class->close_finish = g_unix_output_stream_close_finish;
121 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
123 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
124 G_TYPE_UNIX_OUTPUT_STREAM,
125 GUnixOutputStreamPrivate);
130 * g_unix_output_stream_new:
131 * @fd: unix's file descriptor.
132 * @close_fd_at_close: a #gboolean.
134 * Creates a new unix output stream for @fd. If @close_fd_at_close
135 * is %TRUE, the fd will be closed when the output stream is destroyed.
137 * Returns: #GOutputStream. If @close_fd_at_close is %TRUE, then
138 * @fd will be closed when the #GOutputStream is closed.
141 g_unix_output_stream_new (int fd,
142 gboolean close_fd_at_close)
144 GUnixOutputStream *stream;
146 g_return_val_if_fail (fd != -1, NULL);
148 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM, NULL);
150 stream->priv->fd = fd;
151 stream->priv->close_fd_at_close = close_fd_at_close;
153 return G_OUTPUT_STREAM (stream);
157 g_unix_output_stream_write (GOutputStream *stream,
160 GCancellable *cancellable,
163 GUnixOutputStream *unix_stream;
165 struct pollfd poll_fds[2];
169 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
171 cancel_fd = g_cancellable_get_fd (cancellable);
176 poll_fds[0].events = POLLOUT;
177 poll_fds[0].fd = unix_stream->priv->fd;
178 poll_fds[1].events = POLLIN;
179 poll_fds[1].fd = cancel_fd;
180 poll_ret = poll (poll_fds, 2, -1);
182 while (poll_ret == -1 && errno == EINTR);
188 g_set_error (error, G_IO_ERROR,
189 g_io_error_from_errno (errsv),
190 _("Error writing to unix: %s"),
198 if (g_cancellable_set_error_if_cancelled (cancellable, error))
201 res = write (unix_stream->priv->fd, buffer, count);
209 g_set_error (error, G_IO_ERROR,
210 g_io_error_from_errno (errsv),
211 _("Error writing to unix: %s"),
222 g_unix_output_stream_close (GOutputStream *stream,
223 GCancellable *cancellable,
226 GUnixOutputStream *unix_stream;
229 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
231 if (!unix_stream->priv->close_fd_at_close)
236 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
237 res = close (unix_stream->priv->fd);
242 g_set_error (error, G_IO_ERROR,
243 g_io_error_from_errno (errsv),
244 _("Error closing unix: %s"),
256 GAsyncReadyCallback callback;
258 GCancellable *cancellable;
259 GUnixOutputStream *stream;
263 write_async_cb (WriteAsyncData *data,
264 GIOCondition condition,
267 GSimpleAsyncResult *simple;
268 GError *error = NULL;
269 gssize count_written;
273 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
279 count_written = write (data->stream->priv->fd, data->buffer, data->count);
280 if (count_written == -1)
287 g_set_error (&error, G_IO_ERROR,
288 g_io_error_from_errno (errsv),
289 _("Error reading from unix: %s"),
295 simple = g_simple_async_result_new (G_OBJECT (data->stream),
298 g_unix_output_stream_write_async);
300 g_simple_async_result_set_op_res_gssize (simple, count_written);
302 if (count_written == -1)
304 g_simple_async_result_set_from_error (simple, error);
305 g_error_free (error);
308 /* Complete immediately, not in idle, since we're already in a mainloop callout */
309 g_simple_async_result_complete (simple);
310 g_object_unref (simple);
316 g_unix_output_stream_write_async (GOutputStream *stream,
320 GCancellable *cancellable,
321 GAsyncReadyCallback callback,
325 GUnixOutputStream *unix_stream;
326 WriteAsyncData *data;
328 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
330 data = g_new0 (WriteAsyncData, 1);
332 data->buffer = buffer;
333 data->callback = callback;
334 data->user_data = user_data;
335 data->cancellable = cancellable;
336 data->stream = unix_stream;
338 source = _g_fd_source_new (unix_stream->priv->fd,
342 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
343 g_source_attach (source, NULL);
345 g_source_unref (source);
349 g_unix_output_stream_write_finish (GOutputStream *stream,
350 GAsyncResult *result,
353 GSimpleAsyncResult *simple;
356 simple = G_SIMPLE_ASYNC_RESULT (result);
357 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
359 nwritten = g_simple_async_result_get_op_res_gssize (simple);
364 GOutputStream *stream;
365 GAsyncReadyCallback callback;
370 close_async_cb (CloseAsyncData *data)
372 GUnixOutputStream *unix_stream;
373 GSimpleAsyncResult *simple;
374 GError *error = NULL;
378 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
380 if (!unix_stream->priv->close_fd_at_close)
388 res = close (unix_stream->priv->fd);
393 g_set_error (&error, G_IO_ERROR,
394 g_io_error_from_errno (errsv),
395 _("Error closing unix: %s"),
404 simple = g_simple_async_result_new (G_OBJECT (data->stream),
407 g_unix_output_stream_close_async);
411 g_simple_async_result_set_from_error (simple, error);
412 g_error_free (error);
415 /* Complete immediately, not in idle, since we're already in a mainloop callout */
416 g_simple_async_result_complete (simple);
417 g_object_unref (simple);
423 g_unix_output_stream_close_async (GOutputStream *stream,
425 GCancellable *cancellable,
426 GAsyncReadyCallback callback,
430 CloseAsyncData *data;
432 data = g_new0 (CloseAsyncData, 1);
434 data->stream = stream;
435 data->callback = callback;
436 data->user_data = user_data;
438 idle = g_idle_source_new ();
439 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
440 g_source_attach (idle, NULL);
441 g_source_unref (idle);
445 g_unix_output_stream_close_finish (GOutputStream *stream,
446 GAsyncResult *result,
449 /* Failures handled in generic close_finish code */
453 #define __G_UNIX_OUTPUT_STREAM_C__
454 #include "gioaliasdef.c"