Blame src/io/tpax_create_memory_snapshot.c

5874a9
/**************************************************************/
5874a9
/*  tpax: a topological pax implementation                    */
bef28d
/*  Copyright (C) 2020--2024  SysDeer Technologies, LLC       */
5874a9
/*  Released under GPLv2 and GPLv3; see COPYING.TPAX.         */
5874a9
/**************************************************************/
e0fd2a
e0fd2a
#include <stdint.h>
e0fd2a
#include <stdlib.h>
e0fd2a
#include <string.h>
e0fd2a
#include <unistd.h>
e0fd2a
#include <fcntl.h>
e0fd2a
#include <errno.h>
e0fd2a
#include <grp.h>
e0fd2a
#include <pwd.h>
e0fd2a
#include <sys/stat.h>
e0fd2a
e0fd2a
#include <tpax/tpax.h>
e0fd2a
#include <tpax/tpax_specs.h>
e0fd2a
#include "tpax_driver_impl.h"
e0fd2a
#include "tpax_errinfo_impl.h"
e0fd2a
e0fd2a
#ifndef ssizeof
e0fd2a
#define ssizeof(x) (ssize_t)(sizeof(x))
e0fd2a
#endif
e0fd2a
0ee4a8
int tpax_io_create_memory_snapshot(
e0fd2a
	const struct tpax_driver_ctx *  dctx,
a2aed5
	int                             fdat,
e0fd2a
	const char *                    path,
e0fd2a
	const struct stat *             srcst,
e0fd2a
	void *                          addr)
e0fd2a
{
e0fd2a
	int          fd;
e0fd2a
	char *       ch;
e0fd2a
	char *       cap;
e0fd2a
	ssize_t      nread;
e0fd2a
	struct stat  dstst;
e0fd2a
e0fd2a
	/* record errors */
e0fd2a
	tpax_driver_set_ectx(
e0fd2a
		dctx,0,path);
e0fd2a
e0fd2a
	/* memory snapshot internal limit */
e0fd2a
	if (srcst->st_size >= 0x80000000)
e0fd2a
		return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_REGION_SIZE);
e0fd2a
e0fd2a
	/* open */
a2aed5
	if ((fd = openat(fdat,path,O_CLOEXEC|O_NOCTTY|O_NOFOLLOW,0)) < 0)
e0fd2a
		return TPAX_SYSTEM_ERROR(dctx);
e0fd2a
e0fd2a
	/* stat compare */
e0fd2a
	if ((fstat(fd,&dstst)) < 0) {
e0fd2a
		close(fd);
e0fd2a
		return TPAX_SYSTEM_ERROR(dctx);
e0fd2a
6c8a81
	} else if (tpax_util_stat_compare(srcst,&dstst)) {
e0fd2a
		close(fd);
e0fd2a
		return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FILE_CHANGED);
e0fd2a
	}
e0fd2a
e0fd2a
	/* read loop */
e0fd2a
	ch  = addr;
e0fd2a
	cap = &ch[srcst->st_size];
e0fd2a
e0fd2a
	while (ch < cap) {
e0fd2a
		nread = read(fd,ch,cap-ch);
e0fd2a
e0fd2a
		while ((nread < 0) && (errno == EINTR))
e0fd2a
			nread = read(fd,ch,cap-ch);
e0fd2a
e0fd2a
		if (nread < 0) {
e0fd2a
			close(fd);
e0fd2a
			return TPAX_SYSTEM_ERROR(dctx);
e0fd2a
e0fd2a
		} else if (nread == 0) {
e0fd2a
			close(fd);
e0fd2a
			return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FLOW_ERROR);
e0fd2a
e0fd2a
		} else {
e0fd2a
			ch += nread;
e0fd2a
		}
e0fd2a
	}
e0fd2a
e0fd2a
	/* stat compare */
e0fd2a
	if ((fstat(fd,&dstst)) < 0) {
e0fd2a
		close(fd);
e0fd2a
		return TPAX_SYSTEM_ERROR(dctx);
e0fd2a
6c8a81
	} else if (tpax_util_stat_compare(srcst,&dstst)) {
e0fd2a
		close(fd);
e0fd2a
		return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FILE_CHANGED);
e0fd2a
	}
e0fd2a
e0fd2a
	/* yay */
e0fd2a
	close(fd);
e0fd2a
e0fd2a
	return 0;
e0fd2a
}