summaryrefslogtreecommitdiff
path: root/src/readfile.c
blob: 76f797675b8b6bff9cb489f70c2914fbbb59b2e0 (plain)
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
/*
 *  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 <assert.h>
#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_end(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