summaryrefslogtreecommitdiff
path: root/src/driver/tpax_unit_ctx.c
blob: a94bf050657a59072efe49d2e8231ab4023e5b5e (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
/******************************************************/
/*  tpax: a topological pax implementation            */
/*  Copyright (C) 2020  Z. Gilboa                     */
/*  Released under GPLv2 and GPLv3; see COPYING.TPAX. */
/******************************************************/

#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

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

static int tpax_free_unit_ctx_impl(struct tpax_unit_ctx_impl * ctx, int ret)
{
	if (ctx) {
		free(ctx);
	}

	return ret;
}

int tpax_get_unit_ctx(
	const struct tpax_driver_ctx *	dctx,
	const char *			path,
	struct tpax_unit_ctx **		pctx)
{
	int				ret;
	struct tpax_unit_ctx_impl *	ctx;

	if (!dctx)
		return TPAX_CUSTOM_ERROR(
			dctx,TPAX_ERR_NULL_CONTEXT);

	else if (!(ctx = calloc(1,sizeof(*ctx))))
		return TPAX_BUFFER_ERROR(dctx);

	tpax_driver_set_ectx(
		dctx,0,path);

	if (dctx->cctx->drvflags & TPAX_DRIVER_EXEC_MODE_WRITE_COPY) {
		ret = fstatat(
			tpax_driver_fdcwd(dctx),path,
			&ctx->st,AT_SYMLINK_NOFOLLOW);

		if (ret < 0) {
			free(ctx);
			return TPAX_SYSTEM_ERROR(dctx);
		}
	}

	if (S_ISLNK(ctx->st.st_mode)) {
		if (tpax_readlink(
				path,ctx->linkbuf,
				sizeof(ctx->linkbuf)) < 0) {
			free(ctx);
			return TPAX_SYSTEM_ERROR(dctx);
		}
	}

	ctx->path	= path;
	ctx->link	= ctx->linkbuf;

	ctx->uctx.path	= &ctx->path;
	ctx->uctx.link	= &ctx->link;
	ctx->uctx.st	= &ctx->st;

	*pctx = &ctx->uctx;

	return 0;
}

void tpax_free_unit_ctx(struct tpax_unit_ctx * ctx)
{
	struct tpax_unit_ctx_impl *	ictx;
	uintptr_t			addr;

	if (ctx) {
		addr = (uintptr_t)ctx - offsetof(struct tpax_unit_ctx_impl,uctx);
		ictx = (struct tpax_unit_ctx_impl *)addr;
		tpax_free_unit_ctx_impl(ictx,0);
	}
}