summaryrefslogtreecommitdiffhomepage
path: root/src/io/tpax_io_seek.c
blob: 01e06830078cc6a0f14d31af457fedb570580675 (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
/**************************************************************/
/*  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"

static int tpax_io_seek_fileio(
	struct tpax_driver_ctx *    dctx,
	off_t                       offset,
	int                         whence)
{
	int fdin;

	fdin = tpax_driver_fdin(dctx);

	if ((offset = lseek(fdin,offset,whence)) < 0)
		return TPAX_SYSTEM_ERROR(dctx);

	tpax_set_driver_cpos(dctx,offset);

	return 0;
}

static int tpax_io_seek_mapped(
	struct tpax_driver_ctx *    dctx,
	off_t                       offset,
	int                         whence)
{
	struct tpax_driver_ctx_impl * ictx;
	size_t                        aoffset;

	ictx = tpax_get_driver_ictx(dctx);

	if (whence == SEEK_CUR) {
		offset += tpax_get_driver_cpos(dctx);

	} else if (whence == SEEK_END) {
		offset += ictx->mapsize;
	}

	if ((offset < 0) || ((aoffset = offset) > ictx->mapsize))
		return TPAX_CUSTOM_ERROR(
			dctx,
			TPAX_ERR_FLOW_ERROR);

	tpax_set_driver_cpos(dctx,offset);

	return 0;
}

int tpax_io_seek(struct tpax_driver_ctx * dctx, off_t offset, int whence)
{
	switch (whence) {
		case SEEK_SET:
		case SEEK_CUR:
		case SEEK_END:
			break;

		default:
			return TPAX_CUSTOM_ERROR(
				dctx,
				TPAX_ERR_FLOW_ERROR);
	}

	if (dctx->cctx->srcflags & TPAX_SOURCE_DATA_FILEIO) {
		return tpax_io_seek_fileio(dctx,offset,whence);

	} else if (dctx->cctx->srcflags & TPAX_SOURCE_DATA_MAPPED) {
		return tpax_io_seek_mapped(dctx,offset,whence);

	}

	return TPAX_CUSTOM_ERROR(
		dctx,
		TPAX_ERR_FLOW_ERROR);
}