Blame src/driver/tpax_amain.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
/**************************************************************/
88751e
00bc9c
#include <fcntl.h>
88751e
#include <stdio.h>
88751e
#include <unistd.h>
88751e
#include <tpax/tpax.h>
88751e
#include "tpax_driver_impl.h"
88751e
#include "tpax_dprintf_impl.h"
88751e
88751e
#ifndef TPAX_DRIVER_FLAGS
88751e
#define TPAX_DRIVER_FLAGS	TPAX_DRIVER_VERBOSITY_ERRORS \
23fa88
				| TPAX_DRIVER_VERBOSITY_USAGE \
23fa88
				| TPAX_DRIVER_DIR_MEMBER_RECURSE
88751e
#endif
88751e
88751e
static const char vermsg[] = "%s%s%s (https://git.foss21.org/tpax): "
88751e
			     "version %s%d.%d.%d%s.\n"
88751e
			     "[commit reference: %s%s%s]\n";
88751e
88751e
static const char * const tpax_ver_color[6] = {
88751e
		"\x1b[1m\x1b[35m","\x1b[0m",
88751e
		"\x1b[1m\x1b[32m","\x1b[0m",
88751e
		"\x1b[1m\x1b[34m","\x1b[0m"
88751e
};
88751e
88751e
static const char * const tpax_ver_plain[6] = {
88751e
		"","",
88751e
		"","",
88751e
		"",""
88751e
};
88751e
88751e
static ssize_t tpax_version(struct tpax_driver_ctx * dctx, int fdout)
88751e
{
88751e
	const struct tpax_source_version * verinfo;
88751e
	const char * const * verclr;
88751e
537964
	verinfo = tpax_api_source_version();
88751e
	verclr  = isatty(fdout) ? tpax_ver_color : tpax_ver_plain;
88751e
88751e
	return tpax_dprintf(
88751e
			fdout,vermsg,
88751e
			verclr[0],dctx->program,verclr[1],
88751e
			verclr[2],verinfo->major,verinfo->minor,
88751e
			verinfo->revision,verclr[3],
88751e
			verclr[4],verinfo->commit,verclr[5]);
88751e
}
88751e
88751e
static void tpax_perform_unit_actions(
88751e
	const struct tpax_driver_ctx *	dctx,
88751e
	struct tpax_unit_ctx *		uctx)
88751e
{
9c1558
	if (dctx->cctx->drvflags & TPAX_DRIVER_EXEC_MODE_WRITE)
43c39b
		tpax_archive_enqueue(dctx,uctx);
88751e
}
88751e
5ce6ff
static void tpax_archive_write_and_seal(
5ce6ff
	const struct tpax_driver_ctx *	dctx)
5ce6ff
{
5ce6ff
	if (tpax_archive_write(dctx) == TPAX_OK)
5ce6ff
		tpax_archive_seal(dctx);
5ce6ff
}
5ce6ff
5ce6ff
static void tpax_perform_archive_actions(
5ce6ff
	const struct tpax_driver_ctx *	dctx)
5ce6ff
{
5ce6ff
	if (dctx->cctx->drvflags & TPAX_DRIVER_EXEC_MODE_WRITE)
5ce6ff
		tpax_archive_write_and_seal(dctx);
5ce6ff
}
5ce6ff
88751e
static int tpax_exit(struct tpax_driver_ctx * dctx, int ret)
88751e
{
88751e
	tpax_output_error_vector(dctx);
c9eeca
	tpax_lib_free_driver_ctx(dctx);
88751e
	return ret;
88751e
}
88751e
88751e
int tpax_main(char ** argv, char ** envp, const struct tpax_fd_ctx * fdctx)
88751e
{
88751e
	int			ret;
88751e
	int			fdout;
00bc9c
	int                     fdcwd;
88751e
	uint64_t		flags;
88751e
	struct tpax_driver_ctx *	dctx;
88751e
	struct tpax_unit_ctx *	uctx;
88751e
	const char **		unit;
88751e
88751e
	flags = TPAX_DRIVER_FLAGS;
88751e
	fdout = fdctx ? fdctx->fdout : STDOUT_FILENO;
00bc9c
	fdcwd = fdctx ? fdctx->fdcwd : AT_FDCWD;
88751e
c9eeca
	if ((ret = tpax_lib_get_driver_ctx(argv,envp,flags,fdctx,&dctx)))
88751e
		return (ret == TPAX_USAGE)
88751e
			? !argv || !argv[0] || !argv[1]
88751e
			: TPAX_ERROR;
88751e
88751e
	if (dctx->cctx->drvflags & TPAX_DRIVER_VERSION)
88751e
		if ((tpax_version(dctx,fdout)) < 0)
88751e
			return tpax_exit(dctx,TPAX_ERROR);
88751e
88751e
	for (unit=dctx->units; *unit; unit++) {
c9eeca
		if (!(tpax_lib_get_unit_ctx(dctx,fdcwd,*unit,&uctx))) {
88751e
			tpax_perform_unit_actions(dctx,uctx);
c9eeca
			tpax_lib_free_unit_ctx(uctx);
88751e
		}
88751e
	}
88751e
5ce6ff
	tpax_perform_archive_actions(dctx);
9c1558
88751e
	return tpax_exit(dctx,dctx->errv[0] ? TPAX_ERROR : TPAX_OK);
88751e
}