Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Initial cut at pkg_handle & pkg_event mechanism.
Will Andrews committed 14 years ago
commit eab678d7f7e85e818271b780d1accb3ff1646673
parent ce2dd24
5 files changed +74 -2
modified libpkg/Makefile
@@ -16,6 +16,7 @@ SRCS= pkg.c \
		pkg_error.c \
		pkg_exec.c \
		pkg_file.c \
+
		pkg_handle.c \
		pkg_manifest.c \
		pkg_option.c \
		pkg_ports.c \
modified libpkg/packing.c
@@ -91,13 +91,15 @@ packing_append_file(struct packing *pack, const char *filepath, const char *newp
	archive_entry_clear(pack->entry);
	archive_entry_copy_sourcepath(pack->entry, filepath);

-
	if (archive_read_disk_entry_from_file(pack->aread, pack->entry, -1, NULL) !=
-
										  ARCHIVE_OK) {
+
	retcode = archive_read_disk_entry_from_file(pack->aread, pack->entry, -1, NULL);
+
	if (retcode != ARCHIVE_OK) {
+
		pkg_emit_event(PKG_EVENT_ARCHIVE_ERROR, __DECONST(char *, filepath), (void *)pack->aread);
		retcode = pkg_error_set(EPKG_FATAL,
								"archive_read_disk_entry_from_file(%s): %s",
								filepath, archive_error_string(pack->aread));
		goto cleanup;
	}
+
	retcode = EPKG_OK;

	lstat(filepath, &st);
	archive_entry_copy_stat(pack->entry, &st);
modified libpkg/pkg.h
@@ -562,4 +562,42 @@ int pkg_script_pre_upgrade(struct pkg *);
int pkg_script_post_upgrade(struct pkg *);
int pkg_script_pre_deinstall(struct pkg *);
int pkg_script_post_deinstall(struct pkg *);
+

+
/**
+
 * Event type used to report progress or problems.
+
 */
+
typedef enum {
+
	/* informational */
+
	PKG_EVENT_INSTALL_BEGIN = 0,
+

+
	/* errors */
+
	PKG_EVENT_ARCHIVE_ERROR = 65536,
+
} pkg_event_t;
+

+
/**
+
 * Package handle for global state information
+
 */
+

+
/**
+
 * Event callback mechanism.  Events will be reported using this callback,
+
 * providing an event identifier and up to two event-specific pointers.
+
 */
+
typedef int(*pkg_event_cb)(pkg_event_t, void *, void *);
+

+
struct pkg_handle {
+
	pkg_event_cb event_cb;
+
};
+

+
struct pkg_handle *pkg_get_handle(void);
+
pkg_event_cb pkg_handle_get_event_callback(struct pkg_handle *);
+
void pkg_handle_set_event_callback(struct pkg_handle *, pkg_event_cb);
+

+
/* XXX maybe the event callback should also get a pointer to the handle, and
+
 * just drop arg1 altogether..? */
+

+
#define	pkg_emit_event(ev, arg0, arg1) \
+
	__pkg_emit_event(pkg_get_handle(), ev, arg0, arg1)
+

+
void __pkg_emit_event(struct pkg_handle *, pkg_event_t, void *, void *);
+

#endif
modified libpkg/pkg_add.c
@@ -150,6 +150,8 @@ pkg_add(struct pkgdb *db, const char *path, struct pkg **pkg_p)
	if (retcode != EPKG_OK || pkgdb_has_flag(db, PKGDB_FLAG_IN_FLIGHT) == 0)
		goto cleanup;

+
	pkg_emit_event(PKG_EVENT_INSTALL_BEGIN, pkg, NULL);
+

	/*
	 * Execute pre-install scripts
	 */
modified pkg/main.c
@@ -1,10 +1,12 @@
#include <assert.h>
+
#include <archive.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>

+
#include "pkg.h"
#include "create.h"
#include "delete.h"
#include "info.h"
@@ -95,6 +97,29 @@ exec_help(int argc, char **argv)
	return (EX_USAGE);
}

+
/* XXX: use varargs? */
+
static int
+
event_callback(pkg_event_t ev, void *arg0, void *arg1)
+
{
+
	struct pkg *pkg;
+
	const char *str0, *str1;
+

+
	switch(ev) {
+
	case PKG_EVENT_INSTALL_BEGIN:
+
		pkg = (struct pkg *)arg0;
+
		printf("Installing %s\n", pkg_get(pkg, PKG_NAME));
+
		break;
+
	case PKG_EVENT_ARCHIVE_ERROR:
+
		str0 = (const char *)arg0; /* file path */
+
		str1 = archive_error_string(arg1);
+
		fprintf(stderr, "archive error on %s: %s\n", str0, str1);
+
		break;
+
	default:
+
		break;
+
	}
+
	return 0;
+
}
+

int
main(int argc, char **argv)
{
@@ -102,10 +127,14 @@ main(int argc, char **argv)
	struct commands *command = NULL;
	unsigned int ambiguous = 0;
	size_t len;
+
	struct pkg_handle *hdl;

	if (argc < 2)
		usage();

+
	hdl = pkg_get_handle();
+
	pkg_handle_set_event_callback(hdl, event_callback);
+

	len = strlen(argv[1]);
	for (i = 0; i < cmd_len; i++) {
		if (strncmp(argv[1], cmd[i].name, len) == 0) {