summaryrefslogtreecommitdiff
path: root/src/helper/slbt_archive_import.c
blob: 051c8177580352f35fe48355041f3cec6af6574a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*******************************************************************/
/*  slibtool: a skinny libtool implementation, written in C        */
/*  Copyright (C) 2016--2021  SysDeer Technologies, LLC            */
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
/*******************************************************************/

#include <slibtool/slibtool.h>
#include "slibtool_driver_impl.h"
#include "slibtool_symlink_impl.h"
#include "slibtool_errinfo_impl.h"

/* legacy fallback, no longer in use */
extern int slbt_archive_import_mri(
	const struct slbt_driver_ctx *	dctx,
	struct slbt_exec_ctx *		ectx,
	char *				dstarchive,
	char *				srcarchive);

/* use slibtool's in-memory archive merging facility */
static int slbt_archive_import_impl(
	const struct slbt_driver_ctx *	dctx,
	struct slbt_exec_ctx *		ectx,
	char *				dstarchive,
	char *				srcarchive)
{
	int                             ret;
	struct slbt_archive_ctx *       arctxv[3] = {0,0,0};
	struct slbt_archive_ctx *       arctx;

	(void)ectx;

	if (slbt_get_archive_ctx(dctx,dstarchive,&arctxv[0]) < 0)
		return SLBT_NESTED_ERROR(dctx);

	if (slbt_get_archive_ctx(dctx,srcarchive,&arctxv[1]) < 0) {
		slbt_free_archive_ctx(arctxv[0]);
		return SLBT_NESTED_ERROR(dctx);
	}

	ret = slbt_merge_archives(arctxv,&arctx);

	slbt_free_archive_ctx(arctxv[0]);
	slbt_free_archive_ctx(arctxv[1]);

	if (ret == 0) {
		ret = slbt_store_archive(arctx,dstarchive,0644);
		slbt_free_archive_ctx(arctx);
	}

	return (ret < 0) ? SLBT_NESTED_ERROR(dctx) : 0;
}


int slbt_archive_import(
	const struct slbt_driver_ctx *	dctx,
	struct slbt_exec_ctx *		ectx,
	char *				dstarchive,
	char *				srcarchive)
{
	if (slbt_symlink_is_a_placeholder(
			slbt_driver_fdcwd(dctx),
			srcarchive))
		return 0;

	return slbt_archive_import_impl(
		dctx,ectx,
		dstarchive,
		srcarchive);
}