summaryrefslogtreecommitdiffhomepage
path: root/src/io/tpax_io_read_next.c
blob: 98df2997600ea3cf66eb1ae7bb3d117fccea58f2 (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
/**************************************************************/
/*  tpax: a topological pax implementation                    */
/*  Copyright (C) 2020--2024  SysDeer Technologies, LLC       */
/*  Released under GPLv2 and GPLv3; see COPYING.TPAX.         */
/**************************************************************/

#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include <tpax/tpax.h>
#include <tpax/tpax_specs.h>
#include "tpax_driver_impl.h"
#include "tpax_errinfo_impl.h"

#define TPAX_IO_READ_MAX (2147483647)

static int tpax_io_range_read_next_fileio(
	struct tpax_driver_ctx *    dctx,
	void *                      buf,
	size_t                      size)
{
	int     fdin;
	ssize_t nbytes;
	off_t   cpos;

	fdin = tpax_driver_fdin(dctx);

	nbytes = read(fdin,buf,size);

	while ((nbytes < 0) && (errno = EINTR))
		nbytes = read(fdin,buf,size);

	if (nbytes > 0) {
		cpos = tpax_get_driver_cpos(dctx);
		tpax_set_driver_cpos(dctx,cpos + nbytes);
	}

	return nbytes;
}

static int tpax_io_range_read_next_mapped(
	struct tpax_driver_ctx *    dctx,
	void *                      buf,
	size_t                      size)
{
	struct tpax_driver_ctx_impl * ictx;
	char *                        ch;
	off_t                         cpos;

	ictx = tpax_get_driver_ictx(dctx);
	cpos = tpax_get_driver_cpos(dctx);
	size = (cpos + size <= ictx->mapsize) ? size : ictx->mapsize - cpos;

	ch = ictx->mapaddr;
	ch = &ch[cpos];

	memcpy(buf,ch,size);

	tpax_set_driver_cpos(dctx,cpos + size);

	return size;
}

int tpax_io_range_read_next(struct tpax_driver_ctx * dctx, void * buf, size_t size)
{
	size = (size <= TPAX_IO_READ_MAX) ? size : TPAX_IO_READ_MAX;

	if (dctx->cctx->srcflags & TPAX_SOURCE_DATA_FILEIO) {
		return tpax_io_range_read_next_fileio(dctx,buf,size);

	} else if (dctx->cctx->srcflags & TPAX_SOURCE_DATA_MAPPED) {
		return tpax_io_range_read_next_mapped(dctx,buf,size);

	}

	return TPAX_CUSTOM_ERROR(
		dctx,
		TPAX_ERR_FLOW_ERROR);
}