Debian Codegraph @

examples/cat-util.h

raw symbols SCIP
1 /* cat-util.h
2 *
3 * Copyright 2022 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #pragma once
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28
29 #include <glib/gstdio.h>
30
31 #include <libdex.h>
32
33 /* You don't need to do this in your code. This just allows us to build
34 * Dex on older systems where GLib features we want to use are not available
35 * such as aligned allocations.
36 */
37 #include "dex-compat-private.h"
38
39 G_BEGIN_DECLS
40
41 typedef struct _Cat Cat;
42 typedef struct _Buffer Buffer;
43
44 struct _Cat
45 {
46 gsize buffer_size;
47 int read_fd;
48 int write_fd;
49 gssize to_read;
50 GQueue buffer_pool;
51 DexChannel *channel;
52 GMainLoop *main_loop;
53 GError *error;
54 Buffer *current;
55 };
56
57 struct _Buffer
58 {
59 Cat *cat;
60 GList link;
61 gpointer data;
62 gsize capacity;
63 gssize length;
64 };
65
66 static inline Buffer *
67 buffer_new (Cat *cat)
68 {
69 Buffer *buffer = g_new0 (Buffer, 1);
70
71 buffer->cat = cat;
72 buffer->link.data = buffer;
73 buffer->data = g_aligned_alloc (1, cat->buffer_size, 4096);
74 buffer->capacity = cat->buffer_size;
75 buffer->length = 0;
76
77 return buffer;
78 }
79
80 static inline void
81 buffer_free (Buffer *buffer)
82 {
83 g_aligned_free (buffer->data);
84 g_free (buffer);
85 }
86
87 static inline gboolean
88 cat_error (GError **error,
89 int errsv)
90 {
91 g_set_error_literal (error,
92 G_IO_ERROR,
93 g_io_error_from_errno (errsv),
94 g_strerror (errsv));
95 return FALSE;
96 }
97
98 static inline gboolean
99 cat_init (Cat *cat,
100 int *argc,
101 char ***argv,
102 GError **error)
103 {
104 GOptionContext *context;
105 char *output = NULL;
106 gboolean ret = FALSE;
107 int buffer_size = (1024*256 - 2*sizeof(gpointer)); /* 256k minus malloc overhead */
108 int queue_size = 32;
109 #ifdef HAVE_POSIX_FADVISE
110 gssize len;
111 #endif
112 struct stat stbuf;
113 const GOptionEntry entries[] = {
114 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "Cat contents into OUTPUT", "OUTPUT" },
115 { "buffer-size", 'b', 0, G_OPTION_ARG_INT, &buffer_size, "Read/Write buffer size", "BYTES" },
116 { "queue-size", 'q', 0, G_OPTION_ARG_INT, &queue_size, "Amount of reads that can advance ahead of writes (default 32)", "COUNT" },
117 { 0 }
118 };
119
120 memset (cat, 0, sizeof *cat);
121
122 cat->buffer_size = buffer_size;
123 cat->read_fd = -1;
124 cat->write_fd = -1;
125 cat->channel = dex_channel_new (queue_size);
126 cat->main_loop = g_main_loop_new (NULL, FALSE);
127
128 context = g_option_context_new ("- FILE");
129 g_option_context_add_main_entries (context, entries, NULL);
130 if (!g_option_context_parse (context, argc, argv, error))
131 goto cleanup;
132
133 cat->write_fd = STDOUT_FILENO;
134
135 if (output != NULL)
136 {
137 g_unlink (output);
138
139 if (-1 == (cat->write_fd = open (output, O_WRONLY|O_CREAT, 0644)))
140 goto cleanup;
141 }
142
143 if (*argc > 2)
144 {
145 g_set_error_literal (error,
146 G_IO_ERROR,
147 G_IO_ERROR_INVAL,
148 "Only a single file is supported");
149 goto cleanup;
150 }
151 else if (*argc == 2)
152 {
153 if (-1 == (cat->read_fd = open ((*argv)[1], O_RDONLY)))
154 goto cleanup;
155 }
156 else
157 {
158 cat->read_fd = STDIN_FILENO;
159 }
160
161 #ifdef HAVE_POSIX_FADVISE
162 len = lseek (cat->read_fd, 0, SEEK_END);
163
164 if (len > 0)
165 {
166 posix_fadvise (cat->read_fd, 0, len, POSIX_FADV_SEQUENTIAL);
167 posix_fadvise (cat->write_fd, 0, len, POSIX_FADV_SEQUENTIAL);
168
169 lseek (cat->read_fd, 0, SEEK_SET);
170 }
171 #endif
172
173 if (fstat (cat->read_fd, &stbuf) == 0)
174 cat->to_read = stbuf.st_size;
175 else
176 cat->to_read = -1;
177
178 ret = TRUE;
179
180 cleanup:
181 if (ret == FALSE)
182 {
183 int errsv = errno;
184 char *help = g_option_context_get_help (context, TRUE, NULL);
185 g_printerr ("%s\n", help);
186 g_free (help);
187
188 if (error && !*error)
189 g_set_error_literal (error,
190 G_IO_ERROR,
191 g_io_error_from_errno (errsv),
192 g_strerror (errsv));
193 }
194
195 g_option_context_free (context);
196 g_free (output);
197
198 return ret;
199 }
200
201 static inline void
202 cat_clear (Cat *cat)
203 {
204 if (cat->write_fd >= 0)
205 close (cat->write_fd);
206
207 if (cat->read_fd >= 0)
208 close (cat->read_fd);
209
210 while (cat->buffer_pool.head != NULL)
211 {
212 Buffer *buffer = cat->buffer_pool.head->data;
213 g_queue_unlink (&cat->buffer_pool, &buffer->link);
214 buffer_free (buffer);
215 }
216
217 dex_clear (&cat->channel);
218 g_clear_pointer (&cat->current, buffer_free);
219 g_clear_pointer (&cat->main_loop, g_main_loop_unref);
220 }
221
222 static inline Buffer *
223 cat_pop_buffer (Cat *cat)
224 {
225 if (cat->buffer_pool.length == 0)
226 return buffer_new (cat);
227
228 return g_queue_pop_head_link (&cat->buffer_pool)->data;
229 }
230
231 static inline void
232 cat_push_buffer (Cat *cat,
233 Buffer *buffer)
234 {
235 g_assert (cat != NULL);
236 g_assert (buffer != NULL);
237 g_assert (buffer->link.data == buffer);
238 g_assert (buffer->link.prev == NULL);
239 g_assert (buffer->link.next == NULL);
240
241 buffer->length = 0;
242
243 g_queue_push_head_link (&cat->buffer_pool, &buffer->link);
244 }
245
246 static inline DexFuture *
247 cat_run_cb (DexFuture *future,
248 gpointer user_data)
249 {
250 Cat *cat = user_data;
251 dex_future_get_value (future, &cat->error);
252 g_main_loop_quit (cat->main_loop);
253 return NULL;
254 }
255
256 static inline DexFuture *
257 cat_close_send (DexFuture *future,
258 gpointer user_data)
259 {
260 Cat *cat = user_data;
261 dex_channel_close_send (cat->channel);
262 return NULL;
263 }
264
265 static inline gboolean
266 cat_run (Cat *cat,
267 DexFuture *read_routine,
268 DexFuture *write_routine,
269 GError **error)
270 {
271 DexFuture *future;
272
273 future = dex_future_finally (read_routine, cat_close_send, cat, NULL);
274 future = dex_future_all (future, write_routine, NULL);
275 future = dex_future_finally (future, cat_run_cb, cat, NULL);
276
277 g_main_loop_run (cat->main_loop);
278
279 dex_unref (future);
280
281 if (cat->error)
282 {
283 g_propagate_error (error, g_steal_pointer (&cat->error));
284 return FALSE;
285 }
286
287 return TRUE;
288 }
289
290 G_END_DECLS