Coverage Report

Created: 2026-07-16 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/irssi/subprojects/glib-2.74.7/gio/gpollableinputstream.c
Line
Count
Source
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright (C) 2010 Red Hat, Inc.
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General
18
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "config.h"
22
23
#include <errno.h>
24
25
#include "gpollableinputstream.h"
26
#include "gasynchelper.h"
27
#include "glibintl.h"
28
29
/**
30
 * SECTION:gpollableinputstream
31
 * @short_description: Interface for pollable input streams
32
 * @include: gio/gio.h
33
 * @see_also: #GInputStream, #GPollableOutputStream, #GFileDescriptorBased
34
 *
35
 * #GPollableInputStream is implemented by #GInputStreams that
36
 * can be polled for readiness to read. This can be used when
37
 * interfacing with a non-GIO API that expects
38
 * UNIX-file-descriptor-style asynchronous I/O rather than GIO-style.
39
 *
40
 * Since: 2.28
41
 */
42
43
0
G_DEFINE_INTERFACE (GPollableInputStream, g_pollable_input_stream, G_TYPE_INPUT_STREAM)
44
0
45
0
static gboolean g_pollable_input_stream_default_can_poll         (GPollableInputStream *stream);
46
0
static gssize   g_pollable_input_stream_default_read_nonblocking (GPollableInputStream  *stream,
47
0
                  void                  *buffer,
48
0
                  gsize                  count,
49
0
                  GError               **error);
50
0
51
0
static void
52
0
g_pollable_input_stream_default_init (GPollableInputStreamInterface *iface)
53
0
{
54
0
  iface->can_poll         = g_pollable_input_stream_default_can_poll;
55
0
  iface->read_nonblocking = g_pollable_input_stream_default_read_nonblocking;
56
0
}
57
58
static gboolean
59
g_pollable_input_stream_default_can_poll (GPollableInputStream *stream)
60
0
{
61
0
  return TRUE;
62
0
}
63
64
/**
65
 * g_pollable_input_stream_can_poll:
66
 * @stream: a #GPollableInputStream.
67
 *
68
 * Checks if @stream is actually pollable. Some classes may implement
69
 * #GPollableInputStream but have only certain instances of that class
70
 * be pollable. If this method returns %FALSE, then the behavior of
71
 * other #GPollableInputStream methods is undefined.
72
 *
73
 * For any given stream, the value returned by this method is constant;
74
 * a stream cannot switch from pollable to non-pollable or vice versa.
75
 *
76
 * Returns: %TRUE if @stream is pollable, %FALSE if not.
77
 *
78
 * Since: 2.28
79
 */
80
gboolean
81
g_pollable_input_stream_can_poll (GPollableInputStream *stream)
82
0
{
83
0
  g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), FALSE);
84
85
0
  return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->can_poll (stream);
86
0
}
87
88
/**
89
 * g_pollable_input_stream_is_readable:
90
 * @stream: a #GPollableInputStream.
91
 *
92
 * Checks if @stream can be read.
93
 *
94
 * Note that some stream types may not be able to implement this 100%
95
 * reliably, and it is possible that a call to g_input_stream_read()
96
 * after this returns %TRUE would still block. To guarantee
97
 * non-blocking behavior, you should always use
98
 * g_pollable_input_stream_read_nonblocking(), which will return a
99
 * %G_IO_ERROR_WOULD_BLOCK error rather than blocking.
100
 *
101
 * Returns: %TRUE if @stream is readable, %FALSE if not. If an error
102
 *   has occurred on @stream, this will result in
103
 *   g_pollable_input_stream_is_readable() returning %TRUE, and the
104
 *   next attempt to read will return the error.
105
 *
106
 * Since: 2.28
107
 */
108
gboolean
109
g_pollable_input_stream_is_readable (GPollableInputStream *stream)
110
0
{
111
0
  g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), FALSE);
112
113
0
  return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->is_readable (stream);
114
0
}
115
116
/**
117
 * g_pollable_input_stream_create_source:
118
 * @stream: a #GPollableInputStream.
119
 * @cancellable: (nullable): a #GCancellable, or %NULL
120
 *
121
 * Creates a #GSource that triggers when @stream can be read, or
122
 * @cancellable is triggered or an error occurs. The callback on the
123
 * source is of the #GPollableSourceFunc type.
124
 *
125
 * As with g_pollable_input_stream_is_readable(), it is possible that
126
 * the stream may not actually be readable even after the source
127
 * triggers, so you should use g_pollable_input_stream_read_nonblocking()
128
 * rather than g_input_stream_read() from the callback.
129
 *
130
 * Returns: (transfer full): a new #GSource
131
 *
132
 * Since: 2.28
133
 */
134
GSource *
135
g_pollable_input_stream_create_source (GPollableInputStream *stream,
136
               GCancellable         *cancellable)
137
0
{
138
0
  g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), NULL);
139
140
0
  return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
141
0
    create_source (stream, cancellable);
142
0
}
143
144
static gssize
145
g_pollable_input_stream_default_read_nonblocking (GPollableInputStream  *stream,
146
              void                  *buffer,
147
              gsize                  count,
148
              GError               **error)
149
0
{
150
0
  if (!g_pollable_input_stream_is_readable (stream))
151
0
    {
152
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK,
153
0
                           g_strerror (EAGAIN));
154
0
      return -1;
155
0
    }
156
157
0
  return G_INPUT_STREAM_GET_CLASS (stream)->
158
0
    read_fn (G_INPUT_STREAM (stream), buffer, count, NULL, error);
159
0
}
160
161
/**
162
 * g_pollable_input_stream_read_nonblocking:
163
 * @stream: a #GPollableInputStream
164
 * @buffer: (array length=count) (element-type guint8) (out caller-allocates): a
165
 *     buffer to read data into (which should be at least @count bytes long).
166
 * @count: (in): the number of bytes you want to read
167
 * @cancellable: (nullable): a #GCancellable, or %NULL
168
 * @error: #GError for error reporting, or %NULL to ignore.
169
 *
170
 * Attempts to read up to @count bytes from @stream into @buffer, as
171
 * with g_input_stream_read(). If @stream is not currently readable,
172
 * this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can
173
 * use g_pollable_input_stream_create_source() to create a #GSource
174
 * that will be triggered when @stream is readable.
175
 *
176
 * Note that since this method never blocks, you cannot actually
177
 * use @cancellable to cancel it. However, it will return an error
178
 * if @cancellable has already been cancelled when you call, which
179
 * may happen if you call this method after a source triggers due
180
 * to having been cancelled.
181
 *
182
 * Virtual: read_nonblocking
183
 * Returns: the number of bytes read, or -1 on error (including
184
 *   %G_IO_ERROR_WOULD_BLOCK).
185
 */
186
gssize
187
g_pollable_input_stream_read_nonblocking (GPollableInputStream  *stream,
188
            void                  *buffer,
189
            gsize                  count,
190
            GCancellable          *cancellable,
191
            GError               **error)
192
0
{
193
0
  gssize res;
194
195
0
  g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), -1);
196
0
  g_return_val_if_fail (buffer != NULL, 0);
197
198
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
199
0
    return -1;
200
201
0
  if (count == 0)
202
0
    return 0;
203
204
0
  if (((gssize) count) < 0)
205
0
    {
206
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
207
0
       _("Too large count value passed to %s"), G_STRFUNC);
208
0
      return -1;
209
0
    }
210
211
0
  if (cancellable)
212
0
    g_cancellable_push_current (cancellable);
213
214
0
  res = G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
215
0
    read_nonblocking (stream, buffer, count, error);
216
217
0
  if (cancellable)
218
0
    g_cancellable_pop_current (cancellable);
219
220
0
  return res;
221
0
}