summaryrefslogtreecommitdiff
path: root/src/readfile.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2020-11-25 17:58:50 -0800
committerVito Caputo <vcaputo@pengaru.com>2020-11-25 18:07:05 -0800
commit8ec1588722809a35a7d149bff10de56424e2658a (patch)
treedac9aca6321b92f84505293227c5151193dd8ee6 /src/readfile.c
parent7b15a68d12e2df7f45c4311d0281ff9a78693e81 (diff)
src: initial commit of jio WIP source
This is a very quick and dirty experimental hack written in some sort of bastard continuation-passing style in C w/io_uring using journal-file introspection and manipulation duty as an excuse for its existence. Consider this unfinished prototype quality code.
Diffstat (limited to 'src/readfile.c')
-rw-r--r--src/readfile.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/readfile.c b/src/readfile.c
new file mode 100644
index 0000000..8af7438
--- /dev/null
+++ b/src/readfile.c
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2020 - Vito Caputo - <vcaputo@pengaru.com>
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published
+ * by the Free Software Foundation.
+ *
+ * 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 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/>.
+ */
+
+#include <fcntl.h>
+#include <liburing.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <iou.h>
+#include <thunk.h>
+
+#include "readfile.h"
+#include "op.h"
+
+/* Implements iou-based file read into a supplied buffer.
+ */
+
+
+/* call user-supplied closure now that buf is populated */
+THUNK_DEFINE_STATIC(read_done, iou_t *, iou, iou_op_t *, op, int, fd, char *, buf, size_t *, size, thunk_t *, closure)
+{
+ assert(iou);
+ assert(op);
+ assert(closure);
+
+ if (op->result < 0)
+ return op->result;
+
+ (void) close(fd);
+
+ *size = op->result;
+
+ return thunk_dispatch(closure);
+}
+
+
+/* ask iou to read from the opened fd into buf */
+THUNK_DEFINE_STATIC(open_done, iou_t *, iou, iou_op_t *, op, char *, buf, size_t *, size, thunk_t *, closure)
+{
+ thunk_t *read_closure;
+ iou_op_t *read_op;
+
+ assert(iou);
+ assert(op);
+
+ if (op->result < 0)
+ return op->result;
+
+ read_op = iou_op_new(iou);
+ if (!read_op)
+ return -ENOMEM;
+
+ read_closure = THUNK(read_done(iou, read_op, op->result, buf, size, closure));
+ if (!read_closure)
+ return -ENOMEM;
+
+ io_uring_prep_read(read_op->sqe, op->result, buf, *size, 0);
+ op_queue(iou, read_op, read_closure);
+
+ return 0;
+}
+
+
+/* read a file into a buffer via iou, dispatching closure when complete */
+/* size specifies the buf size, and will also get the length of what's read
+ * written to it upon completion.
+ */
+/* returns < 0 on error, 0 on successful queueing of operation */
+THUNK_DEFINE(readfile, iou_t *, iou, const char *, path, char *, buf, size_t *, size, thunk_t *, closure)
+{
+ iou_op_t *op;
+
+ op = iou_op_new(iou);
+ if (!op)
+ return -ENOMEM;
+
+ /* req iou to open path, passing the req to read its contents */
+ io_uring_prep_openat(op->sqe, 0, path, 0, O_RDONLY);
+ op_queue(iou, op, THUNK(open_done(iou, op, buf, size, closure)));
+
+ return 0;
+}
© All Rights Reserved