Blame src/logic/tpax_archive_append.c

5874a9
/**************************************************************/
5874a9
/*  tpax: a topological pax implementation                    */
5874a9
/*  Copyright (C) 2020--2021  SysDeer Technologies, LLC       */
5874a9
/*  Released under GPLv2 and GPLv3; see COPYING.TPAX.         */
5874a9
/**************************************************************/
3db888
3db888
#include <stdint.h>
3db888
#include <stdlib.h>
3db888
#include <string.h>
3db888
#include <unistd.h>
3db888
#include <fcntl.h>
3db888
#include <errno.h>
3db888
#include <grp.h>
3db888
#include <pwd.h>
022508
#include <sys/mman.h>
3db888
#include <sys/stat.h>
3db888
3db888
#include <tpax/tpax.h>
3db888
#include <tpax/tpax_specs.h>
3db888
#include "tpax_driver_impl.h"
022508
#include "tpax_getdents_impl.h"
3db888
#include "tpax_tmpfile_impl.h"
3db888
#include "tpax_errinfo_impl.h"
3db888
3db888
#ifndef ssizeof
3db888
#define ssizeof(x) (ssize_t)(sizeof(x))
3db888
#endif
3db888
3db888
static int tpax_archive_append_memory_data(
3db888
	int                             fdout,
3db888
	void *                          buf,
3db888
	ssize_t                         nbytes)
3db888
{
3db888
	ssize_t ret;
3db888
	char  * ch;
3db888
3db888
	for (ch=buf; nbytes; ch+=ret) {
3db888
		ret = write(fdout,ch,nbytes);
3db888
3db888
		while ((ret < 0) && (errno == EINTR))
3db888
			ret = write(fdout,ch,nbytes);
3db888
3db888
		if (ret < 0)
3db888
			return ret;
3db888
3db888
		nbytes -= ret;
3db888
	}
3db888
3db888
	return 0;
3db888
}
3db888
9f55a1
static char * tpax_append_prefix_item(
9f55a1
	const struct tpax_driver_ctx *  dctx,
9f55a1
	const char *                    prefix)
9f55a1
{
9f55a1
	struct tpax_driver_ctx_impl *   ictx;
9f55a1
	char **                         prefv;
9f55a1
	char **                         psrc;
9f55a1
	char **                         pdst;
9f55a1
	off_t                           elements;
9f55a1
	char *                          pitem;
9f55a1
9f55a1
	ictx = tpax_get_driver_ictx(dctx);
9f55a1
9f55a1
	for (psrc=ictx->prefixv; *psrc; psrc++)
9f55a1
		if (!strcmp(*psrc,prefix))
9f55a1
			return *psrc;
9f55a1
9f55a1
	if (ictx->prefixp == ictx->prefcap) {
9f55a1
		elements  = ictx->prefcap - ictx->prefixv;
9f55a1
		elements += 256;
9f55a1
9f55a1
		if (!(prefv = calloc(elements,sizeof(char *))))
9f55a1
			return 0;
9f55a1
9f55a1
		for (psrc=ictx->prefixv,pdst=prefv; *psrc; psrc++,pdst++)
9f55a1
			*pdst = *psrc;
9f55a1
9f55a1
		if (ictx->prefixv != ictx->prefptr)
9f55a1
			free(ictx->prefixv);
9f55a1
9f55a1
		ictx->prefixv = prefv;
9f55a1
		ictx->prefixp = pdst;
9f55a1
		ictx->prefcap = &prefv[--elements];
9f55a1
	}
9f55a1
9f55a1
	if (!(pitem = strdup(prefix)))
9f55a1
		return 0;
9f55a1
9f55a1
	*ictx->prefixp++ = pitem;
9f55a1
9f55a1
	return pitem;
9f55a1
}
9f55a1
76f93a
static char * tpax_append_prefix_item_from_path(
76f93a
	const struct tpax_driver_ctx *  dctx,
76f93a
	const char *                    path,
76f93a
	const char *                    mark)
76f93a
{
76f93a
	off_t   nbytes;
76f93a
	char    pathbuf[PATH_MAX];
76f93a
76f93a
	if ((nbytes = (mark - path)) >= (PATH_MAX - 1))
76f93a
		return 0;
76f93a
76f93a
	memcpy(pathbuf,path,nbytes);
76f93a
	pathbuf[nbytes++] = '/';
76f93a
	pathbuf[nbytes]   = '\0';
76f93a
76f93a
	return tpax_append_prefix_item(dctx,pathbuf);
76f93a
}
76f93a
3db888
static int tpax_archive_append_pad(
409008
	const struct tpax_driver_ctx *  dctx,
409008
	int                             fdout,
409008
	const struct stat *             st)
3db888
{
409008
	int     ret;
409008
	off_t   cpos;
3db888
	ssize_t nbytes;
3db888
	char    buf[512];
3db888
3db888
	nbytes  = st->st_size;
3db888
	nbytes += 0x1ff;
3db888
	nbytes |= 0x1ff;
3db888
	nbytes ^= 0x1ff;
3db888
	nbytes -= st->st_size;
3db888
3db888
	memset(buf,0,nbytes);
3db888
409008
	cpos  = tpax_get_driver_cpos(dctx);
409008
	cpos += st->st_size + nbytes;
409008
409008
	if (!(ret = tpax_archive_append_memory_data(fdout,buf,nbytes)))
409008
		tpax_set_driver_cpos(dctx,cpos);
409008
409008
	return ret;
3db888
}
3db888
022508
static struct tpax_dirent_buffer * tpax_dirent_buf_first_alloc(
022508
	const struct tpax_driver_ctx * dctx)
022508
{
022508
	void * addr;
022508
	struct tpax_driver_ctx_impl * ictx;
022508
022508
	addr = (struct tpax_dirent_buffer *)mmap(
022508
		0,TPAX_DIRENT_BUFLEN,
022508
		PROT_READ|PROT_WRITE,
022508
		MAP_PRIVATE|MAP_ANONYMOUS,
022508
		-1,0);
022508
022508
	if (addr == MAP_FAILED)
022508
		return 0;
022508
022508
	ictx                  = tpax_get_driver_ictx(dctx);
022508
	ictx->dirents         = (struct tpax_dirent_buffer *)addr;
022508
	ictx->dirents->cdent  = ictx->dirents->dbuf;
022508
022508
	ictx->dirents->size   = TPAX_DIRENT_BUFLEN;
022508
	ictx->dirents->next   = 0;
022508
022508
	ictx->dirents->nfree  = TPAX_DIRENT_BUFLEN;
022508
	ictx->dirents->nfree -= offsetof(struct tpax_dirent_buffer,dbuf);
022508
022508
	return ictx->dirents;
022508
}
022508
022508
static struct tpax_dirent_buffer * tpax_dirent_buf_next_alloc(
022508
	struct tpax_dirent_buffer * current)
022508
{
022508
	void * addr;
022508
022508
	addr = (struct tpax_dirent_buffer *)mmap(
022508
		0,TPAX_DIRENT_BUFLEN,
022508
		PROT_READ|PROT_WRITE,
022508
		MAP_PRIVATE|MAP_ANONYMOUS,
022508
		-1,0);
022508
022508
	if (addr == MAP_FAILED)
022508
		return 0;
022508
022508
	current->next         = (struct tpax_dirent_buffer *)addr;
022508
	current->next->cdent  = current->next->dbuf;
022508
022508
	current->next->size   = TPAX_DIRENT_BUFLEN;
022508
	current->next->next   = 0;
022508
022508
	current->next->nfree  = TPAX_DIRENT_BUFLEN;
022508
	current->next->nfree -= offsetof(struct tpax_dirent_buffer,dbuf);
022508
022508
	return current->next;
022508
}
022508
022508
static int tpax_archive_append_ret(
022508
	int                     ret,
022508
	struct tpax_unit_ctx *  unit)
022508
{
022508
	if (unit)
022508
		tpax_free_unit_ctx(unit);
022508
022508
	return ret;
022508
}
022508
3179bb
static int tpax_archive_append_queue_item(
3179bb
	const struct tpax_driver_ctx *  dctx,
3179bb
	const struct dirent *           dirent,
3179bb
	const struct tpax_dirent *      parent,
9f55a1
	const char *                    prefix,
3179bb
	int                             depth,
9f55a1
	int                             flags,
3179bb
	int                             fd,
3179bb
	bool *                          fkeep)
3179bb
{
3179bb
	struct tpax_dirent_buffer *     dentbuf;
3179bb
	struct tpax_dirent *            cdent;
3179bb
	const char *                    src;
3179bb
	char *                          dst;
3179bb
	char *                          cap;
3179bb
	size_t                          needed;
3179bb
3179bb
	if (!(dentbuf = tpax_get_driver_dirents(dctx)))
3179bb
		if (!(dentbuf = tpax_dirent_buf_first_alloc(dctx)))
3179bb
			return tpax_archive_append_ret(
3179bb
				TPAX_SYSTEM_ERROR(dctx),
afec65
				0);
3179bb
3179bb
	needed  = dirent->d_reclen;
3179bb
	needed += offsetof(struct tpax_dirent,dirent);
3179bb
	needed += 0x7;
3179bb
	needed |= 0x7;
3179bb
	needed ^= 0x7;
3179bb
3179bb
	for (; dentbuf->next && (dentbuf->nfree < needed); )
3179bb
		dentbuf = dentbuf->next;
3179bb
3179bb
	if (dentbuf->nfree < needed)
3179bb
		if (!(dentbuf = tpax_dirent_buf_next_alloc(dentbuf)))
3179bb
			return tpax_archive_append_ret(
3179bb
				TPAX_SYSTEM_ERROR(dctx),
afec65
				0);
3179bb
3179bb
	*fkeep        = true;
3179bb
	cdent         = dentbuf->cdent;
3179bb
3179bb
	cdent->fdat   = fd;
3179bb
	cdent->depth  = depth;
9f55a1
	cdent->flags  = flags;
3179bb
	cdent->nsize  = needed;
3179bb
	cdent->parent = parent;
9f55a1
	cdent->prefix = prefix;
3179bb
3179bb
	memset(&cdent->dirent,0,offsetof(struct dirent,d_name));
3179bb
3179bb
	cdent->dirent.d_ino    = dirent->d_ino;
3179bb
	cdent->dirent.d_type   = dirent->d_type;
3179bb
	cdent->dirent.d_reclen = dirent->d_reclen;
3179bb
3179bb
	src  = dirent->d_name;
3179bb
	dst  = cdent->dirent.d_name;
3179bb
3179bb
	cap  = dst - offsetof(struct dirent,d_name);
3179bb
	cap -= offsetof(struct tpax_dirent,dirent);
3179bb
	cap += needed;
3179bb
3179bb
	for (; *src; )
3179bb
		*dst++ = *src++;
3179bb
3179bb
	for (; dst
3179bb
		*dst++ = 0;
3179bb
3179bb
	dentbuf->cdent  = (struct tpax_dirent *)cap;
3179bb
	dentbuf->nfree -= needed;
3179bb
a0e0a4
	tpax_set_driver_dirmark(dctx,cdent);
a0e0a4
3179bb
	return 0;
3179bb
}
3179bb
afec65
static int tpax_archive_append_dir_entries(
022508
	const struct tpax_driver_ctx *  dctx,
afec65
	struct tpax_dirent *            dent)
022508
{
022508
	int                             fd;
afec65
	int                             fdat;
afec65
	int                             depth;
022508
	bool                            fkeep;
022508
	long                            nbytes;
022508
	struct dirent *                 dirent;
022508
	struct dirent *                 dirents;
afec65
	struct tpax_unit_ctx *          uctx;
022508
	struct stat                     st;
022508
	uintptr_t                       addr;
022508
afec65
	/* init */
afec65
	fdat  = dent->fdat;
afec65
	depth = dent->depth;
022508
afec65
	/* uctx on the fly */
afec65
	if (tpax_get_unit_ctx(
afec65
			dctx,fdat,
afec65
			dent->dirent.d_name,
afec65
			&uctx) < 0)
022508
		return TPAX_NESTED_ERROR(dctx);
022508
022508
	/* verify that recursion item is still a directory */
afec65
	if (!S_ISDIR(uctx->st->st_mode))
022508
		return tpax_archive_append_ret(
022508
			TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FLOW_ERROR),
afec65
			uctx);
022508
022508
	/* obtain buffer for file-system directory entries */
022508
	dirents = tpax_get_driver_getdents_buffer(dctx);
022508
	dirent  = dirents;
022508
	fkeep   = false;
022508
	nbytes  = 0;
022508
	depth++;
022508
022508
	/* open directory and obtain first directory entries */
afec65
	if ((fd = openat(fdat,dent->dirent.d_name,O_RDONLY|O_DIRECTORY|O_CLOEXEC,0)) < 0)
022508
		return tpax_archive_append_ret(
022508
			TPAX_SYSTEM_ERROR(dctx),
afec65
			uctx);
022508
022508
	nbytes = tpax_getdents(fd,dirents,TPAX_DIRENT_BUFLEN);
022508
022508
	while ((nbytes == -EINTR) || ((nbytes < 0) && (errno == EINTR)))
022508
		nbytes = tpax_getdents(fd,dirents,TPAX_DIRENT_BUFLEN);
022508
022508
	if (nbytes < 0)
022508
		return tpax_archive_append_ret(
022508
			TPAX_SYSTEM_ERROR(dctx),
afec65
			uctx);
022508
022508
	/* iterate */
022508
	for (; nbytes>0; ) {
022508
		if (!strcmp(dirent->d_name,".")) {
022508
			(void)0;
022508
022508
		} else if (!strcmp(dirent->d_name,"..")) {
022508
			(void)0;
022508
022508
		} else {
022508
			if (dirent->d_type == DT_UNKNOWN) {
022508
				if (fstatat(fd,dirent->d_name,&st,AT_SYMLINK_NOFOLLOW))
022508
					return tpax_archive_append_ret(
022508
						TPAX_SYSTEM_ERROR(dctx),
afec65
						uctx);
022508
022508
				if (S_ISDIR(st.st_mode)) {
022508
					dirent->d_type = DT_DIR;
022508
				}
022508
			}
022508
afec65
			if (tpax_archive_append_queue_item(
afec65
					dctx,dirent,
afec65
					dent,0,depth,
afec65
					TPAX_ITEM_IMPLICIT,
afec65
					fd,&fkeep) < 0)
afec65
				return tpax_archive_append_ret(
afec65
					TPAX_NESTED_ERROR(dctx),
afec65
					uctx);
022508
		}
022508
022508
		addr    = (uintptr_t)dirent;
022508
		addr   += dirent->d_reclen;
022508
		nbytes -= dirent->d_reclen;
022508
		dirent  = (struct dirent *)addr;
022508
022508
		if (nbytes == 0) {
022508
			nbytes = tpax_getdents(fd,dirents,TPAX_DIRENT_BUFLEN);
022508
022508
			while ((nbytes == -EINTR) || ((nbytes < 0) && (errno == EINTR)))
022508
				nbytes = tpax_getdents(fd,dirents,TPAX_DIRENT_BUFLEN);
022508
022508
			if (nbytes < 0)
022508
				tpax_archive_append_ret(
022508
					TPAX_SYSTEM_ERROR(dctx),
afec65
					uctx);
022508
022508
			dirent = dirents;
022508
		}
3db888
	}
3db888
022508
	/* all done */
022508
	return tpax_archive_append_ret(
022508
		fkeep ? 0 : close(fd),
afec65
		uctx);
022508
}
022508
76f93a
static const char * tpax_path_prefix_mark(const char * path)
76f93a
{
76f93a
	const char *    src;
76f93a
	char *          mark;
76f93a
	char            pathbuf[PATH_MAX];
76f93a
76f93a
	src  = path;
76f93a
	mark = pathbuf;
76f93a
76f93a
	for (; *src; )
76f93a
		*mark++ = *src++;
76f93a
76f93a
	for (--mark; (*mark == '/'); mark--)
76f93a
		(void)0;
76f93a
76f93a
	*++mark = '\0';
76f93a
76f93a
	for (; (mark > pathbuf) && (mark[-1] != '/'); )
76f93a
		mark--;
76f93a
76f93a
	return (mark <= pathbuf) ? 0 : &path[--mark-pathbuf];
76f93a
}
76f93a
76f93a
static int tpax_dirent_init_from_uctx(
76f93a
	const struct stat * st,
76f93a
	const char *        basename,
76f93a
	struct dirent *     dirent)
76f93a
{
76f93a
	/* st_mode to d_type translation */
76f93a
	if (S_ISREG(st->st_mode))
76f93a
		dirent->d_type = DT_REG;
76f93a
	else if (S_ISLNK(st->st_mode))
76f93a
		dirent->d_type = DT_LNK;
76f93a
	else if (S_ISDIR(st->st_mode))
76f93a
		dirent->d_type = DT_DIR;
76f93a
	else if (S_ISCHR(st->st_mode))
76f93a
		dirent->d_type = DT_CHR;
76f93a
	else if (S_ISBLK(st->st_mode))
76f93a
		dirent->d_type = DT_CHR;
76f93a
	else if (S_ISFIFO(st->st_mode))
76f93a
		dirent->d_type = DT_CHR;
76f93a
	else
76f93a
		return -1;
76f93a
76f93a
	/* d_off, d_ino */
76f93a
	dirent->d_off    = 0;
76f93a
	dirent->d_ino    = st->st_ino;
76f93a
76f93a
	/* d_reclen */
76f93a
	dirent->d_reclen  = offsetof(struct dirent,d_name);
76f93a
	dirent->d_reclen += strlen(basename);
76f93a
76f93a
	dirent->d_reclen += 0x1;
76f93a
	dirent->d_reclen |= 0x1;
76f93a
	dirent->d_reclen ^= 0x1;
76f93a
76f93a
	/* d_name */
76f93a
	strcpy(dirent->d_name,basename);
76f93a
76f93a
	return 0;
76f93a
}
76f93a
f5ae32
int tpax_archive_append_item(
022508
	const struct tpax_driver_ctx *  dctx,
f5ae32
	const struct tpax_unit_ctx *    uctx)
022508
{
76f93a
	int                             fdat;
76f93a
	const char *                    name;
76f93a
	const char *                    mark;
76f93a
	const char *                    prefix;
76f93a
	bool                            fkeep;
76f93a
	struct dirent *                 dirent;
76f93a
	char                            entbuf[PATH_MAX + sizeof(struct dirent)];
76f93a
76f93a
	/* init */
76f93a
	fdat   = tpax_driver_fdcwd(dctx);
76f93a
	dirent = (struct dirent *)entbuf;
76f93a
	prefix = 0;
76f93a
76f93a
	/* split path to prefix + basename */
76f93a
	if ((mark = tpax_path_prefix_mark(*uctx->path)))
76f93a
		if (!(prefix = tpax_append_prefix_item_from_path(
76f93a
				dctx,*uctx->path,mark)))
76f93a
			return tpax_archive_append_ret(
76f93a
				TPAX_BUFFER_ERROR(dctx),
76f93a
				0);
76f93a
76f93a
	name = mark ? ++mark : *uctx->path;
76f93a
76f93a
	if (prefix)
76f93a
		if ((fdat = openat(fdat,prefix,O_RDONLY|O_DIRECTORY|O_CLOEXEC,0)) < 0)
76f93a
			return tpax_archive_append_ret(
76f93a
				TPAX_SYSTEM_ERROR(dctx),
76f93a
				0);
76f93a
76f93a
	/* explicit item directory entry */
76f93a
	if (tpax_dirent_init_from_uctx(uctx->st,name,dirent) < 0)
76f93a
			return tpax_archive_append_ret(
76f93a
				TPAX_CUSTOM_ERROR(
76f93a
					dctx,
76f93a
					TPAX_ERR_FLOW_ERROR),
76f93a
				0);
76f93a
76f93a
	/* add to queue */
76f93a
	if (tpax_archive_append_queue_item(
76f93a
			dctx,dirent,0,prefix,0,
76f93a
			TPAX_ITEM_EXPLICIT,
76f93a
			fdat,&fkeep) < 0)
76f93a
		return tpax_archive_append_ret(
76f93a
			TPAX_NESTED_ERROR(dctx),
76f93a
			0);
022508
76f93a
	(void)tpax_archive_append_pad;
afec65
	(void)tpax_archive_append_dir_entries;
022508
022508
	return 0;
409008
}
409008
409008
int tpax_archive_seal(const struct tpax_driver_ctx * dctx)
409008
{
409008
	int     fdout;
409008
	off_t   cpos;
409008
	ssize_t nbytes;
409008
	ssize_t nwritten;
409008
	ssize_t blksize;
409008
	char    buf[512];
409008
409008
	blksize = tpax_get_archive_block_size(dctx);
409008
	cpos    = tpax_get_driver_cpos(dctx);
409008
409008
	if (cpos % 512)
409008
		return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FLOW_ERROR);
409008
409008
	fdout = tpax_driver_fdout(dctx);
409008
	memset(buf,0,sizeof(buf));
409008
409008
	switch (cpos % blksize) {
409008
		case 0:
409008
			nbytes = cpos + blksize;
409008
			break;
409008
409008
		default:
409008
			nbytes  = cpos / blksize;
409008
			nbytes *= blksize;
409008
			nbytes += blksize;
409008
409008
			if (nbytes-cpos == 512)
409008
				nbytes += blksize;
409008
	}
409008
409008
	for (nwritten=cpos; nwritten
409008
		if (tpax_archive_append_memory_data(fdout,buf,512) < 0)
409008
			return TPAX_SYSTEM_ERROR(dctx);
409008
409008
		tpax_set_driver_cpos(dctx,nwritten);
409008
	}
409008
409008
	return 0;
3db888
}