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