Blame src/arbits/output/slbt_ar_output_members.c

47341a
/*******************************************************************/
47341a
/*  slibtool: a skinny libtool implementation, written in C        */
47341a
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
47341a
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
47341a
/*******************************************************************/
47341a
ef5cd3
#include <time.h>
ef5cd3
#include <locale.h>
ddfc22
#include <inttypes.h>
47341a
#include <slibtool/slibtool.h>
47341a
#include <slibtool/slibtool_output.h>
47341a
#include "slibtool_driver_impl.h"
47341a
#include "slibtool_dprintf_impl.h"
47341a
#include "slibtool_errinfo_impl.h"
47341a
#include "slibtool_ar_impl.h"
47341a
47341a
#define SLBT_PRETTY_FLAGS       (SLBT_PRETTY_YAML      \
47341a
	                         | SLBT_PRETTY_POSIX    \
47341a
	                         | SLBT_PRETTY_HEXDATA)
47341a
ddfc22
#define PPRIU64 "%"PRIu64
ddfc22
ef5cd3
const char slbt_ar_perm_strs[8][4] = {
ef5cd3
	{'-','-','-','\0'},
ef5cd3
	{'-','-','x','\0'},
ef5cd3
	{'-','w','-','\0'},
ef5cd3
	{'-','w','x','\0'},
ef5cd3
	{'r','-','-','\0'},
ef5cd3
	{'r','-','x','\0'},
ef5cd3
	{'r','w','-','\0'},
ef5cd3
	{'r','w','x','\0'}
ef5cd3
};
ef5cd3
ef5cd3
static unsigned slbt_ar_output_decimal_len_from_val(size_t val, unsigned min)
ef5cd3
{
ef5cd3
	unsigned ret;
ef5cd3
ef5cd3
	for (ret=0; val; ret++)
ef5cd3
		val /= 10;
ef5cd3
ef5cd3
	return (ret > min) ? ret : min;
ef5cd3
}
47341a
017165
static int slbt_ar_output_one_member_posix(
017165
	int                             fdout,
017165
	struct ar_meta_member_info *    memberp)
017165
{
017165
	return slbt_dprintf(
017165
		fdout,"%s\n",
017165
		memberp->ar_file_header.ar_member_name);
017165
}
017165
ef5cd3
static int slbt_ar_output_one_member_posix_verbose(
ef5cd3
	int                             fdout,
ef5cd3
	struct ar_meta_member_info *    memberp,
ef5cd3
	const char *                    fmtstr,
ef5cd3
	locale_t                        arlocale)
ef5cd3
{
ef5cd3
	unsigned    ownerbits;
ef5cd3
	unsigned    groupbits;
ef5cd3
	unsigned    worldbits;
ef5cd3
	time_t      artimeval;
ef5cd3
	struct tm   artimeloc;
ef5cd3
	char        artimestr[64] = {0};
ef5cd3
ef5cd3
	ownerbits = (memberp->ar_file_header.ar_file_mode & 0700) >> 6;
ef5cd3
	groupbits = (memberp->ar_file_header.ar_file_mode & 0070) >> 3;
ef5cd3
	worldbits = (memberp->ar_file_header.ar_file_mode & 0007);
ef5cd3
	artimeval = memberp->ar_file_header.ar_time_date_stamp;
ef5cd3
ef5cd3
	if (localtime_r(&artimeval,&artimeloc))
ef5cd3
		strftime_l(
ef5cd3
			artimestr,sizeof(artimestr),
ef5cd3
			"%b %e %H:%M %Y",&artimeloc,
ef5cd3
			arlocale);
ef5cd3
ef5cd3
	return slbt_dprintf(
ef5cd3
		fdout,fmtstr,
ef5cd3
		slbt_ar_perm_strs[ownerbits],
ef5cd3
		slbt_ar_perm_strs[groupbits],
ef5cd3
		slbt_ar_perm_strs[worldbits],
ef5cd3
		memberp->ar_file_header.ar_uid,
ef5cd3
		memberp->ar_file_header.ar_gid,
ef5cd3
		memberp->ar_file_header.ar_file_size,
ef5cd3
		artimestr,
ef5cd3
		memberp->ar_file_header.ar_member_name);
ef5cd3
}
ef5cd3
47341a
static int slbt_ar_output_members_posix(
47341a
	const struct slbt_driver_ctx *  dctx,
47341a
	const struct slbt_archive_meta * meta,
47341a
	const struct slbt_fd_ctx *       fdctx)
47341a
{
47341a
	struct ar_meta_member_info **   memberp;
017165
	int                             fdout;
ef5cd3
	size_t                          testval;
ef5cd3
	size_t                          sizelen;
ef5cd3
	size_t                          uidlen;
ef5cd3
	size_t                          gidlen;
ef5cd3
	locale_t                        arloc;
ef5cd3
	char                            fmtstr[64];
017165
017165
	fdout = fdctx->fdout;
ef5cd3
	arloc = 0;
ef5cd3
ef5cd3
	if (dctx->cctx->fmtflags & SLBT_PRETTY_VERBOSE) {
ef5cd3
		for (sizelen=0,memberp=meta->a_memberv; *memberp; memberp++)
ef5cd3
			if ((testval = memberp[0]->ar_file_header.ar_file_size) > sizelen)
ef5cd3
				sizelen = testval;
ef5cd3
ef5cd3
		for (uidlen=0,memberp=meta->a_memberv; *memberp; memberp++)
ef5cd3
			if ((testval = memberp[0]->ar_file_header.ar_uid) > uidlen)
ef5cd3
				uidlen = testval;
ef5cd3
ef5cd3
		for (gidlen=0,memberp=meta->a_memberv; *memberp; memberp++)
ef5cd3
			if ((testval = memberp[0]->ar_file_header.ar_gid) > gidlen)
ef5cd3
				gidlen = testval;
ef5cd3
ef5cd3
		sizelen = slbt_ar_output_decimal_len_from_val(sizelen,6);
ef5cd3
		uidlen  = slbt_ar_output_decimal_len_from_val(uidlen,1);
ef5cd3
		gidlen  = slbt_ar_output_decimal_len_from_val(gidlen,1);
ef5cd3
		arloc   = newlocale(LC_ALL,setlocale(LC_ALL,0),0);
ef5cd3
54813c
		sprintf(
54813c
			fmtstr,
54813c
			"%%s%%s%%s "
54813c
			"%%"   PPRIU64 "u"
54813c
			"/%%-" PPRIU64 "u "
54813c
			"%%"   PPRIU64 "u "
54813c
			"%%s "
54813c
			"%%s\n",
54813c
			uidlen,
54813c
			gidlen,
54813c
			sizelen);
ef5cd3
	}
47341a
47341a
	for (memberp=meta->a_memberv; *memberp; memberp++) {
47341a
		switch ((*memberp)->ar_member_attr) {
47341a
			case AR_MEMBER_ATTR_ARMAP:
47341a
			case AR_MEMBER_ATTR_LINKINFO:
47341a
			case AR_MEMBER_ATTR_NAMESTRS:
47341a
				break;
47341a
47341a
			default:
ef5cd3
				if (arloc) {
ef5cd3
					if (slbt_ar_output_one_member_posix_verbose(
ef5cd3
							fdout,*memberp,fmtstr,arloc) < 0)
ef5cd3
						return SLBT_SYSTEM_ERROR(dctx,0);
ef5cd3
				} else {
ef5cd3
					if (slbt_ar_output_one_member_posix(
ef5cd3
							fdout,*memberp) < 0)
ef5cd3
						return SLBT_SYSTEM_ERROR(dctx,0);
ef5cd3
				}
47341a
		}
47341a
	}
47341a
ef5cd3
	if (arloc)
ef5cd3
		freelocale(arloc);
ef5cd3
47341a
	return 0;
47341a
}
47341a
c4f93f
static int slbt_ar_output_one_member_yaml(
c4f93f
	int                             fdout,
c4f93f
	struct ar_meta_member_info *    memberp)
c4f93f
{
c4f93f
	return slbt_dprintf(
c4f93f
		fdout,
c4f93f
		"    - [ member: %s ]\n",
c4f93f
		memberp->ar_file_header.ar_member_name);
c4f93f
}
c4f93f
ddfc22
static int slbt_ar_output_one_member_yaml_verbose(
ddfc22
	int                             fdout,
ddfc22
	struct ar_meta_member_info *    memberp,
ddfc22
	locale_t                        arlocale)
ddfc22
{
ddfc22
	time_t      artimeval;
ddfc22
	struct tm   artimeloc;
ddfc22
	char        artimestr[64] = {0};
ddfc22
ddfc22
	artimeval = memberp->ar_file_header.ar_time_date_stamp;
ddfc22
ddfc22
	if (localtime_r(&artimeval,&artimeloc))
ddfc22
		strftime_l(
ddfc22
			artimestr,sizeof(artimestr),
ddfc22
			"%Y/%m/%d @ %H:%M",&artimeloc,
ddfc22
			arlocale);
ddfc22
ddfc22
	return slbt_dprintf(
ddfc22
		fdout,
ddfc22
		"    - Member:\n"
ddfc22
		"      - [ name:       " "%s"    " ]\n"
ddfc22
		"      - [ timestamp:  " "%s"    " ]\n"
ddfc22
		"      - [ filesize:   " PPRIU64 " ]\n"
ddfc22
		"      - [ uid:        " "%d"    " ]\n"
ddfc22
		"      - [ gid:        " "%d"    " ]\n"
ddfc22
		"      - [ mode:       " "%d"    " ]\n\n",
ddfc22
		memberp->ar_file_header.ar_member_name,
ddfc22
		artimestr,
ddfc22
		memberp->ar_file_header.ar_file_size,
ddfc22
		memberp->ar_file_header.ar_uid,
ddfc22
		memberp->ar_file_header.ar_gid,
ddfc22
		memberp->ar_file_header.ar_file_mode);
ddfc22
}
ddfc22
47341a
static int slbt_ar_output_members_yaml(
47341a
	const struct slbt_driver_ctx *  dctx,
47341a
	const struct slbt_archive_meta * meta,
47341a
	const struct slbt_fd_ctx *       fdctx)
47341a
{
47341a
	struct ar_meta_member_info **   memberp;
c4f93f
	int                             fdout;
ddfc22
	locale_t                        arloc;
c4f93f
c4f93f
	fdout = fdctx->fdout;
ddfc22
	arloc = 0;
ddfc22
ddfc22
	if (dctx->cctx->fmtflags & SLBT_PRETTY_VERBOSE) {
ddfc22
		arloc   = newlocale(LC_ALL,setlocale(LC_ALL,0),0);
ddfc22
	}
47341a
11d50e
	if (slbt_dprintf(fdctx->fdout,"  - Members:\n") < 0)
47341a
		return SLBT_SYSTEM_ERROR(dctx,0);
47341a
47341a
	for (memberp=meta->a_memberv; *memberp; memberp++) {
47341a
		switch ((*memberp)->ar_member_attr) {
47341a
			case AR_MEMBER_ATTR_ARMAP:
47341a
			case AR_MEMBER_ATTR_LINKINFO:
47341a
			case AR_MEMBER_ATTR_NAMESTRS:
47341a
				break;
47341a
47341a
			default:
ddfc22
				if (arloc) {
ddfc22
					if (slbt_ar_output_one_member_yaml_verbose(
ddfc22
							fdout,*memberp,arloc) < 0)
ddfc22
						return SLBT_SYSTEM_ERROR(dctx,0);
ddfc22
				} else {
ddfc22
					if (slbt_ar_output_one_member_yaml(
ddfc22
							fdout,*memberp) < 0)
ddfc22
						return SLBT_SYSTEM_ERROR(dctx,0);
ddfc22
				}
47341a
		}
47341a
	}
47341a
ddfc22
	if (arloc)
ddfc22
		freelocale(arloc);
ddfc22
47341a
	return 0;
47341a
}
47341a
47341a
int slbt_ar_output_members(const struct slbt_archive_meta * meta)
47341a
{
47341a
	const struct slbt_driver_ctx *  dctx;
47341a
	struct slbt_fd_ctx              fdctx;
47341a
47341a
	dctx = (slbt_archive_meta_ictx(meta))->dctx;
47341a
47341a
	if (slbt_get_driver_fdctx(dctx,&fdctx) < 0)
47341a
		return SLBT_NESTED_ERROR(dctx);
47341a
47341a
	if (!meta->a_memberv)
47341a
		return 0;
47341a
47341a
	switch (dctx->cctx->fmtflags & SLBT_PRETTY_FLAGS) {
47341a
		case SLBT_PRETTY_YAML:
47341a
			return slbt_ar_output_members_yaml(
47341a
				dctx,meta,&fdctx);
47341a
47341a
		case SLBT_PRETTY_POSIX:
47341a
			return slbt_ar_output_members_posix(
47341a
				dctx,meta,&fdctx);
47341a
47341a
		default:
47341a
			return slbt_ar_output_members_yaml(
47341a
				dctx,meta,&fdctx);
47341a
	}
47341a
}