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);
186 g_set_error (error, G_IO_ERROR,
187 g_io_error_from_errno (errno),
188 _("Error writing to unix: %s"),
196 if (g_cancellable_set_error_if_cancelled (cancellable, error))
199 res = write (unix_stream->priv->fd, buffer, count);
205 g_set_error (error, G_IO_ERROR,
206 g_io_error_from_errno (errno),
207 _("Error writing to unix: %s"),
218 g_unix_output_stream_close (GOutputStream *stream,
219 GCancellable *cancellable,
222 GUnixOutputStream *unix_stream;
225 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
227 if (!unix_stream->priv->close_fd_at_close)
232 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
233 res = close (unix_stream->priv->fd);
236 g_set_error (error, G_IO_ERROR,
237 g_io_error_from_errno (errno),
238 _("Error closing unix: %s"),
250 GAsyncReadyCallback callback;
252 GCancellable *cancellable;
253 GUnixOutputStream *stream;
257 write_async_cb (WriteAsyncData *data,
258 GIOCondition condition,
261 GSimpleAsyncResult *simple;
262 GError *error = NULL;
263 gssize count_written;
267 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
273 count_written = write (data->stream->priv->fd, data->buffer, data->count);
274 if (count_written == -1)
279 g_set_error (&error, G_IO_ERROR,
280 g_io_error_from_errno (errno),
281 _("Error reading from unix: %s"),
287 simple = g_simple_async_result_new (G_OBJECT (data->stream),
290 g_unix_output_stream_write_async);
292 g_simple_async_result_set_op_res_gssize (simple, count_written);
294 if (count_written == -1)
296 g_simple_async_result_set_from_error (simple, error);
297 g_error_free (error);
300 /* Complete immediately, not in idle, since we're already in a mainloop callout */
301 g_simple_async_result_complete (simple);
302 g_object_unref (simple);
308 g_unix_output_stream_write_async (GOutputStream *stream,
312 GCancellable *cancellable,
313 GAsyncReadyCallback callback,
317 GUnixOutputStream *unix_stream;
318 WriteAsyncData *data;
320 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
322 data = g_new0 (WriteAsyncData, 1);
324 data->buffer = buffer;
325 data->callback = callback;
326 data->user_data = user_data;
327 data->cancellable = cancellable;
328 data->stream = unix_stream;
330 source = _g_fd_source_new (unix_stream->priv->fd,
334 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
335 g_source_attach (source, NULL);
337 g_source_unref (source);
341 g_unix_output_stream_write_finish (GOutputStream *stream,
342 GAsyncResult *result,
345 GSimpleAsyncResult *simple;
348 simple = G_SIMPLE_ASYNC_RESULT (result);
349 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
351 nwritten = g_simple_async_result_get_op_res_gssize (simple);
356 GOutputStream *stream;
357 GAsyncReadyCallback callback;
362 close_async_cb (CloseAsyncData *data)
364 GUnixOutputStream *unix_stream;
365 GSimpleAsyncResult *simple;
366 GError *error = NULL;
370 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
372 if (!unix_stream->priv->close_fd_at_close)
380 res = close (unix_stream->priv->fd);
383 g_set_error (&error, G_IO_ERROR,
384 g_io_error_from_errno (errno),
385 _("Error closing unix: %s"),
394 simple = g_simple_async_result_new (G_OBJECT (data->stream),
397 g_unix_output_stream_close_async);
401 g_simple_async_result_set_from_error (simple, error);
402 g_error_free (error);
405 /* Complete immediately, not in idle, since we're already in a mainloop callout */
406 g_simple_async_result_complete (simple);
407 g_object_unref (simple);
413 g_unix_output_stream_close_async (GOutputStream *stream,
415 GCancellable *cancellable,
416 GAsyncReadyCallback callback,
420 CloseAsyncData *data;
422 data = g_new0 (CloseAsyncData, 1);
424 data->stream = stream;
425 data->callback = callback;
426 data->user_data = user_data;
428 idle = g_idle_source_new ();
429 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
430 g_source_attach (idle, NULL);
431 g_source_unref (idle);
435 g_unix_output_stream_close_finish (GOutputStream *stream,
436 GAsyncResult *result,
439 /* Failures handled in generic close_finish code */
443 #define __G_UNIX_OUTPUT_STREAM_C__
444 #include "gioaliasdef.c"