Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
backup_libraries: now fully respectes rootdir
Baptiste Daroussin committed 22 days ago
commit c79dfc9e1c5e9274a1247a28140ece599b26fc0b
parent 04e8f3d
2 files changed +71 -8
modified libpkg/backup_lib.c
@@ -1,5 +1,5 @@
/*-
-
 * Copyright (c) 2020 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2020-2026 Baptiste Daroussin <bapt@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
@@ -32,6 +32,19 @@
#include "private/pkg.h"
#include "private/pkgdb.h"

+
static const char *
+
backup_library_relative_path(void)
+
{
+
	const char *path = ctx.backup_library_path;
+

+
	if (ctx.pkg_rootdir != NULL) {
+
		size_t rootlen = strlen(ctx.pkg_rootdir);
+
		if (strncmp(path, ctx.pkg_rootdir, rootlen) == 0)
+
			path += rootlen;
+
	}
+
	return (path);
+
}
+

static int
register_backup(struct pkgdb *db, struct pkg *orig, int fd, const char *libname)
{
@@ -92,7 +105,7 @@ register_backup(struct pkgdb *db, struct pkg *orig, int fd, const char *libname)
		pkg_file_free(e->value);
		pkghash_del(pkg->filehash, libname);
	}
-
	xasprintf(&lpath, "%s/%s", ctx.backup_library_path, libname);
+
	xasprintf(&lpath, "%s/%s", backup_library_relative_path(), libname);
	pkg_addfile(pkg, lpath, sum, false);
	free(lpath);

@@ -119,6 +132,7 @@ static void
backup_library(struct pkgdb *db, struct pkg *p, const char *path)
{
	const char *libname;
+
	const char *bkpath;
	int from, to, backupdir;

	if ((libname = strrchr(path, '/')) == NULL)
@@ -128,6 +142,7 @@ backup_library(struct pkgdb *db, struct pkg *p, const char *path)

	pkg_open_root_fd(p);
	to = -1;
+
	bkpath = backup_library_relative_path();

	from = openat(p->rootfd, RELATIVE_PATH(path), O_RDONLY);
	if (from == -1) {
@@ -135,19 +150,19 @@ backup_library(struct pkgdb *db, struct pkg *p, const char *path)
		return;
	}

-
	if (mkdirat(p->rootfd, RELATIVE_PATH(ctx.backup_library_path), 0755) == -1) {
-
		if (!mkdirat_p(p->rootfd, RELATIVE_PATH(ctx.backup_library_path))) {
+
	if (mkdirat(p->rootfd, RELATIVE_PATH(bkpath), 0755) == -1) {
+
		if (!mkdirat_p(p->rootfd, RELATIVE_PATH(bkpath))) {
			pkg_emit_errno("Unable to create the library backup "
-
			    "directory", ctx.backup_library_path);
+
			    "directory", bkpath);
			close(from);
			return;
		}
	}
-
	backupdir = openat(p->rootfd, RELATIVE_PATH(ctx.backup_library_path),
+
	backupdir = openat(p->rootfd, RELATIVE_PATH(bkpath),
	    O_DIRECTORY);
	if (backupdir == -1) {
		pkg_emit_error("Unable to open the library backup "
-
		    "directory %s", ctx.backup_library_path);
+
		    "directory %s", bkpath);
		goto out;
	}

modified tests/frontend/backup_lib.sh
@@ -8,7 +8,8 @@ tests_init \
	depends \
	multiple_upgrade \
	per_library_packages \
-
	per_library_delete
+
	per_library_delete \
+
	rootdir_default_path

basic_body() {
	atf_skip_on Darwin The macOS linker uses different flags
@@ -541,3 +542,50 @@ EOF
	atf_check \
	    pkg -r ${TMPDIR}/target info -e test-backup-libbar.so.1
}
+

+
rootdir_default_path_body()
+
{
+
	atf_skip_on Darwin The macOS linker uses different flags
+

+
	atf_check touch empty.c
+
	atf_check cc -shared -Wl,-soname=libtest.so.1 empty.c -o libtest.so.1
+

+
	atf_check sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "1"
+
	cat << EOF >> test.ucl
+
files: {
+
	${TMPDIR}/libtest.so.1: "",
+
}
+
EOF
+
	atf_check pkg create -M test.ucl
+

+
	atf_check sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "2"
+
	atf_check pkg create -M test.ucl
+

+
	atf_check mkdir ${TMPDIR}/target ${TMPDIR}/reposconf
+

+
	atf_check \
+
	    pkg -o REPOS_DIR=/dev/null -r ${TMPDIR}/target \
+
	        install -qfy ${TMPDIR}/test-1.pkg
+

+
	rm test-1.pkg
+
	atf_check -o ignore pkg repo .
+
	cat <<EOF > ${TMPDIR}/reposconf/repo.conf
+
local: {
+
	url: file://${TMPDIR},
+
	enabled: true
+
}
+
EOF
+

+
	default_path=$(pkg config BACKUP_LIBRARY_PATH)
+
	atf_check -o ignore \
+
	    pkg -o BACKUP_LIBRARIES=true \
+
	        -o REPOS_DIR=${TMPDIR}/reposconf -r ${TMPDIR}/target \
+
	        upgrade -y
+

+
	atf_check test -f ${TMPDIR}/target${default_path}/libtest.so.1
+

+
	atf_check test ! -d ${TMPDIR}/target${default_path}${default_path}
+

+
	atf_check -o inline:"${default_path}/libtest.so.1\n" \
+
	    pkg -r ${TMPDIR}/target query "%Fp" test-backup-libtest.so.1
+
}