Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Merge branch 'upstream-master'
Shawn Webb committed 3 years ago
commit e294fea49d1a20e355ea2403fe9f39141cc32fea
parent 0287b56
29 files changed +173 -132
modified .cirrus.yml
@@ -11,6 +11,9 @@ freebsd_task:
                - name: freebsd13-amd64
                  freebsd_instance:
                     image: freebsd-13-0-release-amd64
+
                - name: freebsd13.1-amd64
+
                  freebsd_instance:
+
                     image: freebsd-13-1-release-amd64
                - name: freebsd14-amd64
                  freebsd_instance:
                     image_family: freebsd-14-0-snap
@@ -36,40 +39,40 @@ debian_task:
                - make -j4 || make V=1
        check_script: make check || { kyua report --verbose ; exit 1 ; }

-
osx_task:
-
        osx_instance:
-
                image: catalina-xcode
-
        install_script: scripts/install_deps.sh
-
        env:
-
                LDFLAGS: -L/usr/local/opt/libarchive/lib -L/usr/local/opt/openssl/lib
-
                CPPFLAGS: -I/usr/local/opt/libarchive/include -I/usr/local/opt/openssl/include -ftrivial-auto-var-init=pattern
-
                CXXFLAGS: -I/usr/local/opt/libarchive/include -I/usr/local/opt/openssl/include -ftrivial-auto-var-init=pattern
-
                CFLAGS: -I/usr/local/opt/libarchive/include -I/usr/local/opt/openssl/include -ftrivial-auto-var-init=pattern
-
        script:
-
                - ./configure --with-asan --with-ubsan
-
                - make -j4 || make V=1
-
        check_script: make check || { kyua report --verbose ; exit 1 ;}
+
#osx_task:
+
#        osx_instance:
+
#                image: catalina-xcode
+
#        install_script: scripts/install_deps.sh
+
#        env:
+
#                LDFLAGS: -L/usr/local/opt/libarchive/lib -L/usr/local/opt/openssl/lib
+
#                CPPFLAGS: -I/usr/local/opt/libarchive/include -I/usr/local/opt/openssl/include -ftrivial-auto-var-init=pattern
+
#                CXXFLAGS: -I/usr/local/opt/libarchive/include -I/usr/local/opt/openssl/include -ftrivial-auto-var-init=pattern
+
#                CFLAGS: -I/usr/local/opt/libarchive/include -I/usr/local/opt/openssl/include -ftrivial-auto-var-init=pattern
+
#        script:
+
#                - ./configure --with-asan --with-ubsan
+
#                - make -j4 || make V=1
+
#        check_script: make check || { kyua report --verbose ; exit 1 ;}

-
fedora_task:
-
        container:
-
                image: fedora:latest
-
        env:
-
                # False-positive leak detection in recent glibc:
-
                # Direct leak of 12 byte(s) in 2 object(s) allocated from:
-
                # #0 0x7fe222e5d3cf in __interceptor_malloc (/lib64/libasan.so.6+0xab3cf)
-
                # #1 0x7fe22260f08f in __vasprintf_internal (/lib64/libc.so.6+0x7f08f)
-
                ASAN_OPTIONS: detect_leaks=0
-
        install_script:
-
                - dnf -y upgrade
-
                - dnf -y install gcc g++ make kyua libatf-c-devel libatf-sh-devel libatf-sh libbsd-devel zlib-devel
-
                                 bzip2-devel libarchive-devel openssl-devel xz-devel m4 lz4-devel expat-devel
-
                                 libxml2-devel libacl-devel libzstd-devel
-
                                 libasan-static libubsan-static libasan-static libubsan-static
-
                                 bsdtar diffutils openssl which xz python3 bzip2
-
        script:
-
                - ./configure --with-libarchive.pc --with-asan --with-ubsan
-
                - make -j4 || make V=1
-
        check_script: make check || { kyua report --verbose ; exit 1 ; }
+
#fedora_task:
+
#        container:
+
#                image: fedora:latest
+
#        env:
+
#                # False-positive leak detection in recent glibc:
+
#                # Direct leak of 12 byte(s) in 2 object(s) allocated from:
+
#                # #0 0x7fe222e5d3cf in __interceptor_malloc (/lib64/libasan.so.6+0xab3cf)
+
#                # #1 0x7fe22260f08f in __vasprintf_internal (/lib64/libc.so.6+0x7f08f)
+
#                ASAN_OPTIONS: detect_leaks=0
+
#        install_script:
+
#                - dnf -y upgrade
+
#                - dnf -y install gcc g++ make kyua libatf-c-devel libatf-sh-devel libatf-sh libbsd-devel zlib-devel
+
#                                 bzip2-devel libarchive-devel openssl-devel xz-devel m4 lz4-devel expat-devel
+
#                                 libxml2-devel libacl-devel libzstd-devel
+
#                                 libasan-static libubsan-static libasan-static libubsan-static
+
#                                 bsdtar diffutils openssl which xz python3 bzip2
+
#        script:
+
#                - ./configure --with-libarchive.pc --with-asan --with-ubsan
+
#                - make -j4 || make V=1
+
#        check_script: make check || { kyua report --verbose ; exit 1 ; }

alpine_task:
        container:
modified auto.def
@@ -359,3 +359,4 @@ foreach dir [list external/blake2 external/picosat \
	make-template $dir/Makefile.autosetup $dir/Makefile
}

+
file mkdir external/libucl/src scripts/periodic scripts/completion
modified compat/Makefile.autosetup
@@ -8,7 +8,10 @@ SRCS= closefrom.c \
	funopen.c

LOCAL_CFLAGS=	-I$(top_srcdir)/compat \
-
		-I$(top_srcdir)
+
		-I$(top_srcdir) \
+
		-I$(top_builddir)
+

+
VPATH=	$(top_srcdir)/compat

include $(MK)/static-lib.mk

modified docs/Makefile.autosetup
@@ -46,18 +46,20 @@ MAN3GZ= $(MAN3:.3=.3.gz)
MAN5GZ=	$(MAN5:.5=.5.gz)
MAN8GZ=	$(MAN8:.8=.8.gz)

+
VPATH=	$(top_srcdir)/docs
+

.SUFFIXES: .3.gz .5.gz .8.gz .3 .5 .8

all: $(MAN3GZ) $(MAN5GZ) $(MAN8GZ)

.3.3.gz:
-
	gzip -kfn $< > $@
+
	gzip -knc $< > $@

.5.5.gz:
-
	gzip -kfn $< > $@
+
	gzip -knc $< > $@

.8.8.gz:
-
	gzip -kfn $< > $@
+
	gzip -knc $< > $@

clean:
	rm -f *.gz
modified external/blake2/Makefile.autosetup
@@ -3,4 +3,6 @@ LIB= blake2
SRCS=	blake2b-ref.c blake2s-ref.c
LOCAL_CFLAGS=	-I$(top_srcdir)/compat -O3

+
VPATH=	$(top_srcdir)/external/blake2
+

include $(MK)/static-lib.mk
modified external/libfetch/Makefile.autosetup
@@ -6,9 +6,12 @@ SRCS= common.c \

LOCAL_CFLAGS=	-I$(top_srcdir)/compat \
		-I$(top_srcdir) \
+
		-I$(top_builddir) \
		-Wno-unused-parameter \
		-Wno-pointer-sign \
		-DWITH_SSL \
		-DINET6

+
VPATH=	$(top_srcdir)/external/libfetch
+

include $(MK)/static-lib.mk
modified external/libucl/Makefile.autosetup
@@ -17,4 +17,6 @@ LOCAL_CFLAGS= -I$(top_srcdir)/external/libucl/include \
		-Wno-unused-parameter \
		-Wno-pointer-sign

+
VPATH=	$(top_srcdir)/external/libucl
+

include $(MK)/static-lib.mk
modified external/linenoise/Makefile.autosetup
@@ -1,5 +1,6 @@
include @builddir@/mk/defs.mk
LIB=	linenoise
SRCS=	linenoise.c
+
VPATH=	$(top_srcdir)/external/linenoise

include $(MK)/static-lib.mk
modified external/msgpuck/Makefile.autosetup
@@ -5,5 +5,7 @@ LIB= msgpuck
SRCS=	msgpuck.c \
	hints.c

+
VPATH=	$(top_srcdir)/external/msgpuck
+

include $(MK)/static-lib.mk

modified external/picosat/Makefile.autosetup
@@ -4,4 +4,7 @@ SRCS= picosat.c \
	version.c
LOCAL_CFLAGS=	-Wno-strict-aliasing \
		-Wno-unused -DNGETRUSAGE
+

+
VPATH=	$(top_srcdir)/external/picosat
+

include $(MK)/static-lib.mk
modified external/sqlite/Makefile.autosetup
@@ -4,6 +4,7 @@ SRCS= sqlite3.c \
	shell.c

LOCAL_CFLAGS=	-I$(top_srcdir)/external/linenoise \
+
		-I$(top_builddir)/external/sqlite \
		-D_HAVE_SQLITE_CONFIG_H \
		-DHAVE_LINENOISE \
		-Wno-unused-variable \
@@ -39,4 +40,6 @@ LOCAL_CFLAGS= -I$(top_srcdir)/external/linenoise \
		-Dmain=sqlite3_shell \
		-DNDEBUG

+
VPATH=	$(top_srcdir)/external/sqlite
+

include $(MK)/static-lib.mk
modified external/yxml/Makefile.autosetup
@@ -4,7 +4,9 @@ LIB= yxml

SRCS=	yxml.c

-
LOCAL_CFLAGS=	-Wno-unused-parameter -I.
+
LOCAL_CFLAGS=	-Wno-unused-parameter -I. -I$(top_srcdir)/external/yxml
+

+
VPATH=	$(top_srcdir)/external/yxml

include $(MK)/static-lib.mk

modified libpkg/Makefile.autosetup
@@ -64,8 +64,11 @@ LOCAL_CFLAGS= -I$(top_srcdir)/compat \
		-I$(top_srcdir)/external/liblua/ \
		-I$(top_srcdir)/external/msgpuck \
		-I$(top_srcdir)/libpkg/repo \
+
		-I$(top_builddir)/libpkg/repo \
		-I$(top_srcdir)/libpkg \
+
		-I$(top_builddir)/libpkg \
		-I$(top_srcdir) \
+
		-I$(top_builddir) \
		-Wno-pointer-sign \
		-Wno-missing-field-initializers \
		-Wno-sign-compare \
@@ -123,7 +126,7 @@ LOCAL_LDFLAGS+= -L$(top_builddir)/external/libmachista -lmachista_pic \
SRCS+=		pkg_macho.c
@else
SRCS+=		pkg_elf.c
-
LOCAL_LDFLAGS+=	-Wl,--version-script=$(top_builddir)/libpkg/libpkg.ver \
+
LOCAL_LDFLAGS+=	-Wl,--version-script=$(top_srcdir)/libpkg/libpkg.ver \
@endif

@if libelf-internal
@@ -148,6 +151,8 @@ LOCAL_CFLAGS+= @PKG_LIBLDNS_CFLAGS@
LOCAL_LDFLAGS+=	@PKG_LIBLDNS_LDFLAGS@ @PKG_LIBLDNS_LIBS@
@endif

+
VPATH=	$(top_srcdir)/libpkg
+

include $(MK)/lib.mk

$(OBJS) $(SHOBJS): $(top_builddir)/pkg_config.h
modified libpkg/pkg.h.in
@@ -1051,9 +1051,6 @@ int pkgdb_compact(struct pkgdb *db);
int pkg_add(struct pkgdb *db, const char *path, unsigned flags,
    struct pkg_manifest_key *keys, const char *location);

-
int pkg_add_from_remote(struct pkgdb *db, const char *path, unsigned flags,
-
    struct pkg_manifest_key *keys, const char *location, struct pkg *rp);
-

#define PKG_ADD_UPGRADE			(1U << 0)
/* (1U << 1) removed intentionally */
#define PKG_ADD_AUTOMATIC		(1U << 2)
modified libpkg/pkg_add.c
@@ -973,7 +973,7 @@ cleanup:
}

static int
-
pkg_add_cleanup_old(struct pkgdb *db, struct pkg *old, struct pkg *new, int flags)
+
pkg_add_cleanup_old(struct pkgdb *db, struct pkg *old, struct pkg *new, struct triggers *t, int flags)
{
	struct pkg_file *f;
	int ret = EPKG_OK;
@@ -1013,6 +1013,8 @@ pkg_add_cleanup_old(struct pkgdb *db, struct pkg *old, struct pkg *new, int flag
						backup_library(db, old, f->path);
					}
				}
+

+
				trigger_is_it_a_cleanup(t, f->path);
				pkg_delete_file(old, f, flags & PKG_DELETE_FORCE ? 1 : 0);
			}
		}
@@ -1054,7 +1056,7 @@ pkg_add_triggers(void)
static int
pkg_add_common(struct pkgdb *db, const char *path, unsigned flags,
    struct pkg_manifest_key *keys, const char *reloc, struct pkg *remote,
-
    struct pkg *local)
+
    struct pkg *local, struct triggers *t)
{
	struct archive		*a;
	struct archive_entry	*ae;
@@ -1191,7 +1193,7 @@ pkg_add_common(struct pkgdb *db, const char *path, unsigned flags,
	if (local != NULL) {
		pkg_open_root_fd(local);
		pkg_debug(1, "Cleaning up old version");
-
		if (pkg_add_cleanup_old(db, local, pkg, flags) != EPKG_OK) {
+
		if (pkg_add_cleanup_old(db, local, pkg, t, flags) != EPKG_OK) {
			retcode = EPKG_FATAL;
			goto cleanup;
		}
@@ -1287,26 +1289,26 @@ int
pkg_add(struct pkgdb *db, const char *path, unsigned flags,
    struct pkg_manifest_key *keys, const char *location)
{
-
	return pkg_add_common(db, path, flags, keys, location, NULL, NULL);
+
	return pkg_add_common(db, path, flags, keys, location, NULL, NULL, NULL);
}

int
pkg_add_from_remote(struct pkgdb *db, const char *path, unsigned flags,
-
    struct pkg_manifest_key *keys, const char *location, struct pkg *rp)
+
    struct pkg_manifest_key *keys, const char *location, struct pkg *rp, struct triggers *t)
{
-
	return pkg_add_common(db, path, flags, keys, location, rp, NULL);
+
	return pkg_add_common(db, path, flags, keys, location, rp, NULL, t);
}

int
pkg_add_upgrade(struct pkgdb *db, const char *path, unsigned flags,
    struct pkg_manifest_key *keys, const char *location,
-
    struct pkg *rp, struct pkg *lp)
+
    struct pkg *rp, struct pkg *lp, struct triggers *t)
{
	if (pkgdb_ensure_loaded(db, lp,
	    PKG_LOAD_FILES|PKG_LOAD_SCRIPTS|PKG_LOAD_DIRS|PKG_LOAD_LUA_SCRIPTS) != EPKG_OK)
		return (EPKG_FATAL);

-
	return pkg_add_common(db, path, flags, keys, location, rp, lp);
+
	return pkg_add_common(db, path, flags, keys, location, rp, lp, t);
}

int
modified libpkg/pkg_config.c
@@ -617,7 +617,7 @@ add_repo(const ucl_object_t *obj, struct pkg_repo *r, const char *rname, pkg_ini
			 * forget all stuff parsed
			 */
			pkg_debug(1, "PkgConfig: disabling repo %s", rname);
-
			LL_DELETE(repos, r);
+
			DL_DELETE(repos, r);
			pkg_repo_free(r);
			return;
		}
@@ -790,6 +790,7 @@ walk_repo_obj(const ucl_object_t *obj, const char *file, pkg_init_flags flags)
	ucl_object_iter_t it = NULL;
	struct pkg_repo *r;
	const char *key;
+
	char *yaml;

	while ((cur = ucl_iterate_object(obj, &it, true))) {
		key = ucl_object_key(cur);
@@ -799,9 +800,12 @@ walk_repo_obj(const ucl_object_t *obj, const char *file, pkg_init_flags flags)
			pkg_debug(1, "PkgConfig: overwriting repository %s", key);
		if (cur->type == UCL_OBJECT)
			add_repo(cur, r, key, flags);
-
		else
+
		else {
+
			yaml = ucl_object_emit(cur, UCL_EMIT_YAML);
			pkg_emit_error("Ignoring bad configuration entry in %s: %s",
-
			    file, ucl_object_emit(cur, UCL_EMIT_YAML));
+
			    file, yaml);
+
			free(yaml);
+
		}
	}
}

modified libpkg/pkg_delete.c
@@ -54,7 +54,7 @@
#endif

int
-
pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags)
+
pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags, struct triggers *t)
{
	xstring		*message = NULL;
	int		 ret;
@@ -99,7 +99,7 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags)
		}
	}

-
	if ((ret = pkg_delete_files(pkg, flags & PKG_DELETE_FORCE ? 1 : 0))
+
	if ((ret = pkg_delete_files(pkg, flags & PKG_DELETE_FORCE ? 1 : 0, t))
            != EPKG_OK)
		return (ret);

@@ -324,7 +324,7 @@ pkg_delete_file(struct pkg *pkg, struct pkg_file *file, unsigned force)
}

int
-
pkg_delete_files(struct pkg *pkg, unsigned force)
+
pkg_delete_files(struct pkg *pkg, unsigned force, struct triggers *t)
	/* force: 0 ... be careful and vocal about it.
	 *        1 ... remove files without bothering about checksums.
	 *        2 ... like 1, but remain silent if removal fails.
@@ -345,6 +345,7 @@ pkg_delete_files(struct pkg *pkg, unsigned force)
	while (pkg_files(pkg, &file) == EPKG_OK) {
		append_touched_file(file->path);
		pkg_emit_progress_tick(cur_file++, nfiles);
+
		trigger_is_it_a_cleanup(t, file->path);
		pkg_delete_file(pkg, file, force);
	}

modified libpkg/pkg_jobs.c
@@ -102,6 +102,7 @@ pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db)
	(*j)->pinning = true;
	(*j)->flags = PKG_FLAG_NONE;
	(*j)->conservative = pkg_object_bool(pkg_config_get("CONSERVATIVE_UPGRADE"));
+
	(*j)->triggers.dfd = -1;

	return (EPKG_OK);
}
@@ -187,6 +188,14 @@ pkg_jobs_free(struct pkg_jobs *j)
	pkg_jobs_universe_free(j->universe);
	LL_FREE(j->jobs, free);
	LL_FREE(j->patterns, pkg_jobs_pattern_free);
+
	if (j->triggers.cleanup != NULL) {
+
		tll_free_and_free(*j->triggers.cleanup, trigger_free);
+
		free(j->triggers.cleanup);
+
	}
+
	if (j->triggers.dfd != -1)
+
		close(j->triggers.dfd);
+
	if (j->triggers.schema != NULL)
+
		ucl_object_unref(j->triggers.schema);
	free(j);
}

@@ -813,8 +822,7 @@ pkg_jobs_has_replacement(struct pkg_jobs *j, const char *uid)
}

static int
-
pkg_jobs_try_remote_candidate(struct pkg_jobs *j, const char *cond, const char *pattern,
-
    const char *uid, match_t m)
+
pkg_jobs_try_remote_candidate(struct pkg_jobs *j, const char *cond, const char *pattern, match_t m)
{
	struct pkg *p = NULL;
	struct pkgdb_it *it;
@@ -824,7 +832,6 @@ pkg_jobs_try_remote_candidate(struct pkg_jobs *j, const char *cond, const char *
				PKG_LOAD_ANNOTATIONS|PKG_LOAD_CONFLICTS;
	int rc = EPKG_FATAL;
	xstring *qmsg = NULL;
-
	struct pkg_job_universe_item *unit;

	if ((it = pkgdb_repo_query_cond(j->db, cond, pattern, m, j->reponame)) == NULL)
		return (EPKG_FATAL);
@@ -835,28 +842,6 @@ pkg_jobs_try_remote_candidate(struct pkg_jobs *j, const char *cond, const char *
			pkg_debug(1, "replacement %s is already used", p->uid);
			continue;
		}
-

-
		fprintf(qmsg->fp, "%s has no direct installation candidates, change it to "
-
				"%s? ", uid, p->uid);
-
		fflush(qmsg->fp);
-
		if (pkg_emit_query_yesno(true, qmsg->buf)) {
-
			/* Change the origin of the local package */
-
			pkg_validate(p, j->db);
-
			unit = pkg_jobs_universe_find(j->universe, uid);
-
			if (unit != NULL)
-
				pkg_jobs_universe_change_uid(j->universe, unit,
-
				    p->uid, false);
-
			else
-
				assert(0);
-

-
			rc = EPKG_OK;
-
			pkg_jobs_process_remote_pkg(j, p, NULL, 0);
-
			if (rc == EPKG_OK) {
-
				/* Avoid freeing */
-
				p = NULL;
-
			}
-
			break;
-
		}
	}


@@ -880,7 +865,7 @@ pkg_jobs_guess_upgrade_candidate(struct pkg_jobs *j, const char *pattern)
	/* First of all, try to search a package with the same name */
	pos = strchr(pattern, '/');
	if (pos != NULL && pos[1] != '\0') {
-
		if (pkg_jobs_try_remote_candidate(j, pos + 1, NULL, opattern, MATCH_INTERNAL)
+
		if (pkg_jobs_try_remote_candidate(j, pos + 1, NULL, MATCH_INTERNAL)
						== EPKG_OK)
			return (EPKG_OK);

@@ -903,12 +888,12 @@ pkg_jobs_guess_upgrade_candidate(struct pkg_jobs *j, const char *pattern)
		/* Try exact pattern without numbers */
		cpy = xmalloc(len + 1);
		strlcpy(cpy, pos, len + 1);
-
		if (pkg_jobs_try_remote_candidate(j, cpy, NULL, opattern, MATCH_INTERNAL) != EPKG_OK) {
+
		if (pkg_jobs_try_remote_candidate(j, cpy, NULL, MATCH_INTERNAL) != EPKG_OK) {
			free(cpy);
			cpy = sqlite3_mprintf(" WHERE name REGEXP ('^' || %.*Q || '[0-9.]*$')",
					len, pos);

-
			if (pkg_jobs_try_remote_candidate(j, cpy, opattern, NULL, MATCH_ALL)
+
			if (pkg_jobs_try_remote_candidate(j, cpy, opattern, MATCH_ALL)
					== EPKG_OK)
				rc = EPKG_OK;
			sqlite3_free(cpy);
@@ -2072,9 +2057,9 @@ pkg_jobs_handle_install(struct pkg_solved *ps, struct pkg_jobs *j,
		flags |= PKG_ADD_AUTOMATIC;

	if (old != NULL)
-
		retcode = pkg_add_upgrade(j->db, target, flags, keys, NULL, new, old);
+
		retcode = pkg_add_upgrade(j->db, target, flags, keys, NULL, new, old, &j->triggers);
	else
-
		retcode = pkg_add_from_remote(j->db, target, flags, keys, NULL, new);
+
		retcode = pkg_add_from_remote(j->db, target, flags, keys, NULL, new, &j->triggers);

	return (retcode);
}
@@ -2088,9 +2073,8 @@ pkg_jobs_execute(struct pkg_jobs *j)
	int flags = 0;
	int retcode = EPKG_FATAL;
	pkg_plugin_hook_t pre, post;
-
	trigger_t *cleanup_triggers;

-
	cleanup_triggers = triggers_load(true);
+
//j->triggers.cleanup = triggers_load(true);
	if (j->type == PKG_JOBS_INSTALL) {
		pre = PKG_PLUGIN_HOOK_PRE_INSTALL;
		post = PKG_PLUGIN_HOOK_POST_INSTALL;
@@ -2158,9 +2142,9 @@ pkg_jobs_execute(struct pkg_jobs *j)
			 * in further they will be upgraded correctly.
			 */
			if (j->type == PKG_JOBS_UPGRADE)
-
				retcode = pkg_delete(p, j->db, flags | PKG_DELETE_CONFLICT);
+
				retcode = pkg_delete(p, j->db, flags | PKG_DELETE_CONFLICT, &j->triggers);
			else
-
				retcode = pkg_delete(p, j->db, flags);
+
				retcode = pkg_delete(p, j->db, flags, &j->triggers);
			if (retcode != EPKG_OK)
				goto cleanup;
			break;
@@ -2185,7 +2169,7 @@ pkg_jobs_execute(struct pkg_jobs *j)
	}

	pkg_plugins_hook_run(post, j, j->db);
-
	triggers_execute(cleanup_triggers);
+
	triggers_execute(j->triggers.cleanup);

cleanup:
	pkgdb_release_lock(j->db, PKGDB_LOCK_EXCLUSIVE);
modified libpkg/pkg_solve.c
@@ -299,7 +299,7 @@ pkg_solve_handle_provide (struct pkg_solve_problem *problem,
			if (libfound && strcmp(pkg->arch, orig->arch) != 0) {
				pkg_debug(2, "solver: require %s: package %s-%s(%c) provides wrong ABI %s, "
					"wanted %s", pr->provide, pkg->name, pkg->version,
-
					pkg->type == PKG_INSTALLED ? 'l' : 'r', orig->arch, pkg->arch);
+
					pkg->type == PKG_INSTALLED ? 'l' : 'r', pkg->arch, orig->arch);
				continue;
			}
		}
modified libpkg/private/pkg.h
@@ -1,5 +1,5 @@
/*-
-
 * Copyright (c) 2011-2020 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2011-2022 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
 * Copyright (c) 2013 Matthew Seaman <matthew@FreeBSD.org>
 * Copyright (c) 2013-2017 Vsevolod Stakhov <vsevolod@FreeBSD.org>
@@ -268,6 +268,14 @@ struct trigger {
};
typedef tll(struct trigger *) trigger_t;

+
struct triggers {
+
	ucl_object_t *schema;
+
	int dfd;
+
	trigger_t *cleanup;
+
	trigger_t *post_transaction;
+
	trigger_t *post_install;
+
};
+

struct pkg_create {
	bool overwrite;
	bool expand_manifest;
@@ -605,7 +613,7 @@ int pkg_get_myarch_legacy(char *pkgarch, size_t sz);
 * required by other packages.
 * @return An error code.
 */
-
int pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags);
+
int pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags, struct triggers *);
#define PKG_DELETE_FORCE (1<<0)
#define PKG_DELETE_UPGRADE (1<<1)
#define PKG_DELETE_NOSCRIPT (1<<2)
@@ -679,7 +687,7 @@ pkg_formats packing_format_from_string(const char *str);
const char* packing_format_to_string(pkg_formats format);
bool packing_is_valid_format(const char *str);

-
int pkg_delete_files(struct pkg *pkg, unsigned force);
+
int pkg_delete_files(struct pkg *pkg, unsigned force, struct triggers *t);
int pkg_delete_dirs(struct pkgdb *db, struct pkg *pkg, struct pkg *p);

/* pkgdb commands */
@@ -741,7 +749,10 @@ char *pkg_checksum_generate_fileat(int fd, const char *path,

int pkg_add_upgrade(struct pkgdb *db, const char *path, unsigned flags,
    struct pkg_manifest_key *keys, const char *location,
-
    struct pkg *rp, struct pkg *lp);
+
    struct pkg *rp, struct pkg *lp, struct triggers *);
+
int pkg_add_from_remote(struct pkgdb *db, const char *path, unsigned flags,
+
    struct pkg_manifest_key *keys, const char *location, struct pkg *rp,
+
    struct triggers *);
void pkg_delete_dir(struct pkg *pkg, struct pkg_dir *dir);
void pkg_delete_file(struct pkg *pkg, struct pkg_file *file, unsigned force);
int pkg_open_root_fd(struct pkg *pkg);
@@ -816,6 +827,8 @@ int pkg_archive_extattrs(int, struct archive_entry *);

trigger_t *triggers_load(bool cleanup_only);
int triggers_execute(trigger_t *cleanup_triggers);
+
void trigger_is_it_a_cleanup(struct triggers *t, const char *path);
+
void trigger_free(struct trigger *);
void append_touched_dir(const char *path);
void append_touched_file(const char *path);
bool stringlist_contains(stringlist_t *l, const char *name);
modified libpkg/private/pkg_jobs.h
@@ -113,6 +113,7 @@ struct pkg_jobs {
	bool conservative;
	bool pinning;
	void		*lockedpkgs;
+
	struct triggers triggers;
};

#define PKG_PATTERN_FLAG_FILE (1 << 0)
modified libpkg/repo/binary/Makefile.autosetup
@@ -7,11 +7,15 @@ SRCS= binary.c \
	query.c \
	update.c
LOCAL_CFLAGS=	-I$(top_srcdir)/libpkg \
+
		-I$(top_builddir)/libpkg \
		-I$(top_srcdir)/external/uthash \
		-I$(top_srcdir)/external/sqlite \
		-I$(top_srcdir)/external/libucl/include \
		-I$(top_srcdir)/external/include \
		-I$(top_srcdir)/compat \
		-I$(top_srcdir)/ \
+
		-I$(top_builddir)/
+

+
VPATH=	$(top_srcdir)/libpkg/repo/binary

include $(MK)/static-lib.mk
modified libpkg/triggers.c
@@ -134,6 +134,7 @@ trigger_load(int dfd, const char *name, bool cleanup_only, ucl_object_t *schema)
	fd = openat(dfd, name, O_RDONLY);
	if (fd == -1) {
		pkg_emit_error("Unable to open the tigger: %s", name);
+
		pkg_emit_errno("plop", name);
		return (NULL);
	}

@@ -258,6 +259,30 @@ err:
	return (NULL);
}

+
void
+
trigger_is_it_a_cleanup(struct triggers *t, const char *path)
+
{
+
	const char *trigger_name;
+
	struct trigger *trig;
+

+
	if (t->schema == NULL)
+
		t->schema = trigger_open_schema();
+
	if (strncmp(path, ctx.triggers_path, strlen(ctx.triggers_path)) != 0)
+
		return;
+

+
	trigger_name = path + strlen(ctx.triggers_path);
+

+
	if (t->dfd == -1)
+
		t->dfd = openat(ctx.rootfd, RELATIVE_PATH(ctx.triggers_path), O_DIRECTORY);
+
	trig = trigger_load(t->dfd, RELATIVE_PATH(trigger_name), true, t->schema);
+
	if (trig != NULL) {
+
		if (t->cleanup == NULL)
+
			t->cleanup = xcalloc(1, sizeof(*t->cleanup));
+

+
		tll_push_back(*t->cleanup, trig);
+
	}
+
}
+

trigger_t *
triggers_load(bool cleanup_only)
{
@@ -502,32 +527,9 @@ int
triggers_execute(trigger_t *cleanup_triggers)
{
	trigger_t *triggers;
-
	pkghash *th = NULL;
	int ret = EPKG_OK;

	triggers = triggers_load(false);
-

-
	/*
-
	 * Generate a hash table to ease the lookup later
-
	 */
-
	if (cleanup_triggers != NULL && tll_length(*cleanup_triggers) > 0) {
-
		tll_foreach(*triggers, it)
-
			pkghash_safe_add(th, it->item->name, it->item->name, NULL);
-
	}
-

-
	/*
-
	 * only keep from the cleanup the one that are not anymore in triggers
-
	 */
-
	if (th != NULL) {
-
		if (cleanup_triggers != NULL) {
-
			tll_foreach(*cleanup_triggers, it) {
-
				if (pkghash_get(th, it->item->name) != NULL)
-
					tll_remove_and_free(*cleanup_triggers, it, trigger_free);
-
			}
-
		}
-
		pkghash_destroy(th);
-
	}
-

	pkg_emit_triggers_begin();
	if (cleanup_triggers != NULL) {
		tll_foreach(*cleanup_triggers, it) {
@@ -564,11 +566,6 @@ triggers_execute(trigger_t *cleanup_triggers)
	pkg_emit_triggers_finished();

cleanup:
-
	if (cleanup_triggers != NULL) {
-
		tll_free_and_free(*cleanup_triggers, trigger_free);
-
		free(cleanup_triggers);
-
	}
-

	tll_free_and_free(*triggers, trigger_free);
	free(triggers);

modified libpkg/utils.c
@@ -971,12 +971,12 @@ hidden_tempfile(char *buf, int buflen, const char *path)
	 * keep if suffix of at least 5 if possible
	 */
	if (fname != NULL) {
-
		if (strlen(fname) > (NAME_MAX - 15))
+
		if (strlen(fname) >= (NAME_MAX - 15))
			prefix = ".";
		snprintf(buf, buflen, "%.*s%s%s", (int)(fname - path), path, prefix, fname);
		nbuflen = buflen;
	} else {
-
		if (strlen(path) > NAME_MAX - 15)
+
		if (strlen(path) >= NAME_MAX - 15)
			prefix = ".";
		snprintf(buf, buflen, "%s%s", prefix, path);
		nbuflen = NAME_MAX;
modified scripts/Makefile.autosetup
@@ -20,6 +20,8 @@ PWEEKLY= periodic/400.status-pkg
COMPLETION=	completion/_pkg \
		completion/_pkg.bash

+
VPATH=	$(top_srcdir)/scripts
+

all: $(PDAILY) $(PSECURITY) $(PWEEKLY) $(COMPLETION)

clean:
modified src/Makefile.autosetup
@@ -41,6 +41,7 @@ LOCAL_CFLAGS= -I$(top_srcdir)/compat \
		-I$(top_srcdir)/external/libucl/include \
		-I$(top_srcdir)/external/include \
		-I$(top_builddir)/ \
+
		-I$(top_srcdir)/libpkg \
		-I$(top_builddir)/libpkg \
		-DGITHASH=\"@GITHASH@\" \
		-DHAVE_CONFIG_H
@@ -84,6 +85,7 @@ STATIC_ARG= -static
STATIC_LDFLAGS=	$(LIBPKGFLAT) $(LIBS) $(OTHER_LIBS)
@endif

+
VPATH=	$(top_srcdir)/src

include $(MK)/prog.mk

@@ -106,7 +108,7 @@ $(PROG): $(top_builddir)/libpkg/libpkg_flat.a
install: $(PROG)
	install -d -m 755 $(DESTDIR)$(sbindir)
	install -m 755 pkg $(DESTDIR)$(sbindir)/pkg
-
	install -m 644 pkg.conf.sample $(DESTDIR)$(etcdir)/
+
	install -m 644 $(top_srcdir)/src/pkg.conf.sample $(DESTDIR)$(etcdir)/

clean: clean-pkg-static
clean-pkg-static:
modified src/create.c
@@ -105,8 +105,8 @@ pkg_create_matches(int argc, char **argv, match_t match, struct pkg_create *pc)

		foundone = false;
		while ((ret = pkgdb_it_next(it, &pkg, query_flags)) == EPKG_OK) {
-
			pkg = NULL;
			tll_push_back(pkg_head, pkg);
+
			pkg = NULL;
			foundone = true;
		}
		if (!foundone) {
modified src/main.c
@@ -294,13 +294,15 @@ show_plugin_info(void)
{
	const pkg_object	*conf;
	struct pkg_plugin	*p = NULL;
+
	const char		*dump;

	while (pkg_plugins(&p) == EPKG_OK) {
		conf = pkg_plugin_conf(p);
		printf("Configuration for plugin: %s\n",
		    pkg_plugin_get(p, PKG_PLUGIN_NAME));
-

-
		printf("%s\n", pkg_object_dump(conf));
+
		dump = pkg_object_dump(conf);
+
		printf("%s\n", dump);
+
		free(dump);
	}
}

modified src/version.c
@@ -601,7 +601,7 @@ category_new(char *categorypath, const char *category)
{
	struct category	*cat = NULL;
	xstring		*makecmd;
-
	char		*results, *d, *key;
+
	char		*results, *d;
	char		*argv[5];

	makecmd = xstring_new();
@@ -626,7 +626,7 @@ category_new(char *categorypath, const char *category)

	pkghash_add(categories, cat->name, cat, NULL);
	while ((d = strsep(&results, " \n")) != NULL)
-
		pkghash_safe_add(cat->ports, key, NULL, NULL);
+
		pkghash_safe_add(cat->ports, d, NULL, NULL);

cleanup:
	xstring_free(makecmd);