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
|
/*
* cat.c
*
* Copyright 2022 Christian Hergert <chergert@redhat.com>
*
* This program 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 program 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 Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include <unistd.h>
#include <gio/gio.h>
#include <gio/gunixinputstream.h>
#include <gio/gunixoutputstream.h>
#include "cat-util.h"
/* NOTE:
*
* `cat` from coreutils is certainly faster than this, especially if you're
* doing things like `./examples/cat foo > bar` as it will use
* copy_file_range() to avoid reading into userspace.
*
* `gio cat` is likely faster than this doing synchronous IO on the calling
* thread because it doesn't have to coordinate across thread pools.
*/
static DexFuture *
cat_read_fiber (gpointer user_data)
{
Cat *cat = user_data;
GInputStream *stream = g_unix_input_stream_new (cat->read_fd, FALSE);
Buffer *buffer = NULL;
for (;;)
{
Buffer *next;
/* Suspend while sending the buffer to the channel, which may
* help throttle reads if they get too far ahead of writes.
*/
if (buffer != NULL)
dex_await (dex_channel_send (cat->channel,
dex_future_new_for_pointer (g_steal_pointer (&buffer))),
NULL);
/* Get next buffer from the pool */
next = cat_pop_buffer (cat);
/* Suspend while reading into the buffer */
next->length = dex_await_int64 (dex_input_stream_read (stream,
next->data,
next->capacity,
G_PRIORITY_DEFAULT),
NULL);
/* If we got length <= 0, we failed or finished */
if (next->length <= 0)
{
dex_channel_close_send (cat->channel);
cat_push_buffer (cat, next);
break;
}
buffer = next;
}
/* Suspend while we close the stream */
dex_await (dex_input_stream_close (stream, 0), NULL);
g_object_unref (stream);
return dex_future_new_for_boolean (TRUE);
}
static DexFuture *
cat_write_fiber (gpointer user_data)
{
Cat *cat = user_data;
GOutputStream *stream = g_unix_output_stream_new (cat->write_fd, FALSE);
for (;;)
{
Buffer *buffer;
gssize len;
/* Suspend while we wait for another buffer (or error on channel closed) */
if (!(buffer = dex_await_pointer (dex_channel_receive (cat->channel), NULL)))
break;
/* Suspend while we write the buffer contents to output stream */
len = dex_await_int64 (dex_output_stream_write (G_OUTPUT_STREAM (stream),
buffer->data,
buffer->length,
G_PRIORITY_DEFAULT),
NULL);
/* Give the buffer back to the pool */
cat_push_buffer (cat, buffer);
/* Bail if we got a failure or empty buffer */
if (len <= 0)
break;
}
/* Asynchronously close the stream, which may cause flushing to occur. */
dex_await (dex_output_stream_close (stream, 0), NULL);
/* Release the stream, which should not block since it was closed above */
g_object_unref (stream);
return dex_future_new_for_boolean (TRUE);
}
int
main (int argc,
char *argv[])
{
GError *error = NULL;
Cat cat;
int ret = EXIT_SUCCESS;
dex_init ();
if (!cat_init (&cat, &argc, &argv, &error) ||
!cat_run (&cat,
dex_scheduler_spawn (NULL, 0, cat_read_fiber, &cat, NULL),
dex_scheduler_spawn (NULL, 0, cat_write_fiber, &cat, NULL),
&error))
{
g_printerr ("cat: %s\n", error->message);
g_clear_error (&error);
ret = EXIT_FAILURE;
}
cat_clear (&cat);
return ret;
}
|