Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
triggers: rework the cleanup triggers
Baptiste Daroussin committed 3 years ago
commit 399d7a2eaf4b63dc1d06fbd0afe225044ab3b2f3
parent 76cf123
7 files changed +71 -56
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
@@ -948,7 +948,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;
@@ -988,6 +988,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);
			}
		}
@@ -1029,7 +1031,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;
@@ -1166,7 +1168,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;
		}
@@ -1262,26 +1264,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_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);
}

@@ -2048,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);
}
@@ -2065,7 +2074,7 @@ pkg_jobs_execute(struct pkg_jobs *j)
	int retcode = EPKG_FATAL;
	pkg_plugin_hook_t pre, post;

-
	j->triggers.cleanup = 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;
@@ -2133,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;
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);
@@ -813,6 +824,8 @@ int set_attrsat(int fd, const char *path, mode_t perm, uid_t uid, gid_t gid, con

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,11 +113,7 @@ struct pkg_jobs {
	bool conservative;
	bool pinning;
	void		*lockedpkgs;
-
	struct {
-
		trigger_t *cleanup;
-
		trigger_t *post_transaction;
-
		trigger_t *post_install;
-
	} triggers;
+
	struct triggers triggers;
};

#define PKG_PATTERN_FLAG_FILE (1 << 0)
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);