Debian Codegraph @

examples/cat.c

raw symbols SCIP
1 /*
2 * cat.c
3 *
4 * Copyright 2022 Christian Hergert <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but 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 Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include <unistd.h>
23
24 #include <gio/gio.h>
25 #include <gio/gunixinputstream.h>
26 #include <gio/gunixoutputstream.h>
27
28 #include "cat-util.h"
29
30 /* NOTE:
31 *
32 * `cat` from coreutils is certainly faster than this, especially if you're
33 * doing things like `./examples/cat foo > bar` as it will use
34 * copy_file_range() to avoid reading into userspace.
35 *
36 * `gio cat` is likely faster than this doing synchronous IO on the calling
37 * thread because it doesn't have to coordinate across thread pools.
38 */
39
40 static DexFuture *
41 cat_read_fiber (gpointer user_data)
42 {
43 Cat *cat = user_data;
44 GInputStream *stream = g_unix_input_stream_new (cat->read_fd, FALSE);
45 Buffer *buffer = NULL;
46
47 for (;;)
48 {
49 Buffer *next;
50
51 /* Suspend while sending the buffer to the channel, which may
52 * help throttle reads if they get too far ahead of writes.
53 */
54 if (buffer != NULL)
55 dex_await (dex_channel_send (cat->channel,
56 dex_future_new_for_pointer (g_steal_pointer (&buffer))),
57 NULL);
58
59 /* Get next buffer from the pool */
60 next = cat_pop_buffer (cat);
61
62 /* Suspend while reading into the buffer */
63 next->length = dex_await_int64 (dex_input_stream_read (stream,
64 next->data,
65 next->capacity,
66 G_PRIORITY_DEFAULT),
67 NULL);
68
69 /* If we got length <= 0, we failed or finished */
70 if (next->length <= 0)
71 {
72 dex_channel_close_send (cat->channel);
73 cat_push_buffer (cat, next);
74 break;
75 }
76
77 buffer = next;
78 }
79
80 /* Suspend while we close the stream */
81 dex_await (dex_input_stream_close (stream, 0), NULL);
82
83 g_object_unref (stream);
84
85 return dex_future_new_for_boolean (TRUE);
86 }
87
88 static DexFuture *
89 cat_write_fiber (gpointer user_data)
90 {
91 Cat *cat = user_data;
92 GOutputStream *stream = g_unix_output_stream_new (cat->write_fd, FALSE);
93
94 for (;;)
95 {
96 Buffer *buffer;
97 gssize len;
98
99 /* Suspend while we wait for another buffer (or error on channel closed) */
100 if (!(buffer = dex_await_pointer (dex_channel_receive (cat->channel), NULL)))
101 break;
102
103 /* Suspend while we write the buffer contents to output stream */
104 len = dex_await_int64 (dex_output_stream_write (G_OUTPUT_STREAM (stream),
105 buffer->data,
106 buffer->length,
107 G_PRIORITY_DEFAULT),
108 NULL);
109
110 /* Give the buffer back to the pool */
111 cat_push_buffer (cat, buffer);
112
113 /* Bail if we got a failure or empty buffer */
114 if (len <= 0)
115 break;
116 }
117
118 /* Asynchronously close the stream, which may cause flushing to occur. */
119 dex_await (dex_output_stream_close (stream, 0), NULL);
120
121 /* Release the stream, which should not block since it was closed above */
122 g_object_unref (stream);
123
124 return dex_future_new_for_boolean (TRUE);
125 }
126
127 int
128 main (int argc,
129 char *argv[])
130 {
131 GError *error = NULL;
132 Cat cat;
133 int ret = EXIT_SUCCESS;
134
135 dex_init ();
136
137 if (!cat_init (&cat, &argc, &argv, &error) ||
138 !cat_run (&cat,
139 dex_scheduler_spawn (NULL, 0, cat_read_fiber, &cat, NULL),
140 dex_scheduler_spawn (NULL, 0, cat_write_fiber, &cat, NULL),
141 &error))
142 {
143 g_printerr ("cat: %s\n", error->message);
144 g_clear_error (&error);
145 ret = EXIT_FAILURE;
146 }
147
148 cat_clear (&cat);
149
150 return ret;
151 }