Small documentation fixes
[glib.git] / gio / gunixinputstream.c
blob98280cdff0185892a938a1c39eaaeb566f018b14
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
23 #include "config.h"
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <poll.h>
33 #include <glib.h>
34 #include <glib/gstdio.h>
35 #include "gioerror.h"
36 #include "gsimpleasyncresult.h"
37 #include "gunixinputstream.h"
38 #include "gcancellable.h"
39 #include "gasynchelper.h"
40 #include "glibintl.h"
42 #include "gioalias.h"
44 /**
45 * SECTION:gunixinputstream
46 * @short_description: Streaming input operations for Unix file descriptors
47 * @include: gio/gunixinputstream.h
48 * @see_also: #GInputStream
50 * #GUnixInputStream implements #GInputStream for reading from a
51 * unix file descriptor, including asynchronous operations. The file
52 * descriptor must be selectable, so it doesn't work with opened files.
53 **/
55 G_DEFINE_TYPE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM);
57 struct _GUnixInputStreamPrivate {
58 int fd;
59 gboolean close_fd_at_close;
62 static gssize g_unix_input_stream_read (GInputStream *stream,
63 void *buffer,
64 gsize count,
65 GCancellable *cancellable,
66 GError **error);
67 static gboolean g_unix_input_stream_close (GInputStream *stream,
68 GCancellable *cancellable,
69 GError **error);
70 static void g_unix_input_stream_read_async (GInputStream *stream,
71 void *buffer,
72 gsize count,
73 int io_priority,
74 GCancellable *cancellable,
75 GAsyncReadyCallback callback,
76 gpointer data);
77 static gssize g_unix_input_stream_read_finish (GInputStream *stream,
78 GAsyncResult *result,
79 GError **error);
80 static void g_unix_input_stream_skip_async (GInputStream *stream,
81 gsize count,
82 int io_priority,
83 GCancellable *cancellable,
84 GAsyncReadyCallback callback,
85 gpointer data);
86 static gssize g_unix_input_stream_skip_finish (GInputStream *stream,
87 GAsyncResult *result,
88 GError **error);
89 static void g_unix_input_stream_close_async (GInputStream *stream,
90 int io_priority,
91 GCancellable *cancellable,
92 GAsyncReadyCallback callback,
93 gpointer data);
94 static gboolean g_unix_input_stream_close_finish (GInputStream *stream,
95 GAsyncResult *result,
96 GError **error);
99 static void
100 g_unix_input_stream_finalize (GObject *object)
102 GUnixInputStream *stream;
104 stream = G_UNIX_INPUT_STREAM (object);
106 G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize (object);
109 static void
110 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
112 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
113 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
115 g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
117 gobject_class->finalize = g_unix_input_stream_finalize;
119 stream_class->read_fn = g_unix_input_stream_read;
120 stream_class->close_fn = g_unix_input_stream_close;
121 stream_class->read_async = g_unix_input_stream_read_async;
122 stream_class->read_finish = g_unix_input_stream_read_finish;
123 if (0)
125 /* TODO: Implement instead of using fallbacks */
126 stream_class->skip_async = g_unix_input_stream_skip_async;
127 stream_class->skip_finish = g_unix_input_stream_skip_finish;
129 stream_class->close_async = g_unix_input_stream_close_async;
130 stream_class->close_finish = g_unix_input_stream_close_finish;
133 static void
134 g_unix_input_stream_init (GUnixInputStream *unix_stream)
136 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
137 G_TYPE_UNIX_INPUT_STREAM,
138 GUnixInputStreamPrivate);
142 * g_unix_input_stream_new:
143 * @fd: unix file descriptor.
144 * @close_fd_at_close: a #gboolean.
146 * Creates a new #GUnixInputStream for the given @fd. If @close_fd_at_close
147 * is %TRUE, the file descriptor will be closed when the stream is closed.
149 * Returns: a #GUnixInputStream.
151 GInputStream *
152 g_unix_input_stream_new (int fd,
153 gboolean close_fd_at_close)
155 GUnixInputStream *stream;
157 g_return_val_if_fail (fd != -1, NULL);
159 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM, NULL);
161 stream->priv->fd = fd;
162 stream->priv->close_fd_at_close = close_fd_at_close;
164 return G_INPUT_STREAM (stream);
167 static gssize
168 g_unix_input_stream_read (GInputStream *stream,
169 void *buffer,
170 gsize count,
171 GCancellable *cancellable,
172 GError **error)
174 GUnixInputStream *unix_stream;
175 gssize res;
176 struct pollfd poll_fds[2];
177 int poll_ret;
178 int cancel_fd;
180 unix_stream = G_UNIX_INPUT_STREAM (stream);
182 cancel_fd = g_cancellable_get_fd (cancellable);
183 if (cancel_fd != -1)
187 poll_fds[0].events = POLLIN;
188 poll_fds[0].fd = unix_stream->priv->fd;
189 poll_fds[1].events = POLLIN;
190 poll_fds[1].fd = cancel_fd;
191 poll_ret = poll (poll_fds, 2, -1);
193 while (poll_ret == -1 && errno == EINTR);
195 if (poll_ret == -1)
197 int errsv = errno;
199 g_set_error (error, G_IO_ERROR,
200 g_io_error_from_errno (errsv),
201 _("Error reading from unix: %s"),
202 g_strerror (errsv));
203 return -1;
207 while (1)
209 if (g_cancellable_set_error_if_cancelled (cancellable, error))
210 break;
211 res = read (unix_stream->priv->fd, buffer, count);
212 if (res == -1)
214 int errsv = errno;
216 if (errsv == EINTR)
217 continue;
219 g_set_error (error, G_IO_ERROR,
220 g_io_error_from_errno (errsv),
221 _("Error reading from unix: %s"),
222 g_strerror (errsv));
225 break;
228 return res;
231 static gboolean
232 g_unix_input_stream_close (GInputStream *stream,
233 GCancellable *cancellable,
234 GError **error)
236 GUnixInputStream *unix_stream;
237 int res;
239 unix_stream = G_UNIX_INPUT_STREAM (stream);
241 if (!unix_stream->priv->close_fd_at_close)
242 return TRUE;
244 while (1)
246 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
247 res = close (unix_stream->priv->fd);
248 if (res == -1)
250 int errsv = errno;
252 g_set_error (error, G_IO_ERROR,
253 g_io_error_from_errno (errsv),
254 _("Error closing unix: %s"),
255 g_strerror (errsv));
257 break;
260 return res != -1;
263 typedef struct {
264 gsize count;
265 void *buffer;
266 GAsyncReadyCallback callback;
267 gpointer user_data;
268 GCancellable *cancellable;
269 GUnixInputStream *stream;
270 } ReadAsyncData;
272 static gboolean
273 read_async_cb (ReadAsyncData *data,
274 GIOCondition condition,
275 int fd)
277 GSimpleAsyncResult *simple;
278 GError *error = NULL;
279 gssize count_read;
281 /* We know that we can read from fd once without blocking */
282 while (1)
284 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
286 count_read = -1;
287 break;
289 count_read = read (data->stream->priv->fd, data->buffer, data->count);
290 if (count_read == -1)
292 int errsv = errno;
294 if (errsv == EINTR)
295 continue;
297 g_set_error (&error, G_IO_ERROR,
298 g_io_error_from_errno (errsv),
299 _("Error reading from unix: %s"),
300 g_strerror (errsv));
302 break;
305 simple = g_simple_async_result_new (G_OBJECT (data->stream),
306 data->callback,
307 data->user_data,
308 g_unix_input_stream_read_async);
310 g_simple_async_result_set_op_res_gssize (simple, count_read);
312 if (count_read == -1)
314 g_simple_async_result_set_from_error (simple, error);
315 g_error_free (error);
318 /* Complete immediately, not in idle, since we're already in a mainloop callout */
319 g_simple_async_result_complete (simple);
320 g_object_unref (simple);
322 return FALSE;
325 static void
326 g_unix_input_stream_read_async (GInputStream *stream,
327 void *buffer,
328 gsize count,
329 int io_priority,
330 GCancellable *cancellable,
331 GAsyncReadyCallback callback,
332 gpointer user_data)
334 GSource *source;
335 GUnixInputStream *unix_stream;
336 ReadAsyncData *data;
338 unix_stream = G_UNIX_INPUT_STREAM (stream);
340 data = g_new0 (ReadAsyncData, 1);
341 data->count = count;
342 data->buffer = buffer;
343 data->callback = callback;
344 data->user_data = user_data;
345 data->cancellable = cancellable;
346 data->stream = unix_stream;
348 source = _g_fd_source_new (unix_stream->priv->fd,
349 POLLIN,
350 cancellable);
352 g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
353 g_source_attach (source, NULL);
355 g_source_unref (source);
358 static gssize
359 g_unix_input_stream_read_finish (GInputStream *stream,
360 GAsyncResult *result,
361 GError **error)
363 GSimpleAsyncResult *simple;
364 gssize nread;
366 simple = G_SIMPLE_ASYNC_RESULT (result);
367 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_input_stream_read_async);
369 nread = g_simple_async_result_get_op_res_gssize (simple);
370 return nread;
373 static void
374 g_unix_input_stream_skip_async (GInputStream *stream,
375 gsize count,
376 int io_priority,
377 GCancellable *cancellable,
378 GAsyncReadyCallback callback,
379 gpointer data)
381 g_warn_if_reached ();
382 /* TODO: Not implemented */
385 static gssize
386 g_unix_input_stream_skip_finish (GInputStream *stream,
387 GAsyncResult *result,
388 GError **error)
390 g_warn_if_reached ();
391 return 0;
392 /* TODO: Not implemented */
396 typedef struct {
397 GInputStream *stream;
398 GAsyncReadyCallback callback;
399 gpointer user_data;
400 } CloseAsyncData;
402 static void
403 close_async_data_free (gpointer _data)
405 CloseAsyncData *data = _data;
407 g_free (data);
410 static gboolean
411 close_async_cb (CloseAsyncData *data)
413 GUnixInputStream *unix_stream;
414 GSimpleAsyncResult *simple;
415 GError *error = NULL;
416 gboolean result;
417 int res;
419 unix_stream = G_UNIX_INPUT_STREAM (data->stream);
421 if (!unix_stream->priv->close_fd_at_close)
423 result = TRUE;
424 goto out;
427 while (1)
429 res = close (unix_stream->priv->fd);
430 if (res == -1)
432 int errsv = errno;
434 g_set_error (&error, G_IO_ERROR,
435 g_io_error_from_errno (errsv),
436 _("Error closing unix: %s"),
437 g_strerror (errsv));
439 break;
442 result = res != -1;
444 out:
445 simple = g_simple_async_result_new (G_OBJECT (data->stream),
446 data->callback,
447 data->user_data,
448 g_unix_input_stream_close_async);
450 if (!result)
452 g_simple_async_result_set_from_error (simple, error);
453 g_error_free (error);
456 /* Complete immediately, not in idle, since we're already in a mainloop callout */
457 g_simple_async_result_complete (simple);
458 g_object_unref (simple);
460 return FALSE;
463 static void
464 g_unix_input_stream_close_async (GInputStream *stream,
465 int io_priority,
466 GCancellable *cancellable,
467 GAsyncReadyCallback callback,
468 gpointer user_data)
470 GSource *idle;
471 CloseAsyncData *data;
473 data = g_new0 (CloseAsyncData, 1);
475 data->stream = stream;
476 data->callback = callback;
477 data->user_data = user_data;
479 idle = g_idle_source_new ();
480 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
481 g_source_attach (idle, NULL);
482 g_source_unref (idle);
485 static gboolean
486 g_unix_input_stream_close_finish (GInputStream *stream,
487 GAsyncResult *result,
488 GError **error)
490 /* Failures handled in generic close_finish code */
491 return TRUE;
494 #define __G_UNIX_INPUT_STREAM_C__
495 #include "gioaliasdef.c"