/src/glib/gio/gpollableoutputstream.c
Line | Count | Source (jump to first uncovered line) |
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 "gpollableoutputstream.h" |
26 | | #include "gasynchelper.h" |
27 | | #include "gfiledescriptorbased.h" |
28 | | #include "glibintl.h" |
29 | | |
30 | | /** |
31 | | * GPollableOutputStream: |
32 | | * |
33 | | * `GPollableOutputStream` is implemented by [class@Gio.OutputStream]s that |
34 | | * can be polled for readiness to write. This can be used when |
35 | | * interfacing with a non-GIO API that expects |
36 | | * UNIX-file-descriptor-style asynchronous I/O rather than GIO-style. |
37 | | * |
38 | | * Some classes may implement `GPollableOutputStream` but have only certain |
39 | | * instances of that class be pollable. If [method@Gio.PollableOutputStream.can_poll] |
40 | | * returns false, then the behavior of other `GPollableOutputStream` methods is |
41 | | * undefined. |
42 | | * |
43 | | * Since: 2.28 |
44 | | */ |
45 | | |
46 | | G_DEFINE_INTERFACE (GPollableOutputStream, g_pollable_output_stream, G_TYPE_OUTPUT_STREAM) |
47 | | |
48 | | static gboolean g_pollable_output_stream_default_can_poll (GPollableOutputStream *stream); |
49 | | static gssize g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream *stream, |
50 | | const void *buffer, |
51 | | gsize count, |
52 | | GError **error); |
53 | | static GPollableReturn g_pollable_output_stream_default_writev_nonblocking (GPollableOutputStream *stream, |
54 | | const GOutputVector *vectors, |
55 | | gsize n_vectors, |
56 | | gsize *bytes_written, |
57 | | GError **error); |
58 | | |
59 | | static void |
60 | | g_pollable_output_stream_default_init (GPollableOutputStreamInterface *iface) |
61 | 0 | { |
62 | 0 | iface->can_poll = g_pollable_output_stream_default_can_poll; |
63 | 0 | iface->write_nonblocking = g_pollable_output_stream_default_write_nonblocking; |
64 | 0 | iface->writev_nonblocking = g_pollable_output_stream_default_writev_nonblocking; |
65 | 0 | } |
66 | | |
67 | | static gboolean |
68 | | g_pollable_output_stream_default_can_poll (GPollableOutputStream *stream) |
69 | 0 | { |
70 | 0 | return TRUE; |
71 | 0 | } |
72 | | |
73 | | /** |
74 | | * g_pollable_output_stream_can_poll: |
75 | | * @stream: a #GPollableOutputStream. |
76 | | * |
77 | | * Checks if @stream is actually pollable. Some classes may implement |
78 | | * #GPollableOutputStream but have only certain instances of that |
79 | | * class be pollable. If this method returns %FALSE, then the behavior |
80 | | * of other #GPollableOutputStream methods is undefined. |
81 | | * |
82 | | * For any given stream, the value returned by this method is constant; |
83 | | * a stream cannot switch from pollable to non-pollable or vice versa. |
84 | | * |
85 | | * Returns: %TRUE if @stream is pollable, %FALSE if not. |
86 | | * |
87 | | * Since: 2.28 |
88 | | */ |
89 | | gboolean |
90 | | g_pollable_output_stream_can_poll (GPollableOutputStream *stream) |
91 | 0 | { |
92 | 0 | g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), FALSE); |
93 | | |
94 | 0 | return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->can_poll (stream); |
95 | 0 | } |
96 | | |
97 | | /** |
98 | | * g_pollable_output_stream_is_writable: |
99 | | * @stream: a #GPollableOutputStream. |
100 | | * |
101 | | * Checks if @stream can be written. |
102 | | * |
103 | | * Note that some stream types may not be able to implement this 100% |
104 | | * reliably, and it is possible that a call to g_output_stream_write() |
105 | | * after this returns %TRUE would still block. To guarantee |
106 | | * non-blocking behavior, you should always use |
107 | | * g_pollable_output_stream_write_nonblocking(), which will return a |
108 | | * %G_IO_ERROR_WOULD_BLOCK error rather than blocking. |
109 | | * |
110 | | * The behaviour of this method is undefined if |
111 | | * g_pollable_output_stream_can_poll() returns %FALSE for @stream. |
112 | | * |
113 | | * Returns: %TRUE if @stream is writable, %FALSE if not. If an error |
114 | | * has occurred on @stream, this will result in |
115 | | * g_pollable_output_stream_is_writable() returning %TRUE, and the |
116 | | * next attempt to write will return the error. |
117 | | * |
118 | | * Since: 2.28 |
119 | | */ |
120 | | gboolean |
121 | | g_pollable_output_stream_is_writable (GPollableOutputStream *stream) |
122 | 0 | { |
123 | 0 | g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), FALSE); |
124 | | |
125 | 0 | return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->is_writable (stream); |
126 | 0 | } |
127 | | |
128 | | /** |
129 | | * g_pollable_output_stream_create_source: |
130 | | * @stream: a #GPollableOutputStream. |
131 | | * @cancellable: (nullable): a #GCancellable, or %NULL |
132 | | * |
133 | | * Creates a #GSource that triggers when @stream can be written, or |
134 | | * @cancellable is triggered or an error occurs. The callback on the |
135 | | * source is of the #GPollableSourceFunc type. |
136 | | * |
137 | | * As with g_pollable_output_stream_is_writable(), it is possible that |
138 | | * the stream may not actually be writable even after the source |
139 | | * triggers, so you should use g_pollable_output_stream_write_nonblocking() |
140 | | * rather than g_output_stream_write() from the callback. |
141 | | * |
142 | | * The behaviour of this method is undefined if |
143 | | * g_pollable_output_stream_can_poll() returns %FALSE for @stream. |
144 | | * |
145 | | * Returns: (transfer full): a new #GSource |
146 | | * |
147 | | * Since: 2.28 |
148 | | */ |
149 | | GSource * |
150 | | g_pollable_output_stream_create_source (GPollableOutputStream *stream, |
151 | | GCancellable *cancellable) |
152 | 0 | { |
153 | 0 | g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), NULL); |
154 | | |
155 | 0 | return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)-> |
156 | 0 | create_source (stream, cancellable); |
157 | 0 | } |
158 | | |
159 | | static gssize |
160 | | g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream *stream, |
161 | | const void *buffer, |
162 | | gsize count, |
163 | | GError **error) |
164 | 0 | { |
165 | 0 | if (!g_pollable_output_stream_is_writable (stream)) |
166 | 0 | { |
167 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK, |
168 | 0 | g_strerror (EAGAIN)); |
169 | 0 | return -1; |
170 | 0 | } |
171 | | |
172 | 0 | return G_OUTPUT_STREAM_GET_CLASS (stream)-> |
173 | 0 | write_fn (G_OUTPUT_STREAM (stream), buffer, count, NULL, error); |
174 | 0 | } |
175 | | |
176 | | static GPollableReturn |
177 | | g_pollable_output_stream_default_writev_nonblocking (GPollableOutputStream *stream, |
178 | | const GOutputVector *vectors, |
179 | | gsize n_vectors, |
180 | | gsize *bytes_written, |
181 | | GError **error) |
182 | 0 | { |
183 | 0 | gsize _bytes_written = 0; |
184 | 0 | GPollableOutputStreamInterface *iface = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream); |
185 | 0 | gsize i; |
186 | 0 | GError *err = NULL; |
187 | |
|
188 | 0 | for (i = 0; i < n_vectors; i++) |
189 | 0 | { |
190 | 0 | gssize res; |
191 | | |
192 | | /* Would we overflow here? In that case simply return and let the caller |
193 | | * handle this like a short write */ |
194 | 0 | if (_bytes_written > G_MAXSIZE - vectors[i].size) |
195 | 0 | break; |
196 | | |
197 | 0 | res = iface->write_nonblocking (stream, vectors[i].buffer, vectors[i].size, &err); |
198 | 0 | if (res == -1) |
199 | 0 | { |
200 | 0 | if (bytes_written) |
201 | 0 | *bytes_written = _bytes_written; |
202 | | |
203 | | /* If something was written already we handle this like a short |
204 | | * write and assume that the next call would either give the same |
205 | | * error again or successfully finish writing without errors or data |
206 | | * loss |
207 | | */ |
208 | 0 | if (_bytes_written > 0) |
209 | 0 | { |
210 | 0 | g_clear_error (&err); |
211 | 0 | return G_POLLABLE_RETURN_OK; |
212 | 0 | } |
213 | 0 | else if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) |
214 | 0 | { |
215 | 0 | g_clear_error (&err); |
216 | 0 | return G_POLLABLE_RETURN_WOULD_BLOCK; |
217 | 0 | } |
218 | 0 | else |
219 | 0 | { |
220 | 0 | g_propagate_error (error, err); |
221 | 0 | return G_POLLABLE_RETURN_FAILED; |
222 | 0 | } |
223 | 0 | } |
224 | | |
225 | 0 | _bytes_written += res; |
226 | | /* if we had a short write break the loop here */ |
227 | 0 | if ((gsize) res < vectors[i].size) |
228 | 0 | break; |
229 | 0 | } |
230 | | |
231 | 0 | if (bytes_written) |
232 | 0 | *bytes_written = _bytes_written; |
233 | |
|
234 | 0 | return G_POLLABLE_RETURN_OK; |
235 | 0 | } |
236 | | |
237 | | /** |
238 | | * g_pollable_output_stream_write_nonblocking: (virtual write_nonblocking) |
239 | | * @stream: a #GPollableOutputStream |
240 | | * @buffer: (array length=count) (element-type guint8): a buffer to write |
241 | | * data from |
242 | | * @count: the number of bytes you want to write |
243 | | * @cancellable: (nullable): a #GCancellable, or %NULL |
244 | | * @error: #GError for error reporting, or %NULL to ignore. |
245 | | * |
246 | | * Attempts to write up to @count bytes from @buffer to @stream, as |
247 | | * with g_output_stream_write(). If @stream is not currently writable, |
248 | | * this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can |
249 | | * use g_pollable_output_stream_create_source() to create a #GSource |
250 | | * that will be triggered when @stream is writable. |
251 | | * |
252 | | * Note that since this method never blocks, you cannot actually |
253 | | * use @cancellable to cancel it. However, it will return an error |
254 | | * if @cancellable has already been cancelled when you call, which |
255 | | * may happen if you call this method after a source triggers due |
256 | | * to having been cancelled. |
257 | | * |
258 | | * Also note that if %G_IO_ERROR_WOULD_BLOCK is returned some underlying |
259 | | * transports like D/TLS require that you re-send the same @buffer and |
260 | | * @count in the next write call. |
261 | | * |
262 | | * The behaviour of this method is undefined if |
263 | | * g_pollable_output_stream_can_poll() returns %FALSE for @stream. |
264 | | * |
265 | | * Returns: the number of bytes written, or -1 on error (including |
266 | | * %G_IO_ERROR_WOULD_BLOCK). |
267 | | */ |
268 | | gssize |
269 | | g_pollable_output_stream_write_nonblocking (GPollableOutputStream *stream, |
270 | | const void *buffer, |
271 | | gsize count, |
272 | | GCancellable *cancellable, |
273 | | GError **error) |
274 | 0 | { |
275 | 0 | gssize res; |
276 | |
|
277 | 0 | g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), -1); |
278 | 0 | g_return_val_if_fail (buffer != NULL, 0); |
279 | | |
280 | 0 | if (g_cancellable_set_error_if_cancelled (cancellable, error)) |
281 | 0 | return -1; |
282 | | |
283 | 0 | if (count == 0) |
284 | 0 | return 0; |
285 | | |
286 | 0 | if (((gssize) count) < 0) |
287 | 0 | { |
288 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, |
289 | 0 | _("Too large count value passed to %s"), G_STRFUNC); |
290 | 0 | return -1; |
291 | 0 | } |
292 | | |
293 | 0 | if (cancellable) |
294 | 0 | g_cancellable_push_current (cancellable); |
295 | |
|
296 | 0 | res = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)-> |
297 | 0 | write_nonblocking (stream, buffer, count, error); |
298 | |
|
299 | 0 | if (cancellable) |
300 | 0 | g_cancellable_pop_current (cancellable); |
301 | |
|
302 | 0 | return res; |
303 | 0 | } |
304 | | |
305 | | /** |
306 | | * g_pollable_output_stream_writev_nonblocking: (virtual writev_nonblocking) |
307 | | * @stream: a #GPollableOutputStream |
308 | | * @vectors: (array length=n_vectors): the buffer containing the #GOutputVectors to write. |
309 | | * @n_vectors: the number of vectors to write |
310 | | * @bytes_written: (out) (optional): location to store the number of bytes that were |
311 | | * written to the stream |
312 | | * @cancellable: (nullable): a #GCancellable, or %NULL |
313 | | * @error: #GError for error reporting, or %NULL to ignore. |
314 | | * |
315 | | * Attempts to write the bytes contained in the @n_vectors @vectors to @stream, |
316 | | * as with g_output_stream_writev(). If @stream is not currently writable, |
317 | | * this will immediately return %@G_POLLABLE_RETURN_WOULD_BLOCK, and you can |
318 | | * use g_pollable_output_stream_create_source() to create a #GSource |
319 | | * that will be triggered when @stream is writable. @error will *not* be |
320 | | * set in that case. |
321 | | * |
322 | | * Note that since this method never blocks, you cannot actually |
323 | | * use @cancellable to cancel it. However, it will return an error |
324 | | * if @cancellable has already been cancelled when you call, which |
325 | | * may happen if you call this method after a source triggers due |
326 | | * to having been cancelled. |
327 | | * |
328 | | * Also note that if %G_POLLABLE_RETURN_WOULD_BLOCK is returned some underlying |
329 | | * transports like D/TLS require that you re-send the same @vectors and |
330 | | * @n_vectors in the next write call. |
331 | | * |
332 | | * The behaviour of this method is undefined if |
333 | | * g_pollable_output_stream_can_poll() returns %FALSE for @stream. |
334 | | * |
335 | | * Returns: %@G_POLLABLE_RETURN_OK on success, %G_POLLABLE_RETURN_WOULD_BLOCK |
336 | | * if the stream is not currently writable (and @error is *not* set), or |
337 | | * %G_POLLABLE_RETURN_FAILED if there was an error in which case @error will |
338 | | * be set. |
339 | | * |
340 | | * Since: 2.60 |
341 | | */ |
342 | | GPollableReturn |
343 | | g_pollable_output_stream_writev_nonblocking (GPollableOutputStream *stream, |
344 | | const GOutputVector *vectors, |
345 | | gsize n_vectors, |
346 | | gsize *bytes_written, |
347 | | GCancellable *cancellable, |
348 | | GError **error) |
349 | 0 | { |
350 | 0 | GPollableOutputStreamInterface *iface; |
351 | 0 | GPollableReturn res; |
352 | 0 | gsize _bytes_written = 0; |
353 | |
|
354 | 0 | if (bytes_written) |
355 | 0 | *bytes_written = 0; |
356 | |
|
357 | 0 | g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), G_POLLABLE_RETURN_FAILED); |
358 | 0 | g_return_val_if_fail (vectors != NULL || n_vectors == 0, G_POLLABLE_RETURN_FAILED); |
359 | 0 | g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_POLLABLE_RETURN_FAILED); |
360 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, G_POLLABLE_RETURN_FAILED); |
361 | | |
362 | 0 | if (g_cancellable_set_error_if_cancelled (cancellable, error)) |
363 | 0 | return G_POLLABLE_RETURN_FAILED; |
364 | | |
365 | 0 | if (n_vectors == 0) |
366 | 0 | return G_POLLABLE_RETURN_OK; |
367 | | |
368 | 0 | iface = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream); |
369 | 0 | g_return_val_if_fail (iface->writev_nonblocking != NULL, G_POLLABLE_RETURN_FAILED); |
370 | | |
371 | 0 | if (cancellable) |
372 | 0 | g_cancellable_push_current (cancellable); |
373 | |
|
374 | 0 | res = iface-> |
375 | 0 | writev_nonblocking (stream, vectors, n_vectors, &_bytes_written, error); |
376 | |
|
377 | 0 | if (cancellable) |
378 | 0 | g_cancellable_pop_current (cancellable); |
379 | |
|
380 | 0 | if (res == G_POLLABLE_RETURN_FAILED) |
381 | 0 | g_warn_if_fail (error == NULL || (*error != NULL && !g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))); |
382 | 0 | else if (res == G_POLLABLE_RETURN_WOULD_BLOCK) |
383 | 0 | g_warn_if_fail (error == NULL || *error == NULL); |
384 | | |
385 | | /* in case of not-OK nothing must've been written */ |
386 | 0 | g_warn_if_fail (res == G_POLLABLE_RETURN_OK || _bytes_written == 0); |
387 | |
|
388 | 0 | if (bytes_written) |
389 | 0 | *bytes_written = _bytes_written; |
390 | |
|
391 | 0 | return res; |
392 | 0 | } |