Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Refactor script handling, this also allow to order scripts the right way when installing, deleting and upgrading
Baptiste Daroussin committed 13 years ago
commit cb6ad50443572a18417448c57d30b7a4ec1ea796
parent 263a1c5
8 files changed +50 -133
modified libpkg/pkg.c
@@ -74,7 +74,6 @@ pkg_new(struct pkg **pkg, pkg_t type)
	STAILQ_INIT(&(*pkg)->rdeps);
	STAILQ_INIT(&(*pkg)->files);
	STAILQ_INIT(&(*pkg)->dirs);
-
	STAILQ_INIT(&(*pkg)->scripts);
	STAILQ_INIT(&(*pkg)->options);
	STAILQ_INIT(&(*pkg)->users);
	STAILQ_INIT(&(*pkg)->groups);
@@ -98,6 +97,9 @@ pkg_reset(struct pkg *pkg, pkg_t type)
	for (i = 0; i < PKG_NUM_FIELDS; i++)
		sbuf_reset(pkg->fields[i]);

+
	for (i = 0; i < PKG_NUM_SCRIPTS; i++)
+
		sbuf_reset(pkg->scripts[i]);
+

	pkg->flatsize = 0;
	pkg->new_flatsize = 0;
	pkg->new_pkgsize = 0;
@@ -111,7 +113,6 @@ pkg_reset(struct pkg *pkg, pkg_t type)
	pkg_list_free(pkg, PKG_RDEPS);
	pkg_list_free(pkg, PKG_FILES);
	pkg_list_free(pkg, PKG_DIRS);
-
	pkg_list_free(pkg, PKG_SCRIPTS);
	pkg_list_free(pkg, PKG_OPTIONS);
	pkg_list_free(pkg, PKG_USERS);
	pkg_list_free(pkg, PKG_GROUPS);
@@ -130,13 +131,15 @@ pkg_free(struct pkg *pkg)
	for (int i = 0; i < PKG_NUM_FIELDS; i++)
		sbuf_free(pkg->fields[i]);

+
	for (int i = 0; i < PKG_NUM_SCRIPTS; i++)
+
		sbuf_free(pkg->scripts[i]);
+

	pkg_list_free(pkg, PKG_LICENSES);
	pkg_list_free(pkg, PKG_CATEGORIES);
	pkg_list_free(pkg, PKG_DEPS);
	pkg_list_free(pkg, PKG_RDEPS);
	pkg_list_free(pkg, PKG_FILES);
	pkg_list_free(pkg, PKG_DIRS);
-
	pkg_list_free(pkg, PKG_SCRIPTS);
	pkg_list_free(pkg, PKG_OPTIONS);
	pkg_list_free(pkg, PKG_USERS);
	pkg_list_free(pkg, PKG_GROUPS);
@@ -417,14 +420,6 @@ pkg_dirs(struct pkg *pkg, struct pkg_dir **d)
}

int
-
pkg_scripts(struct pkg *pkg, struct pkg_script **s)
-
{
-
	assert(pkg != NULL);
-

-
	PKG_LIST_NEXT(&pkg->scripts, *s);
-
}
-

-
int
pkg_options(struct pkg *pkg, struct pkg_option **o)
{
	assert(pkg != NULL);
@@ -699,17 +694,13 @@ pkg_adddir_attr(struct pkg *pkg, const char *path, const char *uname, const char
}

int
-
pkg_addscript(struct pkg *pkg, const char *data, pkg_script_t type)
+
pkg_addscript(struct pkg *pkg, const char *data, pkg_script type)
{
-
	struct pkg_script *s;
+
	struct sbuf **sbuf;

	assert(pkg != NULL);
-

-
	pkg_script_new(&s);
-
	sbuf_set(&s->data, data);
-
	s->type = type;
-

-
	STAILQ_INSERT_TAIL(&pkg->scripts, s, next);
+
	sbuf = &pkg->scripts[type];
+
	sbuf_set(sbuf, data);

	return (EPKG_OK);
}
@@ -719,7 +710,7 @@ pkg_addscript_file(struct pkg *pkg, const char *path)
{
	char *filename;
	char *data;
-
	pkg_script_t type;
+
	pkg_script type;
	int ret = EPKG_OK;
	off_t sz = 0;

@@ -771,31 +762,22 @@ pkg_addscript_file(struct pkg *pkg, const char *path)
}

int
-
pkg_appendscript(struct pkg *pkg, const char *cmd, pkg_script_t type)
+
pkg_appendscript(struct pkg *pkg, const char *cmd, pkg_script type)
{
-
	struct pkg_script *s = NULL;
+
	struct sbuf **s;

	assert(pkg != NULL);
	assert(cmd != NULL && cmd[0] != '\0');

-
	while (pkg_scripts(pkg, &s) == EPKG_OK) {
-
		if (pkg_script_type(s) == type) {
-
			break;
-
		}
-
	}
+
	s = &pkg->scripts[type];

	if (s != NULL) {
-
		sbuf_cat(s->data, cmd);
-
		sbuf_finish(s->data);
+
		sbuf_cat(*s, cmd);
+
		sbuf_finish(*s);
		return (EPKG_OK);
	}

-
	pkg_script_new(&s);
-
	sbuf_set(&s->data, cmd);
-

-
	s->type = type;
-

-
	STAILQ_INSERT_TAIL(&pkg->scripts, s, next);
+
	sbuf_set(s, cmd);

	return (EPKG_OK);
}
@@ -869,8 +851,6 @@ pkg_list_is_empty(struct pkg *pkg, pkg_list list) {
			return (STAILQ_EMPTY(&pkg->users));
		case PKG_GROUPS:
			return (STAILQ_EMPTY(&pkg->groups));
-
		case PKG_SCRIPTS:
-
			return (STAILQ_EMPTY(&pkg->scripts));
		case PKG_SHLIBS:
			return (STAILQ_EMPTY(&pkg->shlibs));
	}
@@ -888,7 +868,6 @@ pkg_list_free(struct pkg *pkg, pkg_list list) {
	struct pkg_dir *dir;
	struct pkg_user *u;
	struct pkg_group *g;
-
	struct pkg_script *s;
	struct pkg_shlib *sl;

	switch (list) {
@@ -928,10 +907,6 @@ pkg_list_free(struct pkg *pkg, pkg_list list) {
			LIST_FREE(&pkg->groups, g, pkg_group_free);
			pkg->flags &= ~PKG_LOAD_GROUPS;
			break;
-
		case PKG_SCRIPTS:
-
			LIST_FREE(&pkg->scripts, s, pkg_script_free);
-
			pkg->flags &= ~PKG_LOAD_SCRIPTS;
-
			break;
		case PKG_SHLIBS:
			LIST_FREE(&pkg->shlibs, sl, pkg_shlib_free);
			pkg->flags &= ~PKG_LOAD_SHLIBS;
modified libpkg/pkg.h
@@ -47,7 +47,6 @@ struct pkg_dep;
struct pkg_file;
struct pkg_dir;
struct pkg_category;
-
struct pkg_script;
struct pkg_option;
struct pkg_license;
struct pkg_user;
@@ -216,14 +215,13 @@ typedef enum {
	PKG_DIRS,
	PKG_USERS,
	PKG_GROUPS,
-
	PKG_SCRIPTS,
	PKG_SHLIBS
} pkg_list;

/**
 * Determine the type of a pkg_script.
 */
-
typedef enum _pkg_script_t {
+
typedef enum {
	PKG_SCRIPT_PRE_INSTALL = 0,
	PKG_SCRIPT_POST_INSTALL,
	PKG_SCRIPT_PRE_DEINSTALL,
@@ -233,7 +231,7 @@ typedef enum _pkg_script_t {
	PKG_SCRIPT_INSTALL,
	PKG_SCRIPT_DEINSTALL,
	PKG_SCRIPT_UPGRADE
-
} pkg_script_t;
+
} pkg_script;

typedef enum _pkg_jobs_t {
	PKG_JOBS_INSTALL,
@@ -421,13 +419,6 @@ int pkg_users(struct pkg *pkg, struct pkg_user **user);
int pkg_groups(struct pkg *pkg, struct pkg_group **group);

/**
-
 * Iterates over the scripts of the package.
-
 * @param script Must be set to NULL for the first call.
-
 * @return An error code.
-
 */
-
int pkg_scripts(struct pkg *, struct pkg_script **script);
-

-
/**
 * Iterates over the options of the package.
 * @param  option Must be set to NULL for the first call.
 * @return An error code.
@@ -574,14 +565,14 @@ int pkg_addgid(struct pkg *pkg, const char *group, const char *gidstr);
 * @param path The path to the script on disk.
 @ @return An error code.
 */
-
int pkg_addscript(struct pkg *pkg, const char *data, pkg_script_t type);
+
int pkg_addscript(struct pkg *pkg, const char *data, pkg_script type);

/**
 * Helper which call pkg_addscript() with the content of the file and
 * with the correct type.
 */
int pkg_addscript_file(struct pkg *pkg, const char *path);
-
int pkg_appendscript(struct pkg *pkg, const char *cmd, pkg_script_t type);
+
int pkg_appendscript(struct pkg *pkg, const char *cmd, pkg_script type);

/**
 * Allocate a new struct pkg_option and add it to the options of pkg.
@@ -630,8 +621,7 @@ const char *pkg_group_name(struct pkg_group *);
const char *pkg_group_gidstr(struct pkg_group *);

/* pkg_script */
-
const char *pkg_script_data(struct pkg_script *);
-
pkg_script_t pkg_script_type(struct pkg_script *);
+
const char *pkg_script_get(struct pkg *, pkg_script type);

/* pkg_option */
const char *pkg_option_opt(struct pkg_option *);
modified libpkg/pkg_attributes.c
@@ -283,42 +283,13 @@ pkg_group_gidstr(struct pkg_group *g)
	return (g->gidstr);
}

-

-
/*
-
 * Script
-
 */
-

-
int
-
pkg_script_new(struct pkg_script **script)
-
{
-
	if ((*script = calloc(1, sizeof(struct pkg_script))) == NULL) {
-
		pkg_emit_errno("calloc", "pkg_script");
-
		return (EPKG_FATAL);
-
	}
-

-
	return (EPKG_OK);
-
}
-

-
void
-
pkg_script_free(struct pkg_script *script)
-
{
-
	if (script == NULL)
-
		return;
-

-
	sbuf_free(script->data);
-
	free(script);
-
}
-

const char *
-
pkg_script_data(struct pkg_script *s)
+
pkg_script_get(struct pkg *p, pkg_script i)
{
-
	return (sbuf_get(s->data));
-
}
+
	if (p->scripts[i] == NULL)
+
		return (NULL);

-
pkg_script_t
-
pkg_script_type(struct pkg_script *s)
-
{
-
	return (s->type);
+
	return (sbuf_get(p->scripts[i]));
}

/*
modified libpkg/pkg_manifest.c
@@ -285,7 +285,7 @@ parse_mapping(struct pkg *pkg, yaml_node_t *item, yaml_document_t *doc, int attr
	yaml_node_pair_t *pair;
	yaml_node_t *key;
	yaml_node_t *val;
-
	pkg_script_t script_type;
+
	pkg_script script_type;

	pair = item->data.mapping.pairs.start;

@@ -658,7 +658,6 @@ pkg_emit_manifest(struct pkg *pkg, char **dest)
	struct pkg_option *option = NULL;
	struct pkg_file *file = NULL;
	struct pkg_dir *dir = NULL;
-
	struct pkg_script *script = NULL;
	struct pkg_category *category = NULL;
	struct pkg_license *license = NULL;
	struct pkg_user *user = NULL;
@@ -670,6 +669,7 @@ pkg_emit_manifest(struct pkg *pkg, char **dest)
	int seq = -1;
	int map = -1;
	int depkv;
+
	int i;
/*	int users = -1;
	int groups = -1;*/
	const char *script_types = NULL;
@@ -801,11 +801,14 @@ pkg_emit_manifest(struct pkg *pkg, char **dest)
	}

	map = -1;
-
	while (pkg_scripts(pkg, &script) == EPKG_OK) {
+
	for (i = 0; i < PKG_NUM_SCRIPTS; i++) {
		if (map == -1)
			manifest_append_map(map, mapping, "scripts", BLOCK);

-
		switch (pkg_script_type(script)) {
+
		if (pkg_script_get(pkg, i) == NULL)
+
			continue;
+

+
		switch (i) {
			case PKG_SCRIPT_PRE_INSTALL:
				script_types = "pre-install";
				break;
@@ -834,7 +837,7 @@ pkg_emit_manifest(struct pkg *pkg, char **dest)
				script_types = "post-deinstall";
				break;
		}
-
		urlencode(pkg_script_data(script), &tmpsbuf);
+
		urlencode(pkg_script_get(pkg, i), &tmpsbuf);
		manifest_append_kv(map, script_types, sbuf_get(tmpsbuf), LITERAL);
	}
	if (infos != NULL && *infos != '\0') {
modified libpkg/pkgdb.c
@@ -1428,7 +1428,6 @@ pkgdb_load_scripts(struct pkgdb *db, struct pkg *pkg)
	sqlite3_finalize(stmt);

	if (ret != SQLITE_DONE) {
-
		pkg_list_free(pkg, PKG_SCRIPTS);
		ERROR_SQLITE(db->sqlite);
		return (EPKG_FATAL);
	}
@@ -1722,7 +1721,6 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete)
	struct pkg_dep *dep = NULL;
	struct pkg_file *file = NULL;
	struct pkg_dir *dir = NULL;
-
	struct pkg_script *script = NULL;
	struct pkg_option *option = NULL;
	struct pkg_category *category = NULL;
	struct pkg_license *license = NULL;
@@ -1742,6 +1740,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete)
	const char *arch, *maintainer, *www, *prefix;

	int64_t automatic, flatsize, licenselogic;
+
	int i;

	assert(db != NULL);

@@ -1925,9 +1924,11 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete)
	 * Insert scripts
	 */

-
	while (pkg_scripts(pkg, &script) == EPKG_OK) {
-
		if (run_prstmt(SCRIPTS, pkg_script_data(script),
-
		    pkg_script_type(script), package_id) != SQLITE_DONE) {
+
	for (i = 0; i < PKG_NUM_FIELDS; i++) {
+
		if (pkg_script_get(pkg, i) == NULL)
+
			continue;
+
		if (run_prstmt(SCRIPTS, pkg_script_get(pkg, i),
+
		    i, package_id) != SQLITE_DONE) {
			ERROR_SQLITE(s);
			goto cleanup;
		}
modified libpkg/private/pkg.h
@@ -41,6 +41,7 @@
#include "private/utils.h"

#define PKG_NUM_FIELDS 18
+
#define PKG_NUM_SCRIPTS 8

#define EXTRACT_ARCHIVE_FLAGS  (ARCHIVE_EXTRACT_OWNER |ARCHIVE_EXTRACT_PERM | \
		ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL | \
@@ -60,13 +61,13 @@ struct pkg {
	int64_t flatsize;
	int64_t new_flatsize;
	int64_t new_pkgsize;
+
	struct sbuf * scripts[PKG_NUM_SCRIPTS];
	STAILQ_HEAD(categories, pkg_category) categories;
	STAILQ_HEAD(licenses, pkg_license) licenses;
	STAILQ_HEAD(deps, pkg_dep) deps;
	STAILQ_HEAD(rdeps, pkg_dep) rdeps;
	STAILQ_HEAD(files, pkg_file) files;
	STAILQ_HEAD(dirs, pkg_dir) dirs;
-
	STAILQ_HEAD(scripts, pkg_script) scripts;
	STAILQ_HEAD(options, pkg_option) options;
	STAILQ_HEAD(users, pkg_user) users;
	STAILQ_HEAD(groups, pkg_group) groups;
@@ -116,12 +117,6 @@ struct pkg_dir {
	STAILQ_ENTRY(pkg_dir) next;
};

-
struct pkg_script {
-
	struct sbuf *data;
-
	pkg_script_t type;
-
	STAILQ_ENTRY(pkg_script) next;
-
};
-

struct pkg_option {
	struct sbuf *key;
	struct sbuf *value;
@@ -196,7 +191,7 @@ int pkg_repo_fetch(struct pkg *pkg);

int pkg_start_stop_rc_scripts(struct pkg *, pkg_rc_attr attr);

-
int pkg_script_run(struct pkg *, pkg_script_t type);
+
int pkg_script_run(struct pkg *, pkg_script type);

int pkg_add_user_group(struct pkg *pkg);
int pkg_delete_user_group(struct pkgdb *db, struct pkg *pkg);
@@ -220,9 +215,6 @@ void pkg_category_free(struct pkg_category *);
int pkg_license_new(struct pkg_license **);
void pkg_license_free(struct pkg_license *);

-
int pkg_script_new(struct pkg_script **);
-
void pkg_script_free(struct pkg_script *);
-

int pkg_option_new(struct pkg_option **);
void pkg_option_free(struct pkg_option *);

modified libpkg/scripts.c
@@ -32,18 +32,16 @@
#include "private/pkg.h"

int
-
pkg_script_run(struct pkg * const pkg, pkg_script_t type)
+
pkg_script_run(struct pkg * const pkg, pkg_script type)
{
-
	struct pkg_script *script = NULL;
-
	pkg_script_t stype;
	struct sbuf * const script_cmd = sbuf_new_auto();
	size_t i;
	const char *name, *prefix, *version;

	struct {
		const char * const arg;
-
		const pkg_script_t b;
-
		const pkg_script_t a;
+
		const pkg_script b;
+
		const pkg_script a;
	} const map[] = {
		/* a implies b with argument arg */
		{"PRE-INSTALL",    PKG_SCRIPT_INSTALL,   PKG_SCRIPT_PRE_INSTALL},
@@ -64,23 +62,21 @@ pkg_script_run(struct pkg * const pkg, pkg_script_t type)
	assert(i < sizeof(map) / sizeof(map[0]));
	assert(map[i].a == type);

-
	while (pkg_scripts(pkg, &script) == EPKG_OK) {
+
	for (i = 0; i < PKG_NUM_SCRIPTS; i++) {

-
		stype = pkg_script_type(script);
-

-
		if (stype == map[i].a || stype == map[i].b) {
+
		if (i == map[i].a || i == map[i].b) {
			sbuf_reset(script_cmd);
			sbuf_printf(script_cmd, "PKG_PREFIX=%s\nset -- %s-%s",
			    prefix, name, version);

-
			if (stype == map[i].b) {
+
			if (i == map[i].b) {
				/* add arg **/
				sbuf_cat(script_cmd, " ");
				sbuf_cat(script_cmd, map[i].arg);
			}

			sbuf_cat(script_cmd, "\n");
-
			sbuf_cat(script_cmd, pkg_script_data(script));
+
			sbuf_cat(script_cmd, pkg_script_get(pkg, i));
			sbuf_finish(script_cmd);
			system(sbuf_get(script_cmd));
		}
modified pkg/query.c
@@ -45,7 +45,6 @@ static struct query_flags accepted_query_flags[] = {
	{ 'r', "nov",		1, PKG_LOAD_RDEPS },
	{ 'C', "",		1, PKG_LOAD_CATEGORIES },
	{ 'F', "ps",		1, PKG_LOAD_FILES }, 
-
	{ 'S', "",		1, PKG_LOAD_SCRIPTS },
	{ 'O', "kv",		1, PKG_LOAD_OPTIONS },
	{ 'D', "",		1, PKG_LOAD_DIRS },
	{ 'L', "",		1, PKG_LOAD_LICENSES },
@@ -224,9 +223,6 @@ format_str(struct pkg *pkg, struct sbuf *dest, const char *qstr, void *data)
					else if (qstr[0] == 's')
						sbuf_cat(dest, pkg_file_get((struct pkg_file *)data, PKG_FILE_SUM));
					break;
-
				case 'S':
-
					sbuf_cat(dest, pkg_script_data((struct pkg_script *)data));	
-
					break;
				case 'O':
					qstr++;
					if (qstr[0] == 'k')
@@ -303,7 +299,6 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
	struct pkg_license *lic = NULL;
	struct pkg_user *user = NULL;
	struct pkg_group *group = NULL;
-
	struct pkg_script *scripts = NULL;
	struct pkg_shlib *shlib = NULL;

	switch (multiline) {
@@ -361,12 +356,6 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
				printf("%s\n", sbuf_data(output));
			}
			break;
-
		case 'S':
-
			while (pkg_scripts(pkg, &scripts) == EPKG_OK) {
-
				format_str(pkg, output, qstr, scripts);
-
				printf("%s\n", sbuf_data(output));
-
			}
-
			break;
		case 'B':
			while (pkg_shlibs(pkg, &shlib) == EPKG_OK) {
				format_str(pkg, output, qstr, shlib);