Blame src/logic/tpax_archive_append.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
/**************************************************************/
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
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
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)
c9eeca
		tpax_lib_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,
733811
	int                             fdat,
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
733811
	cdent->fdat   = fdat;
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 */
c9eeca
	if (tpax_lib_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);
86604c
	dirent->d_reclen += strlen(basename) + 1;
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
99ab67
int tpax_archive_append(
022508
	const struct tpax_driver_ctx *  dctx,
f5ae32
	const struct tpax_unit_ctx *    uctx)
022508
{
76f93a
	int                             fdat;
1aa8ec
	uintptr_t                       addr;
76f93a
	const char *                    name;
76f93a
	const char *                    mark;
76f93a
	const char *                    prefix;
76f93a
	bool                            fkeep;
1aa8ec
	struct tpax_dirent_buffer *     dentbuf;
1aa8ec
	struct tpax_dirent *            cdent;
1aa8ec
	struct tpax_dirent *            cnext;
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
1aa8ec
	/* queue directory child items */
1aa8ec
	dentbuf = tpax_get_driver_dirents(dctx);
1aa8ec
	cdent   = tpax_get_driver_dirmark(dctx);
1aa8ec
1aa8ec
	for (; cdent; ) {
1aa8ec
		if (cdent->dirent.d_type == DT_DIR)
dac847
			if (dctx->cctx->drvflags & TPAX_DRIVER_DIR_MEMBER_RECURSE)
dac847
				if (tpax_archive_append_dir_entries(dctx,cdent) < 0)
dac847
					return TPAX_NESTED_ERROR(dctx);
1aa8ec
1aa8ec
		addr  = (uintptr_t)cdent;
1aa8ec
		addr += cdent->nsize;
1aa8ec
		cnext = (struct tpax_dirent *)addr;
1aa8ec
1aa8ec
		if (cnext == dentbuf->cdent) {
1aa8ec
			dentbuf = dentbuf->next;
1aa8ec
			cnext   = dentbuf ? dentbuf->dbuf : 0;
1aa8ec
		}
1aa8ec
1aa8ec
		cdent = cnext;
1aa8ec
	}
1aa8ec
409008
	return 0;
3db888
}