Blame src/util/slbt_copy_file.c

13b6d8
/*******************************************************************/
13b6d8
/*  slibtool: a skinny libtool implementation, written in C        */
49181b
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
13b6d8
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
13b6d8
/*******************************************************************/
13b6d8
13b6d8
#include <slibtool/slibtool.h>
c81d16
#include "slibtool_driver_impl.h"
13b6d8
#include "slibtool_spawn_impl.h"
127b94
#include "slibtool_symlink_impl.h"
42595b
#include "slibtool_errinfo_impl.h"
13b6d8
8dc63d
int slbt_util_copy_file(
fc7ad9
	struct slbt_exec_ctx *	ectx,
fc7ad9
	const char *		from,
fc7ad9
	const char *		to)
13b6d8
{
fc7ad9
	int	ret;
c81d16
	int	fdcwd;
d8d0e9
	char **	oargv;
9bf2c5
	char *	oprogram;
fc7ad9
	char *  src;
fc7ad9
	char *  dst;
13b6d8
	char *	cp[4];
fc7ad9
fc7ad9
	const struct slbt_driver_ctx *  dctx;
fc7ad9
fc7ad9
	/* driver context */
fc7ad9
	dctx = (slbt_get_exec_ictx(ectx))->dctx;
13b6d8
c81d16
	/* fdcwd */
c81d16
	fdcwd = slbt_driver_fdcwd(dctx);
c81d16
127b94
	/* placeholder? */
fc7ad9
	if (slbt_symlink_is_a_placeholder(fdcwd,from))
127b94
		return 0;
127b94
fc7ad9
	/* until we perform an in-memory copy ... */
fc7ad9
	if (!(src = strdup(from)))
fc7ad9
		return SLBT_SYSTEM_ERROR(dctx,0);
fc7ad9
fc7ad9
	if (!(dst = strdup(to))) {
fc7ad9
		free(src);
fc7ad9
		return SLBT_SYSTEM_ERROR(dctx,0);
fc7ad9
	}
fc7ad9
13b6d8
	/* cp argv */
13b6d8
	cp[0] = "cp";
13b6d8
	cp[1] = src;
13b6d8
	cp[2] = dst;
13b6d8
	cp[3] = 0;
13b6d8
13b6d8
	/* alternate argument vector */
9bf2c5
	oprogram      = ectx->program;
d8d0e9
	oargv         = ectx->argv;
13b6d8
	ectx->argv    = cp;
13b6d8
	ectx->program = "cp";
13b6d8
13b6d8
	/* step output */
d8d0e9
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT)) {
0364b5
		if (dctx->cctx->mode == SLBT_MODE_LINK) {
33a569
			if (slbt_output_link(ectx)) {
0364b5
				ectx->argv = oargv;
9bf2c5
				ectx->program = oprogram;
fc7ad9
				free(src); free(dst);
42595b
				return SLBT_NESTED_ERROR(dctx);
0364b5
			}
0364b5
		} else {
33a569
			if (slbt_output_install(ectx)) {
0364b5
				ectx->argv = oargv;
9bf2c5
				ectx->program = oprogram;
fc7ad9
				free(src); free(dst);
42595b
				return SLBT_NESTED_ERROR(dctx);
0364b5
			}
d8d0e9
		}
d8d0e9
	}
13b6d8
fcfea7
	/* cp spawn */
fcfea7
	if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) {
fcfea7
		ret = SLBT_SPAWN_ERROR(dctx);
fcfea7
fcfea7
	} else if (ectx->exitcode) {
fcfea7
		ret = SLBT_CUSTOM_ERROR(
fcfea7
			dctx,
fcfea7
			SLBT_ERR_COPY_ERROR);
fcfea7
	} else {
fcfea7
		ret = 0;
fcfea7
	}
13b6d8
fc7ad9
	free(src);
fc7ad9
	free(dst);
fc7ad9
d8d0e9
	ectx->argv = oargv;
9bf2c5
	ectx->program = oprogram;
d8d0e9
	return ret;
13b6d8
}