summaryrefslogtreecommitdiff
path: root/src/logic/tpax_file_create_tmpfs_snapshot.c
blob: 3bb48b7415eb0b9a3058d9a7b92a50f465bdc0c7 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/******************************************************/
/*  tpax: a topological pax implementation            */
/*  Copyright (C) 2020  Z. Gilboa                     */
/*  Released under GPLv2 and GPLv3; see COPYING.TPAX. */
/******************************************************/

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <sys/stat.h>

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

#ifndef ssizeof
#define ssizeof(x) (ssize_t)(sizeof(x))
#endif

int tpax_file_create_tmpfs_snapshot(
	const struct tpax_driver_ctx *  dctx,
	const char *                    path,
	const struct stat *             srcst)
{
	int          fdsrc;
	int          fdtmp;
	char *       ch;
	ssize_t      nread;
	ssize_t      nbytes;
	ssize_t      ret;
	void *       buf;
	size_t       buflen;
	struct stat  dstst;
	char         sbuf[2048];

	/* record errors */
	tpax_driver_set_ectx(
		dctx,0,path);

	/* tmpfile */
	if ((fdtmp = tpax_tmpfile()) < 0)
		return TPAX_SYSTEM_ERROR(dctx);

	/* buffer */
	if (!(buf = tpax_get_driver_anon_map_addr(dctx,&buflen))) {
		buf    = sbuf;
		buflen = sizeof(sbuf);
	}

	/* open */
	fdsrc = openat(
		tpax_driver_fdcwd(dctx),path,
		O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);

	if (fdsrc < 0) {
		close(fdtmp);
		return TPAX_SYSTEM_ERROR(dctx);
	}

	/* stat compare */
	if ((fstat(fdsrc,&dstst)) < 0) {
		close(fdsrc);
		close(fdtmp);
		return TPAX_SYSTEM_ERROR(dctx);

	} else if (tpax_stat_compare(srcst,&dstst)) {
		close(fdsrc);
		close(fdtmp);
		return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FILE_CHANGED);
	}

	/* read/write loop */
	for (nread=0; nread<srcst->st_size; ) {
		nbytes = read(fdsrc,buf,buflen);

		while ((nbytes < 0) && (errno == EINTR))
			nbytes = read(fdsrc,buf,buflen);

		if (nbytes < 0) {
			close(fdsrc);
			close(fdtmp);
			return TPAX_SYSTEM_ERROR(dctx);

		} else if (nbytes == 0) {
			close(fdsrc);
			close(fdtmp);
			return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FLOW_ERROR);

		} else {
			nread += nbytes;
		}

		for (ch=buf; nbytes; ch+=ret) {
			ret = write(fdtmp,ch,nbytes);

			while ((ret < 0) && (errno == EINTR))
				ret = write(fdtmp,ch,nbytes);

			if (ret < 0) {
				close(fdsrc);
				close(fdtmp);
				return TPAX_SYSTEM_ERROR(dctx);

			} else {
				nbytes -= ret;
			}
		}
	}

	/* stat compare */
	if ((fstat(fdsrc,&dstst)) < 0) {
		close(fdsrc);
		close(fdtmp);
		return TPAX_SYSTEM_ERROR(dctx);

	} else if (tpax_stat_compare(srcst,&dstst)) {
		close(fdsrc);
		close(fdtmp);
		return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FILE_CHANGED);
	}

	/* yay */
	close(fdsrc);

	return fdtmp;
}