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
51 * unix file descriptor, including asynchronous operations. The file
52 * descriptor must 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 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);
187 g_set_error (error, G_IO_ERROR,
188 g_io_error_from_errno (errsv),
189 _("Error writing to unix: %s"),
197 if (g_cancellable_set_error_if_cancelled (cancellable, error))
200 res = write (unix_stream->priv->fd, buffer, count);
208 g_set_error (error, G_IO_ERROR,
209 g_io_error_from_errno (errsv),
210 _("Error writing to unix: %s"),
221 g_unix_output_stream_close (GOutputStream *stream,
222 GCancellable *cancellable,
225 GUnixOutputStream *unix_stream;
228 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
230 if (!unix_stream->priv->close_fd_at_close)
235 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
236 res = close (unix_stream->priv->fd);
241 g_set_error (error, G_IO_ERROR,
242 g_io_error_from_errno (errsv),
243 _("Error closing unix: %s"),
255 GAsyncReadyCallback callback;
257 GCancellable *cancellable;
258 GUnixOutputStream *stream;
262 write_async_cb (WriteAsyncData *data,
263 GIOCondition condition,
266 GSimpleAsyncResult *simple;
267 GError *error = NULL;
268 gssize count_written;
272 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
278 count_written = write (data->stream->priv->fd, data->buffer, data->count);
279 if (count_written == -1)
286 g_set_error (&error, G_IO_ERROR,
287 g_io_error_from_errno (errsv),
288 _("Error reading from unix: %s"),
294 simple = g_simple_async_result_new (G_OBJECT (data->stream),
297 g_unix_output_stream_write_async);
299 g_simple_async_result_set_op_res_gssize (simple, count_written);
301 if (count_written == -1)
303 g_simple_async_result_set_from_error (simple, error);
304 g_error_free (error);
307 /* Complete immediately, not in idle, since we're already in a mainloop callout */
308 g_simple_async_result_complete (simple);
309 g_object_unref (simple);
315 g_unix_output_stream_write_async (GOutputStream *stream,
319 GCancellable *cancellable,
320 GAsyncReadyCallback callback,
324 GUnixOutputStream *unix_stream;
325 WriteAsyncData *data;
327 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
329 data = g_new0 (WriteAsyncData, 1);
331 data->buffer = buffer;
332 data->callback = callback;
333 data->user_data = user_data;
334 data->cancellable = cancellable;
335 data->stream = unix_stream;
337 source = _g_fd_source_new (unix_stream->priv->fd,
341 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
342 g_source_attach (source, NULL);
344 g_source_unref (source);
348 g_unix_output_stream_write_finish (GOutputStream *stream,
349 GAsyncResult *result,
352 GSimpleAsyncResult *simple;
355 simple = G_SIMPLE_ASYNC_RESULT (result);
356 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
358 nwritten = g_simple_async_result_get_op_res_gssize (simple);
363 GOutputStream *stream;
364 GAsyncReadyCallback callback;
369 close_async_cb (CloseAsyncData *data)
371 GUnixOutputStream *unix_stream;
372 GSimpleAsyncResult *simple;
373 GError *error = NULL;
377 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
379 if (!unix_stream->priv->close_fd_at_close)
387 res = close (unix_stream->priv->fd);
392 g_set_error (&error, G_IO_ERROR,
393 g_io_error_from_errno (errsv),
394 _("Error closing unix: %s"),
403 simple = g_simple_async_result_new (G_OBJECT (data->stream),
406 g_unix_output_stream_close_async);
410 g_simple_async_result_set_from_error (simple, error);
411 g_error_free (error);
414 /* Complete immediately, not in idle, since we're already in a mainloop callout */
415 g_simple_async_result_complete (simple);
416 g_object_unref (simple);
422 g_unix_output_stream_close_async (GOutputStream *stream,
424 GCancellable *cancellable,
425 GAsyncReadyCallback callback,
429 CloseAsyncData *data;
431 data = g_new0 (CloseAsyncData, 1);
433 data->stream = stream;
434 data->callback = callback;
435 data->user_data = user_data;
437 idle = g_idle_source_new ();
438 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
439 g_source_attach (idle, NULL);
440 g_source_unref (idle);
444 g_unix_output_stream_close_finish (GOutputStream *stream,
445 GAsyncResult *result,
448 /* Failures handled in generic close_finish code */
452 #define __G_UNIX_OUTPUT_STREAM_C__
453 #include "gioaliasdef.c"