Debian Codegraph @

src/dex-gio.c

raw symbols SCIP
1 /*
2 * dex-gio.c
3 *
4 * Copyright 2022-2023 Christian Hergert <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include "config.h"
23
24 #include <stdarg.h>
25
26 #include <glib/gstdio.h>
27
28 #include "dex-async-pair-private.h"
29 #include "dex-future-private.h"
30 #include "dex-future-set.h"
31 #include "dex-gio.h"
32 #include "dex-promise.h"
33 #include "dex-socket-wait-private.h"
34 #include "dex-thread-pool-scheduler.h"
35 #include "dex-thread.h"
36 #include "dex-watch-private.h"
37
38 typedef struct _DexFileInfoList DexFileInfoList;
39 typedef struct _DexSrvTargetList DexSrvTargetList;
40 typedef struct _DexTlsCertificateList DexTlsCertificateList;
41 typedef struct _DexVariantList DexVariantList;
42
43 #define DEX_TYPE_SRV_TARGET_LIST (dex_srv_target_list_get_type())
44 #define DEX_TYPE_TLS_CERTIFICATE_LIST (dex_tls_certificate_list_get_type())
45 #define DEX_TYPE_VARIANT_LIST (dex_variant_list_get_type())
46
47 GType dex_srv_target_list_get_type (void);
48 GType dex_tls_certificate_list_get_type (void);
49 GType dex_variant_list_get_type (void);
50
51 static DexFileInfoList *
52 dex_file_info_list_copy (DexFileInfoList *list)
53 {
54 return (DexFileInfoList *)g_list_copy_deep ((GList *)list, (GCopyFunc)g_object_ref, NULL);
55 }
56
57 static void
58 dex_file_info_list_free (DexFileInfoList *list)
59 {
60 GList *real_list = (GList *)list;
61 g_list_free_full (real_list, g_object_unref);
62 }
63
64 typedef struct _DexInetAddressList DexInetAddressList;
65
66 G_DEFINE_BOXED_TYPE (DexFileInfoList, dex_file_info_list, dex_file_info_list_copy, dex_file_info_list_free)
67
68 static DexInetAddressList *
69 dex_inet_address_list_copy (DexInetAddressList *list)
70 {
71 return (DexInetAddressList *)g_list_copy_deep ((GList *)list, (GCopyFunc)g_object_ref, NULL);
72 }
73
74 static void
75 dex_inet_address_list_free (DexInetAddressList *list)
76 {
77 GList *real_list = (GList *)list;
78 g_list_free_full (real_list, g_object_unref);
79 }
80
81 G_DEFINE_BOXED_TYPE (DexInetAddressList, dex_inet_address_list, dex_inet_address_list_copy, dex_inet_address_list_free)
82
83 static DexSrvTargetList *
84 dex_srv_target_list_copy (DexSrvTargetList *list)
85 {
86 return (DexSrvTargetList *)g_list_copy_deep ((GList *)list, (GCopyFunc)g_srv_target_copy, NULL);
87 }
88
89 static void
90 dex_srv_target_list_free (DexSrvTargetList *list)
91 {
92 GList *real_list = (GList *)list;
93 g_resolver_free_targets (real_list);
94 }
95
96 G_DEFINE_BOXED_TYPE (DexSrvTargetList, dex_srv_target_list, dex_srv_target_list_copy, dex_srv_target_list_free)
97
98 static DexTlsCertificateList *
99 dex_tls_certificate_list_copy (DexTlsCertificateList *list)
100 {
101 return (DexTlsCertificateList *)g_list_copy_deep ((GList *)list, (GCopyFunc)g_object_ref, NULL);
102 }
103
104 static void
105 dex_tls_certificate_list_free (DexTlsCertificateList *list)
106 {
107 GList *real_list = (GList *)list;
108 g_list_free_full (real_list, g_object_unref);
109 }
110
111 G_DEFINE_BOXED_TYPE (DexTlsCertificateList, dex_tls_certificate_list, dex_tls_certificate_list_copy, dex_tls_certificate_list_free)
112
113 static DexVariantList *
114 dex_variant_list_copy (DexVariantList *list)
115 {
116 return (DexVariantList *)g_list_copy_deep ((GList *)list, (GCopyFunc)g_variant_ref, NULL);
117 }
118
119 static void
120 dex_variant_list_free (DexVariantList *list)
121 {
122 GList *real_list = (GList *)list;
123 g_list_free_full (real_list, (GDestroyNotify)g_variant_unref);
124 }
125
126 G_DEFINE_BOXED_TYPE (DexVariantList, dex_variant_list, dex_variant_list_copy, dex_variant_list_free)
127
128 static inline DexAsyncPair *
129 create_async_pair (const char *name)
130 {
131 DexAsyncPair *async_pair;
132
133 async_pair = (DexAsyncPair *)dex_object_create_instance (DEX_TYPE_ASYNC_PAIR);
134 dex_future_set_static_name (DEX_FUTURE (async_pair), name);
135
136 return async_pair;
137 }
138
139 static void
140 dex_app_info_launch_uris_cb (GObject *object,
141 GAsyncResult *result,
142 gpointer user_data)
143 {
144 DexPromise *promise = user_data;
145 GError *error = NULL;
146
147 if (!g_app_info_launch_uris_finish (G_APP_INFO (object), result, &error))
148 dex_promise_reject (promise, g_steal_pointer (&error));
149 else
150 dex_promise_resolve_boolean (promise, TRUE);
151
152 dex_unref (promise);
153 }
154
155 /**
156 * dex_app_info_launch_uris:
157 * @appinfo: a [[email protected]]
158 * @uris: (element-type utf8): URIs to launch
159 * @context: (nullable): a [[email protected]], or %NULL
160 *
161 * Wraps [[email protected]_uris_async].
162 *
163 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
164 * or rejects with error.
165 *
166 * Since: 1.2
167 */
168 DexFuture *
169 dex_app_info_launch_uris (GAppInfo *appinfo,
170 GList *uris,
171 GAppLaunchContext *context)
172 {
173 DexPromise *promise;
174
175 dex_return_error_if_fail (G_IS_APP_INFO (appinfo));
176
177 promise = dex_promise_new_cancellable ();
178
179 g_app_info_launch_uris_async (appinfo,
180 uris,
181 context,
182 dex_promise_get_cancellable (promise),
183 dex_app_info_launch_uris_cb,
184 dex_ref (promise));
185
186 return DEX_FUTURE (promise);
187 }
188
189 static void
190 dex_app_info_get_default_for_type_cb (GObject *object,
191 GAsyncResult *result,
192 gpointer user_data)
193 {
194 DexPromise *promise = user_data;
195 GError *error = NULL;
196 GAppInfo *app_info;
197
198 g_assert (object == NULL);
199
200 app_info = g_app_info_get_default_for_type_finish (result, &error);
201
202 if (error == NULL)
203 dex_promise_resolve_object (promise, g_steal_pointer (&app_info));
204 else
205 dex_promise_reject (promise, g_steal_pointer (&error));
206
207 dex_unref (promise);
208 }
209
210 /**
211 * dex_app_info_get_default_for_type:
212 * @content_type: a content type
213 * @must_support_uris: whether the app must support URIs
214 *
215 * Wraps [[email protected]_default_for_type_async].
216 *
217 * Returns: (transfer full): a [[email protected]] that resolves to a
218 * [[email protected]] or rejects with error.
219 *
220 * Since: 1.2
221 */
222 DexFuture *
223 dex_app_info_get_default_for_type (const char *content_type,
224 gboolean must_support_uris)
225 {
226 DexPromise *promise;
227
228 dex_return_error_if_fail (content_type != NULL);
229
230 promise = dex_promise_new_cancellable ();
231
232 g_app_info_get_default_for_type_async (content_type,
233 must_support_uris,
234 dex_promise_get_cancellable (promise),
235 dex_app_info_get_default_for_type_cb,
236 dex_ref (promise));
237
238 return DEX_FUTURE (promise);
239 }
240
241 static void
242 dex_app_info_get_default_for_uri_scheme_cb (GObject *object,
243 GAsyncResult *result,
244 gpointer user_data)
245 {
246 DexPromise *promise = user_data;
247 GError *error = NULL;
248 GAppInfo *app_info;
249
250 g_assert (object == NULL);
251
252 app_info = g_app_info_get_default_for_uri_scheme_finish (result, &error);
253
254 if (error == NULL)
255 dex_promise_resolve_object (promise, g_steal_pointer (&app_info));
256 else
257 dex_promise_reject (promise, g_steal_pointer (&error));
258
259 dex_unref (promise);
260 }
261
262 /**
263 * dex_app_info_get_default_for_uri_scheme:
264 * @uri_scheme: a URI scheme
265 *
266 * Wraps [[email protected]_default_for_uri_scheme_async].
267 *
268 * Returns: (transfer full): a [[email protected]] that resolves to a
269 * [[email protected]] or rejects with error.
270 *
271 * Since: 1.2
272 */
273 DexFuture *
274 dex_app_info_get_default_for_uri_scheme (const char *uri_scheme)
275 {
276 DexPromise *promise;
277
278 dex_return_error_if_fail (uri_scheme != NULL);
279
280 promise = dex_promise_new_cancellable ();
281
282 g_app_info_get_default_for_uri_scheme_async (uri_scheme,
283 dex_promise_get_cancellable (promise),
284 dex_app_info_get_default_for_uri_scheme_cb,
285 dex_ref (promise));
286
287 return DEX_FUTURE (promise);
288 }
289
290 static void
291 dex_app_info_launch_default_for_uri_cb (GObject *object,
292 GAsyncResult *result,
293 gpointer user_data)
294 {
295 DexPromise *promise = user_data;
296 GError *error = NULL;
297
298 g_assert (object == NULL);
299
300 if (!g_app_info_launch_default_for_uri_finish (result, &error))
301 dex_promise_reject (promise, g_steal_pointer (&error));
302 else
303 dex_promise_resolve_boolean (promise, TRUE);
304
305 dex_unref (promise);
306 }
307
308 /**
309 * dex_app_info_launch_default_for_uri:
310 * @uri: a URI to launch
311 * @context: (nullable): a [[email protected]], or %NULL
312 *
313 * Wraps [[email protected]_default_for_uri_async].
314 *
315 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
316 * or rejects with error.
317 *
318 * Since: 1.2
319 */
320 DexFuture *
321 dex_app_info_launch_default_for_uri (const char *uri,
322 GAppLaunchContext *context)
323 {
324 DexPromise *promise;
325
326 dex_return_error_if_fail (uri != NULL);
327
328 promise = dex_promise_new_cancellable ();
329
330 g_app_info_launch_default_for_uri_async (uri,
331 context,
332 dex_promise_get_cancellable (promise),
333 dex_app_info_launch_default_for_uri_cb,
334 dex_ref (promise));
335
336 return DEX_FUTURE (promise);
337 }
338
339 static void
340 dex_async_initable_new_cb (GObject *object,
341 GAsyncResult *result,
342 gpointer user_data)
343 {
344 DexPromise *promise = user_data;
345 GError *error = NULL;
346
347 if (!g_async_initable_init_finish (G_ASYNC_INITABLE (object), result, &error))
348 dex_promise_reject (promise, g_steal_pointer (&error));
349 else
350 dex_promise_resolve_object (promise, g_object_ref (object));
351
352 dex_unref (promise);
353 }
354
355 /**
356 * dex_async_initable_new:
357 * @object_type: a type implementing [[email protected]]
358 * @io_priority: priority for the IO operation
359 * @first_property_name: (nullable): first property name, or %NULL
360 * @...: property name and value pairs
361 *
362 * Creates and asynchronously initializes a new object.
363 *
364 * Returns: (transfer full): a [[email protected]] that resolves to the
365 * newly initialized [[email protected]] or rejects with error.
366 *
367 * Since: 1.2
368 */
369 DexFuture *
370 dex_async_initable_new (GType object_type,
371 int io_priority,
372 const char *first_property_name,
373 ...)
374 {
375 DexPromise *promise;
376 GObject *object;
377 va_list args;
378
379 dex_return_error_if_fail (g_type_is_a (object_type, G_TYPE_ASYNC_INITABLE));
380
381 va_start (args, first_property_name);
382 object = g_object_new_valist (object_type, first_property_name, args);
383 va_end (args);
384
385 promise = dex_promise_new_cancellable ();
386
387 g_async_initable_init_async (G_ASYNC_INITABLE (object),
388 io_priority,
389 dex_promise_get_cancellable (promise),
390 dex_async_initable_new_cb,
391 dex_ref (promise));
392
393 g_object_unref (object);
394
395 return DEX_FUTURE (promise);
396 }
397
398 static void
399 dex_file_new_tmp_dir_cb (GObject *object,
400 GAsyncResult *result,
401 gpointer user_data)
402 {
403 DexPromise *promise = user_data;
404 GError *error = NULL;
405 GFile *file;
406
407 g_assert (object == NULL);
408 g_assert (G_IS_ASYNC_RESULT (result));
409 g_assert (DEX_IS_PROMISE (promise));
410
411 if (!(file = g_file_new_tmp_dir_finish (result, &error)))
412 dex_promise_reject (promise, g_steal_pointer (&error));
413 else
414 dex_promise_resolve_object (promise, g_steal_pointer (&file));
415
416 dex_unref (promise);
417 }
418
419 /**
420 * dex_file_new_tmp_dir:
421 * @tmpl: (nullable): template for the directory name, or %NULL
422 * @io_priority: priority for the IO operation
423 *
424 * Wraps [[email protected]_tmp_dir_async] as a [[email protected]].
425 *
426 * Returns: (transfer full): a [[email protected]] that resolves to a
427 * [[email protected]] or rejects with error.
428 *
429 * Since: 1.2
430 */
431 DexFuture *
432 dex_file_new_tmp_dir (const char *tmpl,
433 int io_priority)
434 {
435 DexPromise *promise;
436
437 promise = dex_promise_new_cancellable ();
438
439 g_file_new_tmp_dir_async (tmpl,
440 io_priority,
441 dex_promise_get_cancellable (promise),
442 dex_file_new_tmp_dir_cb,
443 dex_ref (promise));
444
445 return DEX_FUTURE (promise);
446 }
447
448 static void
449 dex_buffered_input_stream_fill_cb (GObject *object,
450 GAsyncResult *result,
451 gpointer user_data)
452 {
453 DexPromise *promise = user_data;
454 GError *error = NULL;
455 gssize len;
456
457 len = g_buffered_input_stream_fill_finish (G_BUFFERED_INPUT_STREAM (object), result, &error);
458
459 if (error == NULL)
460 dex_promise_resolve_int64 (promise, len);
461 else
462 dex_promise_reject (promise, g_steal_pointer (&error));
463
464 dex_unref (promise);
465 }
466
467 /**
468 * dex_buffered_input_stream_fill:
469 * @stream: a [[email protected]]
470 * @count: the number of bytes to fill, or -1
471 * @io_priority: priority for the IO operation
472 *
473 * Wraps [[email protected]_async].
474 *
475 * Returns: (transfer full): a [[email protected]] that resolves to the number
476 * of bytes read as a `gint64`, or rejects with error.
477 *
478 * Since: 1.2
479 */
480 DexFuture *
481 dex_buffered_input_stream_fill (GBufferedInputStream *stream,
482 gssize count,
483 int io_priority)
484 {
485 DexPromise *promise;
486
487 dex_return_error_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
488
489 promise = dex_promise_new_cancellable ();
490
491 g_buffered_input_stream_fill_async (stream,
492 count,
493 io_priority,
494 dex_promise_get_cancellable (promise),
495 dex_buffered_input_stream_fill_cb,
496 dex_ref (promise));
497
498 return DEX_FUTURE (promise);
499 }
500
501 static void
502 dex_input_stream_read_bytes_cb (GObject *object,
503 GAsyncResult *result,
504 gpointer user_data)
505 {
506 DexAsyncPair *async_pair = user_data;
507 GError *error = NULL;
508 GBytes *bytes;
509
510 bytes = g_input_stream_read_bytes_finish (G_INPUT_STREAM (object), result, &error);
511
512 if (error == NULL)
513 dex_async_pair_return_boxed (async_pair, G_TYPE_BYTES, bytes);
514 else
515 dex_async_pair_return_error (async_pair, error);
516
517 dex_unref (async_pair);
518 }
519
520 /**
521 * dex_input_stream_read_bytes:
522 * @stream: a [[email protected]]
523 * @count: size in bytes to read from the stream
524 * @io_priority: the [IO priority][[email protected]#io-priority] of the
525 * request
526 *
527 * Reads @count bytes from the stream.
528 *
529 * Returns: (transfer full): a [[email protected]] that resolves
530 * to a [[email protected]].
531 */
532 DexFuture *
533 dex_input_stream_read_bytes (GInputStream *stream,
534 gsize count,
535 int io_priority)
536 {
537 DexAsyncPair *async_pair;
538
539 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
540
541 async_pair = create_async_pair (G_STRFUNC);
542
543 g_input_stream_read_bytes_async (stream,
544 count,
545 io_priority,
546 async_pair->cancellable,
547 dex_input_stream_read_bytes_cb,
548 dex_ref (async_pair));
549
550 return DEX_FUTURE (async_pair);
551 }
552
553 static void
554 dex_data_input_stream_read_line_cb (GObject *object,
555 GAsyncResult *result,
556 gpointer user_data)
557 {
558 DexPromise *promise = user_data;
559 GError *error = NULL;
560 char *line = NULL;
561
562 line = g_data_input_stream_read_line_finish (G_DATA_INPUT_STREAM (object), result, NULL, &error);
563
564 if (error == NULL)
565 dex_promise_resolve_string (promise, g_steal_pointer (&line));
566 else
567 dex_promise_reject (promise, g_steal_pointer (&error));
568
569 dex_unref (promise);
570 }
571
572 static void
573 dex_data_input_stream_read_line_utf8_cb (GObject *object,
574 GAsyncResult *result,
575 gpointer user_data)
576 {
577 DexPromise *promise = user_data;
578 GError *error = NULL;
579 char *line = NULL;
580
581 line = g_data_input_stream_read_line_finish_utf8 (G_DATA_INPUT_STREAM (object),
582 result,
583 NULL,
584 &error);
585
586 if (error == NULL)
587 dex_promise_resolve_string (promise, g_steal_pointer (&line));
588 else
589 dex_promise_reject (promise, g_steal_pointer (&error));
590
591 dex_unref (promise);
592 }
593
594 static void
595 dex_data_input_stream_read_upto_cb (GObject *object,
596 GAsyncResult *result,
597 gpointer user_data)
598 {
599 DexPromise *promise = user_data;
600 GError *error = NULL;
601 char *data = NULL;
602
603 data = g_data_input_stream_read_upto_finish (G_DATA_INPUT_STREAM (object),
604 result,
605 NULL,
606 &error);
607
608 if (error == NULL)
609 dex_promise_resolve_string (promise, g_steal_pointer (&data));
610 else
611 dex_promise_reject (promise, g_steal_pointer (&error));
612
613 dex_unref (promise);
614 }
615
616 /**
617 * dex_data_input_stream_read_line:
618 * @stream: a [[email protected]]
619 * @io_priority: the [IO priority][[email protected]#io-priority] of the
620 * request
621 *
622 * Reads a line from the data input stream.
623 *
624 * Wraps [[email protected]_line_async].
625 *
626 * Returns: (transfer full): a [[email protected]] that resolves
627 * to a string containing the line (without the line terminator), or %NULL
628 * if the end of the stream is reached.
629 *
630 * Since: 1.1
631 */
632 DexFuture *
633 dex_data_input_stream_read_line (GDataInputStream *stream,
634 int io_priority)
635 {
636 DexPromise *promise;
637
638 dex_return_error_if_fail (G_IS_DATA_INPUT_STREAM (stream));
639
640 promise = dex_promise_new_cancellable ();
641
642 g_data_input_stream_read_line_async (stream,
643 io_priority,
644 dex_promise_get_cancellable (promise),
645 dex_data_input_stream_read_line_cb,
646 dex_ref (promise));
647
648 return DEX_FUTURE (promise);
649 }
650
651 /**
652 * dex_data_input_stream_read_line_utf8:
653 * @stream: a [[email protected]]
654 * @io_priority: the [IO priority][[email protected]#io-priority] of the
655 * request
656 *
657 * Reads a UTF-8 line from the data input stream.
658 *
659 * Wraps [[email protected]_line_async] with
660 * [[email protected]_line_finish_utf8].
661 *
662 * Returns: (transfer full): a [[email protected]] that resolves
663 * to a string containing the line (without the line terminator), or %NULL
664 * if the end of the stream is reached.
665 *
666 * Since: 1.2
667 */
668 DexFuture *
669 dex_data_input_stream_read_line_utf8 (GDataInputStream *stream,
670 int io_priority)
671 {
672 DexPromise *promise;
673
674 dex_return_error_if_fail (G_IS_DATA_INPUT_STREAM (stream));
675
676 promise = dex_promise_new_cancellable ();
677
678 g_data_input_stream_read_line_async (stream,
679 io_priority,
680 dex_promise_get_cancellable (promise),
681 dex_data_input_stream_read_line_utf8_cb,
682 dex_ref (promise));
683
684 return DEX_FUTURE (promise);
685 }
686
687 /**
688 * dex_data_input_stream_read_upto:
689 * @stream: a [[email protected]]
690 * @stop_chars: characters to terminate the read
691 * @stop_chars_len: length of @stop_chars, or -1 if @stop_chars is nul-terminated
692 * @io_priority: the [IO priority][[email protected]#io-priority] of the
693 * request
694 *
695 * Reads data from the stream until one of @stop_chars is found.
696 *
697 * Wraps [[email protected]_upto_async].
698 *
699 * Returns: (transfer full): a [[email protected]] that resolves
700 * to a string containing the read data, not including the stop character, or
701 * %NULL if the end of the stream is reached.
702 *
703 * Since: 1.2
704 */
705 DexFuture *
706 dex_data_input_stream_read_upto (GDataInputStream *stream,
707 const char *stop_chars,
708 gssize stop_chars_len,
709 int io_priority)
710 {
711 DexPromise *promise;
712
713 dex_return_error_if_fail (G_IS_DATA_INPUT_STREAM (stream));
714 dex_return_error_if_fail (stop_chars != NULL);
715
716 promise = dex_promise_new_cancellable ();
717
718 g_data_input_stream_read_upto_async (stream,
719 stop_chars,
720 stop_chars_len,
721 io_priority,
722 dex_promise_get_cancellable (promise),
723 dex_data_input_stream_read_upto_cb,
724 dex_ref (promise));
725
726 return DEX_FUTURE (promise);
727 }
728
729 static void
730 dex_output_stream_write_bytes_cb (GObject *object,
731 GAsyncResult *result,
732 gpointer user_data)
733 {
734 DexAsyncPair *async_pair = user_data;
735 GError *error = NULL;
736 gssize len;
737
738 len = g_output_stream_write_bytes_finish (G_OUTPUT_STREAM (object), result, &error);
739
740 if (error == NULL)
741 dex_async_pair_return_int64 (async_pair, len);
742 else
743 dex_async_pair_return_error (async_pair, error);
744
745 dex_unref (async_pair);
746 }
747
748 /**
749 * dex_output_stream_write_bytes:
750 * @stream: a [[email protected]]
751 * @bytes: the [[email protected]] to write to the stream
752 * @io_priority: the [IO priority][[email protected]#io-priority] of the
753 * request
754 *
755 * Writes @bytes to @stream.
756 *
757 * This function takes a reference to @bytes and may be released after
758 * calling this function.
759 *
760 * Returns: (transfer full): a [[email protected]] that resolves to
761 * a `gint64`.
762 */
763 DexFuture *
764 dex_output_stream_write_bytes (GOutputStream *stream,
765 GBytes *bytes,
766 int io_priority)
767 {
768 DexAsyncPair *async_pair;
769
770 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), NULL);
771
772 async_pair = create_async_pair (G_STRFUNC);
773
774 g_output_stream_write_bytes_async (stream,
775 bytes,
776 io_priority,
777 async_pair->cancellable,
778 dex_output_stream_write_bytes_cb,
779 dex_ref (async_pair));
780
781 return DEX_FUTURE (async_pair);
782 }
783
784 typedef struct
785 {
786 DexPromise *promise;
787 GOutputVector *vectors;
788 } OutputStreamWritevAll;
789
790 static void
791 output_stream_writev_all_free (OutputStreamWritevAll *state)
792 {
793 dex_clear (&state->promise);
794 g_free (state->vectors);
795 g_free (state);
796 }
797
798 static void
799 dex_output_stream_writev_all_cb (GObject *object,
800 GAsyncResult *result,
801 gpointer user_data)
802 {
803 OutputStreamWritevAll *state = user_data;
804 GError *error = NULL;
805 gsize bytes_written = 0;
806
807 if (!g_output_stream_writev_all_finish (G_OUTPUT_STREAM (object),
808 result,
809 &bytes_written,
810 &error))
811 dex_promise_reject (state->promise, g_steal_pointer (&error));
812 else
813 dex_promise_resolve_uint64 (state->promise, bytes_written);
814
815 output_stream_writev_all_free (state);
816 }
817
818 /**
819 * dex_output_stream_writev_all:
820 * @stream: a [[email protected]]
821 * @vectors: (array length=n_vectors): vectors to write to @stream
822 * @n_vectors: the number of vectors to write
823 * @io_priority: the [IO priority][[email protected]#io-priority] of the
824 * request
825 *
826 * Writes all bytes in @vectors to @stream.
827 *
828 * This function copies the @vectors array before starting the operation, but
829 * the buffers referenced by the vectors must remain valid until the returned
830 * future completes.
831 *
832 * Wraps [[email protected]_all_async].
833 *
834 * Returns: (transfer full): a [[email protected]] that resolves to a `guint64`
835 * containing the number of bytes written, or rejects with error.
836 *
837 * Since: 1.2
838 */
839 DexFuture *
840 dex_output_stream_writev_all (GOutputStream *stream,
841 const GOutputVector *vectors,
842 gsize n_vectors,
843 int io_priority)
844 {
845 OutputStreamWritevAll *state;
846 DexPromise *promise;
847
848 dex_return_error_if_fail (G_IS_OUTPUT_STREAM (stream));
849 dex_return_error_if_fail (vectors != NULL || n_vectors == 0);
850
851 promise = dex_promise_new_cancellable ();
852
853 state = g_new0 (OutputStreamWritevAll, 1);
854 state->promise = dex_ref (promise);
855 state->vectors = g_memdup2 (vectors, sizeof *vectors * n_vectors);
856
857 g_output_stream_writev_all_async (stream,
858 state->vectors,
859 n_vectors,
860 io_priority,
861 dex_promise_get_cancellable (promise),
862 dex_output_stream_writev_all_cb,
863 state);
864
865 return DEX_FUTURE (promise);
866 }
867
868 static void
869 dex_file_create_cb (GObject *object,
870 GAsyncResult *result,
871 gpointer user_data)
872 {
873 DexPromise *promise = user_data;
874 GFileOutputStream *stream;
875 GError *error = NULL;
876
877 if (!(stream = g_file_create_finish (G_FILE (object), result, &error)))
878 dex_promise_reject (promise, g_steal_pointer (&error));
879 else
880 dex_promise_resolve_object (promise, g_steal_pointer (&stream));
881
882 dex_unref (promise);
883 }
884
885 /**
886 * dex_file_create:
887 * @file: a [[email protected]]
888 * @flags: flags for creating the file
889 * @io_priority: priority for the IO operation
890 *
891 * Wraps [[email protected]] as a [[email protected]].
892 *
893 * Returns: (transfer full): a [[email protected]] that resolves to
894 * a [[email protected]] or rejects with error.
895 *
896 * Since: 1.1
897 */
898 DexFuture *
899 dex_file_create (GFile *file,
900 GFileCreateFlags flags,
901 int io_priority)
902 {
903 DexPromise *promise;
904
905 dex_return_error_if_fail (G_IS_FILE (file));
906
907 promise = dex_promise_new_cancellable ();
908 g_file_create_async (file,
909 flags,
910 io_priority,
911 dex_promise_get_cancellable (promise),
912 dex_file_create_cb,
913 dex_ref (promise));
914 return DEX_FUTURE (promise);
915 }
916
917 static void
918 dex_file_append_to_cb (GObject *object,
919 GAsyncResult *result,
920 gpointer user_data)
921 {
922 DexPromise *promise = user_data;
923 GFileOutputStream *stream;
924 GError *error = NULL;
925
926 if (!(stream = g_file_append_to_finish (G_FILE (object), result, &error)))
927 dex_promise_reject (promise, g_steal_pointer (&error));
928 else
929 dex_promise_resolve_object (promise, g_steal_pointer (&stream));
930
931 dex_unref (promise);
932 }
933
934 /**
935 * dex_file_append_to:
936 * @file: a [[email protected]]
937 * @flags: flags for appending to the file
938 * @io_priority: priority for the IO operation
939 *
940 * Wraps [[email protected]_to_async] as a [[email protected]].
941 *
942 * Returns: (transfer full): a [[email protected]] that resolves to a
943 * [[email protected]] or rejects with error.
944 *
945 * Since: 1.2
946 */
947 DexFuture *
948 dex_file_append_to (GFile *file,
949 GFileCreateFlags flags,
950 int io_priority)
951 {
952 DexPromise *promise;
953
954 dex_return_error_if_fail (G_IS_FILE (file));
955
956 promise = dex_promise_new_cancellable ();
957 g_file_append_to_async (file,
958 flags,
959 io_priority,
960 dex_promise_get_cancellable (promise),
961 dex_file_append_to_cb,
962 dex_ref (promise));
963 return DEX_FUTURE (promise);
964 }
965
966 static void
967 dex_file_read_cb (GObject *object,
968 GAsyncResult *result,
969 gpointer user_data)
970 {
971 DexAsyncPair *async_pair = user_data;
972 GFileInputStream *stream;
973 GError *error = NULL;
974
975 if ((stream = g_file_read_finish (G_FILE (object), result, &error)))
976 dex_async_pair_return_object (async_pair, stream);
977 else
978 dex_async_pair_return_error (async_pair, error);
979
980 dex_unref (async_pair);
981 }
982
983 /**
984 * dex_file_read:
985 * @file: a [[email protected]]
986 * @io_priority: the [IO priority][[email protected]#io-priority] of the
987 * request
988 *
989 * Asynchronously opens a file for reading.
990 *
991 * Returns: (transfer full): a [[email protected]] that resolves
992 * to a [[email protected]].
993 */
994 DexFuture *
995 dex_file_read (GFile *file,
996 int io_priority)
997 {
998 DexAsyncPair *async_pair;
999
1000 g_return_val_if_fail (G_IS_FILE (file), NULL);
1001
1002 async_pair = create_async_pair (G_STRFUNC);
1003
1004 g_file_read_async (file,
1005 io_priority,
1006 async_pair->cancellable,
1007 dex_file_read_cb,
1008 dex_ref (async_pair));
1009
1010 return DEX_FUTURE (async_pair);
1011 }
1012
1013 static void
1014 dex_file_replace_cb (GObject *object,
1015 GAsyncResult *result,
1016 gpointer user_data)
1017 {
1018 DexAsyncPair *async_pair = user_data;
1019 GFileOutputStream *stream;
1020 GError *error = NULL;
1021
1022 if ((stream = g_file_replace_finish (G_FILE (object), result, &error)))
1023 dex_async_pair_return_object (async_pair, stream);
1024 else
1025 dex_async_pair_return_error (async_pair, error);
1026
1027 dex_unref (async_pair);
1028 }
1029
1030 /**
1031 * dex_file_replace:
1032 * @file: a [[email protected]]
1033 * @etag: (nullable): the etag or %NULL
1034 * @flags: a set of [[email protected]]
1035 *
1036 * Opens a stream that will replace @file on disk when the input
1037 * stream is closed.
1038 *
1039 * Returns: (transfer full): a [[email protected]] that resolves
1040 * to a [[email protected]].
1041 */
1042 DexFuture *
1043 dex_file_replace (GFile *file,
1044 const char *etag,
1045 gboolean make_backup,
1046 GFileCreateFlags flags,
1047 int io_priority)
1048 {
1049 DexAsyncPair *async_pair;
1050
1051 g_return_val_if_fail (G_IS_FILE (file), NULL);
1052
1053 async_pair = create_async_pair (G_STRFUNC);
1054
1055 g_file_replace_async (file,
1056 etag,
1057 make_backup,
1058 flags,
1059 io_priority,
1060 async_pair->cancellable,
1061 dex_file_replace_cb,
1062 dex_ref (async_pair));
1063
1064 return DEX_FUTURE (async_pair);
1065 }
1066
1067 static void
1068 dex_file_open_readwrite_cb (GObject *object,
1069 GAsyncResult *result,
1070 gpointer user_data)
1071 {
1072 DexPromise *promise = user_data;
1073 GFileIOStream *stream;
1074 GError *error = NULL;
1075
1076 if (!(stream = g_file_open_readwrite_finish (G_FILE (object), result, &error)))
1077 dex_promise_reject (promise, g_steal_pointer (&error));
1078 else
1079 dex_promise_resolve_object (promise, g_steal_pointer (&stream));
1080
1081 dex_unref (promise);
1082 }
1083
1084 /**
1085 * dex_file_open_readwrite:
1086 * @file: a [[email protected]]
1087 * @io_priority: priority for the IO operation
1088 *
1089 * Wraps [[email protected]_readwrite_async] as a [[email protected]].
1090 *
1091 * Returns: (transfer full): a [[email protected]] that resolves to a
1092 * [[email protected]] or rejects with error.
1093 *
1094 * Since: 1.2
1095 */
1096 DexFuture *
1097 dex_file_open_readwrite (GFile *file,
1098 int io_priority)
1099 {
1100 DexPromise *promise;
1101
1102 dex_return_error_if_fail (G_IS_FILE (file));
1103
1104 promise = dex_promise_new_cancellable ();
1105 g_file_open_readwrite_async (file,
1106 io_priority,
1107 dex_promise_get_cancellable (promise),
1108 dex_file_open_readwrite_cb,
1109 dex_ref (promise));
1110 return DEX_FUTURE (promise);
1111 }
1112
1113 static void
1114 dex_file_create_readwrite_cb (GObject *object,
1115 GAsyncResult *result,
1116 gpointer user_data)
1117 {
1118 DexPromise *promise = user_data;
1119 GFileIOStream *stream;
1120 GError *error = NULL;
1121
1122 if (!(stream = g_file_create_readwrite_finish (G_FILE (object), result, &error)))
1123 dex_promise_reject (promise, g_steal_pointer (&error));
1124 else
1125 dex_promise_resolve_object (promise, g_steal_pointer (&stream));
1126
1127 dex_unref (promise);
1128 }
1129
1130 /**
1131 * dex_file_create_readwrite:
1132 * @file: a [[email protected]]
1133 * @flags: flags for creating the file
1134 * @io_priority: priority for the IO operation
1135 *
1136 * Wraps [[email protected]_readwrite_async] as a [[email protected]].
1137 *
1138 * Returns: (transfer full): a [[email protected]] that resolves to a
1139 * [[email protected]] or rejects with error.
1140 *
1141 * Since: 1.2
1142 */
1143 DexFuture *
1144 dex_file_create_readwrite (GFile *file,
1145 GFileCreateFlags flags,
1146 int io_priority)
1147 {
1148 DexPromise *promise;
1149
1150 dex_return_error_if_fail (G_IS_FILE (file));
1151
1152 promise = dex_promise_new_cancellable ();
1153 g_file_create_readwrite_async (file,
1154 flags,
1155 io_priority,
1156 dex_promise_get_cancellable (promise),
1157 dex_file_create_readwrite_cb,
1158 dex_ref (promise));
1159 return DEX_FUTURE (promise);
1160 }
1161
1162 static void
1163 dex_file_replace_readwrite_cb (GObject *object,
1164 GAsyncResult *result,
1165 gpointer user_data)
1166 {
1167 DexPromise *promise = user_data;
1168 GFileIOStream *stream;
1169 GError *error = NULL;
1170
1171 if (!(stream = g_file_replace_readwrite_finish (G_FILE (object), result, &error)))
1172 dex_promise_reject (promise, g_steal_pointer (&error));
1173 else
1174 dex_promise_resolve_object (promise, g_steal_pointer (&stream));
1175
1176 dex_unref (promise);
1177 }
1178
1179 /**
1180 * dex_file_replace_readwrite:
1181 * @file: a [[email protected]]
1182 * @etag: (nullable): the etag or %NULL
1183 * @make_backup: if a backup file should be created
1184 * @flags: flags for replacing the file
1185 * @io_priority: priority for the IO operation
1186 *
1187 * Wraps [[email protected]_readwrite_async] as a [[email protected]].
1188 *
1189 * Returns: (transfer full): a [[email protected]] that resolves to a
1190 * [[email protected]] or rejects with error.
1191 *
1192 * Since: 1.2
1193 */
1194 DexFuture *
1195 dex_file_replace_readwrite (GFile *file,
1196 const char *etag,
1197 gboolean make_backup,
1198 GFileCreateFlags flags,
1199 int io_priority)
1200 {
1201 DexPromise *promise;
1202
1203 dex_return_error_if_fail (G_IS_FILE (file));
1204
1205 promise = dex_promise_new_cancellable ();
1206 g_file_replace_readwrite_async (file,
1207 etag,
1208 make_backup,
1209 flags,
1210 io_priority,
1211 dex_promise_get_cancellable (promise),
1212 dex_file_replace_readwrite_cb,
1213 dex_ref (promise));
1214 return DEX_FUTURE (promise);
1215 }
1216
1217 static void
1218 dex_file_replace_contents_bytes_cb (GObject *object,
1219 GAsyncResult *result,
1220 gpointer user_data)
1221 {
1222 DexPromise *promise = user_data;
1223 GError *error = NULL;
1224 char *new_etag = NULL;
1225
1226 g_assert (G_IS_FILE (object));
1227 g_assert (G_IS_ASYNC_RESULT (result));
1228 g_assert (DEX_IS_PROMISE (promise));
1229
1230 if (!g_file_replace_contents_finish (G_FILE (object), result, &new_etag, &error))
1231 dex_promise_reject (promise, g_steal_pointer (&error));
1232 else
1233 dex_promise_resolve_string (promise, g_steal_pointer (&new_etag));
1234
1235 dex_unref (promise);
1236 }
1237
1238 typedef struct
1239 {
1240 DexPromise *promise;
1241 GBytes *bytes;
1242 } FileReplaceContents;
1243
1244 static void
1245 dex_file_replace_contents_cb (GObject *object,
1246 GAsyncResult *result,
1247 gpointer user_data)
1248 {
1249 FileReplaceContents *state = user_data;
1250 GError *error = NULL;
1251 char *new_etag = NULL;
1252
1253 g_assert (G_IS_FILE (object));
1254 g_assert (G_IS_ASYNC_RESULT (result));
1255 g_assert (DEX_IS_PROMISE (state->promise));
1256
1257 if (!g_file_replace_contents_finish (G_FILE (object), result, &new_etag, &error))
1258 dex_promise_reject (state->promise, g_steal_pointer (&error));
1259 else
1260 dex_promise_resolve_string (state->promise, g_steal_pointer (&new_etag));
1261
1262 dex_clear (&state->promise);
1263 g_clear_pointer (&state->bytes, g_bytes_unref);
1264 g_free (state);
1265 }
1266
1267 /**
1268 * dex_file_replace_contents:
1269 * @file: a [[email protected]]
1270 * @contents: (array length=length) (element-type guint8): the contents to write
1271 * @length: the length of @contents
1272 * @etag: (nullable): the etag or %NULL
1273 * @make_backup: if a backup file should be created
1274 * @flags: a set of [[email protected]]
1275 *
1276 * Wraps [[email protected]_contents_async].
1277 *
1278 * This function copies @contents into a [[email protected]] so the caller does
1279 * not need to keep the buffer alive for the duration of the operation.
1280 *
1281 * Returns: (transfer full): a [[email protected]] which resolves to the new
1282 * etag, or %NULL if no etag is available.
1283 *
1284 * Since: 1.2
1285 */
1286 DexFuture *
1287 dex_file_replace_contents (GFile *file,
1288 const char *contents,
1289 gsize length,
1290 const char *etag,
1291 gboolean make_backup,
1292 GFileCreateFlags flags)
1293 {
1294 FileReplaceContents *state;
1295 DexPromise *promise;
1296 gconstpointer data;
1297 gsize size;
1298
1299 dex_return_error_if_fail (G_IS_FILE (file));
1300 dex_return_error_if_fail (contents != NULL || length == 0);
1301
1302 promise = dex_promise_new_cancellable ();
1303
1304 state = g_new0 (FileReplaceContents, 1);
1305 state->promise = dex_ref (promise);
1306 state->bytes = g_bytes_new (contents, length);
1307
1308 data = g_bytes_get_data (state->bytes, &size);
1309
1310 g_file_replace_contents_async (file,
1311 data,
1312 size,
1313 etag,
1314 make_backup,
1315 flags,
1316 dex_promise_get_cancellable (promise),
1317 dex_file_replace_contents_cb,
1318 state);
1319
1320 return DEX_FUTURE (promise);
1321 }
1322
1323 /**
1324 * dex_file_replace_contents_bytes:
1325 * @file: a [[email protected]]
1326 * @contents: a [[email protected]]
1327 * @etag: (nullable): the etag or %NULL
1328 * @make_backup: if a backup file should be created
1329 * @flags: a set of [[email protected]]
1330 *
1331 * Wraps [[email protected]_contents_bytes_async]
1332 *
1333 * Returns: (transfer full): a [[email protected]] which resolves to the
1334 * new etag. Therefore, it is possible to be %NULL without an
1335 * error having occurred.
1336 */
1337 DexFuture *
1338 dex_file_replace_contents_bytes (GFile *file,
1339 GBytes *contents,
1340 const char *etag,
1341 gboolean make_backup,
1342 GFileCreateFlags flags)
1343 {
1344 DexPromise *promise;
1345
1346 dex_return_error_if_fail (G_IS_FILE (file));
1347 dex_return_error_if_fail (contents != NULL);
1348
1349 promise = dex_promise_new_cancellable ();
1350
1351 g_file_replace_contents_bytes_async (file,
1352 contents,
1353 etag,
1354 make_backup,
1355 flags,
1356 dex_promise_get_cancellable (promise),
1357 dex_file_replace_contents_bytes_cb,
1358 dex_ref (promise));
1359
1360 return DEX_FUTURE (promise);
1361 }
1362
1363 static void
1364 dex_input_stream_read_cb (GObject *object,
1365 GAsyncResult *result,
1366 gpointer user_data)
1367 {
1368 DexAsyncPair *async_pair = user_data;
1369 GError *error = NULL;
1370 gssize len;
1371
1372 len = g_input_stream_read_finish (G_INPUT_STREAM (object), result, &error);
1373
1374 if (error == NULL)
1375 dex_async_pair_return_int64 (async_pair, len);
1376 else
1377 dex_async_pair_return_error (async_pair, error);
1378
1379 dex_unref (async_pair);
1380 }
1381
1382 /**
1383 * dex_input_stream_read:
1384 * @self: a [[email protected]]
1385 * @buffer: (array length=count) (element-type guint8) (out caller-allocates)
1386 * @count: (in): size in bytes to read from the stream into the @buffer
1387 * @io_priority: the [IO priority][[email protected]#io-priority] of the
1388 * request
1389 *
1390 * Reads @count bytes from an input stream into a pre-allocated @buffer. The
1391 * @buffer must stay valid for the lifetime of this future.
1392 *
1393 * Returns: (transfer full): a [[email protected]] that reads @counts bytes
1394 * into @buffer
1395 */
1396 DexFuture *
1397 dex_input_stream_read (GInputStream *self,
1398 gpointer buffer,
1399 gsize count,
1400 int io_priority)
1401 {
1402 DexAsyncPair *async_pair;
1403
1404 g_return_val_if_fail (G_IS_INPUT_STREAM (self), NULL);
1405
1406 async_pair = create_async_pair (G_STRFUNC);
1407
1408 g_input_stream_read_async (self,
1409 buffer,
1410 count,
1411 io_priority,
1412 async_pair->cancellable,
1413 dex_input_stream_read_cb,
1414 dex_ref (async_pair));
1415
1416 return DEX_FUTURE (async_pair);
1417 }
1418
1419 static void
1420 dex_input_stream_skip_cb (GObject *object,
1421 GAsyncResult *result,
1422 gpointer user_data)
1423 {
1424 DexAsyncPair *async_pair = user_data;
1425 GError *error = NULL;
1426 gssize len;
1427
1428 len = g_input_stream_skip_finish (G_INPUT_STREAM (object), result, &error);
1429
1430 if (error == NULL)
1431 dex_async_pair_return_int64 (async_pair, len);
1432 else
1433 dex_async_pair_return_error (async_pair, error);
1434
1435 dex_unref (async_pair);
1436 }
1437
1438 /**
1439 * dex_input_stream_skip:
1440 * @self: a [[email protected]]
1441 * @count: the number of bytes to skip
1442 * @io_priority: the [IO priority][[email protected]#io-priority] of the
1443 * request
1444 *
1445 * Returns: (transfer full): a [[email protected]] that resolves
1446 * to the number of bytes skipped as a `gint64`.
1447 */
1448 DexFuture *
1449 dex_input_stream_skip (GInputStream *self,
1450 gsize count,
1451 int io_priority)
1452 {
1453 DexAsyncPair *async_pair;
1454
1455 g_return_val_if_fail (G_IS_INPUT_STREAM (self), NULL);
1456
1457 async_pair = create_async_pair (G_STRFUNC);
1458
1459 g_input_stream_skip_async (self,
1460 count,
1461 io_priority,
1462 async_pair->cancellable,
1463 dex_input_stream_skip_cb,
1464 dex_ref (async_pair));
1465
1466 return DEX_FUTURE (async_pair);
1467 }
1468
1469 static void
1470 dex_output_stream_write_cb (GObject *object,
1471 GAsyncResult *result,
1472 gpointer user_data)
1473 {
1474 DexAsyncPair *async_pair = user_data;
1475 GError *error = NULL;
1476 gssize len;
1477
1478 len = g_output_stream_write_finish (G_OUTPUT_STREAM (object), result, &error);
1479
1480 if (error == NULL)
1481 dex_async_pair_return_int64 (async_pair, len);
1482 else
1483 dex_async_pair_return_error (async_pair, error);
1484
1485 dex_unref (async_pair);
1486 }
1487
1488 /**
1489 * dex_output_stream_write:
1490 * @self: a [[email protected]]
1491 * @buffer: (array length=count) (element-type guint8)
1492 * @count: size in bytes to write to the output stream
1493 * @io_priority: the [IO priority][[email protected]#io-priority] of the
1494 * request
1495 *
1496 * Returns: (transfer full): a [[email protected]] that resolves
1497 * to the number of bytes written as a `gint64`
1498 */
1499 DexFuture *
1500 dex_output_stream_write (GOutputStream *self,
1501 gconstpointer buffer,
1502 gsize count,
1503 int io_priority)
1504 {
1505 DexAsyncPair *async_pair;
1506
1507 g_return_val_if_fail (G_IS_OUTPUT_STREAM (self), NULL);
1508
1509 async_pair = create_async_pair (G_STRFUNC);
1510
1511 g_output_stream_write_async (self,
1512 buffer,
1513 count,
1514 io_priority,
1515 async_pair->cancellable,
1516 dex_output_stream_write_cb,
1517 dex_ref (async_pair));
1518
1519 return DEX_FUTURE (async_pair);
1520 }
1521
1522 static void
1523 dex_output_stream_close_cb (GObject *object,
1524 GAsyncResult *result,
1525 gpointer user_data)
1526 {
1527 DexAsyncPair *async_pair = user_data;
1528 GError *error = NULL;
1529
1530 g_output_stream_close_finish (G_OUTPUT_STREAM (object), result, &error);
1531
1532 if (error == NULL)
1533 dex_async_pair_return_boolean (async_pair, TRUE);
1534 else
1535 dex_async_pair_return_error (async_pair, error);
1536
1537 dex_unref (async_pair);
1538 }
1539
1540 /**
1541 * dex_output_stream_close:
1542 * @self: a [[email protected]]
1543 * @io_priority: the [IO priority][[email protected]#io-priority] of the
1544 * request
1545 *
1546 * Returns: (transfer full): a [[email protected]] that resolves
1547 * to true or rejects with error.
1548 */
1549 DexFuture *
1550 dex_output_stream_close (GOutputStream *self,
1551 int io_priority)
1552 {
1553 DexAsyncPair *async_pair;
1554
1555 g_return_val_if_fail (G_IS_OUTPUT_STREAM (self), NULL);
1556
1557 async_pair = create_async_pair (G_STRFUNC);
1558
1559 g_output_stream_close_async (self,
1560 io_priority,
1561 async_pair->cancellable,
1562 dex_output_stream_close_cb,
1563 dex_ref (async_pair));
1564
1565 return DEX_FUTURE (async_pair);
1566 }
1567
1568 static void
1569 dex_output_stream_flush_cb (GObject *object,
1570 GAsyncResult *result,
1571 gpointer user_data)
1572 {
1573 DexPromise *promise = user_data;
1574 GError *error = NULL;
1575
1576 if (!g_output_stream_flush_finish (G_OUTPUT_STREAM (object), result, &error))
1577 dex_promise_reject (promise, g_steal_pointer (&error));
1578 else
1579 dex_promise_resolve_boolean (promise, TRUE);
1580
1581 dex_unref (promise);
1582 }
1583
1584 /**
1585 * dex_output_stream_flush:
1586 * @self: a [[email protected]]
1587 * @io_priority: priority for the IO operation
1588 *
1589 * Wraps [[email protected]_async].
1590 *
1591 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
1592 * or rejects with error.
1593 *
1594 * Since: 1.2
1595 */
1596 DexFuture *
1597 dex_output_stream_flush (GOutputStream *self,
1598 int io_priority)
1599 {
1600 DexPromise *promise;
1601
1602 dex_return_error_if_fail (G_IS_OUTPUT_STREAM (self));
1603
1604 promise = dex_promise_new_cancellable ();
1605
1606 g_output_stream_flush_async (self,
1607 io_priority,
1608 dex_promise_get_cancellable (promise),
1609 dex_output_stream_flush_cb,
1610 dex_ref (promise));
1611
1612 return DEX_FUTURE (promise);
1613 }
1614
1615 static void
1616 dex_input_stream_close_cb (GObject *object,
1617 GAsyncResult *result,
1618 gpointer user_data)
1619 {
1620 DexAsyncPair *async_pair = user_data;
1621 GError *error = NULL;
1622
1623 g_input_stream_close_finish (G_INPUT_STREAM (object), result, &error);
1624
1625 if (error == NULL)
1626 dex_async_pair_return_boolean (async_pair, TRUE);
1627 else
1628 dex_async_pair_return_error (async_pair, error);
1629
1630 dex_unref (async_pair);
1631 }
1632
1633 /**
1634 * dex_input_stream_close:
1635 * @self: a [[email protected]]
1636 * @io_priority: the [IO priority][[email protected]#io-priority] of the
1637 * request
1638 *
1639 * Returns: (transfer full): a [[email protected]] that resolves
1640 * to true if successful or rejects with error.
1641 */
1642 DexFuture *
1643 dex_input_stream_close (GInputStream *self,
1644 int io_priority)
1645 {
1646 DexAsyncPair *async_pair;
1647
1648 g_return_val_if_fail (G_IS_INPUT_STREAM (self), NULL);
1649
1650 async_pair = create_async_pair (G_STRFUNC);
1651
1652 g_input_stream_close_async (self,
1653 io_priority,
1654 async_pair->cancellable,
1655 dex_input_stream_close_cb,
1656 dex_ref (async_pair));
1657
1658 return DEX_FUTURE (async_pair);
1659 }
1660
1661 static void
1662 dex_output_stream_splice_cb (GObject *object,
1663 GAsyncResult *result,
1664 gpointer user_data)
1665 {
1666 DexAsyncPair *async_pair = user_data;
1667 GError *error = NULL;
1668 gssize len;
1669
1670 len = g_output_stream_splice_finish (G_OUTPUT_STREAM (object), result, &error);
1671
1672 if (error == NULL)
1673 dex_async_pair_return_int64 (async_pair, len);
1674 else
1675 dex_async_pair_return_error (async_pair, error);
1676
1677 dex_unref (async_pair);
1678 }
1679
1680 /**
1681 * dex_output_stream_splice:
1682 * @output: a [[email protected]]
1683 * @input: a [[email protected]]
1684 * @flags: a set of [[email protected]]
1685 * @io_priority: the [IO priority][[email protected]#io-priority] of the
1686 * request
1687 *
1688 * Returns: (transfer full): a [[email protected]] that resolves to the
1689 * number of bytes spliced as a `gint64` or rejects with error.
1690 */
1691 DexFuture *
1692 dex_output_stream_splice (GOutputStream *output,
1693 GInputStream *input,
1694 GOutputStreamSpliceFlags flags,
1695 int io_priority)
1696 {
1697 DexAsyncPair *async_pair;
1698
1699 g_return_val_if_fail (G_IS_OUTPUT_STREAM (output), NULL);
1700 g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
1701
1702 async_pair = create_async_pair (G_STRFUNC);
1703
1704 g_output_stream_splice_async (output,
1705 input,
1706 flags,
1707 io_priority,
1708 async_pair->cancellable,
1709 dex_output_stream_splice_cb,
1710 dex_ref (async_pair));
1711
1712 return DEX_FUTURE (async_pair);
1713 }
1714
1715 static void
1716 dex_file_query_info_cb (GObject *object,
1717 GAsyncResult *result,
1718 gpointer user_data)
1719 {
1720 DexAsyncPair *async_pair = user_data;
1721 GError *error = NULL;
1722 GFileInfo *info;
1723
1724 info = g_file_query_info_finish (G_FILE (object), result, &error);
1725
1726 if (error == NULL)
1727 dex_async_pair_return_object (async_pair, info);
1728 else
1729 dex_async_pair_return_error (async_pair, error);
1730
1731 dex_unref (async_pair);
1732 }
1733
1734 /**
1735 * dex_file_query_info:
1736 * @file: a [[email protected]]
1737 * @attributes: an attribute query string (see: [GFile documentation]([email protected]))
1738 * @flags: a set of [[email protected]]
1739 * @io_priority: the [IO priority][[email protected]#io-priority] of the
1740 * request
1741 *
1742 * Returns: (transfer full): a [[email protected]] that resolves
1743 * to a [[email protected]] or rejects with error.
1744 */
1745 DexFuture *
1746 dex_file_query_info (GFile *file,
1747 const char *attributes,
1748 GFileQueryInfoFlags flags,
1749 int io_priority)
1750 {
1751 DexAsyncPair *async_pair;
1752
1753 g_return_val_if_fail (G_IS_FILE (file), NULL);
1754
1755 async_pair = create_async_pair (G_STRFUNC);
1756
1757 g_file_query_info_async (file,
1758 attributes,
1759 flags,
1760 io_priority,
1761 async_pair->cancellable,
1762 dex_file_query_info_cb,
1763 dex_ref (async_pair));
1764
1765 return DEX_FUTURE (async_pair);
1766 }
1767
1768 static void
1769 dex_file_query_filesystem_info_cb (GObject *object,
1770 GAsyncResult *result,
1771 gpointer user_data)
1772 {
1773 DexPromise *promise = user_data;
1774 GFileInfo *info;
1775 GError *error = NULL;
1776
1777 if (!(info = g_file_query_filesystem_info_finish (G_FILE (object), result, &error)))
1778 dex_promise_reject (promise, g_steal_pointer (&error));
1779 else
1780 dex_promise_resolve_object (promise, g_steal_pointer (&info));
1781
1782 dex_unref (promise);
1783 }
1784
1785 /**
1786 * dex_file_query_filesystem_info:
1787 * @file: a [[email protected]]
1788 * @attributes: an attribute query string
1789 * @io_priority: priority for the IO operation
1790 *
1791 * Wraps [[email protected]_filesystem_info_async].
1792 *
1793 * Returns: (transfer full): a [[email protected]] that resolves to a
1794 * [[email protected]] or rejects with error.
1795 *
1796 * Since: 1.2
1797 */
1798 DexFuture *
1799 dex_file_query_filesystem_info (GFile *file,
1800 const char *attributes,
1801 int io_priority)
1802 {
1803 DexPromise *promise;
1804
1805 dex_return_error_if_fail (G_IS_FILE (file));
1806
1807 promise = dex_promise_new_cancellable ();
1808 g_file_query_filesystem_info_async (file,
1809 attributes,
1810 io_priority,
1811 dex_promise_get_cancellable (promise),
1812 dex_file_query_filesystem_info_cb,
1813 dex_ref (promise));
1814 return DEX_FUTURE (promise);
1815 }
1816
1817 static void
1818 dex_file_query_file_type_cb (GObject *object,
1819 GAsyncResult *result,
1820 gpointer user_data)
1821 {
1822 DexPromise *promise = user_data;
1823 GFileInfo *file_info;
1824 GError *error = NULL;
1825
1826 if ((file_info = g_file_query_info_finish (G_FILE (object), result, &error)))
1827 {
1828 GValue value = G_VALUE_INIT;
1829
1830 g_value_init (&value, G_TYPE_FILE_TYPE);
1831 g_value_set_enum (&value, g_file_info_get_file_type (file_info));
1832
1833 dex_promise_resolve (promise, &value);
1834
1835 g_value_unset (&value);
1836 g_object_unref (file_info);
1837 }
1838 else dex_promise_reject (promise, error);
1839
1840 dex_unref (promise);
1841 }
1842
1843 /**
1844 * dex_file_query_file_type:
1845 * @file: a [[email protected]]
1846 * @flags: a set of [[email protected]]
1847 * @io_priority: the [IO priority][[email protected]#io-priority] of the
1848 * request
1849 *
1850 * Returns: (transfer full): a [[email protected]] that resolves to a
1851 * [[email protected]].
1852 */
1853 DexFuture *
1854 dex_file_query_file_type (GFile *file,
1855 GFileQueryInfoFlags flags,
1856 int io_priority)
1857 {
1858 DexPromise *promise;
1859
1860 dex_return_error_if_fail (G_IS_FILE (file));
1861
1862 promise = dex_promise_new_cancellable ();
1863 g_file_query_info_async (file,
1864 G_FILE_ATTRIBUTE_STANDARD_TYPE,
1865 flags,
1866 io_priority,
1867 dex_promise_get_cancellable (promise),
1868 dex_file_query_file_type_cb,
1869 dex_ref (promise));
1870 return DEX_FUTURE (promise);
1871 }
1872
1873 static void
1874 dex_file_query_default_handler_cb (GObject *object,
1875 GAsyncResult *result,
1876 gpointer user_data)
1877 {
1878 DexPromise *promise = user_data;
1879 GAppInfo *app_info;
1880 GError *error = NULL;
1881
1882 if (!(app_info = g_file_query_default_handler_finish (G_FILE (object), result, &error)))
1883 dex_promise_reject (promise, g_steal_pointer (&error));
1884 else
1885 dex_promise_resolve_object (promise, g_steal_pointer (&app_info));
1886
1887 dex_unref (promise);
1888 }
1889
1890 /**
1891 * dex_file_query_default_handler:
1892 * @file: a [[email protected]]
1893 * @io_priority: priority for the IO operation
1894 *
1895 * Wraps [[email protected]_default_handler_async].
1896 *
1897 * Returns: (transfer full): a [[email protected]] that resolves to a
1898 * [[email protected]] or rejects with error.
1899 *
1900 * Since: 1.2
1901 */
1902 DexFuture *
1903 dex_file_query_default_handler (GFile *file,
1904 int io_priority)
1905 {
1906 DexPromise *promise;
1907
1908 dex_return_error_if_fail (G_IS_FILE (file));
1909
1910 promise = dex_promise_new_cancellable ();
1911 g_file_query_default_handler_async (file,
1912 io_priority,
1913 dex_promise_get_cancellable (promise),
1914 dex_file_query_default_handler_cb,
1915 dex_ref (promise));
1916 return DEX_FUTURE (promise);
1917 }
1918
1919 static void
1920 dex_file_find_enclosing_mount_cb (GObject *object,
1921 GAsyncResult *result,
1922 gpointer user_data)
1923 {
1924 DexPromise *promise = user_data;
1925 GMount *mount;
1926 GError *error = NULL;
1927
1928 if (!(mount = g_file_find_enclosing_mount_finish (G_FILE (object), result, &error)))
1929 dex_promise_reject (promise, g_steal_pointer (&error));
1930 else
1931 dex_promise_resolve_object (promise, g_steal_pointer (&mount));
1932
1933 dex_unref (promise);
1934 }
1935
1936 /**
1937 * dex_file_find_enclosing_mount:
1938 * @file: a [[email protected]]
1939 * @io_priority: priority for the IO operation
1940 *
1941 * Wraps [[email protected]_enclosing_mount_async].
1942 *
1943 * Returns: (transfer full): a [[email protected]] that resolves to a
1944 * [[email protected]] or rejects with error.
1945 *
1946 * Since: 1.2
1947 */
1948 DexFuture *
1949 dex_file_find_enclosing_mount (GFile *file,
1950 int io_priority)
1951 {
1952 DexPromise *promise;
1953
1954 dex_return_error_if_fail (G_IS_FILE (file));
1955
1956 promise = dex_promise_new_cancellable ();
1957 g_file_find_enclosing_mount_async (file,
1958 io_priority,
1959 dex_promise_get_cancellable (promise),
1960 dex_file_find_enclosing_mount_cb,
1961 dex_ref (promise));
1962 return DEX_FUTURE (promise);
1963 }
1964
1965 static void
1966 dex_file_make_directory_cb (GObject *object,
1967 GAsyncResult *result,
1968 gpointer user_data)
1969 {
1970 DexAsyncPair *async_pair = user_data;
1971 GError *error = NULL;
1972
1973 g_file_make_directory_finish (G_FILE (object), result, &error);
1974
1975 if (error == NULL)
1976 dex_async_pair_return_boolean (async_pair, TRUE);
1977 else
1978 dex_async_pair_return_error (async_pair, error);
1979
1980 dex_unref (async_pair);
1981 }
1982
1983 /**
1984 * dex_file_make_directory:
1985 * @file: a [[email protected]]
1986 * @io_priority: the [IO priority][[email protected]#io-priority] of the
1987 * request
1988 *
1989 * Asynchronously creates a directory and returns [[email protected]] which
1990 * can be observed for the result.
1991 *
1992 * Returns: (transfer full): a [[email protected]]
1993 */
1994 DexFuture *
1995 dex_file_make_directory (GFile *file,
1996 int io_priority)
1997 {
1998 DexAsyncPair *async_pair;
1999
2000 g_return_val_if_fail (G_IS_FILE (file), NULL);
2001
2002 async_pair = create_async_pair (G_STRFUNC);
2003
2004 g_file_make_directory_async (file,
2005 io_priority,
2006 async_pair->cancellable,
2007 dex_file_make_directory_cb,
2008 dex_ref (async_pair));
2009
2010 return DEX_FUTURE (async_pair);
2011 }
2012
2013 static void
2014 dex_file_make_symbolic_link_cb (GObject *object,
2015 GAsyncResult *result,
2016 gpointer user_data)
2017 {
2018 DexPromise *promise = user_data;
2019 GError *error = NULL;
2020
2021 if (!g_file_make_symbolic_link_finish (G_FILE (object), result, &error))
2022 dex_promise_reject (promise, g_steal_pointer (&error));
2023 else
2024 dex_promise_resolve_boolean (promise, TRUE);
2025
2026 dex_unref (promise);
2027 }
2028
2029 /**
2030 * dex_file_make_symbolic_link:
2031 * @file: a [[email protected]]
2032 * @symlink_value: the path for the target of the symbolic link
2033 * @io_priority: priority for the IO operation
2034 *
2035 * Wraps [[email protected]_symbolic_link_async].
2036 *
2037 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
2038 * or rejects with error.
2039 *
2040 * Since: 1.2
2041 */
2042 DexFuture *
2043 dex_file_make_symbolic_link (GFile *file,
2044 const char *symlink_value,
2045 int io_priority)
2046 {
2047 DexPromise *promise;
2048
2049 dex_return_error_if_fail (G_IS_FILE (file));
2050 dex_return_error_if_fail (symlink_value != NULL);
2051
2052 promise = dex_promise_new_cancellable ();
2053 g_file_make_symbolic_link_async (file,
2054 symlink_value,
2055 io_priority,
2056 dex_promise_get_cancellable (promise),
2057 dex_file_make_symbolic_link_cb,
2058 dex_ref (promise));
2059 return DEX_FUTURE (promise);
2060 }
2061
2062 typedef struct
2063 {
2064 GFile *file;
2065 DexPromise *promise;
2066 } MakeDirectory;
2067
2068 static void
2069 dex_file_make_directory_with_parents_worker (gpointer data)
2070 {
2071 MakeDirectory *state = data;
2072 GCancellable *cancellable = dex_promise_get_cancellable (state->promise);
2073 GError *error = NULL;
2074
2075 if (!g_file_make_directory_with_parents (state->file, cancellable, &error))
2076 {
2077 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS) &&
2078 g_file_query_file_type (state->file, G_FILE_QUERY_INFO_NONE, NULL) == G_FILE_TYPE_DIRECTORY)
2079 {
2080 g_clear_error (&error);
2081 dex_promise_resolve_boolean (state->promise, TRUE);
2082 }
2083 else
2084 {
2085 dex_promise_reject (state->promise, g_steal_pointer (&error));
2086 }
2087 }
2088 else
2089 {
2090 dex_promise_resolve_boolean (state->promise, TRUE);
2091 }
2092
2093 g_clear_object (&state->file);
2094 dex_clear (&state->promise);
2095 g_free (state);
2096 }
2097
2098 /**
2099 * dex_file_make_directory_with_parents:
2100 * @file: a [[email protected]]
2101 *
2102 * Creates a directory at @file.
2103 *
2104 * If @file already exists and is a directory, then the future
2105 * will resolve to %TRUE.
2106 *
2107 * Returns: (transfer full): a [[email protected]] that resolves to
2108 * a boolean or rejects with error.
2109 *
2110 * Since: 1.0
2111 */
2112 DexFuture *
2113 dex_file_make_directory_with_parents (GFile *file)
2114 {
2115 MakeDirectory *state;
2116 DexPromise *promise;
2117
2118 dex_return_error_if_fail (G_IS_FILE (file));
2119
2120 promise = dex_promise_new_cancellable ();
2121
2122 state = g_new0 (MakeDirectory, 1);
2123 state->file = g_object_ref (file);
2124 state->promise = dex_ref (promise);
2125
2126 dex_scheduler_push (dex_thread_pool_scheduler_get_default (),
2127 dex_file_make_directory_with_parents_worker,
2128 state);
2129
2130 return DEX_FUTURE (promise);
2131 }
2132
2133 static void
2134 dex_file_enumerate_children_cb (GObject *object,
2135 GAsyncResult *result,
2136 gpointer user_data)
2137 {
2138 DexAsyncPair *async_pair = user_data;
2139 GError *error = NULL;
2140 GFileEnumerator *enumerator;
2141
2142 enumerator = g_file_enumerate_children_finish (G_FILE (object), result, &error);
2143
2144 if (error == NULL)
2145 dex_async_pair_return_object (async_pair, enumerator);
2146 else
2147 dex_async_pair_return_error (async_pair, error);
2148
2149 dex_unref (async_pair);
2150 }
2151
2152 /**
2153 * dex_file_enumerate_children:
2154 * @file: a [[email protected]]
2155 * @attributes: an attribute query string (see: [`GFile` documentation]([email protected]))
2156 * @flags: a set of [[email protected]]
2157 * @io_priority: the [IO priority][[email protected]#io-priority] of the
2158 * request
2159 *
2160 * Returns: (transfer full): a [[email protected]] that resolves to a
2161 * [[email protected]] or rejects with error.
2162 */
2163 DexFuture *
2164 dex_file_enumerate_children (GFile *file,
2165 const char *attributes,
2166 GFileQueryInfoFlags flags,
2167 int io_priority)
2168 {
2169 DexAsyncPair *async_pair;
2170
2171 g_return_val_if_fail (G_IS_FILE (file), NULL);
2172
2173 async_pair = create_async_pair (G_STRFUNC);
2174
2175 g_file_enumerate_children_async (file,
2176 attributes,
2177 flags,
2178 io_priority,
2179 async_pair->cancellable,
2180 dex_file_enumerate_children_cb,
2181 dex_ref (async_pair));
2182
2183 return DEX_FUTURE (async_pair);
2184 }
2185
2186 static void
2187 dex_file_enumerator_next_files_cb (GObject *object,
2188 GAsyncResult *result,
2189 gpointer user_data)
2190 {
2191 DexAsyncPair *async_pair = user_data;
2192 GError *error = NULL;
2193 GList *infos;
2194
2195 infos = g_file_enumerator_next_files_finish (G_FILE_ENUMERATOR (object), result, &error);
2196
2197 if (error == NULL)
2198 dex_async_pair_return_boxed (async_pair, DEX_TYPE_FILE_INFO_LIST, infos);
2199 else
2200 dex_async_pair_return_error (async_pair, error);
2201
2202 dex_unref (async_pair);
2203 }
2204
2205 /**
2206 * dex_file_enumerator_next_files:
2207 * @file_enumerator: a [[email protected]]
2208 * @num_files: the number of files
2209 * @io_priority: the [IO priority][[email protected]#io-priority] of the
2210 * request
2211 *
2212 * Wraps [[email protected]_files_async].
2213 *
2214 * Use [[email protected]_boxed] to await for the result of this function.
2215 *
2216 * When on a fiber, you can do:
2217 *
2218 * ```c
2219 * g_autolist(GFileInfo) infos = dex_await_boxed (dex_file_enumerator_next_files (enumerator, 100, 0), &error);
2220 * ```
2221 *
2222 * Returns: (transfer full): a [[email protected]] that resolves to
2223 * a [[email protected]] of [[email protected]]
2224 */
2225 DexFuture *
2226 dex_file_enumerator_next_files (GFileEnumerator *file_enumerator,
2227 int num_files,
2228 int io_priority)
2229 {
2230 DexAsyncPair *async_pair;
2231
2232 g_return_val_if_fail (G_IS_FILE_ENUMERATOR (file_enumerator), NULL);
2233
2234 async_pair = create_async_pair (G_STRFUNC);
2235
2236 g_file_enumerator_next_files_async (file_enumerator,
2237 num_files,
2238 io_priority,
2239 async_pair->cancellable,
2240 dex_file_enumerator_next_files_cb,
2241 dex_ref (async_pair));
2242
2243 return DEX_FUTURE (async_pair);
2244 }
2245
2246 static void
2247 dex_file_enumerator_close_cb (GObject *object,
2248 GAsyncResult *result,
2249 gpointer user_data)
2250 {
2251 DexPromise *promise = user_data;
2252 GError *error = NULL;
2253
2254 if (!g_file_enumerator_close_finish (G_FILE_ENUMERATOR (object), result, &error))
2255 dex_promise_reject (promise, g_steal_pointer (&error));
2256 else
2257 dex_promise_resolve_boolean (promise, TRUE);
2258
2259 dex_unref (promise);
2260 }
2261
2262 /**
2263 * dex_file_enumerator_close:
2264 * @file_enumerator: a [[email protected]]
2265 * @io_priority: priority for the IO operation
2266 *
2267 * Wraps [[email protected]_async].
2268 *
2269 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
2270 * or rejects with error.
2271 *
2272 * Since: 1.2
2273 */
2274 DexFuture *
2275 dex_file_enumerator_close (GFileEnumerator *file_enumerator,
2276 int io_priority)
2277 {
2278 DexPromise *promise;
2279
2280 dex_return_error_if_fail (G_IS_FILE_ENUMERATOR (file_enumerator));
2281
2282 promise = dex_promise_new_cancellable ();
2283
2284 g_file_enumerator_close_async (file_enumerator,
2285 io_priority,
2286 dex_promise_get_cancellable (promise),
2287 dex_file_enumerator_close_cb,
2288 dex_ref (promise));
2289
2290 return DEX_FUTURE (promise);
2291 }
2292
2293 static void
2294 dex_file_input_stream_query_info_cb (GObject *object,
2295 GAsyncResult *result,
2296 gpointer user_data)
2297 {
2298 DexPromise *promise = user_data;
2299 GError *error = NULL;
2300 GFileInfo *info;
2301
2302 if (!(info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (object), result, &error)))
2303 dex_promise_reject (promise, g_steal_pointer (&error));
2304 else
2305 dex_promise_resolve_object (promise, g_steal_pointer (&info));
2306
2307 dex_unref (promise);
2308 }
2309
2310 /**
2311 * dex_file_input_stream_query_info:
2312 * @stream: a [[email protected]]
2313 * @attributes: file attributes to query
2314 * @io_priority: priority for the IO operation
2315 *
2316 * Wraps [[email protected]_info_async].
2317 *
2318 * Returns: (transfer full): a [[email protected]] that resolves to a
2319 * [[email protected]] or rejects with error.
2320 *
2321 * Since: 1.2
2322 */
2323 DexFuture *
2324 dex_file_input_stream_query_info (GFileInputStream *stream,
2325 const char *attributes,
2326 int io_priority)
2327 {
2328 DexPromise *promise;
2329
2330 dex_return_error_if_fail (G_IS_FILE_INPUT_STREAM (stream));
2331 dex_return_error_if_fail (attributes != NULL);
2332
2333 promise = dex_promise_new_cancellable ();
2334
2335 g_file_input_stream_query_info_async (stream,
2336 attributes,
2337 io_priority,
2338 dex_promise_get_cancellable (promise),
2339 dex_file_input_stream_query_info_cb,
2340 dex_ref (promise));
2341
2342 return DEX_FUTURE (promise);
2343 }
2344
2345 static void
2346 dex_file_io_stream_query_info_cb (GObject *object,
2347 GAsyncResult *result,
2348 gpointer user_data)
2349 {
2350 DexPromise *promise = user_data;
2351 GError *error = NULL;
2352 GFileInfo *info;
2353
2354 if (!(info = g_file_io_stream_query_info_finish (G_FILE_IO_STREAM (object), result, &error)))
2355 dex_promise_reject (promise, g_steal_pointer (&error));
2356 else
2357 dex_promise_resolve_object (promise, g_steal_pointer (&info));
2358
2359 dex_unref (promise);
2360 }
2361
2362 /**
2363 * dex_file_io_stream_query_info:
2364 * @stream: a [[email protected]]
2365 * @attributes: file attributes to query
2366 * @io_priority: priority for the IO operation
2367 *
2368 * Wraps [[email protected]_info_async].
2369 *
2370 * Returns: (transfer full): a [[email protected]] that resolves to a
2371 * [[email protected]] or rejects with error.
2372 *
2373 * Since: 1.2
2374 */
2375 DexFuture *
2376 dex_file_io_stream_query_info (GFileIOStream *stream,
2377 const char *attributes,
2378 int io_priority)
2379 {
2380 DexPromise *promise;
2381
2382 dex_return_error_if_fail (G_IS_FILE_IO_STREAM (stream));
2383 dex_return_error_if_fail (attributes != NULL);
2384
2385 promise = dex_promise_new_cancellable ();
2386
2387 g_file_io_stream_query_info_async (stream,
2388 attributes,
2389 io_priority,
2390 dex_promise_get_cancellable (promise),
2391 dex_file_io_stream_query_info_cb,
2392 dex_ref (promise));
2393
2394 return DEX_FUTURE (promise);
2395 }
2396
2397 static void
2398 dex_file_output_stream_query_info_cb (GObject *object,
2399 GAsyncResult *result,
2400 gpointer user_data)
2401 {
2402 DexPromise *promise = user_data;
2403 GError *error = NULL;
2404 GFileInfo *info;
2405
2406 if (!(info = g_file_output_stream_query_info_finish (G_FILE_OUTPUT_STREAM (object), result, &error)))
2407 dex_promise_reject (promise, g_steal_pointer (&error));
2408 else
2409 dex_promise_resolve_object (promise, g_steal_pointer (&info));
2410
2411 dex_unref (promise);
2412 }
2413
2414 /**
2415 * dex_file_output_stream_query_info:
2416 * @stream: a [[email protected]]
2417 * @attributes: file attributes to query
2418 * @io_priority: priority for the IO operation
2419 *
2420 * Wraps [[email protected]_info_async].
2421 *
2422 * Returns: (transfer full): a [[email protected]] that resolves to a
2423 * [[email protected]] or rejects with error.
2424 *
2425 * Since: 1.2
2426 */
2427 DexFuture *
2428 dex_file_output_stream_query_info (GFileOutputStream *stream,
2429 const char *attributes,
2430 int io_priority)
2431 {
2432 DexPromise *promise;
2433
2434 dex_return_error_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
2435 dex_return_error_if_fail (attributes != NULL);
2436
2437 promise = dex_promise_new_cancellable ();
2438
2439 g_file_output_stream_query_info_async (stream,
2440 attributes,
2441 io_priority,
2442 dex_promise_get_cancellable (promise),
2443 dex_file_output_stream_query_info_cb,
2444 dex_ref (promise));
2445
2446 return DEX_FUTURE (promise);
2447 }
2448
2449 static void
2450 dex_file_copy_cb (GObject *object,
2451 GAsyncResult *result,
2452 gpointer user_data)
2453 {
2454 DexAsyncPair *async_pair = user_data;
2455 GError *error = NULL;
2456
2457 g_file_copy_finish (G_FILE (object), result, &error);
2458
2459 if (error == NULL)
2460 dex_async_pair_return_boolean (async_pair, TRUE);
2461 else
2462 dex_async_pair_return_error (async_pair, error);
2463
2464 dex_unref (async_pair);
2465 }
2466
2467 /**
2468 * dex_file_copy:
2469 * @source: a [[email protected]]
2470 * @destination: a [[email protected]]
2471 * @flags: a set of [[email protected]]
2472 * @io_priority: the [IO priority][[email protected]#io-priority] of the
2473 * request
2474 *
2475 * Asynchronously copies a file and returns a [[email protected]] which
2476 * can be observed for the result.
2477 *
2478 * Returns: (transfer full): a [[email protected]] that resolves
2479 * to true if successful otherwise rejects with error.
2480 */
2481 DexFuture *
2482 dex_file_copy (GFile *source,
2483 GFile *destination,
2484 GFileCopyFlags flags,
2485 int io_priority)
2486 {
2487 DexAsyncPair *async_pair;
2488
2489 g_return_val_if_fail (G_IS_FILE (source), NULL);
2490 g_return_val_if_fail (G_IS_FILE (destination), NULL);
2491
2492 async_pair = create_async_pair (G_STRFUNC);
2493
2494 g_file_copy_async (source,
2495 destination,
2496 flags,
2497 io_priority,
2498 async_pair->cancellable,
2499 NULL, NULL, /* TODO: Progress support */
2500 dex_file_copy_cb,
2501 dex_ref (async_pair));
2502
2503 return DEX_FUTURE (async_pair);
2504 }
2505
2506 typedef struct
2507 {
2508 GDestroyNotify notify;
2509 gpointer data;
2510 DexPromise *promise;
2511 } FileCopy;
2512
2513 static void
2514 dex_file_copy_with_progress_cb (GObject *object,
2515 GAsyncResult *result,
2516 gpointer user_data)
2517 {
2518 FileCopy *state = user_data;
2519 GError *error = NULL;
2520
2521 if (!g_file_copy_finish (G_FILE (object), result, &error))
2522 dex_promise_reject (state->promise, g_steal_pointer (&error));
2523 else
2524 dex_promise_resolve_boolean (state->promise, TRUE);
2525
2526 if (state->notify != NULL)
2527 state->notify (state->data);
2528 dex_clear (&state->promise);
2529 g_free (state);
2530 }
2531
2532 /**
2533 * dex_file_copy_with_progress:
2534 * @source: source [[email protected]]
2535 * @destination: destination [[email protected]]
2536 * @flags: a set of [[email protected]]
2537 * @io_priority: the [IO priority][[email protected]#io-priority] of the
2538 * request
2539 * @progress_callback: (nullable) (scope notified) (closure progress_callback_data):
2540 * [[email protected]] function for updates
2541 * @progress_callback_data: `gpointer` to user data for the callback function
2542 * @progress_callback_data_destroy: (nullable): a function to destroy the
2543 * @progress_callback_data, or %NULL
2544 *
2545 * Wraps [[email protected]_async] with progress callback support.
2546 *
2547 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
2548 * or rejects with error
2549 *
2550 * Since: 1.2
2551 */
2552 DexFuture *
2553 dex_file_copy_with_progress (GFile *source,
2554 GFile *destination,
2555 GFileCopyFlags flags,
2556 int io_priority,
2557 GFileProgressCallback progress_callback,
2558 gpointer progress_callback_data,
2559 GDestroyNotify progress_callback_data_destroy)
2560 {
2561 FileCopy *state;
2562 DexPromise *promise;
2563
2564 dex_return_error_if_fail (G_IS_FILE (source));
2565 dex_return_error_if_fail (G_IS_FILE (destination));
2566
2567 promise = dex_promise_new_cancellable ();
2568
2569 state = g_new0 (FileCopy, 1);
2570 state->data = progress_callback_data;
2571 state->notify = progress_callback_data_destroy;
2572 state->promise = dex_ref (promise);
2573
2574 g_file_copy_async (source,
2575 destination,
2576 flags,
2577 io_priority,
2578 dex_promise_get_cancellable (promise),
2579 progress_callback,
2580 progress_callback_data,
2581 dex_file_copy_with_progress_cb,
2582 state);
2583
2584 return DEX_FUTURE (promise);
2585 }
2586
2587 static void
2588 dex_file_delete_cb (GObject *object,
2589 GAsyncResult *result,
2590 gpointer user_data)
2591 {
2592 DexPromise *promise = user_data;
2593 GError *error = NULL;
2594
2595 g_file_delete_finish (G_FILE (object), result, &error);
2596
2597 if (error == NULL)
2598 dex_promise_resolve_boolean (promise, TRUE);
2599 else
2600 dex_promise_reject (promise, error);
2601
2602 dex_unref (promise);
2603 }
2604
2605 /**
2606 * dex_file_delete:
2607 * @file: a [[email protected]]
2608 * @io_priority: the [IO priority][[email protected]#io-priority] of the
2609 * request
2610 *
2611 * Asynchronously deletes a file and returns a [[email protected]] which
2612 * can be observed for the result.
2613 *
2614 * Returns: (transfer full): a [[email protected]] that resolves
2615 * to true or rejects with error.
2616 */
2617 DexFuture *
2618 dex_file_delete (GFile *file,
2619 int io_priority)
2620 {
2621 DexPromise *promise;
2622
2623 g_return_val_if_fail (G_IS_FILE (file), NULL);
2624
2625 promise = dex_promise_new_cancellable ();
2626
2627 g_file_delete_async (file,
2628 io_priority,
2629 dex_promise_get_cancellable (promise),
2630 dex_file_delete_cb,
2631 dex_ref (promise));
2632
2633 return DEX_FUTURE (promise);
2634 }
2635
2636 static void
2637 dex_file_trash_cb (GObject *object,
2638 GAsyncResult *result,
2639 gpointer user_data)
2640 {
2641 DexPromise *promise = user_data;
2642 GError *error = NULL;
2643
2644 if (!g_file_trash_finish (G_FILE (object), result, &error))
2645 dex_promise_reject (promise, g_steal_pointer (&error));
2646 else
2647 dex_promise_resolve_boolean (promise, TRUE);
2648
2649 dex_unref (promise);
2650 }
2651
2652 /**
2653 * dex_file_trash:
2654 * @file: a [[email protected]]
2655 * @io_priority: priority for the IO operation
2656 *
2657 * Wraps [[email protected]_async].
2658 *
2659 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
2660 * or rejects with error.
2661 *
2662 * Since: 1.2
2663 */
2664 DexFuture *
2665 dex_file_trash (GFile *file,
2666 int io_priority)
2667 {
2668 DexPromise *promise;
2669
2670 dex_return_error_if_fail (G_IS_FILE (file));
2671
2672 promise = dex_promise_new_cancellable ();
2673 g_file_trash_async (file,
2674 io_priority,
2675 dex_promise_get_cancellable (promise),
2676 dex_file_trash_cb,
2677 dex_ref (promise));
2678 return DEX_FUTURE (promise);
2679 }
2680
2681 /**
2682 * dex_socket_wait:
2683 * @socket: a [[email protected]]
2684 * @condition: a [[email protected]] to wait for
2685 *
2686 * Creates a [[email protected]] that resolves when @socket satisfies
2687 * @condition.
2688 *
2689 * The future resolves to the [[email protected]] reported by the socket
2690 * source. This may include additional conditions such as %G_IO_HUP or
2691 * %G_IO_ERR.
2692 *
2693 * Returns: (transfer full): a [[email protected]] that resolves to a
2694 * [[email protected]].
2695 *
2696 * Since: 1.2
2697 */
2698 DexFuture *
2699 dex_socket_wait (GSocket *socket,
2700 GIOCondition condition)
2701 {
2702 dex_return_error_if_fail (G_IS_SOCKET (socket));
2703 dex_return_error_if_fail (condition != 0);
2704
2705 return dex_socket_wait_new (socket, condition);
2706 }
2707
2708 static void
2709 dex_socket_listener_accept_cb (GObject *object,
2710 GAsyncResult *result,
2711 gpointer user_data)
2712 {
2713 DexAsyncPair *async_pair = user_data;
2714 GSocketConnection *conn;
2715 GError *error = NULL;
2716
2717 conn = g_socket_listener_accept_finish (G_SOCKET_LISTENER (object), result, NULL, &error);
2718
2719 if (error == NULL)
2720 dex_async_pair_return_object (async_pair, conn);
2721 else
2722 dex_async_pair_return_error (async_pair, error);
2723
2724 dex_unref (async_pair);
2725 }
2726
2727 /**
2728 * dex_socket_listener_accept:
2729 * @listener: a [[email protected]]
2730 *
2731 * Returns: (transfer full): a [[email protected]] that resolves to
2732 * a [[email protected]] or rejects with error.
2733 */
2734 DexFuture *
2735 dex_socket_listener_accept (GSocketListener *listener)
2736 {
2737 DexAsyncPair *async_pair;
2738
2739 g_return_val_if_fail (G_IS_SOCKET_LISTENER (listener), NULL);
2740
2741 async_pair = create_async_pair (G_STRFUNC);
2742
2743 g_socket_listener_accept_async (listener,
2744 async_pair->cancellable,
2745 dex_socket_listener_accept_cb,
2746 dex_ref (async_pair));
2747
2748 return DEX_FUTURE (async_pair);
2749 }
2750
2751 static void
2752 dex_socket_client_connect_cb (GObject *object,
2753 GAsyncResult *result,
2754 gpointer user_data)
2755 {
2756 DexAsyncPair *async_pair = user_data;
2757 GSocketConnection *conn;
2758 GError *error = NULL;
2759
2760 conn = g_socket_client_connect_finish (G_SOCKET_CLIENT (object), result, &error);
2761
2762 if (error == NULL)
2763 dex_async_pair_return_object (async_pair, conn);
2764 else
2765 dex_async_pair_return_error (async_pair, error);
2766
2767 dex_unref (async_pair);
2768 }
2769
2770 /**
2771 * dex_socket_client_connect:
2772 * @socket_client: a [[email protected]]
2773 * @socket_connectable: a [[email protected]]
2774 *
2775 * Returns: (transfer full): a [[email protected]] that resolves to a
2776 * [[email protected]] or rejects with error.
2777 */
2778 DexFuture *
2779 dex_socket_client_connect (GSocketClient *socket_client,
2780 GSocketConnectable *socket_connectable)
2781 {
2782 DexAsyncPair *async_pair;
2783
2784 g_return_val_if_fail (G_IS_SOCKET_CLIENT (socket_client), NULL);
2785 g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (socket_connectable), NULL);
2786
2787 async_pair = create_async_pair (G_STRFUNC);
2788
2789 g_socket_client_connect_async (socket_client,
2790 socket_connectable,
2791 async_pair->cancellable,
2792 dex_socket_client_connect_cb,
2793 dex_ref (async_pair));
2794
2795 return DEX_FUTURE (async_pair);
2796 }
2797
2798 static void
2799 dex_socket_client_connect_to_host_cb (GObject *object,
2800 GAsyncResult *result,
2801 gpointer user_data)
2802 {
2803 DexPromise *promise = user_data;
2804 GError *error = NULL;
2805 GSocketConnection *conn;
2806
2807 if (!(conn = g_socket_client_connect_to_host_finish (G_SOCKET_CLIENT (object), result, &error)))
2808 dex_promise_reject (promise, g_steal_pointer (&error));
2809 else
2810 dex_promise_resolve_object (promise, g_steal_pointer (&conn));
2811
2812 dex_unref (promise);
2813 }
2814
2815 /**
2816 * dex_socket_client_connect_to_host:
2817 * @socket_client: a [[email protected]]
2818 * @host_and_port: host and optional port to connect to
2819 * @default_port: default port to use
2820 *
2821 * Wraps [[email protected]_to_host_async].
2822 *
2823 * Returns: (transfer full): a [[email protected]] that resolves to a
2824 * [[email protected]] or rejects with error.
2825 *
2826 * Since: 1.2
2827 */
2828 DexFuture *
2829 dex_socket_client_connect_to_host (GSocketClient *socket_client,
2830 const char *host_and_port,
2831 guint16 default_port)
2832 {
2833 DexPromise *promise;
2834
2835 dex_return_error_if_fail (G_IS_SOCKET_CLIENT (socket_client));
2836 dex_return_error_if_fail (host_and_port != NULL);
2837
2838 promise = dex_promise_new_cancellable ();
2839
2840 g_socket_client_connect_to_host_async (socket_client,
2841 host_and_port,
2842 default_port,
2843 dex_promise_get_cancellable (promise),
2844 dex_socket_client_connect_to_host_cb,
2845 dex_ref (promise));
2846
2847 return DEX_FUTURE (promise);
2848 }
2849
2850 static void
2851 dex_socket_client_connect_to_service_cb (GObject *object,
2852 GAsyncResult *result,
2853 gpointer user_data)
2854 {
2855 DexPromise *promise = user_data;
2856 GError *error = NULL;
2857 GSocketConnection *conn;
2858
2859 if (!(conn = g_socket_client_connect_to_service_finish (G_SOCKET_CLIENT (object), result, &error)))
2860 dex_promise_reject (promise, g_steal_pointer (&error));
2861 else
2862 dex_promise_resolve_object (promise, g_steal_pointer (&conn));
2863
2864 dex_unref (promise);
2865 }
2866
2867 /**
2868 * dex_socket_client_connect_to_service:
2869 * @socket_client: a [[email protected]]
2870 * @domain: domain to connect to
2871 * @service: service to connect to
2872 *
2873 * Wraps [[email protected]_to_service_async].
2874 *
2875 * Returns: (transfer full): a [[email protected]] that resolves to a
2876 * [[email protected]] or rejects with error.
2877 *
2878 * Since: 1.2
2879 */
2880 DexFuture *
2881 dex_socket_client_connect_to_service (GSocketClient *socket_client,
2882 const char *domain,
2883 const char *service)
2884 {
2885 DexPromise *promise;
2886
2887 dex_return_error_if_fail (G_IS_SOCKET_CLIENT (socket_client));
2888 dex_return_error_if_fail (domain != NULL);
2889 dex_return_error_if_fail (service != NULL);
2890
2891 promise = dex_promise_new_cancellable ();
2892
2893 g_socket_client_connect_to_service_async (socket_client,
2894 domain,
2895 service,
2896 dex_promise_get_cancellable (promise),
2897 dex_socket_client_connect_to_service_cb,
2898 dex_ref (promise));
2899
2900 return DEX_FUTURE (promise);
2901 }
2902
2903 static void
2904 dex_socket_client_connect_to_uri_cb (GObject *object,
2905 GAsyncResult *result,
2906 gpointer user_data)
2907 {
2908 DexPromise *promise = user_data;
2909 GError *error = NULL;
2910 GSocketConnection *conn;
2911
2912 if (!(conn = g_socket_client_connect_to_uri_finish (G_SOCKET_CLIENT (object), result, &error)))
2913 dex_promise_reject (promise, g_steal_pointer (&error));
2914 else
2915 dex_promise_resolve_object (promise, g_steal_pointer (&conn));
2916
2917 dex_unref (promise);
2918 }
2919
2920 /**
2921 * dex_socket_client_connect_to_uri:
2922 * @socket_client: a [[email protected]]
2923 * @uri: URI to connect to
2924 * @default_port: default port to use
2925 *
2926 * Wraps [[email protected]_to_uri_async].
2927 *
2928 * Returns: (transfer full): a [[email protected]] that resolves to a
2929 * [[email protected]] or rejects with error.
2930 *
2931 * Since: 1.2
2932 */
2933 DexFuture *
2934 dex_socket_client_connect_to_uri (GSocketClient *socket_client,
2935 const char *uri,
2936 guint16 default_port)
2937 {
2938 DexPromise *promise;
2939
2940 dex_return_error_if_fail (G_IS_SOCKET_CLIENT (socket_client));
2941 dex_return_error_if_fail (uri != NULL);
2942
2943 promise = dex_promise_new_cancellable ();
2944
2945 g_socket_client_connect_to_uri_async (socket_client,
2946 uri,
2947 default_port,
2948 dex_promise_get_cancellable (promise),
2949 dex_socket_client_connect_to_uri_cb,
2950 dex_ref (promise));
2951
2952 return DEX_FUTURE (promise);
2953 }
2954
2955 static void
2956 dex_socket_connection_connect_cb (GObject *object,
2957 GAsyncResult *result,
2958 gpointer user_data)
2959 {
2960 DexPromise *promise = user_data;
2961 GError *error = NULL;
2962
2963 if (!g_socket_connection_connect_finish (G_SOCKET_CONNECTION (object), result, &error))
2964 dex_promise_reject (promise, g_steal_pointer (&error));
2965 else
2966 dex_promise_resolve_boolean (promise, TRUE);
2967
2968 dex_unref (promise);
2969 }
2970
2971 /**
2972 * dex_socket_connection_connect:
2973 * @connection: a [[email protected]]
2974 * @address: a [[email protected]]
2975 *
2976 * Wraps [[email protected]_async].
2977 *
2978 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
2979 * or rejects with error.
2980 *
2981 * Since: 1.2
2982 */
2983 DexFuture *
2984 dex_socket_connection_connect (GSocketConnection *connection,
2985 GSocketAddress *address)
2986 {
2987 DexPromise *promise;
2988
2989 dex_return_error_if_fail (G_IS_SOCKET_CONNECTION (connection));
2990 dex_return_error_if_fail (G_IS_SOCKET_ADDRESS (address));
2991
2992 promise = dex_promise_new_cancellable ();
2993
2994 g_socket_connection_connect_async (connection,
2995 address,
2996 dex_promise_get_cancellable (promise),
2997 dex_socket_connection_connect_cb,
2998 dex_ref (promise));
2999
3000 return DEX_FUTURE (promise);
3001 }
3002
3003 static void
3004 dex_socket_address_enumerator_next_cb (GObject *object,
3005 GAsyncResult *result,
3006 gpointer user_data)
3007 {
3008 DexPromise *promise = user_data;
3009 GError *error = NULL;
3010 GSocketAddress *address;
3011
3012 if (!(address = g_socket_address_enumerator_next_finish (G_SOCKET_ADDRESS_ENUMERATOR (object), result, &error)))
3013 {
3014 if (error != NULL)
3015 dex_promise_reject (promise, g_steal_pointer (&error));
3016 else
3017 dex_promise_resolve_object (promise, NULL);
3018 }
3019 else
3020 {
3021 dex_promise_resolve_object (promise, g_steal_pointer (&address));
3022 }
3023
3024 dex_unref (promise);
3025 }
3026
3027 /**
3028 * dex_socket_address_enumerator_next:
3029 * @enumerator: a [[email protected]]
3030 *
3031 * Wraps [[email protected]_async].
3032 *
3033 * Returns: (transfer full): a [[email protected]] that resolves to a
3034 * [[email protected]], %NULL at the end of the enumeration, or
3035 * rejects with error.
3036 *
3037 * Since: 1.2
3038 */
3039 DexFuture *
3040 dex_socket_address_enumerator_next (GSocketAddressEnumerator *enumerator)
3041 {
3042 DexPromise *promise;
3043
3044 dex_return_error_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator));
3045
3046 promise = dex_promise_new_cancellable ();
3047
3048 g_socket_address_enumerator_next_async (enumerator,
3049 dex_promise_get_cancellable (promise),
3050 dex_socket_address_enumerator_next_cb,
3051 dex_ref (promise));
3052
3053 return DEX_FUTURE (promise);
3054 }
3055
3056 static void
3057 dex_io_stream_close_cb (GObject *object,
3058 GAsyncResult *result,
3059 gpointer user_data)
3060 {
3061 DexAsyncPair *async_pair = user_data;
3062 GError *error = NULL;
3063
3064 g_io_stream_close_finish (G_IO_STREAM (object), result, &error);
3065
3066 if (error == NULL)
3067 dex_async_pair_return_boolean (async_pair, TRUE);
3068 else
3069 dex_async_pair_return_error (async_pair, error);
3070
3071 dex_unref (async_pair);
3072 }
3073
3074 /**
3075 * dex_io_stream_close:
3076 * @io_stream: a [[email protected]]
3077 * @io_priority: the [IO priority][[email protected]#io-priority] of the
3078 * request
3079 *
3080 * Returns: (transfer full): a [[email protected]] that resolves to
3081 * true or rejects with error.
3082 */
3083 DexFuture *
3084 dex_io_stream_close (GIOStream *io_stream,
3085 int io_priority)
3086 {
3087 DexAsyncPair *async_pair;
3088
3089 g_return_val_if_fail (G_IS_IO_STREAM (io_stream), NULL);
3090
3091 async_pair = create_async_pair (G_STRFUNC);
3092
3093 g_io_stream_close_async (io_stream,
3094 io_priority,
3095 async_pair->cancellable,
3096 dex_io_stream_close_cb,
3097 dex_ref (async_pair));
3098
3099 return DEX_FUTURE (async_pair);
3100 }
3101
3102 static void
3103 dex_io_stream_splice_cb (GObject *object,
3104 GAsyncResult *result,
3105 gpointer user_data)
3106 {
3107 DexPromise *promise = user_data;
3108 GError *error = NULL;
3109
3110 g_assert (object == NULL);
3111
3112 if (!g_io_stream_splice_finish (result, &error))
3113 dex_promise_reject (promise, g_steal_pointer (&error));
3114 else
3115 dex_promise_resolve_boolean (promise, TRUE);
3116
3117 dex_unref (promise);
3118 }
3119
3120 /**
3121 * dex_io_stream_splice:
3122 * @stream1: a [[email protected]]
3123 * @stream2: a [[email protected]]
3124 * @flags: splice flags
3125 * @io_priority: priority for the IO operation
3126 *
3127 * Wraps [[email protected]_async].
3128 *
3129 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
3130 * or rejects with error.
3131 *
3132 * Since: 1.2
3133 */
3134 DexFuture *
3135 dex_io_stream_splice (GIOStream *stream1,
3136 GIOStream *stream2,
3137 GIOStreamSpliceFlags flags,
3138 int io_priority)
3139 {
3140 DexPromise *promise;
3141
3142 dex_return_error_if_fail (G_IS_IO_STREAM (stream1));
3143 dex_return_error_if_fail (G_IS_IO_STREAM (stream2));
3144
3145 promise = dex_promise_new_cancellable ();
3146
3147 g_io_stream_splice_async (stream1,
3148 stream2,
3149 flags,
3150 io_priority,
3151 dex_promise_get_cancellable (promise),
3152 dex_io_stream_splice_cb,
3153 dex_ref (promise));
3154
3155 return DEX_FUTURE (promise);
3156 }
3157
3158 static void
3159 dex_tls_connection_handshake_cb (GObject *object,
3160 GAsyncResult *result,
3161 gpointer user_data)
3162 {
3163 DexPromise *promise = user_data;
3164 GError *error = NULL;
3165
3166 g_assert (G_IS_TLS_CONNECTION (object));
3167 g_assert (G_IS_ASYNC_RESULT (result));
3168 g_assert (DEX_IS_PROMISE (promise));
3169
3170 if (g_tls_connection_handshake_finish (G_TLS_CONNECTION (object), result, &error))
3171 dex_promise_resolve_boolean (promise, TRUE);
3172 else
3173 dex_promise_reject (promise, g_steal_pointer (&error));
3174
3175 dex_unref (promise);
3176 }
3177
3178 /**
3179 * dex_tls_connection_handshake:
3180 * @tls_connection: a [[email protected]]
3181 * @io_priority: the [IO priority][[email protected]#io-priority] of the
3182 * request
3183 *
3184 * Returns: (transfer full): a [[email protected]] that resolves to
3185 * true or rejects with error.
3186 *
3187 * Since: 1.2
3188 */
3189 DexFuture *
3190 dex_tls_connection_handshake (GTlsConnection *tls_connection,
3191 int io_priority)
3192 {
3193 DexPromise *promise;
3194
3195 g_return_val_if_fail (G_IS_TLS_CONNECTION (tls_connection), NULL);
3196
3197 promise = dex_promise_new_cancellable ();
3198
3199 g_tls_connection_handshake_async (tls_connection,
3200 io_priority,
3201 dex_promise_get_cancellable (promise),
3202 dex_tls_connection_handshake_cb,
3203 dex_ref (promise));
3204
3205 return DEX_FUTURE (promise);
3206 }
3207
3208 static void
3209 dex_dtls_connection_handshake_cb (GObject *object,
3210 GAsyncResult *result,
3211 gpointer user_data)
3212 {
3213 DexPromise *promise = user_data;
3214 GError *error = NULL;
3215
3216 if (!g_dtls_connection_handshake_finish (G_DTLS_CONNECTION (object), result, &error))
3217 dex_promise_reject (promise, g_steal_pointer (&error));
3218 else
3219 dex_promise_resolve_boolean (promise, TRUE);
3220
3221 dex_unref (promise);
3222 }
3223
3224 /**
3225 * dex_dtls_connection_handshake:
3226 * @dtls_connection: a [[email protected]]
3227 * @io_priority: priority for the IO operation
3228 *
3229 * Wraps [[email protected]_async].
3230 *
3231 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
3232 * or rejects with error.
3233 *
3234 * Since: 1.2
3235 */
3236 DexFuture *
3237 dex_dtls_connection_handshake (GDtlsConnection *dtls_connection,
3238 int io_priority)
3239 {
3240 DexPromise *promise;
3241
3242 dex_return_error_if_fail (G_IS_DTLS_CONNECTION (dtls_connection));
3243
3244 promise = dex_promise_new_cancellable ();
3245
3246 g_dtls_connection_handshake_async (dtls_connection,
3247 io_priority,
3248 dex_promise_get_cancellable (promise),
3249 dex_dtls_connection_handshake_cb,
3250 dex_ref (promise));
3251
3252 return DEX_FUTURE (promise);
3253 }
3254
3255 static void
3256 dex_dtls_connection_shutdown_cb (GObject *object,
3257 GAsyncResult *result,
3258 gpointer user_data)
3259 {
3260 DexPromise *promise = user_data;
3261 GError *error = NULL;
3262
3263 if (!g_dtls_connection_shutdown_finish (G_DTLS_CONNECTION (object), result, &error))
3264 dex_promise_reject (promise, g_steal_pointer (&error));
3265 else
3266 dex_promise_resolve_boolean (promise, TRUE);
3267
3268 dex_unref (promise);
3269 }
3270
3271 /**
3272 * dex_dtls_connection_shutdown:
3273 * @dtls_connection: a [[email protected]]
3274 * @shutdown_read: whether to shut down the read side
3275 * @shutdown_write: whether to shut down the write side
3276 * @io_priority: priority for the IO operation
3277 *
3278 * Wraps [[email protected]_async].
3279 *
3280 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
3281 * or rejects with error.
3282 *
3283 * Since: 1.2
3284 */
3285 DexFuture *
3286 dex_dtls_connection_shutdown (GDtlsConnection *dtls_connection,
3287 gboolean shutdown_read,
3288 gboolean shutdown_write,
3289 int io_priority)
3290 {
3291 DexPromise *promise;
3292
3293 dex_return_error_if_fail (G_IS_DTLS_CONNECTION (dtls_connection));
3294
3295 promise = dex_promise_new_cancellable ();
3296
3297 g_dtls_connection_shutdown_async (dtls_connection,
3298 shutdown_read,
3299 shutdown_write,
3300 io_priority,
3301 dex_promise_get_cancellable (promise),
3302 dex_dtls_connection_shutdown_cb,
3303 dex_ref (promise));
3304
3305 return DEX_FUTURE (promise);
3306 }
3307
3308 static void
3309 dex_dtls_connection_close_cb (GObject *object,
3310 GAsyncResult *result,
3311 gpointer user_data)
3312 {
3313 DexPromise *promise = user_data;
3314 GError *error = NULL;
3315
3316 if (!g_dtls_connection_close_finish (G_DTLS_CONNECTION (object), result, &error))
3317 dex_promise_reject (promise, g_steal_pointer (&error));
3318 else
3319 dex_promise_resolve_boolean (promise, TRUE);
3320
3321 dex_unref (promise);
3322 }
3323
3324 /**
3325 * dex_dtls_connection_close:
3326 * @dtls_connection: a [[email protected]]
3327 * @io_priority: priority for the IO operation
3328 *
3329 * Wraps [[email protected]_async].
3330 *
3331 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
3332 * or rejects with error.
3333 *
3334 * Since: 1.2
3335 */
3336 DexFuture *
3337 dex_dtls_connection_close (GDtlsConnection *dtls_connection,
3338 int io_priority)
3339 {
3340 DexPromise *promise;
3341
3342 dex_return_error_if_fail (G_IS_DTLS_CONNECTION (dtls_connection));
3343
3344 promise = dex_promise_new_cancellable ();
3345
3346 g_dtls_connection_close_async (dtls_connection,
3347 io_priority,
3348 dex_promise_get_cancellable (promise),
3349 dex_dtls_connection_close_cb,
3350 dex_ref (promise));
3351
3352 return DEX_FUTURE (promise);
3353 }
3354
3355 static void
3356 dex_resolver_lookup_by_name_cb (GObject *object,
3357 GAsyncResult *result,
3358 gpointer user_data)
3359 {
3360 DexAsyncPair *async_pair = user_data;
3361 GError *error = NULL;
3362 GList *list;
3363
3364 list = g_resolver_lookup_by_name_finish (G_RESOLVER (object), result, &error);
3365
3366 if (error == NULL)
3367 dex_async_pair_return_boxed (async_pair, DEX_TYPE_INET_ADDRESS_LIST, list);
3368 else
3369 dex_async_pair_return_error (async_pair, error);
3370
3371 dex_unref (async_pair);
3372 }
3373
3374 /**
3375 * dex_resolver_lookup_by_name:
3376 * @resolver: a [[email protected]]
3377 * @address: the address to look up
3378 *
3379 * Returns: (transfer full): a [[email protected]] that resolves to a
3380 * [[email protected]] of [[email protected]].
3381 */
3382 DexFuture *
3383 dex_resolver_lookup_by_name (GResolver *resolver,
3384 const char *address)
3385 {
3386 DexAsyncPair *async_pair;
3387
3388 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
3389 g_return_val_if_fail (address != NULL, NULL);
3390
3391 async_pair = create_async_pair (G_STRFUNC);
3392
3393 g_resolver_lookup_by_name_async (resolver,
3394 address,
3395 async_pair->cancellable,
3396 dex_resolver_lookup_by_name_cb,
3397 dex_ref (async_pair));
3398
3399 return DEX_FUTURE (async_pair);
3400 }
3401
3402 static void
3403 dex_resolver_lookup_by_name_with_flags_cb (GObject *object,
3404 GAsyncResult *result,
3405 gpointer user_data)
3406 {
3407 DexPromise *promise = user_data;
3408 GError *error = NULL;
3409 GList *list;
3410
3411 list = g_resolver_lookup_by_name_with_flags_finish (G_RESOLVER (object), result, &error);
3412
3413 if (error == NULL)
3414 dex_promise_resolve_boxed (promise, DEX_TYPE_INET_ADDRESS_LIST, list);
3415 else
3416 dex_promise_reject (promise, g_steal_pointer (&error));
3417
3418 dex_unref (promise);
3419 }
3420
3421 /**
3422 * dex_resolver_lookup_by_name_with_flags:
3423 * @resolver: a [[email protected]]
3424 * @address: hostname to look up
3425 * @flags: lookup flags
3426 *
3427 * Wraps [[email protected]_by_name_with_flags_async].
3428 *
3429 * Returns: (transfer full): a [[email protected]] that resolves to a
3430 * [[email protected]] of [[email protected]] or rejects with error.
3431 *
3432 * Since: 1.2
3433 */
3434 DexFuture *
3435 dex_resolver_lookup_by_name_with_flags (GResolver *resolver,
3436 const char *address,
3437 GResolverNameLookupFlags flags)
3438 {
3439 DexPromise *promise;
3440
3441 dex_return_error_if_fail (G_IS_RESOLVER (resolver));
3442 dex_return_error_if_fail (address != NULL);
3443
3444 promise = dex_promise_new_cancellable ();
3445
3446 g_resolver_lookup_by_name_with_flags_async (resolver,
3447 address,
3448 flags,
3449 dex_promise_get_cancellable (promise),
3450 dex_resolver_lookup_by_name_with_flags_cb,
3451 dex_ref (promise));
3452
3453 return DEX_FUTURE (promise);
3454 }
3455
3456 static void
3457 dex_resolver_lookup_by_address_cb (GObject *object,
3458 GAsyncResult *result,
3459 gpointer user_data)
3460 {
3461 DexPromise *promise = user_data;
3462 GError *error = NULL;
3463 char *name;
3464
3465 if (!(name = g_resolver_lookup_by_address_finish (G_RESOLVER (object), result, &error)))
3466 dex_promise_reject (promise, g_steal_pointer (&error));
3467 else
3468 dex_promise_resolve_string (promise, g_steal_pointer (&name));
3469
3470 dex_unref (promise);
3471 }
3472
3473 /**
3474 * dex_resolver_lookup_by_address:
3475 * @resolver: a [[email protected]]
3476 * @address: a [[email protected]]
3477 *
3478 * Wraps [[email protected]_by_address_async].
3479 *
3480 * Returns: (transfer full): a [[email protected]] that resolves to a hostname
3481 * string or rejects with error.
3482 *
3483 * Since: 1.2
3484 */
3485 DexFuture *
3486 dex_resolver_lookup_by_address (GResolver *resolver,
3487 GInetAddress *address)
3488 {
3489 DexPromise *promise;
3490
3491 dex_return_error_if_fail (G_IS_RESOLVER (resolver));
3492 dex_return_error_if_fail (G_IS_INET_ADDRESS (address));
3493
3494 promise = dex_promise_new_cancellable ();
3495
3496 g_resolver_lookup_by_address_async (resolver,
3497 address,
3498 dex_promise_get_cancellable (promise),
3499 dex_resolver_lookup_by_address_cb,
3500 dex_ref (promise));
3501
3502 return DEX_FUTURE (promise);
3503 }
3504
3505 static void
3506 dex_resolver_lookup_service_cb (GObject *object,
3507 GAsyncResult *result,
3508 gpointer user_data)
3509 {
3510 DexPromise *promise = user_data;
3511 GError *error = NULL;
3512 GList *list;
3513
3514 list = g_resolver_lookup_service_finish (G_RESOLVER (object), result, &error);
3515
3516 if (error == NULL)
3517 dex_promise_resolve_boxed (promise, DEX_TYPE_SRV_TARGET_LIST, list);
3518 else
3519 dex_promise_reject (promise, g_steal_pointer (&error));
3520
3521 dex_unref (promise);
3522 }
3523
3524 /**
3525 * dex_resolver_lookup_service:
3526 * @resolver: a [[email protected]]
3527 * @service: service name
3528 * @protocol: protocol name
3529 * @domain: domain name
3530 *
3531 * Wraps [[email protected]_service_async].
3532 *
3533 * Returns: (transfer full): a [[email protected]] that resolves to a
3534 * [[email protected]] of [[email protected]] or rejects with error.
3535 *
3536 * Since: 1.2
3537 */
3538 DexFuture *
3539 dex_resolver_lookup_service (GResolver *resolver,
3540 const char *service,
3541 const char *protocol,
3542 const char *domain)
3543 {
3544 DexPromise *promise;
3545
3546 dex_return_error_if_fail (G_IS_RESOLVER (resolver));
3547 dex_return_error_if_fail (service != NULL);
3548 dex_return_error_if_fail (protocol != NULL);
3549 dex_return_error_if_fail (domain != NULL);
3550
3551 promise = dex_promise_new_cancellable ();
3552
3553 g_resolver_lookup_service_async (resolver,
3554 service,
3555 protocol,
3556 domain,
3557 dex_promise_get_cancellable (promise),
3558 dex_resolver_lookup_service_cb,
3559 dex_ref (promise));
3560
3561 return DEX_FUTURE (promise);
3562 }
3563
3564 static void
3565 dex_resolver_lookup_records_cb (GObject *object,
3566 GAsyncResult *result,
3567 gpointer user_data)
3568 {
3569 DexPromise *promise = user_data;
3570 GError *error = NULL;
3571 GList *list;
3572
3573 list = g_resolver_lookup_records_finish (G_RESOLVER (object), result, &error);
3574
3575 if (error == NULL)
3576 dex_promise_resolve_boxed (promise, DEX_TYPE_VARIANT_LIST, list);
3577 else
3578 dex_promise_reject (promise, g_steal_pointer (&error));
3579
3580 dex_unref (promise);
3581 }
3582
3583 /**
3584 * dex_resolver_lookup_records:
3585 * @resolver: a [[email protected]]
3586 * @rrname: DNS record name
3587 * @record_type: DNS record type
3588 *
3589 * Wraps [[email protected]_records_async].
3590 *
3591 * Returns: (transfer full): a [[email protected]] that resolves to a
3592 * [[email protected]] of [[email protected]] or rejects with error.
3593 *
3594 * Since: 1.2
3595 */
3596 DexFuture *
3597 dex_resolver_lookup_records (GResolver *resolver,
3598 const char *rrname,
3599 GResolverRecordType record_type)
3600 {
3601 DexPromise *promise;
3602
3603 dex_return_error_if_fail (G_IS_RESOLVER (resolver));
3604 dex_return_error_if_fail (rrname != NULL);
3605
3606 promise = dex_promise_new_cancellable ();
3607
3608 g_resolver_lookup_records_async (resolver,
3609 rrname,
3610 record_type,
3611 dex_promise_get_cancellable (promise),
3612 dex_resolver_lookup_records_cb,
3613 dex_ref (promise));
3614
3615 return DEX_FUTURE (promise);
3616 }
3617
3618 static void
3619 dex_network_monitor_can_reach_cb (GObject *object,
3620 GAsyncResult *result,
3621 gpointer user_data)
3622 {
3623 DexPromise *promise = user_data;
3624 GError *error = NULL;
3625
3626 if (!g_network_monitor_can_reach_finish (G_NETWORK_MONITOR (object), result, &error))
3627 dex_promise_reject (promise, g_steal_pointer (&error));
3628 else
3629 dex_promise_resolve_boolean (promise, TRUE);
3630
3631 dex_unref (promise);
3632 }
3633
3634 /**
3635 * dex_network_monitor_can_reach:
3636 * @monitor: a [[email protected]]
3637 * @connectable: a [[email protected]]
3638 *
3639 * Wraps [[email protected]_reach_async].
3640 *
3641 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
3642 * or rejects with error.
3643 *
3644 * Since: 1.2
3645 */
3646 DexFuture *
3647 dex_network_monitor_can_reach (GNetworkMonitor *monitor,
3648 GSocketConnectable *connectable)
3649 {
3650 DexPromise *promise;
3651
3652 dex_return_error_if_fail (G_IS_NETWORK_MONITOR (monitor));
3653 dex_return_error_if_fail (G_IS_SOCKET_CONNECTABLE (connectable));
3654
3655 promise = dex_promise_new_cancellable ();
3656
3657 g_network_monitor_can_reach_async (monitor,
3658 connectable,
3659 dex_promise_get_cancellable (promise),
3660 dex_network_monitor_can_reach_cb,
3661 dex_ref (promise));
3662
3663 return DEX_FUTURE (promise);
3664 }
3665
3666 static void
3667 dex_proxy_connect_cb (GObject *object,
3668 GAsyncResult *result,
3669 gpointer user_data)
3670 {
3671 DexPromise *promise = user_data;
3672 GError *error = NULL;
3673 GIOStream *stream;
3674
3675 if (!(stream = g_proxy_connect_finish (G_PROXY (object), result, &error)))
3676 dex_promise_reject (promise, g_steal_pointer (&error));
3677 else
3678 dex_promise_resolve_object (promise, g_steal_pointer (&stream));
3679
3680 dex_unref (promise);
3681 }
3682
3683 /**
3684 * dex_proxy_connect:
3685 * @proxy: a [[email protected]]
3686 * @connection: a [[email protected]]
3687 * @proxy_address: a [[email protected]]
3688 *
3689 * Wraps [[email protected]_async].
3690 *
3691 * Returns: (transfer full): a [[email protected]] that resolves to a
3692 * [[email protected]] or rejects with error.
3693 *
3694 * Since: 1.2
3695 */
3696 DexFuture *
3697 dex_proxy_connect (GProxy *proxy,
3698 GIOStream *connection,
3699 GProxyAddress *proxy_address)
3700 {
3701 DexPromise *promise;
3702
3703 dex_return_error_if_fail (G_IS_PROXY (proxy));
3704 dex_return_error_if_fail (G_IS_IO_STREAM (connection));
3705 dex_return_error_if_fail (G_IS_PROXY_ADDRESS (proxy_address));
3706
3707 promise = dex_promise_new_cancellable ();
3708
3709 g_proxy_connect_async (proxy,
3710 connection,
3711 proxy_address,
3712 dex_promise_get_cancellable (promise),
3713 dex_proxy_connect_cb,
3714 dex_ref (promise));
3715
3716 return DEX_FUTURE (promise);
3717 }
3718
3719 static void
3720 dex_proxy_resolver_lookup_cb (GObject *object,
3721 GAsyncResult *result,
3722 gpointer user_data)
3723 {
3724 DexPromise *promise = user_data;
3725 GError *error = NULL;
3726 char **proxies;
3727
3728 if (!(proxies = g_proxy_resolver_lookup_finish (G_PROXY_RESOLVER (object), result, &error)))
3729 dex_promise_reject (promise, g_steal_pointer (&error));
3730 else
3731 dex_promise_resolve_boxed (promise, G_TYPE_STRV, g_steal_pointer (&proxies));
3732
3733 dex_unref (promise);
3734 }
3735
3736 /**
3737 * dex_proxy_resolver_lookup:
3738 * @resolver: a [[email protected]]
3739 * @uri: a URI
3740 *
3741 * Wraps [[email protected]_async].
3742 *
3743 * Returns: (transfer full): a [[email protected]] that resolves to a
3744 * string vector of proxy URIs or rejects with error.
3745 *
3746 * Since: 1.2
3747 */
3748 DexFuture *
3749 dex_proxy_resolver_lookup (GProxyResolver *resolver,
3750 const char *uri)
3751 {
3752 DexPromise *promise;
3753
3754 dex_return_error_if_fail (G_IS_PROXY_RESOLVER (resolver));
3755 dex_return_error_if_fail (uri != NULL);
3756
3757 promise = dex_promise_new_cancellable ();
3758
3759 g_proxy_resolver_lookup_async (resolver,
3760 uri,
3761 dex_promise_get_cancellable (promise),
3762 dex_proxy_resolver_lookup_cb,
3763 dex_ref (promise));
3764
3765 return DEX_FUTURE (promise);
3766 }
3767
3768 static void
3769 dex_file_load_contents_bytes_cb (GObject *object,
3770 GAsyncResult *result,
3771 gpointer user_data)
3772 {
3773 DexAsyncPair *async_pair = user_data;
3774 GError *error = NULL;
3775 char *contents = NULL;
3776 gsize len = 0;
3777
3778 g_file_load_contents_finish (G_FILE (object), result, &contents, &len, NULL, &error);
3779
3780 if (error == NULL)
3781 dex_async_pair_return_boxed (async_pair, G_TYPE_BYTES, g_bytes_new_take (contents, len));
3782 else
3783 dex_async_pair_return_error (async_pair, error);
3784
3785 dex_unref (async_pair);
3786 }
3787
3788 /**
3789 * dex_file_load_contents_bytes:
3790 * @file: a [[email protected]]
3791 *
3792 * Returns: (transfer full): a [[email protected]] that resolves
3793 * to a [[email protected]].
3794 */
3795 DexFuture *
3796 dex_file_load_contents_bytes (GFile *file)
3797 {
3798 DexAsyncPair *async_pair;
3799
3800 g_return_val_if_fail (G_IS_FILE (file), NULL);
3801
3802 async_pair = create_async_pair (G_STRFUNC);
3803
3804 g_file_load_contents_async (file,
3805 async_pair->cancellable,
3806 dex_file_load_contents_bytes_cb,
3807 dex_ref (async_pair));
3808
3809 return DEX_FUTURE (async_pair);
3810 }
3811
3812 typedef struct
3813 {
3814 GFileReadMoreCallback callback;
3815 GDestroyNotify notify;
3816 gpointer data;
3817 DexPromise *promise;
3818 } FileLoadPartialContents;
3819
3820 static gboolean
3821 dex_file_load_partial_contents_read_more_cb (const char *file_contents,
3822 goffset file_size,
3823 gpointer callback_data)
3824 {
3825 FileLoadPartialContents *state = callback_data;
3826
3827 g_assert (state != NULL);
3828
3829 if (state->callback == NULL)
3830 return FALSE;
3831
3832 return state->callback (file_contents, file_size, state->data);
3833 }
3834
3835 static void
3836 dex_file_load_partial_contents_free (FileLoadPartialContents *state)
3837 {
3838 if (state->notify != NULL)
3839 state->notify (state->data);
3840
3841 dex_clear (&state->promise);
3842 g_free (state);
3843 }
3844
3845 static void
3846 dex_file_load_partial_contents_bytes_cb (GObject *object,
3847 GAsyncResult *result,
3848 gpointer user_data)
3849 {
3850 FileLoadPartialContents *state = user_data;
3851 GError *error = NULL;
3852 char *contents = NULL;
3853 gsize len = 0;
3854
3855 if (!g_file_load_partial_contents_finish (G_FILE (object), result, &contents, &len, NULL, &error))
3856 dex_promise_reject (state->promise, g_steal_pointer (&error));
3857 else
3858 dex_promise_resolve_boxed (state->promise, G_TYPE_BYTES, g_bytes_new_take (contents, len));
3859
3860 dex_file_load_partial_contents_free (state);
3861 }
3862
3863 /**
3864 * dex_file_load_partial_contents_bytes:
3865 * @file: a [[email protected]]
3866 * @read_more_callback: (nullable) (scope notified) (closure read_more_callback_data):
3867 * callback to determine if more bytes should be loaded
3868 * @read_more_callback_data: user data for @read_more_callback
3869 * @read_more_callback_data_destroy: (nullable): destroys @read_more_callback_data
3870 *
3871 * Wraps [[email protected]_partial_contents_async].
3872 *
3873 * Returns: (transfer full): a [[email protected]] that resolves to a
3874 * [[email protected]] or rejects with error.
3875 *
3876 * Since: 1.2
3877 */
3878 DexFuture *
3879 dex_file_load_partial_contents_bytes (GFile *file,
3880 GFileReadMoreCallback read_more_callback,
3881 gpointer read_more_callback_data,
3882 GDestroyNotify read_more_callback_data_destroy)
3883 {
3884 FileLoadPartialContents *state;
3885 DexPromise *promise;
3886
3887 dex_return_error_if_fail (G_IS_FILE (file));
3888
3889 promise = dex_promise_new_cancellable ();
3890
3891 state = g_new0 (FileLoadPartialContents, 1);
3892 state->callback = read_more_callback;
3893 state->notify = read_more_callback_data_destroy;
3894 state->data = read_more_callback_data;
3895 state->promise = dex_ref (promise);
3896
3897 g_file_load_partial_contents_async (file,
3898 dex_promise_get_cancellable (promise),
3899 dex_file_load_partial_contents_read_more_cb,
3900 dex_file_load_partial_contents_bytes_cb,
3901 state);
3902
3903 return DEX_FUTURE (promise);
3904 }
3905
3906 static void
3907 dex_file_load_bytes_cb (GObject *object,
3908 GAsyncResult *result,
3909 gpointer user_data)
3910 {
3911 DexPromise *promise = user_data;
3912 GError *error = NULL;
3913 GBytes *bytes;
3914
3915 if (!(bytes = g_file_load_bytes_finish (G_FILE (object), result, NULL, &error)))
3916 dex_promise_reject (promise, g_steal_pointer (&error));
3917 else
3918 dex_promise_resolve_boxed (promise, G_TYPE_BYTES, g_steal_pointer (&bytes));
3919
3920 dex_unref (promise);
3921 }
3922
3923 /**
3924 * dex_file_load_bytes:
3925 * @file: a [[email protected]]
3926 *
3927 * Wraps [[email protected]_bytes_async].
3928 *
3929 * Returns: (transfer full): a [[email protected]] that resolves to a
3930 * [[email protected]] or rejects with error.
3931 *
3932 * Since: 1.2
3933 */
3934 DexFuture *
3935 dex_file_load_bytes (GFile *file)
3936 {
3937 DexPromise *promise;
3938
3939 dex_return_error_if_fail (G_IS_FILE (file));
3940
3941 promise = dex_promise_new_cancellable ();
3942 g_file_load_bytes_async (file,
3943 dex_promise_get_cancellable (promise),
3944 dex_file_load_bytes_cb,
3945 dex_ref (promise));
3946 return DEX_FUTURE (promise);
3947 }
3948
3949 static void
3950 dex_subprocess_wait_cb (GObject *object,
3951 GAsyncResult *result,
3952 gpointer user_data)
3953 {
3954 DexPromise *promise = user_data;
3955 GError *error = NULL;
3956
3957 if (!g_subprocess_wait_finish (G_SUBPROCESS (object), result, &error))
3958 dex_promise_reject (promise, g_steal_pointer (&error));
3959 else
3960 dex_promise_resolve_boolean (promise, TRUE);
3961
3962 dex_unref (promise);
3963 }
3964
3965 /**
3966 * dex_subprocess_wait:
3967 * @subprocess: a [[email protected]]
3968 *
3969 * Wraps [[email protected]_async].
3970 *
3971 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
3972 * or rejects with error.
3973 *
3974 * Since: 1.2
3975 */
3976 DexFuture *
3977 dex_subprocess_wait (GSubprocess *subprocess)
3978 {
3979 DexPromise *promise;
3980
3981 dex_return_error_if_fail (G_IS_SUBPROCESS (subprocess));
3982
3983 promise = dex_promise_new_cancellable ();
3984
3985 g_subprocess_wait_async (subprocess,
3986 dex_promise_get_cancellable (promise),
3987 dex_subprocess_wait_cb,
3988 dex_ref (promise));
3989
3990 return DEX_FUTURE (promise);
3991 }
3992
3993 static void
3994 dex_subprocess_wait_check_cb (GObject *object,
3995 GAsyncResult *result,
3996 gpointer user_data)
3997 {
3998 DexAsyncPair *async_pair = user_data;
3999 GError *error = NULL;
4000
4001 if (g_subprocess_wait_check_finish (G_SUBPROCESS (object), result, &error))
4002 dex_async_pair_return_boolean (async_pair, TRUE);
4003 else
4004 dex_async_pair_return_error (async_pair, error);
4005
4006 dex_unref (async_pair);
4007 }
4008
4009 /**
4010 * dex_subprocess_wait_check:
4011 * @subprocess: a [[email protected]]
4012 *
4013 * Creates a future that awaits for @subprocess to complete using
4014 * [[email protected]_check_async].
4015 *
4016 * Returns: (transfer full): a [[email protected]] that will resolve when @subprocess
4017 * exits cleanly or reject upon signal or non-successful exit.
4018 *
4019 * Since: 0.4
4020 */
4021 DexFuture *
4022 dex_subprocess_wait_check (GSubprocess *subprocess)
4023 {
4024 DexAsyncPair *async_pair;
4025
4026 g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), NULL);
4027
4028 async_pair = create_async_pair (G_STRFUNC);
4029
4030 g_subprocess_wait_check_async (subprocess,
4031 async_pair->cancellable,
4032 dex_subprocess_wait_check_cb,
4033 dex_ref (async_pair));
4034
4035 return DEX_FUTURE (async_pair);
4036 }
4037
4038 static void
4039 dex_tls_database_verify_chain_cb (GObject *object,
4040 GAsyncResult *result,
4041 gpointer user_data)
4042 {
4043 DexPromise *promise = user_data;
4044 GError *error = NULL;
4045 GTlsCertificateFlags flags;
4046
4047 flags = g_tls_database_verify_chain_finish (G_TLS_DATABASE (object), result, &error);
4048
4049 if (error == NULL)
4050 dex_promise_resolve_uint (promise, flags);
4051 else
4052 dex_promise_reject (promise, g_steal_pointer (&error));
4053
4054 dex_unref (promise);
4055 }
4056
4057 /**
4058 * dex_tls_database_verify_chain:
4059 * @database: a [[email protected]]
4060 * @chain: a [[email protected]]
4061 * @purpose: certificate purpose
4062 * @identity: (nullable): expected peer identity, or %NULL
4063 * @interaction: (nullable): a [[email protected]], or %NULL
4064 * @flags: verification flags
4065 *
4066 * Wraps [[email protected]_chain_async].
4067 *
4068 * Returns: (transfer full): a [[email protected]] that resolves to
4069 * [[email protected]] or rejects with error.
4070 *
4071 * Since: 1.2
4072 */
4073 DexFuture *
4074 dex_tls_database_verify_chain (GTlsDatabase *database,
4075 GTlsCertificate *chain,
4076 const char *purpose,
4077 GSocketConnectable *identity,
4078 GTlsInteraction *interaction,
4079 GTlsDatabaseVerifyFlags flags)
4080 {
4081 DexPromise *promise;
4082
4083 dex_return_error_if_fail (G_IS_TLS_DATABASE (database));
4084 dex_return_error_if_fail (G_IS_TLS_CERTIFICATE (chain));
4085 dex_return_error_if_fail (purpose != NULL);
4086 dex_return_error_if_fail (identity == NULL || G_IS_SOCKET_CONNECTABLE (identity));
4087 dex_return_error_if_fail (interaction == NULL || G_IS_TLS_INTERACTION (interaction));
4088
4089 promise = dex_promise_new_cancellable ();
4090
4091 g_tls_database_verify_chain_async (database,
4092 chain,
4093 purpose,
4094 identity,
4095 interaction,
4096 flags,
4097 dex_promise_get_cancellable (promise),
4098 dex_tls_database_verify_chain_cb,
4099 dex_ref (promise));
4100
4101 return DEX_FUTURE (promise);
4102 }
4103
4104 static void
4105 dex_tls_database_lookup_certificate_for_handle_cb (GObject *object,
4106 GAsyncResult *result,
4107 gpointer user_data)
4108 {
4109 DexPromise *promise = user_data;
4110 GError *error = NULL;
4111 GTlsCertificate *certificate;
4112
4113 certificate = g_tls_database_lookup_certificate_for_handle_finish (G_TLS_DATABASE (object), result, &error);
4114
4115 if (error == NULL)
4116 dex_promise_resolve_object (promise, g_steal_pointer (&certificate));
4117 else
4118 dex_promise_reject (promise, g_steal_pointer (&error));
4119
4120 dex_unref (promise);
4121 }
4122
4123 /**
4124 * dex_tls_database_lookup_certificate_for_handle:
4125 * @database: a [[email protected]]
4126 * @handle: certificate handle
4127 * @interaction: (nullable): a [[email protected]], or %NULL
4128 * @flags: lookup flags
4129 *
4130 * Wraps [[email protected]_certificate_for_handle_async].
4131 *
4132 * Returns: (transfer full): a [[email protected]] that resolves to a
4133 * [[email protected]] or rejects with error.
4134 *
4135 * Since: 1.2
4136 */
4137 DexFuture *
4138 dex_tls_database_lookup_certificate_for_handle (GTlsDatabase *database,
4139 const char *handle,
4140 GTlsInteraction *interaction,
4141 GTlsDatabaseLookupFlags flags)
4142 {
4143 DexPromise *promise;
4144
4145 dex_return_error_if_fail (G_IS_TLS_DATABASE (database));
4146 dex_return_error_if_fail (handle != NULL);
4147 dex_return_error_if_fail (interaction == NULL || G_IS_TLS_INTERACTION (interaction));
4148
4149 promise = dex_promise_new_cancellable ();
4150
4151 g_tls_database_lookup_certificate_for_handle_async (database,
4152 handle,
4153 interaction,
4154 flags,
4155 dex_promise_get_cancellable (promise),
4156 dex_tls_database_lookup_certificate_for_handle_cb,
4157 dex_ref (promise));
4158
4159 return DEX_FUTURE (promise);
4160 }
4161
4162 static void
4163 dex_tls_database_lookup_certificate_issuer_cb (GObject *object,
4164 GAsyncResult *result,
4165 gpointer user_data)
4166 {
4167 DexPromise *promise = user_data;
4168 GError *error = NULL;
4169 GTlsCertificate *certificate;
4170
4171 certificate = g_tls_database_lookup_certificate_issuer_finish (G_TLS_DATABASE (object), result, &error);
4172
4173 if (error == NULL)
4174 dex_promise_resolve_object (promise, g_steal_pointer (&certificate));
4175 else
4176 dex_promise_reject (promise, g_steal_pointer (&error));
4177
4178 dex_unref (promise);
4179 }
4180
4181 /**
4182 * dex_tls_database_lookup_certificate_issuer:
4183 * @database: a [[email protected]]
4184 * @certificate: a [[email protected]]
4185 * @interaction: (nullable): a [[email protected]], or %NULL
4186 * @flags: lookup flags
4187 *
4188 * Wraps [[email protected]_certificate_issuer_async].
4189 *
4190 * Returns: (transfer full): a [[email protected]] that resolves to a
4191 * [[email protected]] or rejects with error.
4192 *
4193 * Since: 1.2
4194 */
4195 DexFuture *
4196 dex_tls_database_lookup_certificate_issuer (GTlsDatabase *database,
4197 GTlsCertificate *certificate,
4198 GTlsInteraction *interaction,
4199 GTlsDatabaseLookupFlags flags)
4200 {
4201 DexPromise *promise;
4202
4203 dex_return_error_if_fail (G_IS_TLS_DATABASE (database));
4204 dex_return_error_if_fail (G_IS_TLS_CERTIFICATE (certificate));
4205 dex_return_error_if_fail (interaction == NULL || G_IS_TLS_INTERACTION (interaction));
4206
4207 promise = dex_promise_new_cancellable ();
4208
4209 g_tls_database_lookup_certificate_issuer_async (database,
4210 certificate,
4211 interaction,
4212 flags,
4213 dex_promise_get_cancellable (promise),
4214 dex_tls_database_lookup_certificate_issuer_cb,
4215 dex_ref (promise));
4216
4217 return DEX_FUTURE (promise);
4218 }
4219
4220 static void
4221 dex_tls_database_lookup_certificates_issued_by_cb (GObject *object,
4222 GAsyncResult *result,
4223 gpointer user_data)
4224 {
4225 DexPromise *promise = user_data;
4226 GError *error = NULL;
4227 GList *certificates;
4228
4229 certificates = g_tls_database_lookup_certificates_issued_by_finish (G_TLS_DATABASE (object), result, &error);
4230
4231 if (error == NULL)
4232 dex_promise_resolve_boxed (promise, DEX_TYPE_TLS_CERTIFICATE_LIST, certificates);
4233 else
4234 dex_promise_reject (promise, g_steal_pointer (&error));
4235
4236 dex_unref (promise);
4237 }
4238
4239 /**
4240 * dex_tls_database_lookup_certificates_issued_by:
4241 * @database: a [[email protected]]
4242 * @issuer_raw_dn: issuer distinguished name bytes
4243 * @interaction: (nullable): a [[email protected]], or %NULL
4244 * @flags: lookup flags
4245 *
4246 * Wraps [[email protected]_certificates_issued_by_async].
4247 *
4248 * Returns: (transfer full): a [[email protected]] that resolves to a
4249 * [[email protected]] of [[email protected]] or rejects with error.
4250 *
4251 * Since: 1.2
4252 */
4253 DexFuture *
4254 dex_tls_database_lookup_certificates_issued_by (GTlsDatabase *database,
4255 GByteArray *issuer_raw_dn,
4256 GTlsInteraction *interaction,
4257 GTlsDatabaseLookupFlags flags)
4258 {
4259 DexPromise *promise;
4260
4261 dex_return_error_if_fail (G_IS_TLS_DATABASE (database));
4262 dex_return_error_if_fail (issuer_raw_dn != NULL);
4263 dex_return_error_if_fail (interaction == NULL || G_IS_TLS_INTERACTION (interaction));
4264
4265 promise = dex_promise_new_cancellable ();
4266
4267 g_tls_database_lookup_certificates_issued_by_async (database,
4268 issuer_raw_dn,
4269 interaction,
4270 flags,
4271 dex_promise_get_cancellable (promise),
4272 dex_tls_database_lookup_certificates_issued_by_cb,
4273 dex_ref (promise));
4274
4275 return DEX_FUTURE (promise);
4276 }
4277
4278 static void
4279 dex_tls_interaction_ask_password_cb (GObject *object,
4280 GAsyncResult *result,
4281 gpointer user_data)
4282 {
4283 DexPromise *promise = user_data;
4284 GError *error = NULL;
4285 GTlsInteractionResult interaction_result;
4286
4287 interaction_result = g_tls_interaction_ask_password_finish (G_TLS_INTERACTION (object), result, &error);
4288
4289 if (error == NULL)
4290 dex_promise_resolve_enum (promise, G_TYPE_TLS_INTERACTION_RESULT, interaction_result);
4291 else
4292 dex_promise_reject (promise, g_steal_pointer (&error));
4293
4294 dex_unref (promise);
4295 }
4296
4297 /**
4298 * dex_tls_interaction_ask_password:
4299 * @interaction: a [[email protected]]
4300 * @password: a [[email protected]]
4301 *
4302 * Wraps [[email protected]_password_async].
4303 *
4304 * Returns: (transfer full): a [[email protected]] that resolves to a
4305 * [[email protected]] or rejects with error.
4306 *
4307 * Since: 1.2
4308 */
4309 DexFuture *
4310 dex_tls_interaction_ask_password (GTlsInteraction *interaction,
4311 GTlsPassword *password)
4312 {
4313 DexPromise *promise;
4314
4315 dex_return_error_if_fail (G_IS_TLS_INTERACTION (interaction));
4316 dex_return_error_if_fail (G_IS_TLS_PASSWORD (password));
4317
4318 promise = dex_promise_new_cancellable ();
4319
4320 g_tls_interaction_ask_password_async (interaction,
4321 password,
4322 dex_promise_get_cancellable (promise),
4323 dex_tls_interaction_ask_password_cb,
4324 dex_ref (promise));
4325
4326 return DEX_FUTURE (promise);
4327 }
4328
4329 static void
4330 dex_tls_interaction_request_certificate_cb (GObject *object,
4331 GAsyncResult *result,
4332 gpointer user_data)
4333 {
4334 DexPromise *promise = user_data;
4335 GError *error = NULL;
4336 GTlsInteractionResult interaction_result;
4337
4338 interaction_result = g_tls_interaction_request_certificate_finish (G_TLS_INTERACTION (object), result, &error);
4339
4340 if (error == NULL)
4341 dex_promise_resolve_enum (promise, G_TYPE_TLS_INTERACTION_RESULT, interaction_result);
4342 else
4343 dex_promise_reject (promise, g_steal_pointer (&error));
4344
4345 dex_unref (promise);
4346 }
4347
4348 /**
4349 * dex_tls_interaction_request_certificate:
4350 * @interaction: a [[email protected]]
4351 * @connection: a [[email protected]]
4352 * @flags: certificate request flags
4353 *
4354 * Wraps [[email protected]_certificate_async].
4355 *
4356 * Returns: (transfer full): a [[email protected]] that resolves to a
4357 * [[email protected]] or rejects with error.
4358 *
4359 * Since: 1.2
4360 */
4361 DexFuture *
4362 dex_tls_interaction_request_certificate (GTlsInteraction *interaction,
4363 GTlsConnection *connection,
4364 GTlsCertificateRequestFlags flags)
4365 {
4366 DexPromise *promise;
4367
4368 dex_return_error_if_fail (G_IS_TLS_INTERACTION (interaction));
4369 dex_return_error_if_fail (G_IS_TLS_CONNECTION (connection));
4370
4371 promise = dex_promise_new_cancellable ();
4372
4373 g_tls_interaction_request_certificate_async (interaction,
4374 connection,
4375 flags,
4376 dex_promise_get_cancellable (promise),
4377 dex_tls_interaction_request_certificate_cb,
4378 dex_ref (promise));
4379
4380 return DEX_FUTURE (promise);
4381 }
4382
4383 static void
4384 dex_permission_acquire_cb (GObject *object,
4385 GAsyncResult *result,
4386 gpointer user_data)
4387 {
4388 DexPromise *promise = user_data;
4389 GError *error = NULL;
4390
4391 if (!g_permission_acquire_finish (G_PERMISSION (object), result, &error))
4392 dex_promise_reject (promise, g_steal_pointer (&error));
4393 else
4394 dex_promise_resolve_boolean (promise, TRUE);
4395
4396 dex_unref (promise);
4397 }
4398
4399 /**
4400 * dex_permission_acquire:
4401 * @permission: a [[email protected]]
4402 *
4403 * Wraps [[email protected]_async].
4404 *
4405 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
4406 * or rejects with error.
4407 *
4408 * Since: 1.2
4409 */
4410 DexFuture *
4411 dex_permission_acquire (GPermission *permission)
4412 {
4413 DexPromise *promise;
4414
4415 dex_return_error_if_fail (G_IS_PERMISSION (permission));
4416
4417 promise = dex_promise_new_cancellable ();
4418
4419 g_permission_acquire_async (permission,
4420 dex_promise_get_cancellable (promise),
4421 dex_permission_acquire_cb,
4422 dex_ref (promise));
4423
4424 return DEX_FUTURE (promise);
4425 }
4426
4427 static void
4428 dex_permission_release_cb (GObject *object,
4429 GAsyncResult *result,
4430 gpointer user_data)
4431 {
4432 DexPromise *promise = user_data;
4433 GError *error = NULL;
4434
4435 if (!g_permission_release_finish (G_PERMISSION (object), result, &error))
4436 dex_promise_reject (promise, g_steal_pointer (&error));
4437 else
4438 dex_promise_resolve_boolean (promise, TRUE);
4439
4440 dex_unref (promise);
4441 }
4442
4443 /**
4444 * dex_permission_release:
4445 * @permission: a [[email protected]]
4446 *
4447 * Wraps [[email protected]_async].
4448 *
4449 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
4450 * or rejects with error.
4451 *
4452 * Since: 1.2
4453 */
4454 DexFuture *
4455 dex_permission_release (GPermission *permission)
4456 {
4457 DexPromise *promise;
4458
4459 dex_return_error_if_fail (G_IS_PERMISSION (permission));
4460
4461 promise = dex_promise_new_cancellable ();
4462
4463 g_permission_release_async (permission,
4464 dex_promise_get_cancellable (promise),
4465 dex_permission_release_cb,
4466 dex_ref (promise));
4467
4468 return DEX_FUTURE (promise);
4469 }
4470
4471 #ifdef G_OS_UNIX
4472 static void
4473 dex_unix_connection_send_credentials_cb (GObject *object,
4474 GAsyncResult *result,
4475 gpointer user_data)
4476 {
4477 DexPromise *promise = user_data;
4478 GError *error = NULL;
4479
4480 if (!g_unix_connection_send_credentials_finish (G_UNIX_CONNECTION (object), result, &error))
4481 dex_promise_reject (promise, g_steal_pointer (&error));
4482 else
4483 dex_promise_resolve_boolean (promise, TRUE);
4484
4485 dex_unref (promise);
4486 }
4487
4488 /**
4489 * dex_unix_connection_send_credentials:
4490 * @connection: a [[email protected]]
4491 *
4492 * Wraps `g_unix_connection_send_credentials_async()`.
4493 *
4494 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
4495 * or rejects with error.
4496 *
4497 * Since: 1.2
4498 */
4499 DexFuture *
4500 dex_unix_connection_send_credentials (GUnixConnection *connection)
4501 {
4502 DexPromise *promise;
4503
4504 dex_return_error_if_fail (G_IS_UNIX_CONNECTION (connection));
4505
4506 promise = dex_promise_new_cancellable ();
4507
4508 g_unix_connection_send_credentials_async (connection,
4509 dex_promise_get_cancellable (promise),
4510 dex_unix_connection_send_credentials_cb,
4511 dex_ref (promise));
4512
4513 return DEX_FUTURE (promise);
4514 }
4515
4516 static void
4517 dex_unix_connection_receive_credentials_cb (GObject *object,
4518 GAsyncResult *result,
4519 gpointer user_data)
4520 {
4521 DexPromise *promise = user_data;
4522 GError *error = NULL;
4523 GCredentials *credentials;
4524
4525 if (!(credentials = g_unix_connection_receive_credentials_finish (G_UNIX_CONNECTION (object), result, &error)))
4526 dex_promise_reject (promise, g_steal_pointer (&error));
4527 else
4528 dex_promise_resolve_object (promise, g_steal_pointer (&credentials));
4529
4530 dex_unref (promise);
4531 }
4532
4533 /**
4534 * dex_unix_connection_receive_credentials:
4535 * @connection: a [[email protected]]
4536 *
4537 * Wraps `g_unix_connection_receive_credentials_async()`.
4538 *
4539 * Returns: (transfer full): a [[email protected]] that resolves to a
4540 * [[email protected]] or rejects with error.
4541 *
4542 * Since: 1.2
4543 */
4544 DexFuture *
4545 dex_unix_connection_receive_credentials (GUnixConnection *connection)
4546 {
4547 DexPromise *promise;
4548
4549 dex_return_error_if_fail (G_IS_UNIX_CONNECTION (connection));
4550
4551 promise = dex_promise_new_cancellable ();
4552
4553 g_unix_connection_receive_credentials_async (connection,
4554 dex_promise_get_cancellable (promise),
4555 dex_unix_connection_receive_credentials_cb,
4556 dex_ref (promise));
4557
4558 return DEX_FUTURE (promise);
4559 }
4560 #endif
4561
4562 static void
4563 dex_file_query_exists_cb (GObject *object,
4564 GAsyncResult *result,
4565 gpointer user_data)
4566 {
4567 DexPromise *promise = user_data;
4568 GFileInfo *file_info;
4569 GError *error = NULL;
4570
4571 file_info = g_file_query_info_finish (G_FILE (object), result, &error);
4572
4573 if (file_info != NULL)
4574 dex_promise_resolve_boolean (promise, TRUE);
4575 else
4576 dex_promise_reject (promise, g_steal_pointer (&error));
4577
4578 g_clear_object (&file_info);
4579
4580 dex_unref (promise);
4581 }
4582
4583 /**
4584 * dex_file_query_exists:
4585 * @file: a [[email protected]]
4586 *
4587 * Queries to see if @file exists asynchronously.
4588 *
4589 * Returns: (transfer full): a [[email protected]] that will resolve with %TRUE
4590 * if the file exists, otherwise reject with error.
4591 *
4592 * Since: 0.6
4593 */
4594 DexFuture *
4595 dex_file_query_exists (GFile *file)
4596 {
4597 DexPromise *promise;
4598
4599 g_return_val_if_fail (G_IS_FILE (file), NULL);
4600
4601 promise = dex_promise_new_cancellable ();
4602 g_file_query_info_async (file,
4603 G_FILE_ATTRIBUTE_STANDARD_TYPE,
4604 G_FILE_QUERY_INFO_NONE,
4605 G_PRIORITY_DEFAULT,
4606 dex_promise_get_cancellable (promise),
4607 dex_file_query_exists_cb,
4608 dex_ref (promise));
4609 return DEX_FUTURE (promise);
4610 }
4611
4612 static void
4613 dex_async_initable_init_cb (GObject *object,
4614 GAsyncResult *result,
4615 gpointer user_data)
4616 {
4617 DexPromise *promise = user_data;
4618 GError *error = NULL;
4619
4620 if (!g_async_initable_init_finish (G_ASYNC_INITABLE (object), result, &error))
4621 dex_promise_reject (promise, g_steal_pointer (&error));
4622 else
4623 dex_promise_resolve_object (promise, g_object_ref (object));
4624
4625 dex_unref (promise);
4626 }
4627
4628 /**
4629 * dex_async_initable_init:
4630 * @initable: a [[email protected]]
4631 * @io_priority: the [IO priority][[email protected]#io-priority] of the
4632 * request
4633 *
4634 * A helper for [[email protected]_async].
4635 *
4636 * Returns: (transfer full): a [[email protected]] that resolves
4637 * to the @initable instance or rejects with error.
4638 *
4639 * Since: 1.0
4640 */
4641 DexFuture *
4642 dex_async_initable_init (GAsyncInitable *initable,
4643 int io_priority)
4644 {
4645 DexPromise *promise;
4646
4647 dex_return_error_if_fail (G_IS_ASYNC_INITABLE (initable));
4648
4649 promise = dex_promise_new_cancellable ();
4650 g_async_initable_init_async (initable,
4651 io_priority,
4652 dex_promise_get_cancellable (promise),
4653 dex_async_initable_init_cb,
4654 dex_ref (promise));
4655 return DEX_FUTURE (promise);
4656 }
4657
4658 static void
4659 dex_file_set_display_name_cb (GObject *object,
4660 GAsyncResult *result,
4661 gpointer user_data)
4662 {
4663 DexPromise *promise = user_data;
4664 GError *error = NULL;
4665 GFile *file;
4666
4667 if (!(file = g_file_set_display_name_finish (G_FILE (object), result, &error)))
4668 dex_promise_reject (promise, g_steal_pointer (&error));
4669 else
4670 dex_promise_resolve_object (promise, g_steal_pointer (&file));
4671
4672 dex_unref (promise);
4673 }
4674
4675 /**
4676 * dex_file_set_display_name:
4677 * @file: a [[email protected]]
4678 * @display_name: a new display name
4679 * @io_priority: priority for the IO operation
4680 *
4681 * Wraps [[email protected]_display_name_async].
4682 *
4683 * Returns: (transfer full): a [[email protected]] that resolves to the renamed
4684 * [[email protected]] or rejects with error.
4685 *
4686 * Since: 1.2
4687 */
4688 DexFuture *
4689 dex_file_set_display_name (GFile *file,
4690 const char *display_name,
4691 int io_priority)
4692 {
4693 DexPromise *promise;
4694
4695 dex_return_error_if_fail (G_IS_FILE (file));
4696 dex_return_error_if_fail (display_name != NULL);
4697
4698 promise = dex_promise_new_cancellable ();
4699 g_file_set_display_name_async (file,
4700 display_name,
4701 io_priority,
4702 dex_promise_get_cancellable (promise),
4703 dex_file_set_display_name_cb,
4704 dex_ref (promise));
4705 return DEX_FUTURE (promise);
4706 }
4707
4708 static void
4709 dex_file_set_attributes_cb (GObject *object,
4710 GAsyncResult *result,
4711 gpointer user_data)
4712 {
4713 DexPromise *promise = user_data;
4714 GFileInfo *file_info = NULL;
4715 GError *error = NULL;
4716
4717 if (!g_file_set_attributes_finish (G_FILE (object), result, &file_info, &error))
4718 dex_promise_reject (promise, g_steal_pointer (&error));
4719 else
4720 dex_promise_resolve_object (promise, g_steal_pointer (&file_info));
4721
4722 dex_unref (promise);
4723 }
4724
4725 /**
4726 * dex_file_set_attributes:
4727 * @file: a [[email protected]]
4728 * @file_info: a [[email protected]]
4729 * @flags: a set of [[email protected]]
4730 * @io_priority: the [IO priority][[email protected]#io-priority] of the
4731 * request
4732 *
4733 * Returns: (transfer full): a [[email protected]] that resolves to a
4734 * [[email protected]] or rejects with error.
4735 *
4736 * Since: 1.0
4737 */
4738 DexFuture *
4739 dex_file_set_attributes (GFile *file,
4740 GFileInfo *file_info,
4741 GFileQueryInfoFlags flags,
4742 int io_priority)
4743 {
4744 DexPromise *promise;
4745
4746 dex_return_error_if_fail (G_IS_FILE (file));
4747 dex_return_error_if_fail (G_IS_FILE_INFO (file_info));
4748
4749 promise = dex_promise_new_cancellable ();
4750 g_file_set_attributes_async (file,
4751 file_info,
4752 flags,
4753 io_priority,
4754 dex_promise_get_cancellable (promise),
4755 dex_file_set_attributes_cb,
4756 dex_ref (promise));
4757 return DEX_FUTURE (promise);
4758 }
4759
4760 typedef struct
4761 {
4762 GDestroyNotify notify;
4763 gpointer data;
4764 DexPromise *promise;
4765 } FileMove;
4766
4767 static void
4768 dex_file_move_cb (GObject *object,
4769 GAsyncResult *result,
4770 gpointer user_data)
4771 {
4772 FileMove *state = user_data;
4773 GError *error = NULL;
4774
4775 if (!g_file_move_finish (G_FILE (object), result, &error))
4776 dex_promise_reject (state->promise, g_steal_pointer (&error));
4777 else
4778 dex_promise_resolve_boolean (state->promise, TRUE);
4779
4780 if (state->notify)
4781 state->notify (state->data);
4782 dex_clear (&state->promise);
4783 g_free (state);
4784 }
4785
4786 /**
4787 * dex_file_move:
4788 * @source: source [[email protected]]
4789 * @destination: destination [[email protected]]
4790 * @flags: a set of [[email protected]]
4791 * @io_priority: the [IO priority][[email protected]#io-priority] of the
4792 * request
4793 * @progress_callback: (scope notified) (closure progress_callback_data):
4794 * [[email protected]] function for updates
4795 * @progress_callback_data: `gpointer` to user data for the callback function
4796 * @progress_callback_data_destroy: (nullable): a function to destroy the
4797 * @progress_callback_data, or %NULL
4798 *
4799 * Returns: (transfer full): a [[email protected]] that resolves to %TRUE
4800 * or rejects with error
4801 */
4802 DexFuture *
4803 dex_file_move (GFile *source,
4804 GFile *destination,
4805 GFileCopyFlags flags,
4806 int io_priority,
4807 GFileProgressCallback progress_callback,
4808 gpointer progress_callback_data,
4809 GDestroyNotify progress_callback_data_destroy)
4810 {
4811 FileMove *state;
4812 DexPromise *promise;
4813
4814 dex_return_error_if_fail (G_IS_FILE (source));
4815 dex_return_error_if_fail (G_IS_FILE (destination));
4816
4817 promise = dex_promise_new_cancellable ();
4818
4819 state = g_new0 (FileMove, 1);
4820 state->data = progress_callback_data;
4821 state->notify = progress_callback_data_destroy;
4822 state->promise = dex_ref (promise);
4823
4824 g_file_move_async (source,
4825 destination,
4826 flags,
4827 io_priority,
4828 dex_promise_get_cancellable (promise),
4829 progress_callback,
4830 progress_callback_data,
4831 dex_file_move_cb,
4832 state);
4833
4834 return DEX_FUTURE (promise);
4835 }
4836
4837 typedef struct _MkdirWithParents
4838 {
4839 char *path;
4840 int mode;
4841 } MkdirWithParents;
4842
4843 static void
4844 mkdir_with_parents_free (MkdirWithParents *state)
4845 {
4846 g_free (state->path);
4847 g_free (state);
4848 }
4849
4850 static DexFuture *
4851 dex_mkdir_with_parents_thread (gpointer data)
4852 {
4853 MkdirWithParents *state = data;
4854
4855 if (g_mkdir_with_parents (state->path, state->mode) == -1)
4856 return dex_future_new_for_errno (errno);
4857 else
4858 return dex_future_new_for_int (0);
4859 }
4860
4861 /**
4862 * dex_mkdir_with_parents:
4863 * @path: a path to a directory to create
4864 * @mode: the mode for the directory such as `0750`
4865 *
4866 * Similar to [[email protected]_with_parents] but runs on a dedicated thread.
4867 *
4868 * Returns: (transfer full): a [[email protected]] that resolves to 0
4869 * if successful, otherwise rejects with error.
4870 *
4871 * Since: 1.1
4872 */
4873 DexFuture *
4874 dex_mkdir_with_parents (const char *path,
4875 int mode)
4876 {
4877 MkdirWithParents *state;
4878
4879 dex_return_error_if_fail (path != NULL);
4880
4881 /* NOTE: It may be wise in the future to do this with io_uring
4882 * for the cases where we can to avoid a thread.
4883 */
4884
4885 state = g_new0 (MkdirWithParents, 1);
4886 state->path = g_strdup (path);
4887 state->mode = mode;
4888
4889 return dex_thread_spawn ("[mkdir-with-parents]",
4890 dex_mkdir_with_parents_thread,
4891 state,
4892 (GDestroyNotify) mkdir_with_parents_free);
4893 }
4894
4895 static DexFuture *
4896 dex_find_program_in_path_thread (gpointer data)
4897 {
4898 const char *program = data;
4899 char *path;
4900
4901 if ((path = g_find_program_in_path (program)))
4902 return dex_future_new_take_string (g_steal_pointer (&path));
4903
4904 return dex_future_new_reject (G_FILE_ERROR,
4905 g_file_error_from_errno (ENOENT),
4906 "%s", g_strerror (ENOENT));
4907 }
4908
4909 /**
4910 * dex_find_program_in_path:
4911 * @program: the name of the executable such as "grep"
4912 *
4913 * Locates the first executable named program in the user’s path.
4914 *
4915 * This runs [[email protected]_program_in_path] on a dedicated thread.
4916 *
4917 * Returns: (transfer full): a [[email protected]] that resolves to a
4918 * string containing the path or rejects with error.
4919 *
4920 * Since: 1.1
4921 */
4922 DexFuture *
4923 dex_find_program_in_path (const char *program)
4924 {
4925 dex_return_error_if_fail (program != NULL);
4926
4927 return dex_thread_spawn ("[find-program-in-path]",
4928 dex_find_program_in_path_thread,
4929 g_strdup (program),
4930 g_free);
4931 }
4932
4933 static DexFuture *
4934 dex_unlink_thread (gpointer data)
4935 {
4936 const char *path = data;
4937
4938 if (g_unlink (path) == -1)
4939 return dex_future_new_for_errno (errno);
4940
4941 return dex_future_new_for_int (0);
4942 }
4943
4944 /**
4945 * dex_unlink:
4946 * @path: the path to unlink
4947 *
4948 * This runs [[email protected]] on a dedicated thread.
4949 *
4950 * Returns: (transfer full): a [[email protected]] that resolves to an
4951 * int of 0 on success or rejects with error.
4952 *
4953 * Since: 1.1
4954 */
4955 DexFuture *
4956 dex_unlink (const char *path)
4957 {
4958 dex_return_error_if_fail (path != NULL);
4959
4960 return dex_thread_spawn ("[unlink]",
4961 dex_unlink_thread,
4962 g_strdup (path),
4963 g_free);
4964 }
4965
4966 /**
4967 * dex_fd_watch:
4968 * @fd: a file-descriptor
4969 * @events: the POLLOUT|POLLIN style bitmask to watch for
4970 *
4971 * Creates a new GSource that will fire when @events is satisfied.
4972 *
4973 * This is primarily useful when integrating with legacy systems on a
4974 * fiber.
4975 *
4976 * Returns: (transfer full): a [[email protected]] that resolves to the
4977 * revents value when @events is satisfied.
4978 *
4979 * Since: 1.1
4980 */
4981 DexFuture *
4982 dex_fd_watch (int fd,
4983 int events)
4984 {
4985 return dex_watch_new (fd, events);
4986 }