Blame src/cmds/pe_cmd_ar.c

c73e19
/***************************************************************/
c73e19
/*  perk: PE Resource Kit                                      */
c73e19
/*  Copyright (C) 2015--2025  SysDeer Technologies, LLC        */
c73e19
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
c73e19
/***************************************************************/
c73e19
c73e19
#include <perk/perk.h>
c73e19
#include "perk_driver_impl.h"
663098
#include "perk_errinfo_impl.h"
663098
#include "perk_ar_impl.h"
663098
75666b
int pe_ar_list_members(const struct pe_archive_meta *, const char **);
75666b
0f4b7a
static int pe_cmd_ar_perform_unit_actions(
75666b
	const struct pe_driver_ctx * dctx,
75666b
	const char *                 arname,
0f4b7a
	const char **                members,
0f4b7a
	uint64_t                     action)
75666b
{
75666b
	struct pe_unit_ctx * arctx = 0;
0f4b7a
	int  (*pe_ar_fn)(const struct pe_archive_meta *,const char **);
75666b
0f4b7a
	if (action == PERK_DRIVER_AR_LIST_MEMBERS) {
0f4b7a
		pe_ar_fn = pe_ar_list_members;
eb6452
8ba1bc
	} else if (action == PERK_DRIVER_AR_PRINT_MEMBERS) {
eb6452
		pe_ar_fn = pe_ar_print_members;
64d692
64d692
	} else {
64d692
		return 0;
0f4b7a
	}
0f4b7a
64d692
	if (pe_lib_get_unit_ctx(dctx,arname,&arctx) < 0)
64d692
		return PERK_NESTED_ERROR(dctx);
64d692
64d692
	if (arctx->armeta == 0)
64d692
		return PERK_CUSTOM_ERROR(dctx,
64d692
			PERK_ERR_AR_NON_ARCHIVE_IMAGE);
64d692
0f4b7a
	if (pe_ar_fn(arctx->armeta,members) < 0) {
75666b
		pe_lib_free_unit_ctx(arctx);
75666b
		return PERK_NESTED_ERROR(dctx);
75666b
	}
75666b
75666b
	pe_lib_free_unit_ctx(arctx);
75666b
75666b
	return 0;
75666b
}
75666b
663098
static int pe_cmd_ar_verify_cmdline(
663098
	const struct pe_driver_ctx * dctx,
663098
	uint64_t                     flags,
663098
	const char *                 posname,
663098
	const char *                 arname,
75666b
	const char **                members)
663098
{
663098
	uint64_t action;
663098
	uint64_t poscmd;
663098
	uint64_t vercmd;
663098
663098
	action = (flags & AR_ACTION_MASK);
663098
	poscmd = (flags & AR_POSNAME_MASK);
663098
	vercmd = (flags & PERK_DRIVER_VERSION);
663098
75666b
	if (vercmd && !posname && !arname && !members)
663098
		return 0;
663098
663098
	switch (action) {
663098
		case 0:
663098
			return PERK_CUSTOM_ERROR(dctx,
663098
				PERK_ERR_AR_MISSING_ACTION);
663098
663098
		case PERK_DRIVER_AR_LIST_MEMBERS:
663098
		case PERK_DRIVER_AR_DELETE_MEMBERS:
663098
		case PERK_DRIVER_AR_APPEND_MEMBERS:
663098
		case PERK_DRIVER_AR_EXTRACT_MEMBERS:
8ba1bc
		case PERK_DRIVER_AR_PRINT_MEMBERS:
663098
			if (poscmd || posname)
663098
				return PERK_CUSTOM_ERROR(dctx,
663098
					PERK_ERR_AR_INVALID_ANCHORS);
663098
663098
			break;
663098
663098
		case AR_UPDATE_MASK:
663098
		case PERK_DRIVER_AR_REPLACE_MEMBERS:
663098
		case PERK_DRIVER_AR_MOVE_MEMBERS:
663098
			switch (poscmd) {
663098
				case 0:
663098
					if (posname)
663098
						return PERK_CUSTOM_ERROR(dctx,
663098
							PERK_ERR_AR_MISSING_ANCHOR);
663098
					break;
663098
663098
				case PERK_DRIVER_AR_POSITION_AFTER:
663098
				case PERK_DRIVER_AR_POSITION_BEFORE:
663098
					if (!posname)
663098
						return PERK_CUSTOM_ERROR(dctx,
663098
							PERK_ERR_AR_NULL_POSNAME);
663098
					break;
663098
663098
				default:
663098
					return PERK_CUSTOM_ERROR(dctx,
663098
						PERK_ERR_AR_MULTIPLE_ANCHORS);
663098
			}
663098
663098
		default:
663098
			return PERK_CUSTOM_ERROR(dctx,
663098
				PERK_ERR_AR_MULTIPLE_ACTIONS);
663098
	}
663098
663098
	if (!arname)
663098
		return PERK_CUSTOM_ERROR(dctx,
663098
			PERK_ERR_AR_NULL_ARNAME);
663098
663098
	return 0;
663098
}
c73e19
5b248d
int pe_cmd_ar(
5b248d
	const struct pe_driver_ctx * dctx,
5b248d
	uint64_t                     flags,
5b248d
	const char *                 posname,
5b248d
	const char *                 arname,
75666b
	const char **                members)
c73e19
{
0f4b7a
	uint64_t action = (flags & AR_ACTION_MASK);
0f4b7a
75666b
	if (pe_cmd_ar_verify_cmdline(dctx,flags,posname,arname,members) < 0)
663098
		return PERK_NESTED_ERROR(dctx);
663098
0f4b7a
	if (pe_cmd_ar_perform_unit_actions(dctx,arname,members,action) < 0)
0f4b7a
		return PERK_NESTED_ERROR(dctx);
75666b
c73e19
	return 0;
c73e19
}