Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Merge remote branch 'pkgng/master'
Will Andrews committed 14 years ago
commit 1447b5670abde0c2e199f6074e9af8122fea8dd6
parent eab678d
28 files changed +664 -708
modified libpkg/Makefile
@@ -14,13 +14,13 @@ SRCS= pkg.c \
		pkg_delete.c \
		pkg_elf.c \
		pkg_error.c \
-
		pkg_exec.c \
		pkg_file.c \
		pkg_handle.c \
		pkg_manifest.c \
		pkg_option.c \
		pkg_ports.c \
		pkg_repo.c \
+
		pkg_script.c \
		pkg_create_repo.c \
		pkg_util.c \
		pkg_version.c \
@@ -44,7 +44,7 @@ LDADD+= -L${.CURDIR}/../external/sqlite \
		-lutil \
		-lpthread

-
DEBUG_FLAGS+=  -g
+
DEBUG_FLAGS+=  -g -O0 -DDEBUG
.if defined(PROFILE_BUILD)
DEBUG_FLAGS+=	-pg
.endif
modified libpkg/packing.c
@@ -105,6 +105,7 @@ packing_append_file(struct packing *pack, const char *filepath, const char *newp
	archive_entry_copy_stat(pack->entry, &st);

	if (S_ISLNK(st.st_mode)) {
+
		bzero(linkdest, MAXPATHLEN);
		readlink(filepath, linkdest, MAXPATHLEN);
		archive_entry_set_symlink(pack->entry, linkdest);
	}
modified libpkg/pkg.c
@@ -109,6 +109,25 @@ pkg_flatsize(struct pkg *pkg)
	return (pkg->flatsize);
}

+
int
+
pkg_setautomatic(struct pkg *pkg)
+
{
+
	pkg->automatic = true;
+

+
	return (EPKG_OK);
+
}
+

+
int
+
pkg_isautomatic(struct pkg *pkg)
+
{
+
	if (pkg == NULL) {
+
		ERROR_BAD_ARG("pkg");
+
		return (-1);
+
	}
+

+
	return (pkg->automatic);
+
}
+

int64_t
pkg_new_flatsize(struct pkg *pkg)
{
@@ -216,6 +235,18 @@ pkg_files(struct pkg *pkg)
	return ((struct pkg_file **)pkg->files.data);
}

+
const char **
+
pkg_dirs(struct pkg *pkg)
+
{
+
	if (pkg == NULL) {
+
		ERROR_BAD_ARG("pkg");
+
		return (NULL);
+
	}
+

+
	array_init(&pkg->dirs, 1);
+
	return ((const char **)pkg->dirs.data);
+
}
+

struct pkg_conflict **
pkg_conflicts(struct pkg *pkg)
{
@@ -403,6 +434,7 @@ pkg_new(struct pkg **pkg, pkg_t type)
		(*pkg)->fields[fields[i].id].optional = fields[i].optional;
	}

+
	(*pkg)->automatic = false;
	(*pkg)->type = type;

	return (EPKG_OK);
@@ -427,6 +459,7 @@ pkg_reset(struct pkg *pkg, pkg_t type)
	array_reset(&pkg->rdeps, &pkg_free_void);
	array_reset(&pkg->conflicts, &pkg_conflict_free_void);
	array_reset(&pkg->files, &free);
+
	array_reset(&pkg->dirs, &free);
	array_reset(&pkg->scripts, &pkg_script_free_void);
	array_reset(&pkg->options, &pkg_option_free_void);

@@ -446,6 +479,7 @@ pkg_free(struct pkg *pkg)
	array_free(&pkg->rdeps, &pkg_free_void);
	array_free(&pkg->conflicts, &pkg_conflict_free_void);
	array_free(&pkg->files, &free);
+
	array_free(&pkg->dirs, &free);
	array_free(&pkg->scripts, &pkg_script_free_void);
	array_free(&pkg->options, &pkg_option_free_void);

@@ -526,23 +560,32 @@ pkg_addscript(struct pkg *pkg, const char *path)
	filename[0] = '\0';
	filename++;

-
	if (strcmp(filename, "pkg-pre-install") == 0) {
+
	if (strcmp(filename, "pkg-pre-install") == 0 || 
+
			strcmp(filename, "+PRE_INSTALL") == 0) {
		script->type = PKG_SCRIPT_PRE_INSTALL;
-
	} else if (strcmp(filename, "pkg-post-install") == 0) {
+
	} else if (strcmp(filename, "pkg-post-install") == 0 ||
+
			strcmp(filename, "+POST_INSTALL") == 0) {
		script->type = PKG_SCRIPT_POST_INSTALL;
-
	} else if (strcmp(filename, "pkg-install") == 0) {
+
	} else if (strcmp(filename, "pkg-install") == 0 ||
+
			strcmp(filename, "+INSTALL") == 0) {
		script->type = PKG_SCRIPT_INSTALL;
-
	} else if (strcmp(filename, "pkg-pre-deinstall") == 0) {
+
	} else if (strcmp(filename, "pkg-pre-deinstall") == 0 ||
+
			strcmp(filename, "+PRE_DEINSTALL") == 0) {
		script->type = PKG_SCRIPT_PRE_DEINSTALL;
-
	} else if (strcmp(filename, "pkg-post-deinstall") == 0) {
+
	} else if (strcmp(filename, "pkg-post-deinstall") == 0 ||
+
			strcmp(filename, "+POST_DEINSTALL") == 0) {
		script->type = PKG_SCRIPT_POST_DEINSTALL;
-
	} else if (strcmp(filename, "pkg-deinstall") == 0) {
+
	} else if (strcmp(filename, "pkg-deinstall") == 0 ||
+
			strcmp(filename, "+DEINSTALL") == 0) {
		script->type = PKG_SCRIPT_DEINSTALL;
-
	} else if (strcmp(filename, "pkg-pre-upgrade") == 0) {
+
	} else if (strcmp(filename, "pkg-pre-upgrade") == 0 ||
+
			strcmp(filename, "+PRE_UPGRADE") == 0) {
		script->type = PKG_SCRIPT_PRE_UPGRADE;
-
	} else if (strcmp(filename, "pkg-post-upgrade") == 0) {
+
	} else if (strcmp(filename, "pkg-post-upgrade") == 0 ||
+
			strcmp(filename, "+POST_UPGRADE") == 0) {
		script->type = PKG_SCRIPT_POST_UPGRADE;
-
	} else if (strcmp(filename, "pkg-upgrade") == 0) {
+
	} else if (strcmp(filename, "pkg-upgrade") == 0 ||
+
			strcmp(filename, "+UPGRADE") == 0) {
		script->type = PKG_SCRIPT_UPGRADE;
	} else {
		return (pkg_error_set(EPKG_FATAL, "unknown script"));
@@ -672,6 +715,31 @@ pkg_addfile(struct pkg *pkg, const char *path, const char *sha256)
}

int
+
pkg_adddir(struct pkg *pkg, const char *path)
+
{
+
	char **dirs;
+
	int i;
+

+
	if (pkg == NULL)
+
		return (ERROR_BAD_ARG("pkg"));
+

+
	if (path == NULL || path[0] == '\0')
+
		return (ERROR_BAD_ARG("path"));
+

+
	array_init(&pkg->dirs, 10);
+
	dirs = (char **)pkg->dirs.data;
+
	for (i = 0; dirs[i] != NULL; i++) {
+
		if (strcmp(path, dirs[i]) == 0) {
+
			warnx("Duplicate directory listing: %s, ignoring", path);
+
			return (EPKG_OK);
+
		}
+
	}
+
	array_append(&pkg->dirs, strdup(path));
+

+
	return (EPKG_OK);
+
}
+

+
int
pkg_addconflict(struct pkg *pkg, const char *glob)
{
	struct pkg_conflict *conflict;
modified libpkg/pkg.h
@@ -222,6 +222,11 @@ struct pkg ** pkg_rdeps(struct pkg *);
struct pkg_file ** pkg_files(struct pkg *);

/**
+
 * @return NULL-terminated array of C strings.
+
 */
+
const char ** pkg_dirs(struct pkg *pkg);
+

+
/**
 * @return NULL-terminated array of pkg_conflict.
 */
struct pkg_conflict ** pkg_conflicts(struct pkg *);
@@ -258,6 +263,9 @@ int pkg_set(struct pkg *pkg, pkg_attr attr, const char *value);
 */
int pkg_set_from_file(struct pkg *pkg, pkg_attr attr, const char *file);

+
int pkg_setautomatic(struct pkg *pkg);
+
int pkg_isautomatic(struct pkg *pkg);
+

/**
 * Set the uncompressed size of the package.
 * @return An error code.
@@ -293,6 +301,12 @@ int pkg_adddep(struct pkg *pkg, const char *name, const char *origin, const
int pkg_addfile(struct pkg *pkg, const char *path, const char *sha256);

/**
+
 * Add a path
+
 * @return An error code.
+
 */
+
int pkg_adddir(struct pkg *pkg, const char *path);
+

+
/**
 * Allocate a new struct pkg_conflict and add it to the conflicts of pkg.
 * @return An error code.
 */
@@ -319,6 +333,7 @@ int pkg_addoption(struct pkg *pkg, const char *name, const char *value);
 * @return An error code.
 */
int pkg_parse_manifest(struct pkg *pkg, char *buf);
+
int pkg_load_manifest_file(struct pkg *pkg, const char *fpath);

/**
 * Emit a manifest according to the attributes of pkg.
@@ -341,7 +356,7 @@ void pkg_conflict_reset(struct pkg_conflict *);
void pkg_conflict_free(struct pkg_conflict *);
const char * pkg_conflict_glob(struct pkg_conflict *);

-
/* pkg_exec */
+
/* pkg_script */
int pkg_script_new(struct pkg_script **);
void pkg_script_reset(struct pkg_script *);
void pkg_script_free(struct pkg_script *);
@@ -370,7 +385,7 @@ int pkg_finish_repo(char *patj, pem_password_cb *cb, char *rsa_key_path);
 * The db must be free'ed with pkgdb_close().
 * @return An error code.
 */
-
int pkgdb_open(struct pkgdb **db, pkgdb_t remote, int mode);
+
int pkgdb_open(struct pkgdb **db, pkgdb_t type);

/**
 * Close and free the struct pkgdb.
@@ -430,6 +445,7 @@ struct pkgdb_it * pkgdb_query_which(struct pkgdb *db, const char *path);
#define PKG_LOAD_SCRIPTS (1<<5)
#define PKG_LOAD_OPTIONS (1<<6)
#define PKG_LOAD_MTREE (1<<7)
+
#define PKG_LOAD_DIRS (1<<8)

/**
 * Get the next pkg.
@@ -449,6 +465,7 @@ int pkgdb_loaddeps(struct pkgdb *db, struct pkg *pkg);
int pkgdb_loadrdeps(struct pkgdb *db, struct pkg *pkg);
int pkgdb_loadconflicts(struct pkgdb *db, struct pkg *pkg);
int pkgdb_loadfiles(struct pkgdb *db, struct pkg *pkg);
+
int pkgdb_loaddirs(struct pkgdb *db, struct pkg *pkg);
int pkgdb_loadscripts(struct pkgdb *db, struct pkg *pkg);
int pkgdb_loadoptions(struct pkgdb *db, struct pkg *pkg);
int pkgdb_loadmtree(struct pkgdb *db, struct pkg *pkg);
@@ -498,13 +515,6 @@ int pkg_delete(struct pkg *pkg, struct pkgdb *db, int force);
int pkg_repo_fetch(struct pkg *pkg, void *data, fetch_cb cb);

/**
-
 * These functions are helpers for specific parts of pkg_delete().
-
 * Generally speaking, external consumers should not use these.
-
 * @return An error code on failure, or EPKG_OK.
-
 */
-
int pkg_delete_files(struct pkg *pkg, int force);
-

-
/**
 * Get the value of a configuration key
 */
const char * pkg_config(const char *key);
@@ -528,10 +538,6 @@ int pkg_fetch_buffer(const char *url, char **buf, void *data, fetch_cb cb);

/* glue to deal with ports */
int ports_parse_plist(struct pkg *, char *);
-
int ports_parse_depends(struct pkg *, char *);
-
int ports_parse_conflicts(struct pkg *, char *);
-
int ports_parse_scripts(struct pkg *, char *);
-
int ports_parse_options(struct pkg *, char *);

/**
 * Return the last error number
modified libpkg/pkg_add.c
@@ -148,7 +148,7 @@ pkg_add(struct pkgdb *db, const char *path, struct pkg **pkg_p)
	 * problems that could be caught here. */
	retcode = pkgdb_register_pkg(db, pkg);
	if (retcode != EPKG_OK || pkgdb_has_flag(db, PKGDB_FLAG_IN_FLIGHT) == 0)
-
		goto cleanup;
+
		goto cleanup_reg;

	pkg_emit_event(PKG_EVENT_INSTALL_BEGIN, pkg, NULL);

@@ -162,8 +162,9 @@ pkg_add(struct pkgdb *db, const char *path, struct pkg **pkg_p)
	 */
	if (extract == true && (retcode = do_extract(a, ae)) != EPKG_OK) {
		/* If the add failed, clean up */
-
		(void) pkg_delete_files(pkg, 1);
-
		goto cleanup;
+
		pkg_delete_files(pkg, 1);
+
		pkg_delete_dirs(pkg, 1);
+
		goto cleanup_reg;
	}

	/*
@@ -171,10 +172,10 @@ pkg_add(struct pkgdb *db, const char *path, struct pkg **pkg_p)
	 */
	pkg_script_post_install(pkg);

-
	cleanup:
-

+
	cleanup_reg:
	pkgdb_register_finale(db, retcode);

+
	cleanup:
	if (a != NULL)
		archive_read_finish(a);

modified libpkg/pkg_create.c
@@ -23,17 +23,19 @@ pkg_create_from_dir(struct pkg *pkg, const char *root, struct packing *pkg_archi
	struct pkg_file **files;
	struct pkg_script **scripts;
	char *m;
+
	const char *mtree;
	int i;
	const char *scriptname = NULL;

	pkg_emit_manifest(pkg, &m);
-

	packing_append_buffer(pkg_archive, m, "+MANIFEST", strlen(m));
-

	free(m);

	packing_append_buffer(pkg_archive, pkg_get(pkg, PKG_DESC), "+DESC", strlen(pkg_get(pkg, PKG_DESC)));
-
	packing_append_buffer(pkg_archive, pkg_get(pkg, PKG_MTREE), "+MTREE_DIRS", strlen(pkg_get(pkg, PKG_MTREE)));
+

+
	mtree = pkg_get(pkg, PKG_MTREE);
+
	if (mtree != NULL)
+
		packing_append_buffer(pkg_archive, mtree, "+MTREE_DIRS", strlen(mtree));

	if ((scripts = pkg_scripts(pkg)) != NULL) {
		for (i = 0; scripts[i] != NULL; i++) {
@@ -66,7 +68,8 @@ pkg_create_from_dir(struct pkg *pkg, const char *root, struct packing *pkg_archi
					scriptname = "+UPGRADE";
					break;
			}
-
			packing_append_buffer(pkg_archive, pkg_script_data(scripts[i]), scriptname, strlen(pkg_script_data(scripts[i])));
+
			packing_append_buffer(pkg_archive, pkg_script_data(scripts[i]),
+
								  scriptname, strlen(pkg_script_data(scripts[i])));
		}
	}

@@ -120,7 +123,6 @@ pkg_create_fakeroot(const char *outdir, pkg_formats format, const char *rootdir,
	struct packing *pkg_archive = NULL;
	char *manifest = NULL, *manifest_path = NULL;
	int ret = ENOMEM;
-
	off_t sz;

	/* Load the manifest from the metadata directory */
	if (asprintf(&manifest_path, "%s/+MANIFEST", metadatadir) == -1)
@@ -130,11 +132,7 @@ pkg_create_fakeroot(const char *outdir, pkg_formats format, const char *rootdir,
	if (pkg == NULL)
		goto cleanup;

-
	ret = file_to_buffer(manifest_path, &manifest, &sz);
-
	if (ret != EPKG_OK)
-
		goto cleanup;
-

-
	ret = pkg_parse_manifest(pkg, manifest);
+
	ret = pkg_load_manifest_file(pkg, manifest_path);

	/* Create the archive */
	pkg_archive = pkg_create_archive(outdir, pkg, format, 0);
@@ -165,7 +163,7 @@ pkg_create_installed(const char *outdir, pkg_formats format, const char *rootdir
{
	struct packing *pkg_archive;
	int required_flags = PKG_LOAD_DEPS | PKG_LOAD_CONFLICTS | PKG_LOAD_FILES |
-
						 PKG_LOAD_SCRIPTS | PKG_LOAD_OPTIONS |
+
						 PKG_LOAD_DIRS | PKG_LOAD_SCRIPTS | PKG_LOAD_OPTIONS |
						 PKG_LOAD_MTREE;

	if (pkg->type != PKG_INSTALLED)
modified libpkg/pkg_delete.c
@@ -8,6 +8,7 @@

#include "pkg.h"
#include "pkg_error.h"
+
#include "pkg_private.h"
#include "pkg_util.h"

int
@@ -30,6 +31,8 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, int force)
		return (ret);
	if ((ret = pkgdb_loadfiles(db, pkg)) != EPKG_OK)
		return (ret);
+
	if ((ret = pkgdb_loaddirs(db, pkg)) != EPKG_OK)
+
		return (ret);
	if ((ret = pkgdb_loadscripts(db, pkg)) != EPKG_OK)
		return (ret);
	if ((ret = pkgdb_loadmtree(db, pkg)) != EPKG_OK)
@@ -39,10 +42,10 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, int force)

	if (rdeps[0] != NULL) {
		rdep_msg = sbuf_new_auto();
-
		sbuf_printf(rdep_msg, "this package is required by other packages:");
+
		sbuf_printf(rdep_msg, "%s-%s is required by other packages:", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
		for (i = 0;rdeps[i] != NULL; i++) {
			sbuf_cat(rdep_msg, " ");
-
			sbuf_cat(rdep_msg, pkg_get(rdeps[i], PKG_NAME));
+
			sbuf_printf(rdep_msg, "%s-%s", pkg_get(rdeps[i], PKG_NAME), pkg_get(rdeps[i], PKG_VERSION));
		}
		if (!force) {
			sbuf_finish(rdep_msg);
@@ -65,64 +68,57 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, int force)
	if ((ret = pkg_script_post_deinstall(pkg)) != EPKG_OK)
		return (ret);

+
	if ((ret = pkg_delete_dirs(pkg, force)) != EPKG_OK)
+
		return (ret);
+

	return (pkgdb_unregister_pkg(db, pkg_get(pkg, PKG_ORIGIN)));
}

int
pkg_delete_files(struct pkg *pkg, int force)
{
-
	int do_remove, i;
-
	int ret = EPKG_OK;
+
	int i;
	struct pkg_file **files;
	char sha256[65];
	const char *path;
-
	struct stat st;

	files = pkg_files(pkg);
	for (i = 0; files[i] != NULL; i++) {
		path = pkg_file_path(files[i]);
-
		/* check to make sure the file exists */
-
		if (stat(path, &st) == -1) {
-
			/* don't print ENOENT errors on force */
-
			if (!force && errno != ENOENT)
-
				warn("%s", path);
-
			continue;
-
		}
-
		/* Directories */
-

-
		if (path[strlen(path) - 1] == '/') {
-
			/*
-
			 * currently do not warn on this because multiple
-
			 * packages can own the same directory
-
			 */
-
			rmdir(path);
-
			continue;
-
		}

		/* Regular files and links */
		/* check sha256 */
-
		do_remove = 1;
-
		if (pkg_file_sha256(files[i])[0] != '\0') {
+
		if (!force && pkg_file_sha256(files[i])[0] != '\0') {
			if (sha256_file(path, sha256) == -1) {
				warnx("sha256 calculation failed for '%s'",
-
				    pkg_file_path(files[i]));
-
			} else {
-
				if (strcmp(sha256, pkg_file_sha256(files[i])) != 0) {
-
					if (force)
-
						warnx("%s fails original SHA256 checksum", path);
-
					else {
-
						do_remove = 0;
-
						warnx("%s fails original SHA256 checksum, not removing", path);
-
					}
-
				}
+
					  path);
+
			} else if (strcmp(sha256, pkg_file_sha256(files[i])) != 0) {
+
				warnx("%s fails original SHA256 checksum, not removing", path);
+
				continue;
			}
		}

-
		if (do_remove && unlink(pkg_file_path(files[i])) == -1) {
-
			warn("unlink(%s)", pkg_file_path(files[i]));
+
		if (unlink(path) == -1) {
+
			warn("unlink(%s)", path);
			continue;
		}
	}

-
	return (ret);
+
	return (EPKG_OK);
+
}
+

+
int
+
pkg_delete_dirs(struct pkg *pkg, int force)
+
{
+
	int i;
+
	const char **dirs;
+

+
	dirs = pkg_dirs(pkg);
+
	for (i = 0; dirs[i] != NULL; i++) {
+
		if (rmdir(dirs[i]) == -1 && errno != ENOTEMPTY && force != 1) {
+
			warn("rmdir(%s)", dirs[i]);
+
		}
+
	}
+

+
	return (EPKG_OK);
}
deleted libpkg/pkg_exec.c
@@ -1,49 +0,0 @@
-
#include <stdlib.h>
-
#include <err.h>
-

-
#include "pkg.h"
-
#include "pkg_private.h"
-

-
const char *
-
pkg_script_data(struct pkg_script *s)
-
{
-
	return (sbuf_get(s->data));
-
}
-

-
pkg_script_t
-
pkg_script_type(struct pkg_script *s)
-
{
-
	return (s->type);
-
}
-

-
int
-
pkg_script_new(struct pkg_script **script)
-
{
-
	if ((*script = calloc(1, sizeof(struct pkg_script))) == NULL)
-
		err(EXIT_FAILURE, "calloc()");
-

-
	return (0);
-
}
-

-
void
-
pkg_script_reset(struct pkg_script *script)
-
{
-
	sbuf_reset(script->data);
-
}
-

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

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

-
void
-
pkg_script_free_void(void *s)
-
{
-
	if (s != NULL)
-
		pkg_script_free((struct pkg_script *)s);
-
}
modified libpkg/pkg_manifest.c
@@ -29,6 +29,7 @@ static int m_parse_conflict(struct pkg *pkg, char *buf);
static int m_parse_maintainer(struct pkg *pkg, char *buf);
static int m_parse_prefix(struct pkg *pkg, char *buf);
static int m_parse_file(struct pkg *pkg, char *buf);
+
static int m_parse_dir(struct pkg *pkg, char *buf);
static int m_parse_set_string(struct pkg *pkg, char *buf, pkg_attr attr);

#define MANIFEST_FORMAT_KEY "@pkg_format_version"
@@ -51,6 +52,7 @@ static struct manifest_key {
	{ "@maintainer", m_parse_maintainer},
	{ "@prefix", m_parse_prefix},
	{ "@file", m_parse_file},
+
	{ "@dir", m_parse_dir},
};

#define manifest_key_len (int)(sizeof(manifest_key)/sizeof(manifest_key[0]))
@@ -230,6 +232,36 @@ m_parse_file(struct pkg *pkg, char *buf)
	return (EPKG_OK);
}

+
static int
+
m_parse_dir(struct pkg *pkg, char *buf)
+
{
+
	while (isspace(*buf))
+
		buf++;
+

+
	if (*buf == '\0')
+
		return (EPKG_FATAL);
+

+
	pkg_adddir(pkg, buf);
+
	return (EPKG_OK);
+
}
+

+
int
+
pkg_load_manifest_file(struct pkg *pkg, const char *fpath)
+
{
+
	char *manifest = NULL;
+
	off_t sz;
+
	int ret = EPKG_OK;
+

+
	if ((ret = file_to_buffer(fpath, &manifest, &sz)) != EPKG_OK)
+
		return (ret);
+

+
	ret = pkg_parse_manifest(pkg, manifest);
+
	if (ret != EPKG_OK && manifest != NULL)
+
			free(manifest);
+

+
	return (ret);
+
}
+

int
pkg_parse_manifest(struct pkg *pkg, char *buf)
{
@@ -286,6 +318,7 @@ pkg_emit_manifest(struct pkg *pkg, char **dest)
	struct pkg_conflict **conflicts;
	struct pkg_option **options;
	struct pkg_file **files;
+
	const char **dirs;
	int i;
	int len = 0;

@@ -314,35 +347,34 @@ pkg_emit_manifest(struct pkg *pkg, char **dest)
			pkg_flatsize(pkg)
			);

-
	if ((deps = pkg_deps(pkg)) != NULL) {
-
		for (i = 0; deps[i] != NULL; i++) {
-
			sbuf_printf(manifest, "@dep %s %s %s\n",
+
	deps = pkg_deps(pkg);
+
	for (i = 0; deps[i] != NULL; i++) {
+
		sbuf_printf(manifest, "@dep %s %s %s\n",
					pkg_get(deps[i], PKG_NAME),
					pkg_get(deps[i], PKG_ORIGIN),
					pkg_get(deps[i], PKG_VERSION));
-
		}
	}

-
	if ((conflicts = pkg_conflicts(pkg)) != NULL) {
-
		for (i = 0; conflicts[i] != NULL; i++) {
-
			sbuf_printf(manifest, "@conflict %s\n", pkg_conflict_glob(conflicts[i]));
-
		}
+
	conflicts = pkg_conflicts(pkg);
+
	for (i = 0; conflicts[i] != NULL; i++) {
+
		sbuf_printf(manifest, "@conflict %s\n", pkg_conflict_glob(conflicts[i]));
	}

-
	if ((options = pkg_options(pkg)) != NULL)  {
-
		for (i = 0; options[i] != NULL; i++) {
-
			sbuf_printf(manifest, "@option %s %s\n",
-
					pkg_option_opt(options[i]),
+
	options = pkg_options(pkg);
+
	for (i = 0; options[i] != NULL; i++) {
+
		sbuf_printf(manifest, "@option %s %s\n", pkg_option_opt(options[i]),
					pkg_option_value(options[i]));
-
					
-
		}
	}

-
	if ((files = pkg_files(pkg)) != NULL) {
-
		for (i = 0; files[i] != NULL; i++) {
-
			sbuf_printf(manifest, "@file %s %s\n", pkg_file_path(files[i]),
-
						pkg_file_sha256(files[i]));
-
		}
+
	files = pkg_files(pkg);
+
	for (i = 0; files[i] != NULL; i++) {
+
		sbuf_printf(manifest, "@file %s %s\n", pkg_file_path(files[i]),
+
					pkg_file_sha256(files[i]));
+
	}
+

+
	dirs = pkg_dirs(pkg);
+
	for (i = 0; dirs[i] != NULL; i++) {
+
		sbuf_printf(manifest, "@dir %s\n", dirs[i]);
	}

	sbuf_finish(manifest);
modified libpkg/pkg_ports.c
@@ -14,8 +14,10 @@ int
ports_parse_plist(struct pkg *pkg, char *plist)
{
	char *plist_p, *buf, *p, *plist_buf;
+
	char comment[2];
	int nbel, i;
	size_t next;
+
	size_t len;
	char sha256[65];
	char path[MAXPATHLEN];
	char *last_plist_file = NULL;
@@ -24,6 +26,7 @@ ports_parse_plist(struct pkg *pkg, char *plist)
	struct stat st;
	int ret = EPKG_OK;
	off_t sz = 0;
+
	const char *slash;
	int64_t flatsize = 0;
	struct sbuf *exec_scripts = sbuf_new_auto();
	struct sbuf *unexec_scripts = sbuf_new_auto();
@@ -41,6 +44,7 @@ ports_parse_plist(struct pkg *pkg, char *plist)
		return (ret);

	prefix = pkg_get(pkg, PKG_PREFIX);
+
	slash = prefix[strlen(prefix) - 1] == '/' ? "" : "/";

	nbel = split_chr(plist_buf, '\n');

@@ -58,9 +62,11 @@ ports_parse_plist(struct pkg *pkg, char *plist)
					prefix = pkg_get(pkg, PKG_PREFIX);
				else
					prefix = buf;
-
			} else if (STARTS_WITH(plist_p, "@comment ")){
+
				slash = prefix[strlen(prefix) - 1] == '/' ? "" : "/";
+
			} else if (STARTS_WITH(plist_p, "@comment ")) {
				/* DO NOTHING: ignore the comments */
-
			} else if (STARTS_WITH(plist_p, "@unexec ") || STARTS_WITH(plist_p, "@exec ")) {
+
			} else if (STARTS_WITH(plist_p, "@unexec ") ||
+
					   STARTS_WITH(plist_p, "@exec ")) {
				buf = plist_p;

				while (!isspace(buf[0]))
@@ -72,10 +78,39 @@ ports_parse_plist(struct pkg *pkg, char *plist)
				if (format_exec_cmd(&cmd, buf, prefix, last_plist_file) < 0)
					continue;

-
				if (plist_p[1] == 'u')
-
					sbuf_printf(unexec_scripts, "%s\n", cmd);
-
				else
+
				if (plist_p[1] == 'u') {
+
					comment[0] = '\0';
+
					/* workaround to detect the @dirrmtry */
+
					if (STARTS_WITH(cmd, "rmdir ")) {
+
						comment[0] = '#';
+
						comment[1] = '\0';
+
						cmd += 6;
+
					} else if (STARTS_WITH(cmd, "/bin/rmdir ")) {
+
						comment[0] = '#';
+
						comment[1] = '\0';
+
						cmd += 11;
+
					}
+
					if (sbuf_len(unexec_scripts) == 0)
+
						sbuf_cat(unexec_scripts, "#@unexec\n"); /* to be able to regenerate the @unexec in pkg2legacy */
+
					sbuf_printf(unexec_scripts, "%s%s\n",comment, cmd);
+

+
					/* workaround to detect the @dirrmtry */
+
					if (comment[0] == '#') {
+
						buf =strchr(cmd, ' ');
+
						buf[0] = '\0';
+
						while (cmd[0] == '\"')
+
							cmd++;
+

+
						while (cmd[strlen(cmd) -1] == '\"')
+
							cmd[strlen(cmd) -1] = '\0';
+

+
						ret += pkg_adddir(pkg, cmd);
+
					}
+
				} else {
+
					if (sbuf_len(exec_scripts) == 0)
+
						sbuf_cat(exec_scripts, "#@exec\n"); /* to be able to regenerate the @exec in pkg2legacy */
					sbuf_printf(exec_scripts, "%s\n", cmd);
+
				}

				free(cmd);

@@ -90,39 +125,49 @@ ports_parse_plist(struct pkg *pkg, char *plist)
				while (isspace(buf[0]))
					buf++;

-
				if (prefix[strlen(prefix) -1 ] == '/')
-
					snprintf(path, MAXPATHLEN, "%s%s/", prefix, buf);
-
				else
-
					snprintf(path, MAXPATHLEN, "%s/%s/", prefix, buf);
+
				len = strlen(buf);

-
				if (lstat(path, &st) >= 0)
-
					flatsize += st.st_size;
+
				while (isspace(buf[len - 1])) {
+
					buf[len - 1] = '\0';
+
					len--;
+
				}

-
				ret += pkg_addfile(pkg, path, NULL);
+
				snprintf(path, MAXPATHLEN, "%s%s%s/", prefix, slash, buf);
+

+
				ret += pkg_adddir(pkg, path);

			} else {
				warnx("%s is deprecated, ignoring", plist_p);
			}
-
		} else if (strlen(plist_p) > 0){
+
		} else if ((len = strlen(plist_p)) > 0){
			buf = plist_p;
			last_plist_file = buf;
+
			sha256[0] = '\0';

-
			if (prefix[strlen(prefix) - 1] == '/')
-
				snprintf(path, MAXPATHLEN, "%s%s", prefix, buf);
-
			else
-
				snprintf(path, MAXPATHLEN, "%s/%s", prefix, buf);
+
			/* remove spaces at the begining and at the end */
+
			while (isspace(buf[0]))
+
				buf++;

-
			if (lstat(path, &st) >= 0) {
-
				if (!S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode) && sha256_file(path, sha256) == 0)
-
					p = sha256;
+
			while (isspace(buf[len -  1])) {
+
				buf[len - 1] = '\0';
+
				len--;
+
			}
+

+
			snprintf(path, MAXPATHLEN, "%s%s%s", prefix, slash, buf);

-
				flatsize += st.st_size;
+
			if (lstat(path, &st) == 0) {
+
				if (S_ISLNK(st.st_mode)) {
+
					p = NULL;
+
				} else {
+
					flatsize += st.st_size;
+
					if (sha256_file(path, sha256) != 0)
+
						pkg_error_warn("can not compute sha256");
+
					p = sha256;
+
				}
+
				ret += pkg_addfile(pkg, path, p);
			} else {
				warn("lstat(%s)", path);
-
				p = NULL;
			}
-

-
			ret += pkg_addfile(pkg, path, p);
		}

		if (i != nbel) {
@@ -148,126 +193,3 @@ ports_parse_plist(struct pkg *pkg, char *plist)

	return (ret);
}
-

-
int
-
ports_parse_depends(struct pkg *pkg, char *depends)
-
{
-
	int nbel, i;
-
	char *dep_p, *buf, *v, *name;;
-
	size_t next;
-

-
	if (depends == NULL)
-
		return (-1);
-

-
	if (depends[0] == '\0')
-
		return (0);
-

-
	nbel = split_chr(depends, '\n');
-

-
	buf = NULL;
-
	v = NULL;
-

-
	next = strlen(depends);
-
	dep_p = depends;
-

-
	for (i = 0; i <= nbel; i++) {
-

-
		buf = dep_p;
-
		split_chr(dep_p, ':');
-

-
		if ((v = strrchr(dep_p, '-')) == NULL)
-
			return (pkg_error_set(EPKG_FATAL, "bad depends format"));
-
		v[0] = '\0';
-
		v++;
-
		
-
		name = buf;
-
		buf += strlen(buf) + 1;
-
		buf += strlen(buf) + 1;
-

-
		pkg_adddep(pkg, name, buf, v);
-

-
		if (i != nbel) {
-
			dep_p += next + 1;
-
			next = strlen(dep_p);
-
		}
-
	}
-

-
	return (0);
-
}
-

-
int
-
ports_parse_conflicts(struct pkg *pkg, char *conflicts)
-
{
-
	int nbel, i;
-
	char *conflict_p;
-
	size_t next;
-

-
	if (conflicts == NULL)
-
		return (-1);
-

-
	nbel = split_chr(conflicts, ' ');
-
	conflict_p = conflicts;
-

-
	next = strlen(conflict_p);
-
	for (i = 0; i <= nbel; i++) {
-
		pkg_addconflict(pkg, conflict_p);
-
		conflict_p += next + 1;
-
		next = strlen(conflict_p);
-
	}
-

-
	return (0);
-
}
-

-
int
-
ports_parse_scripts(struct pkg *pkg, char *scripts)
-
{
-
	int nbel, i;
-
	char *script_p;
-
	size_t next;
-

-
	if (scripts == NULL)
-
		return (-1);
-

-
	nbel = split_chr(scripts, ' ');
-
	script_p = scripts;
-

-
	next = strlen(script_p);
-
	for (i = 0; i <= nbel; i++) {
-
		pkg_addscript(pkg, script_p);
-

-
		script_p += next + 1;
-
		next = strlen(script_p);
-
	}
-

-
	return (0);
-
}
-

-
int
-
ports_parse_options(struct pkg *pkg, char *options)
-
{
-
	int nbel, i;
-
	char *option_p;
-
	size_t next;
-
	char *value;
-

-
	if (options == NULL)
-
		return (-1);
-

-
	nbel = split_chr(options, ' ');
-
	option_p = options;
-

-
	next = strlen(option_p);
-
	for (i = 0; i <= nbel; i++) {
-

-
		if ((value = strrchr(option_p, '=')) != NULL) {
-
			value[0] = '\0';
-
			value++;
-
			pkg_addoption(pkg, option_p, value);
-
		}
-

-
		option_p += next + 1;
-
		next = strlen(option_p);
-
	}
-

-
	return (0);
-
}
modified libpkg/pkg_private.h
@@ -4,6 +4,7 @@
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sbuf.h>
+
#include <stdbool.h>

#include <archive.h>

@@ -21,6 +22,7 @@ struct pkg {
		int type; /* for which pkg_t this field is defined */
		unsigned int optional :1;
	} fields[PKG_NUM_FIELDS];
+
	bool automatic;
	int64_t flatsize;
	int64_t new_flatsize;
	int64_t new_pkgsize;
@@ -28,6 +30,7 @@ struct pkg {
	struct array rdeps;
	struct array conflicts;
	struct array files;
+
	struct array dirs;
	struct array scripts;
	struct array options;
	int flags;
@@ -70,4 +73,8 @@ int packing_finish(struct packing *pack);
pkg_formats packing_format_from_string(const char *str);

void pkg_free_void(void *);
+

+
int pkg_delete_files(struct pkg *pkg, int force);
+
int pkg_delete_dirs(struct pkg *pkg, int force);
+

#endif
added libpkg/pkg_script.c
@@ -0,0 +1,49 @@
+
#include <stdlib.h>
+
#include <err.h>
+

+
#include "pkg.h"
+
#include "pkg_private.h"
+

+
const char *
+
pkg_script_data(struct pkg_script *s)
+
{
+
	return (sbuf_get(s->data));
+
}
+

+
pkg_script_t
+
pkg_script_type(struct pkg_script *s)
+
{
+
	return (s->type);
+
}
+

+
int
+
pkg_script_new(struct pkg_script **script)
+
{
+
	if ((*script = calloc(1, sizeof(struct pkg_script))) == NULL)
+
		err(EXIT_FAILURE, "calloc()");
+

+
	return (0);
+
}
+

+
void
+
pkg_script_reset(struct pkg_script *script)
+
{
+
	sbuf_reset(script->data);
+
}
+

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

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

+
void
+
pkg_script_free_void(void *s)
+
{
+
	if (s != NULL)
+
		pkg_script_free((struct pkg_script *)s);
+
}
modified libpkg/pkg_util.c
@@ -143,7 +143,7 @@ file_to_buffer(const char *path, char **buffer, off_t *sz)

	if (read(fd, *buffer, st.st_size) == -1) {
		close(fd);
-
		return (pkg_error_set(EPKG_FATAL, "read(): %s", strerror(errno)));
+
		return (pkg_error_set(EPKG_FATAL, "read(%s): %s", path, strerror(errno)));
	}

	close(fd);
modified libpkg/pkgdb.c
@@ -158,11 +158,11 @@ pkgdb_init(sqlite3 *sdb)
	"FOR EACH ROW BEGIN "
		"INSERT OR IGNORE INTO mtree (content) VALUES (NEW.mtree);"
		"INSERT OR REPLACE INTO packages(origin, name, version, comment, desc, mtree_id, "
-
		"message, arch, osversion, maintainer, www, prefix, flatsize) "
+
		"message, arch, osversion, maintainer, www, prefix, flatsize, automatic) "
		"VALUES (NEW.origin, NEW.name, NEW.version, NEW.comment, NEW.desc, "
		"(SELECT id FROM mtree WHERE content = NEW.mtree), "
		"NEW.message, NEW.arch, NEW.osversion, NEW.maintainer, NEW.www, NEW.prefix, "
-
		"NEW.flatsize);"
+
		"NEW.flatsize NEW.automatic);"
	"END;"
	"CREATE TABLE scripts ("
		"package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
@@ -240,51 +240,49 @@ pkgdb_init(sqlite3 *sdb)
}

int
-
pkgdb_open(struct pkgdb **db, pkgdb_t remote, int mode)
+
pkgdb_open(struct pkgdb **db, pkgdb_t type)
{
	int retcode;
-
	struct stat st;
	char *errmsg;
	char localpath[MAXPATHLEN];
	char remotepath[MAXPATHLEN];
	char sql[BUFSIZ];
	const char *dbdir;

-
	/* First check to make sure PKG_DBDIR exists before trying to use it */
	dbdir = pkg_config("PKG_DBDIR");
-
	if (eaccess(dbdir, mode) == -1)
-
		return (pkg_error_set(EPKG_FATAL, "Package database directory "
-
		    "%s error: %s", dbdir, strerror(errno)));
-

-
	snprintf(localpath, sizeof(localpath), "%s/local.sqlite", dbdir);

	if ((*db = calloc(1, sizeof(struct pkgdb))) == NULL)
		return (pkg_error_set(EPKG_FATAL, "calloc(): %s", strerror(errno)));

-
	(*db)->remote = remote;
-

-
	retcode = stat(localpath, &st);
-
	if (retcode == -1 && errno != ENOENT)
-
		return (pkg_error_set(EPKG_FATAL, "can not stat %s: %s", localpath,
-
							  strerror(errno)));
+
	(*db)->type = type;

-
	if (remote == PKGDB_REMOTE) {
-
		snprintf(remotepath, sizeof(localpath), "%s/repo.sqlite", dbdir);
-
		retcode = stat(remotepath, &st);
-
		if (retcode == -1 && errno != ENOENT)
-
			return (pkg_error_set(EPKG_FATAL, "can not stat %s: %s", remotepath,
-
						strerror(errno)));
+
	snprintf(localpath, sizeof(localpath), "%s/local.sqlite", dbdir);
+
	retcode = access(localpath, R_OK);
+
	if (retcode == -1) {
+
		if (errno != ENOENT)
+
			return (pkg_error_set(EPKG_FATAL, "%s: %s", localpath,
+
								  strerror(errno)));
+
		else if (eaccess(dbdir, W_OK) != 0)
+
			return (pkg_error_set(EPKG_FATAL, "can not initialize database in "
+
								 "%s: %s", dbdir, strerror(errno)));
	}

	if (sqlite3_open(localpath, &(*db)->sqlite) != SQLITE_OK)
		return (ERROR_SQLITE((*db)->sqlite));

-
	if (remote == PKGDB_REMOTE) {
-
		sqlite3_snprintf(BUFSIZ, sql, "ATTACH \"%s\" as remote;", remotepath);
+
	if (type == PKGDB_REMOTE) {
+
		snprintf(remotepath, sizeof(remotepath), "%s/repo.sqlite", dbdir);
+

+
		if (access(remotepath, R_OK) != 0)
+
			return (pkg_error_set(EPKG_FATAL, "repo.sqlite: %s",
+
								  strerror(errno)));

-
		if (sqlite3_exec((*db)->sqlite, sql, NULL, NULL, &errmsg) != SQLITE_OK){
+
		sqlite3_snprintf(sizeof(sql), sql, "ATTACH \"%s\" as remote;", remotepath);
+

+
		if (sqlite3_exec((*db)->sqlite, sql, NULL, NULL, &errmsg) != SQLITE_OK) {
+
			pkg_error_set(EPKG_FATAL, "%s", errmsg);
			sqlite3_free(errmsg);
-
			return ERROR_SQLITE((*db)->sqlite);
+
			return (EPKG_FATAL);
		}
	}

@@ -323,7 +321,7 @@ pkgdb_close(struct pkgdb *db)
		return;

	if (db->sqlite != NULL) {
-
		if (db->remote == PKGDB_REMOTE)
+
		if (db->type == PKGDB_REMOTE)
			sqlite3_exec(db->sqlite, "DETACH remote;", NULL, NULL, NULL);

		sqlite3_close(db->sqlite);
@@ -405,6 +403,10 @@ pkgdb_it_next(struct pkgdb_it *it, struct pkg **pkg_p, int flags)
			if ((ret = pkgdb_loadfiles(it->db, pkg)) != EPKG_OK)
				return (ret);

+
		if (flags & PKG_LOAD_DIRS)
+
			if ((ret = pkgdb_loaddirs(it->db, pkg)) != EPKG_OK)
+
				return (ret);
+

		if (flags & PKG_LOAD_SCRIPTS)
			if ((ret = pkgdb_loadscripts(it->db, pkg)) != EPKG_OK)
				return (ret);
@@ -456,25 +458,29 @@ pkgdb_query(struct pkgdb *db, const char *pattern, match_t match)
		break;
	case MATCH_EXACT:
		if (checkorigin == NULL)
-
			comp = " WHERE p.name = ?1";
+
			comp = " WHERE p.name = ?1 "
+
				"OR p.name || \"-\" || p.version = ?1";
		else
			comp = " WHERE p.origin = ?1";
		break;
	case MATCH_GLOB:
		if (checkorigin == NULL)
-
			comp = " WHERE p.name GLOB ?1";
+
			comp = " WHERE p.name GLOB ?1 "
+
				"OR p.name || \"-\" || p.version GLOB ?1";
		else
			comp = " WHERE p.origin GLOB ?1";
		break;
	case MATCH_REGEX:
		if (checkorigin == NULL)
-
			comp = " WHERE p.name REGEXP ?1";
+
			comp = " WHERE p.name REGEXP ?1 "
+
				"OR p.name || \"-\" || p.version REGEXP ?1";
		else
			comp = " WHERE p.origin REGEXP ?1";
		break;
	case MATCH_EREGEX:
		if (checkorigin == NULL)
-
			comp = " WHERE EREGEXP(?1, p.name)";
+
			comp = " WHERE EREGEXP(?1, p.name) "
+
				"OR EREGEXP(?1, p.name || \"-\" || p.version)";
		else
			comp = " WHERE EREGEXP(?1, p.origin)";
		break;
@@ -668,11 +674,6 @@ pkgdb_loadfiles(struct pkgdb *db, struct pkg *pkg)
		"FROM files "
		"WHERE package_id = ?1 "
		"ORDER BY PATH ASC";
-
	const char sqldir[] = ""
-
		"SELECT path "
-
		"FROM pkg_dirs "
-
		"WHERE origin = ?1 "
-
		"ORDER by path DESC";

	if (pkg->type != PKG_INSTALLED)
		return (ERROR_BAD_ARG("pkg"));
@@ -700,19 +701,45 @@ pkgdb_loadfiles(struct pkgdb *db, struct pkg *pkg)
		return (ERROR_SQLITE(db->sqlite));
	}

-
	if (sqlite3_prepare_v2(db->sqlite, sqldir, -1, &stmt, NULL) != SQLITE_OK)
+
	pkg->flags |= PKG_LOAD_FILES;
+
	return (EPKG_OK);
+
}
+

+
int
+
pkgdb_loaddirs(struct pkgdb *db, struct pkg *pkg)
+
{
+
	sqlite3_stmt *stmt;
+
	char *path;
+
	int ret;
+

+
	const char sql[] = ""
+
		"SELECT path "
+
		"FROM pkg_dirs "
+
		"WHERE origin = ?1 "
+
		"ORDER by path DESC";
+

+
	if (pkg->flags & PKG_LOAD_DIRS)
+
		return (EPKG_OK);
+

+
	if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK)
		return (ERROR_SQLITE(db->sqlite));

+
	array_init(&pkg->dirs, 5);
+

	sqlite3_bind_text(stmt, 1, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);

	while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
-
		pkg_file_new(&f);
-
		strlcpy(f->path, sqlite3_column_text(stmt, 0), sizeof(f->path));
-
		array_append(&pkg->files, f);
+
		path = strdup(sqlite3_column_text(stmt, 0));
+
		array_append(&pkg->dirs, path);
	}
	sqlite3_finalize(stmt);

-
	pkg->flags |= PKG_LOAD_FILES;
+
	if (ret != SQLITE_DONE) {
+
		array_reset(&pkg->dirs, &free);
+
		return (ERROR_SQLITE(db->sqlite));
+
	}
+

+
	pkg->flags |= PKG_LOAD_DIRS;
	return (EPKG_OK);
}

@@ -852,6 +879,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
{
	struct pkg **deps;
	struct pkg_file **files;
+
	const char **dirs;
	struct pkg_conflict **conflicts;
	struct pkg_script **scripts;
	struct pkg_option **options;
@@ -877,8 +905,8 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
	const char sql_pkg[] = ""
		"INSERT INTO pkg_mtree( "
			"origin, name, version, comment, desc, mtree, message, arch, "
-
			"osversion, maintainer, www, prefix, flatsize) "
-
		"VALUES( ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13);";
+
			"osversion, maintainer, www, prefix, flatsize, automatic) "
+
		"VALUES( ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14);";
	const char sql_sel_pkg[] = ""
		"SELECT id FROM packages "
		"WHERE origin = ?1;";
@@ -935,6 +963,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
	sqlite3_bind_text(stmt_pkg, 11, pkg_get(pkg, PKG_WWW), -1, SQLITE_STATIC);
	sqlite3_bind_text(stmt_pkg, 12, pkg_get(pkg, PKG_PREFIX), -1, SQLITE_STATIC);
	sqlite3_bind_int64(stmt_pkg, 13, pkg_flatsize(pkg));
+
	sqlite3_bind_int(stmt_pkg, 14, pkg_isautomatic(pkg));

	if ((ret = sqlite3_step(stmt_pkg)) != SQLITE_DONE) {
		if ( ret == SQLITE_CONSTRAINT)
@@ -1006,7 +1035,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
		sqlite3_bind_text(stmt_conflict, 1, pkg_conflict_glob(conflicts[i]), -1, SQLITE_STATIC);
		sqlite3_bind_int64(stmt_conflict, 2, package_id);

-
		if (sqlite3_step(stmt_conflict) != SQLITE_DONE) {
+
		if ((ret = sqlite3_step(stmt_conflict)) != SQLITE_DONE) {
			if ( ret == SQLITE_CONSTRAINT)
				retcode = pkg_error_set(EPKG_FATAL, "constraint violation on "
						"conflicts with %s", pkg_conflict_glob(conflicts[i]));
@@ -1019,8 +1048,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
	}

	/*
-
	 * Insert file
-
	 * and dirs
+
	 * Insert files.
	 */

	if (sqlite3_prepare_v2(s, sql_file, -1, &stmt_file, NULL) != SQLITE_OK) {
@@ -1028,43 +1056,47 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
		goto cleanup;
	}

+
	files = pkg_files(pkg);
+
	for (i = 0; files[i] != NULL; i++) {
+
		path = pkg_file_path(files[i]);
+
		sqlite3_bind_text(stmt_file, 1, pkg_file_path(files[i]), -1, SQLITE_STATIC);
+
		sqlite3_bind_text(stmt_file, 2, pkg_file_sha256(files[i]), -1, SQLITE_STATIC);
+
		sqlite3_bind_int64(stmt_file, 3, package_id);
+

+
		if ((ret = sqlite3_step(stmt_file)) != SQLITE_DONE) {
+
			if (ret == SQLITE_CONSTRAINT)
+
				retcode = pkg_error_set(EPKG_FATAL, "constraint violation on "
+
						"path with %s", pkg_file_path(files[i]));
+
			else
+
				retcode = ERROR_SQLITE(s);
+
			goto cleanup;
+
		}
+
		sqlite3_reset(stmt_file);
+
	}
+

+
	/*
+
	 * Insert dirs.
+
	 */
+

	if (sqlite3_prepare_v2(s, sql_dir, -1, &stmt_dirs, NULL) != SQLITE_OK) {
		retcode = ERROR_SQLITE(s);
		goto cleanup;
	}

-
	files = pkg_files(pkg);
-
	for (i = 0; files[i] != NULL; i++) {
-
		path = pkg_file_path(files[i]);
-
		if (path[strlen(path) - 1 ] != '/') {
-
			sqlite3_bind_text(stmt_file, 1, pkg_file_path(files[i]), -1, SQLITE_STATIC);
-
			sqlite3_bind_text(stmt_file, 2, pkg_file_sha256(files[i]), -1, SQLITE_STATIC);
-
			sqlite3_bind_int64(stmt_file, 3, package_id);
-

-
			if ((ret = sqlite3_step(stmt_file)) != SQLITE_DONE) {
-
				if ( ret == SQLITE_CONSTRAINT)
-
					retcode = pkg_error_set(EPKG_FATAL, "constraint violation on "
-
							"path with %s", pkg_file_path(files[i]));
-
				else
-
					retcode = ERROR_SQLITE(s);
-
				goto cleanup;
-
			}
-
			sqlite3_reset(stmt_file);
-
		} else {
-
			sqlite3_bind_text(stmt_dirs, 1, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);
-
			sqlite3_bind_text(stmt_dirs, 2, pkg_file_path(files[i]), -1, SQLITE_STATIC);
+
	dirs = pkg_dirs(pkg);
+
	for (i = 0; dirs[i] != NULL; i++) {
+
		sqlite3_bind_text(stmt_dirs, 1, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);
+
		sqlite3_bind_text(stmt_dirs, 2, dirs[i], -1, SQLITE_STATIC);
			
-
			if ((ret = sqlite3_step(stmt_dirs)) != SQLITE_DONE) {
-
				if ( ret == SQLITE_CONSTRAINT)
-
					retcode = pkg_error_set(EPKG_FATAL, "constraint violation on "
-
							"dirs with %s", pkg_file_path(files[i]));
-
				else
-
					retcode = ERROR_SQLITE(s);
-
				goto cleanup;
-
			}
-
			sqlite3_reset(stmt_dirs);
+
		if ((ret = sqlite3_step(stmt_dirs)) != SQLITE_DONE) {
+
			if ( ret == SQLITE_CONSTRAINT)
+
				retcode = pkg_error_set(EPKG_FATAL, "constraint violation on "
+
						"dirs with %s", dirs[i]);
+
			else
+
				retcode = ERROR_SQLITE(s);
+
			goto cleanup;
		}
-

+
		sqlite3_reset(stmt_dirs);
	}

	/*
@@ -1251,11 +1283,13 @@ pkgdb_compact(struct pkgdb *db)
struct pkgdb_it *
pkgdb_query_upgrades(struct pkgdb *db)
{
-
	if (db->remote != PKGDB_REMOTE)
-
		return (NULL);
-

	sqlite3_stmt *stmt;

+
	if (db->type != PKGDB_REMOTE) {
+
		pkg_error_set(EPKG_FATAL, "remote database not attached");
+
		return (NULL);
+
	}
+

	const char sql[] = ""
		"SELECT l.id, l.origin, l.name, l.version, l.comment, l.desc, "
		"l.message, l.arch, l.osversion, l.maintainer, "
@@ -1265,8 +1299,10 @@ pkgdb_query_upgrades(struct pkgdb *db)
		"WHERE l.origin = r.origin "
		"AND PKGLT(l.version, r.version)";

-
	if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK)
+
	if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) {
+
		ERROR_SQLITE(db->sqlite);
		return (NULL);
+
	}

	return (pkgdb_it_new(db, stmt, IT_UPGRADE));
}
@@ -1277,8 +1313,10 @@ pkgdb_query_downgrades(struct pkgdb *db)

	sqlite3_stmt *stmt;

-
	if (db->remote != PKGDB_REMOTE)
+
	if (db->type != PKGDB_REMOTE) {
+
		pkg_error_set(EPKG_FATAL, "remote database not attached");
		return (NULL);
+
	}

	const char sql[] = ""
		"SELECT l.id, l.origin, l.name, l.version, l.comment, l.desc, "
@@ -1289,8 +1327,10 @@ pkgdb_query_downgrades(struct pkgdb *db)
		"WHERE l.origin = r.origin "
		"AND PKGGT(l.version, r.version)";

-
	if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK)
+
	if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) {
+
		ERROR_SQLITE(db->sqlite);
		return (NULL);
+
	}

	return (pkgdb_it_new(db, stmt, IT_UPGRADE));
}
modified libpkg/pkgdb.h
@@ -8,7 +8,7 @@
struct pkgdb {
	uint64_t flags;
	sqlite3 *sqlite;
-
	pkgdb_t remote;
+
	pkgdb_t type;
};

#define IT_LOCAL 0
modified libpkg/scripts.c
@@ -16,13 +16,17 @@ pkg_script_pre_install(struct pkg *pkg)
		switch (pkg_script_type(scripts[i])) {
			case PKG_SCRIPT_INSTALL:
				sbuf_reset(script_cmd);
-
				sbuf_printf(script_cmd, "set -- %s-%s PRE-INSTALL\n%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
+
				sbuf_printf(script_cmd, "PKG_PREFIX=%s\nset -- %s-%s PRE-INSTALL\n%s",
+
					pkg_get(pkg, PKG_PREFIX), pkg_get(pkg, PKG_NAME),
+
					pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
				sbuf_finish(script_cmd);
				system(sbuf_data(script_cmd));
				break;
			case PKG_SCRIPT_PRE_INSTALL:
				sbuf_reset(script_cmd);
-
				sbuf_printf(script_cmd, "set -- %s-%s\n%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
+
				sbuf_printf(script_cmd, "PKG_PREFIX=%s\nset -- %s-%s\n%s",
+
				  pkg_get(pkg, PKG_PREFIX), pkg_get(pkg, PKG_NAME),
+
				  pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
				sbuf_finish(script_cmd);
				system(sbuf_data(script_cmd));
				break;
@@ -51,13 +55,17 @@ pkg_script_post_install(struct pkg *pkg)
		switch (pkg_script_type(scripts[i])) {
			case PKG_SCRIPT_INSTALL:
				sbuf_reset(script_cmd);
-
				sbuf_printf(script_cmd, "set -- %s-%s POST-INSTALL\n%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
+
				sbuf_printf(script_cmd, "PKG_PREFIX=%s\nset -- %s-%s POST-INSTALL\n%s",
+
				  pkg_get(pkg, PKG_PREFIX), pkg_get(pkg, PKG_NAME),
+
				  pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
				sbuf_finish(script_cmd);
				system(sbuf_data(script_cmd));
				break;
			case PKG_SCRIPT_POST_INSTALL:
				sbuf_reset(script_cmd);
-
				sbuf_printf(script_cmd, "set -- %s-%s\n%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
+
				sbuf_printf(script_cmd, "PKG_PREFIX=%s\nset -- %s-%s\n%s",
+
				  pkg_get(pkg, PKG_PREFIX), pkg_get(pkg, PKG_NAME),
+
				  pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
				sbuf_finish(script_cmd);
				system(sbuf_data(script_cmd));
				break;
@@ -86,13 +94,17 @@ pkg_script_pre_upgrade(struct pkg *pkg)
		switch (pkg_script_type(scripts[i])) {
			case PKG_SCRIPT_UPGRADE:
				sbuf_reset(script_cmd);
-
				sbuf_printf(script_cmd, "set -- %s-%s PRE-UPGRADE\n%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
+
				sbuf_printf(script_cmd, "PKG_PREFIX=%s\nset -- %s-%s PRE-UPGRADE\n%s",
+
				  pkg_get(pkg, PKG_PREFIX), pkg_get(pkg, PKG_NAME),
+
				  pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
				sbuf_finish(script_cmd);
				system(sbuf_data(script_cmd));
				break;
			case PKG_SCRIPT_PRE_UPGRADE:
				sbuf_reset(script_cmd);
-
				sbuf_printf(script_cmd, "set -- %s-%s\n%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
+
				sbuf_printf(script_cmd, "PKG_PREFIX=%s\nset -- %s-%s\n%s",
+
				  pkg_get(pkg, PKG_PREFIX), pkg_get(pkg, PKG_NAME),
+
				  pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
				sbuf_finish(script_cmd);
				system(sbuf_data(script_cmd));
				break;
@@ -121,13 +133,17 @@ pkg_script_post_upgrade(struct pkg *pkg)
		switch (pkg_script_type(scripts[i])) {
			case PKG_SCRIPT_UPGRADE:
				sbuf_reset(script_cmd);
-
				sbuf_printf(script_cmd, "set -- %s-%s POST-UPGRADE\n%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
+
				sbuf_printf(script_cmd, "PKG_PREFIX=%s\nset -- %s-%s POST-UPGRADE\n%s",
+
				  pkg_get(pkg, PKG_PREFIX), pkg_get(pkg, PKG_NAME),
+
				  pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
				sbuf_finish(script_cmd);
				system(sbuf_data(script_cmd));
				break;
			case PKG_SCRIPT_POST_UPGRADE:
				sbuf_reset(script_cmd);
-
				sbuf_printf(script_cmd, "set -- %s-%s\n%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
+
				sbuf_printf(script_cmd, "PKG_PREFIX=%s\nset -- %s-%s\n%s",
+
				  pkg_get(pkg, PKG_PREFIX), pkg_get(pkg, PKG_NAME),
+
				  pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
				sbuf_finish(script_cmd);
				system(sbuf_data(script_cmd));
				break;
@@ -156,13 +172,17 @@ pkg_script_pre_deinstall(struct pkg *pkg)
		switch (pkg_script_type(scripts[i])) {
			case PKG_SCRIPT_DEINSTALL:
				sbuf_reset(script_cmd);
-
				sbuf_printf(script_cmd, "set -- %s-%s PRE-DEINSTALL\n%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
+
				sbuf_printf(script_cmd, "PKG_PREFIX=%s\nset -- %s-%s DEINSTALL\n%s",
+
				  pkg_get(pkg, PKG_PREFIX), pkg_get(pkg, PKG_NAME),
+
				  pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
				sbuf_finish(script_cmd);
				system(sbuf_data(script_cmd));
				break;
			case PKG_SCRIPT_PRE_DEINSTALL:
				sbuf_reset(script_cmd);
-
				sbuf_printf(script_cmd, "set -- %s-%s\n%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
+
				sbuf_printf(script_cmd, "PKG_PREFIX=%s\nset -- %s-%s\n%s",
+
				  pkg_get(pkg, PKG_PREFIX), pkg_get(pkg, PKG_NAME),
+
				  pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
				sbuf_finish(script_cmd);
				system(sbuf_data(script_cmd));
				break;
@@ -187,23 +207,26 @@ pkg_script_post_deinstall(struct pkg *pkg)
	if ((scripts = pkg_scripts(pkg)) == NULL)
		return (EPKG_OK);

+
	/* two loops because the order matters */
	for (i = 0; scripts[i] != NULL; i++) {
-
		switch (pkg_script_type(scripts[i])) {
-
			case PKG_SCRIPT_DEINSTALL:
-
				sbuf_reset(script_cmd);
-
				sbuf_printf(script_cmd, "set -- %s-%s POST-DEINSTALL\n%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
-
				sbuf_finish(script_cmd);
-
				system(sbuf_data(script_cmd));
-
				break;
-
			case PKG_SCRIPT_POST_DEINSTALL:
-
				sbuf_reset(script_cmd);
-
				sbuf_printf(script_cmd, "set -- %s-%s\n%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
-
				sbuf_finish(script_cmd);
-
				system(sbuf_data(script_cmd));
-
				break;
-
			default:
-
				/* ignored to prevent warning */
-
				break;
+
		if (pkg_script_type(scripts[i]) == PKG_SCRIPT_DEINSTALL) {
+
			sbuf_reset(script_cmd);
+
			sbuf_printf(script_cmd, "PKG_PREFIX=%s\nset -- %s-%s POST-DEINSTALL\n%s",
+
					pkg_get(pkg, PKG_PREFIX), pkg_get(pkg, PKG_NAME),
+
					pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
+
			sbuf_finish(script_cmd);
+
			system(sbuf_data(script_cmd));
+
		}
+
	}
+

+
	for (i = 0; scripts[i] != NULL; i++) {
+
		if (pkg_script_type(scripts[i]) == PKG_SCRIPT_POST_DEINSTALL) {
+
			sbuf_reset(script_cmd);
+
			sbuf_printf(script_cmd, "PKG_PREFIX=%s\nset -- %s-%s\n%s",
+
					pkg_get(pkg, PKG_PREFIX), pkg_get(pkg, PKG_NAME),
+
					pkg_get(pkg, PKG_VERSION), pkg_script_data(scripts[i]));
+
			sbuf_finish(script_cmd);
+
			system(sbuf_data(script_cmd));
		}
	}

modified pkg/Makefile
@@ -12,7 +12,7 @@ SRCS= add.c \
		which.c
BINDIR=		/usr/sbin

-
DEBUG_FLAGS=	-g
+
DEBUG_FLAGS+=	-g -O0
CFLAGS+=	-I${.CURDIR}/../libpkg
LDADD+=		-L../libpkg \
		-lpkgng \
modified pkg/add.c
@@ -67,7 +67,7 @@ exec_add(int argc, char **argv)
		return (EX_NOPERM);
	}

-
	if (pkgdb_open(&db, PKGDB_DEFAULT, R_OK|W_OK) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		pkg_error_warn("can not open database");
		return (1);
	}
modified pkg/create.c
@@ -26,10 +26,10 @@ pkg_create_matches(int argc, char **argv, match_t match, pkg_formats fmt, const
	struct pkgdb_it *it = NULL;
	struct pkg *pkg = NULL;
	int query_flags = PKG_LOAD_DEPS | PKG_LOAD_CONFLICTS | PKG_LOAD_FILES |
-
					  PKG_LOAD_SCRIPTS | PKG_LOAD_OPTIONS |
+
					  PKG_LOAD_DIRS | PKG_LOAD_SCRIPTS | PKG_LOAD_OPTIONS |
					  PKG_LOAD_MTREE;

-
	if (pkgdb_open(&db, PKGDB_DEFAULT, R_OK) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		pkg_error_warn("can not open database");
		pkgdb_close(db);
		return (-1);
modified pkg/delete.c
@@ -55,7 +55,7 @@ exec_delete(int argc, char **argv)
		return (EX_NOPERM);
	}
	
-
	if (pkgdb_open(&db, PKGDB_DEFAULT, R_OK|W_OK) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		pkg_error_warn("can not open database");
		pkgdb_close(db);
		return (-1);
modified pkg/info.c
@@ -115,8 +115,11 @@ exec_info(int argc, char **argv)
	int sign = 0;

	/* TODO: exclusive opts ? */
-
	while ((ch = getopt(argc, argv, "egxXEdrlsqopO")) != -1) {
+
	while ((ch = getopt(argc, argv, "aegxXEdrlsqopO")) != -1) {
		switch (ch) {
+
			case 'a':
+
				match = MATCH_ALL;
+
				break;
			case 'O':
				opt |= INFO_ORIGIN_SEARCH;  /* this is only for ports compat */
				break;
@@ -166,7 +169,7 @@ exec_info(int argc, char **argv)
	if (argc == 0)
		match = MATCH_ALL;

-
	if (pkgdb_open(&db, PKGDB_DEFAULT, R_OK) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		pkg_error_warn("can not open database");
		return (-1);
	}
@@ -224,7 +227,6 @@ exec_info(int argc, char **argv)
			}
		}

-

		if ((it = pkgdb_query(db, pkgname, match)) == NULL) {
			pkg_error_warn("can not query database");
			return (-1);
@@ -256,7 +258,7 @@ exec_info(int argc, char **argv)
						}
						break;
					case 1:
-
						if (sign != GT && sign != LT) {
+
						if (sign != GT && sign != GE) {
							gotone = false;
							continue;
						}
modified pkg/register.c
@@ -15,28 +15,33 @@

#include "register.h"

-
static struct {
-
	const pkg_attr attr;
-
	const char * const flag;
-
} required_flags[] = {
-
	{ PKG_ORIGIN, "-o"},
-
	{ PKG_NAME, "-n"},
-
	{ PKG_VERSION, "-n"},
-
	{ PKG_COMMENT, "-c"},
-
	{ PKG_DESC, "-d"},
-
	{ PKG_PREFIX, "-p"},
-
	{ PKG_MAINTAINER, "-r"},
-
	{ 0, NULL}
+
static const char *scripts[] = {
+
	"+INSTALL",
+
	"+PRE_INSTALL",
+
	"+POST_INSTALL",
+
	"+POST_INSTALL",
+
	"+DEINSTALL",
+
	"+PRE_DEINSTALL",
+
	"+POST_DEINSTALL",
+
	"+UPGRADE",
+
	"+PRE_UPGRADE",
+
	"+POST_UPGRADE",
+
	"pkg-install",
+
	"pkg-pre-install",
+
	"pkg-post-install",
+
	"pkg-deinstall",
+
	"pkg-pre-deinstall",
+
	"pkg-post-deinstall",
+
	"pkg-upgrade",
+
	"pkg-pre-upgrade",
+
	"pkg-post-upgrade",
+
	NULL
};

void
usage_register(void)
{
-
	fprintf(stderr, "usage: pkg register -c comment -d desc -f plist_file -p prefix\n");
-
	fprintf(stderr, "                    -m mtree_file -n pkgname -o origin -r maintainer\n");
-
	fprintf(stderr, "                    [-P depends] [-C conflicts] [-M message_file] [-s scripts]\n");
-
	fprintf(stderr, "                    [-a arch] [-w www] [-O options] [-H] [-i input_dir]\n");
-
	fprintf(stderr, "                    [-l]\n\n");
+
	fprintf(stderr, "usage: pkg register [-l] -m <metadatadir> -f <plist_file>\n\n");
	fprintf(stderr, "For more information see 'pkg help register'.\n");
}

@@ -54,8 +59,10 @@ exec_register(int argc, char **argv)
	char *plist = NULL;
	char *v = NULL;
	char *arch = NULL;
+
	char *mdir = NULL;
	char *www = NULL;
	char *input_path = NULL;
+
	char fpath[MAXPATHLEN];

	const char *desc = NULL;
	size_t size;
@@ -63,6 +70,7 @@ exec_register(int argc, char **argv)
	bool heuristic = false;
	bool legacy = false;

+
	int i;
	int retcode = 0;
	int ret = 0;

@@ -72,70 +80,21 @@ exec_register(int argc, char **argv)
	}

	pkg_new(&pkg, PKG_INSTALLED);
-
	while ((ch = getopt(argc, argv, "vHc:d:f:p:P:m:o:C:n:M:s:a:r:w:O:i:l")) != -1) {
+
	while ((ch = getopt(argc, argv, "a:f:m:i:ld")) != -1) {
		switch (ch) {
-
			case 'v':
-
				/* IGNORE */
-
				break;
-
			case 'c':
-
				ret += pkg_set(pkg, PKG_COMMENT, optarg[0] == '-' ? optarg + 1 : optarg);
-
				break;
-
			case 'd':
-
				ret += pkg_set_from_file(pkg, PKG_DESC, optarg);
-
				break;
			case 'f':
-
				if ((plist = strdup(optarg)) == NULL) 
+
				if ((plist = strdup(optarg)) == NULL)
					errx(1, "cannot allocate memory");

				break;
-
			case 'p':
-
				pkg_set(pkg, PKG_PREFIX, optarg);
-
				break;
-
			case 'P':
-
				ret += ports_parse_depends(pkg, optarg);
-
				break;
			case 'm':
-
				ret += pkg_set_from_file(pkg, PKG_MTREE, optarg);
-
				break;
-
			case 'n':
-
				if ((v = strrchr(optarg, '-')) == NULL)
-
					errx(EX_DATAERR, "bad pkgname format");
-

-
				v[0] = '\0';
-
				v++;
-
				ret += pkg_set(pkg, PKG_NAME, optarg);
-
				ret += pkg_set(pkg, PKG_VERSION, v);
-
				break;
-
			case 'o':
-
				ret += pkg_set(pkg, PKG_ORIGIN, optarg);
-
				break;
-
			case 'C':
-
				ret += ports_parse_conflicts(pkg, optarg);
-
				break;
-
			case 'M':
-
				ret += pkg_set_from_file(pkg, PKG_MESSAGE, optarg);
-
				break;
-
			case 's':
-
				ret += ports_parse_scripts(pkg, optarg);
+
				mdir = strdup(optarg);
				break;
			case 'a':
-
				if ((arch = strdup(optarg)) == NULL)
-
					errx(1, "cannot allocate memory");
-

-
				break;
-
			case 'r': /* responsible */
-
				ret += pkg_set(pkg, PKG_MAINTAINER, optarg);
+
				arch = strdup(optarg);
				break;
-
			case 'w':
-
				if ((www = strdup(optarg)) == NULL)
-
					errx(1, "cannot allocate memory");
-

-
				break;
-
			case 'O':
-
				ret += ports_parse_options(pkg, optarg);
-
				break;
-
			case 'H':
-
				heuristic = true;
+
			case 'd':
+
				pkg_setautomatic(pkg);
				break;
			case 'i':
				if ((input_path = strdup(optarg)) == NULL)
@@ -159,10 +118,6 @@ exec_register(int argc, char **argv)
	if (plist == NULL)
		errx(EX_USAGE, "missing -f flag");

-
	for (unsigned int i = 0; required_flags[i].flag != NULL; i++)
-
		if (pkg_get(pkg, required_flags[i].attr) == NULL)
-
			errx(EX_USAGE, "missing %s flag", required_flags[i].flag);
-

	uname(&u);
	if (arch == NULL) {
		pkg_set(pkg, PKG_ARCH, u.machine);
@@ -171,6 +126,33 @@ exec_register(int argc, char **argv)
		free(arch);
	}

+
	if (mdir == NULL)
+
		errx(EX_USAGE, "missing -m flag");
+

+
	snprintf(fpath, MAXPATHLEN, "%s/+MANIFEST", mdir);
+
	if ((ret = pkg_load_manifest_file(pkg, fpath)) != EPKG_OK) {
+
		pkg_error_warn("can not parse manifest %s", fpath);
+
		return (EX_SOFTWARE);
+
	}
+

+
	snprintf(fpath, MAXPATHLEN, "%s/+DESC", mdir);
+
	if (pkg_set_from_file(pkg, PKG_DESC, fpath) != EPKG_OK)
+
		pkg_error_warn("");
+

+
	snprintf(fpath, MAXPATHLEN, "%s/+DISPLAY", mdir);
+
	if (access(fpath, F_OK) == 0 && pkg_set_from_file(pkg, PKG_MESSAGE, fpath) != EPKG_OK)
+
		pkg_error_warn("");
+

+
	snprintf(fpath, MAXPATHLEN, "%s/+MTREE_DIRS", mdir);
+
	if (access(fpath, F_OK) == 0 && pkg_set_from_file(pkg, PKG_MTREE, fpath) != EPKG_OK)
+
		pkg_error_warn("");
+

+
	for (i = 0; scripts[i] != NULL; i++) {
+
		snprintf(fpath, MAXPATHLEN, "%s/%s", mdir, scripts[i]);
+
		if (access(fpath, F_OK) == 0 && pkg_addscript(pkg, fpath) != EPKG_OK)
+
			pkg_error_warn("");
+
	}
+

	/* if www is not given then try to determine it from description */
	if (www == NULL) {
		desc = pkg_get(pkg, PKG_DESC);
@@ -208,7 +190,7 @@ exec_register(int argc, char **argv)
	if (plist != NULL)
		free(plist);

-
	if (pkgdb_open(&db, PKGDB_DEFAULT, R_OK|W_OK) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		pkg_error_warn("can not open database");
		return (-1);
	}
modified pkg/upgrade.c
@@ -43,7 +43,7 @@ exec_upgrade(int argc, char **argv)
		return (EX_NOPERM);
	}

-
	if (pkgdb_open(&db, PKGDB_REMOTE, R_OK|W_OK) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
		pkg_error_warn("can not open database");
		return (1);
	}
modified pkg/which.c
@@ -34,7 +34,7 @@ exec_which(int argc, char **argv)
		return (EX_USAGE);
	}

-
	if (pkgdb_open(&db, PKGDB_DEFAULT, R_OK) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		pkg_error_warn("can not open database");
		pkgdb_close(db);
		return (-1);
modified ports/bsd.pkgng.mk
@@ -2,6 +2,13 @@ PKG_CMD= /usr/sbin/pkg register
PKG_DELETE=		/usr/sbin/pkg delete
PKG_INFO=		/usr/sbin/pkg info
PKG_VERSION=		/usr/sbin/pkg version
+
PKG_CREATE=		/usr/sbin/pkg create
+
PKG_ADD=		/usr/sbin/pkg add
+

+
PKG_SUFX=		.txz
+

+
METADIR=		${WRKDIR}/.metadir
+
MANIFESTF=		${METADIR}/+MANIFEST

PKGPREINSTALL?=		${PKGDIR}/pkg-pre-install
PKGPOSTINSTALL?=	${PKGDIR}/pkg-post-install
@@ -15,73 +22,77 @@ ACTUAL-PACKAGE-DEPENDS?= \
	if [ "${_LIB_RUN_DEPENDS}" != "  " ]; then \
		for dir in ${_LIB_RUN_DEPENDS:C,[^:]*:([^:]*):?.*,\1,}; do \
			pkgname=$$(${PKG_INFO} -q $${dir\#\#${PORTSDIR}/}); \
-
			${ECHO_CMD} $$pkgname:$${dir\#\#${PORTSDIR}/}; \
+
			${ECHO_CMD} @dep $${pkgname%-*} $${dir\#\#${PORTSDIR}/} $${pkgname\#\#*-}; \
			for pkg in $$(${PKG_INFO} -qd $${dir\#\#${PORTSDIR}/}); do\
				origin=$$(${PKG_INFO} -qo $${pkg%-*}); \
-
				${ECHO_CMD} $$pkg:$$origin; \
+
				${ECHO_CMD} $${pkg%-*} $$origin $${pkg\#\#*}; \
			done; \
		done; \
	fi

-
.if !defined(PKG_ARGS)
-
PKG_ARGS=		-l -v -c -${COMMENT:Q} -d ${DESCR} -f ${TMPPLIST} -p ${PREFIX} -P "`cd ${.CURDIR} && ${MAKE} actual-package-depends | ${GREP} -v -E ${PKG_IGNORE_DEPENDS} | ${SORT} -u -t : -k 2`" ${EXTRA_PKG_ARGS} $${_LATE_PKG_ARGS}
-
.if !defined(NO_MTREE)
-
PKG_ARGS+=		-m ${MTREE_FILE}
-
.endif
-
.if defined(CONFLICTS) && !defined(DISABLE_CONFLICTS)
-
PKG_ARGS+=		-C "${CONFLICTS}"
-
.endif
-
.if defined(CONFLICTS_INSTALL) && !defined(DISABLE_CONFLICTS)
-
PKG_ARGS+=		-C "${CONFLICTS_INSTALL}"
-
.endif
-
PKG_ARGS+= -n ${PKGNAME}
-
PKG_ARGS+= -o ${PKGORIGIN}
-
.if defined(MAINTAINER)
-
PKG_ARGS+= -r ${MAINTAINER}
-
.endif
+
.if !target(fake-pkg)
+
fake-pkg:
+
.if !defined(NO_PKG_REGISTER)
+
	@${ECHO_MSG} "===>   Registering installation for ${PKGNAME}"
+
	@${MKDIR} ${METADIR}
+
	@${ECHO_CMD} "@pkg_format_version 0.9" > ${MANIFESTF}
+
	@${ECHO_CMD} "@name ${PKGNAMEPREFIX}${PORTNAME}${PKGNAMESUFFIX}" >> ${MANIFESTF}
+
	@${ECHO_CMD} "@version ${PKGVERSION}" >> ${MANIFESTF}
+
	@${ECHO_CMD} "@origin ${PKGORIGIN}" >> ${MANIFESTF}
+
	@${ECHO_CMD} "@comment ${COMMENT}" >> ${MANIFESTF}
+
	@${ECHO_CMD} "@maintainer ${MAINTAINER}" >> ${MANIFESTF}
+
	@${ECHO_CMD} "@prefix ${PREFIX}" >> ${MANIFESTF}
.if defined(WWW)
-
PKG_ARGS+= -w ${WWW}
+
	@${ECHO_CMD} "@www ${WWW}" >> ${MANIFESTF}
.endif
-
.if exists(${PKGMESSAGE})
-
PKG_ARGS+= -M ${PKGMESSAGE}
+
	@${MAKE} -C ${.CURDIR} actual-package-depends | ${GREP} -v -E ${PKG_IGNORE_DEPENDS} | ${SORT} -u -t : -k 2 >> ${MANIFESTF}
+
.if !defined(DISABLE_CONFLICTS)
+
.for conflicts in ${CONFLICTS}
+
	@${ECHO_CMD} "@conflict ${conflicts}" >> ${MANIFESTF}
+
.endfor
+
.for conflicts in ${CONFLICTS_INSTALL}
+
	@${ECHO_CMD} "@conflict ${conflicts}" >> ${MANIFESTF}
+
.endfor
.endif
.if exists(${PKGINSTALL})
-
PKGSCRIPTS+=	${PKGINSTALL}
+
	@${CP} ${PKGINSTALL} ${METADIR}/+INSTALL
.endif
.if exists(${PKGPREINSTALL})
-
PKGSCRIPTS+=	${PKGPREINSTALL}
+
	@${CP} ${PKGPREINSTALL} ${METADIR}/+PRE_INSTALL
.endif
.if exists(${PKGPOSTINSTALL})
-
PKGSCRIPTS+=	${PKGPOSTINSTALL}
+
	@${CP} ${PKGPOSTINSTALL} ${METADIR}/+POST_INSTALL
.endif
.if exists(${PKGDEINSTALL})
-
PKGSCRIPTS+=	${PKGDEINSTALL}
+
	@${CP} ${PKGDEINSTALL} ${METADIR}/+DEINSTALL
.endif
.if exists(${PKGPREDEINSTALL})
-
PKGSCRIPTS+=	${PKGPREDEINSTALL}
+
	@${CP} ${PKGPREDEINSTALL} ${METADIR}/+PRE_DEINSTALL
.endif
.if exists(${PKGPOSTDEINSTALL})
-
PKGSCRIPTS+=	${PKGPOSTDEINSTALL}
+
	@${CP} ${PKGPOSTDEINSTALL} ${METADIR}/+POST_DEINSTALL
.endif
.if exists(${PKGUPGRADE})
-
PKGSCRIPTS+=	${PKGUPGRADE}
+
	@${CP} ${PKGUPGRADE} ${METADIR}/+UPGRADE
.endif
.if exists(${PKGPREUPGRADE})
-
PKGSCRIPTS+=	${PKGPREUPGRADE}
+
	@${CP} ${PKGPREUPGRADE} ${METADIR}/+PRE_UPGRADE
.endif
.if exists(${PKGPOSTUPGRADE})
-
PKGSCRIPTS+=	${PKGPOSTUPGRADE}
+
	@${CP} ${PKGPOSTUPGRADE} ${METADIR}/+POST_UPGRADE
.endif
-
.if defined(PKGSCRIPTS)
-
PKG_ARGS+=	-s "${PKGSCRIPTS}"
+
	@${CP} ${DESCR} ${METADIR}/+DESC
+
.if exists(${PKGMESSAGE})
+
	@${CP} ${PKGMESSAGE} ${METADIR}/+DISPLAY
.endif
+
.if !defined(NO_MTREE)
+
	@${CP} ${MTREE_FILE} ${METADIR}/+MTREE_DIRS
+
.endif
+
.if defined(INSTALLS_DEPENDS)
+
	@${PKG_CMD} -d -l -m ${METADIR} -f ${TMPPLIST}
+
.else
+
	@${PKG_CMD} -l -m ${METADIR} -f ${TMPPLIST}
.endif
-

-
.if !target(fake-pkg)
-
fake-pkg:
-
.if !defined(NO_PKG_REGISTER)
-
	@${ECHO_MSG} "===>   Registering installation for ${PKGNAME}"
-
	@${PKG_CMD} ${PKG_ARGS}
.else
	@${DO_NADA}
.endif
@@ -90,7 +101,7 @@ fake-pkg:
.if !target(check-build-conflicts)
check-build-conflicts:
.if ( defined(CONFLICTS) || defined(CONFLICTS_BUILD) ) && !defined(DISABLE_CONFLICTS) && !defined(DEFER_CONFLICTS_CHECK)
-
	@found=`${PKG_INFO} -q -O ${CONFLICTS:C/.+/'&'/} ${CONFLICTS_BUILD:C/.+/'&'/}`; \
+
	@found=`${PKG_INFO} -q -gOo ${CONFLICTS:C/.+/'&'/} ${CONFLICTS_BUILD:C/.+/'&'/}`; \
	conflicts_with=; \
	if [ -n "$${found}" ]; then \
		prfx=`${PKG_INFO} -q -p "$${found}"`; \
@@ -116,7 +127,7 @@ check-build-conflicts:
.if !target(identify-install-conflicts)
identify-install-conflicts:
.if ( defined(CONFLICTS) || defined(CONFLICTS_INSTALL) ) && !defined(DISABLE_CONFLICTS)
-
	@found=`${PKG_INFO} -q -O ${CONFLICTS:C/.+/'&'/} ${CONFLICTS_INSTALL:C/.+/'&'/}`; \
+
	@found=`${PKG_INFO} -q -gOo ${CONFLICTS:C/.+/'&'/} ${CONFLICTS_INSTALL:C/.+/'&'/}`; \
	conflicts_with=; \
	if [ -n "$${found}" ]; then \
		prfx=`${PKG_INFO} -q -p "$${found}"`; \
@@ -143,7 +154,7 @@ identify-install-conflicts:
check-install-conflicts:
.if ( defined(CONFLICTS) || defined(CONFLICTS_INSTALL) || ( defined(CONFLICTS_BUILD) && defined(DEFER_CONFLICTS_CHECK) ) ) && !defined(DISABLE_CONFLICTS) 
.if defined(DEFER_CONFLICTS_CHECK)
-
	@found=`${PKG_INFO} -q -O ${CONFLICTS:C/.+/'&'/} ${CONFLICTS_BUILD:C/.+/'&'/} ${CONFLICTS_INSTALL:C/.+/'&'/}`; \
+
	@found=`${PKG_INFO} -q -gOo ${CONFLICTS:C/.+/'&'/} ${CONFLICTS_BUILD:C/.+/'&'/} ${CONFLICTS_INSTALL:C/.+/'&'/}`; \
	conflicts_with=; \
	if [ -n "$${found}" ]; then \
		prfx=`${PKG_INFO} -q -p "$${found}"`; \
@@ -163,7 +174,7 @@ check-install-conflicts:
		exit 1; \
	fi
.else
-
	@found=`${PKG_INFO} -q -O ${CONFLICTS:C/.+/'&'/} ${CONFLICTS_INSTALL:C/.+/'&'/}`; \
+
	@found=`${PKG_INFO} -q -gOo ${CONFLICTS:C/.+/'&'/} ${CONFLICTS_INSTALL:C/.+/'&'/}`; \
	conflicts_with=; \
	if [ -n "$${found}" ]; then \
		prfx=`${PKG_INFO} -q -p "$${entry}"`; \
@@ -189,16 +200,16 @@ check-install-conflicts:

.if !target(do-package)
do-package: ${TMPPLIST}
-
	@if [ -d ${PACKAGES} ] then; \
+
	@if [ -d ${PACKAGES} ]; then \
		if [ ! -d ${PKGREPOSITORY} ]; then \
			if ! ${MKDIR} ${PKGREPOSITORY}; then \
				${ECHO_MSG} "=> Can't create directory ${PKGREPOSITORY}."; \
				exit 1; \
			fi; \
		fi; \
-
	fi; \
-
	@__softMAKEFLAGS="${__softMAKEFLAGS:S/'/'\''/g}'; \
-
	if ${PKG} create -o ${PKGFILE} ${PORTNAME}; then \
+
	fi;
+
	@__softMAKEFLAGS='${__softMAKEFLAGS:S/'/'\''/g}'; \
+
	if ${PKG_CREATE} -o ${PKGREPOSITORY} ${PORTNAME}; then \
		if [ -d ${PACKAGES} ]; then \
			cd ${.CURDIR} && eval ${MAKE} $${__softMAKEFLAGS} package-links; \
		fi; \
modified ports/pkg2ng
@@ -16,72 +16,42 @@ do
	COMMENT=$( cat ${DB}/+COMMENT )
	DESC="${DB}/+DESC"

+
	ORIGIN=$(pkg_info -qo ${PKG})
+
	MAINTAINER=$( make -C /usr/ports/${ORIGIN} -V MAINTAINER )
	# +CONTENTS
-
	PLIST=$( mktemp /tmp/pkg2ng.plist.XXXXXX )
-
	DEPENDS=$( mktemp /tmp/pkg2ng.depends.XXXXXX )
-
	eval $( awk -v pfile=${PLIST} -v dfile=${DEPENDS} '
-
	BEGIN{
-
		origin=""
-
		depends=""
-
		conflicts=""
-
	}
-
	{
-
		if ( $0 ~ /^@pkgdep/ ) {
-
			dep = $2
-
			getline
-
			orig=$2
-
			sub(/DEPORIGIN/,"",orig)
-
			print dep""orig >> dfile
-
		} else if ( $0 ~ /^@comment ORIGIN/ ) {
-
			origin=$2
-
			sub(/ORIGIN:/,"",origin)
-
		} else if ( $0 ~ /^[^@]/ ) {
-
			print $0 >> pfile
-
		} else if ( $0 ~ /^@exec/) {
-
			print $0 >> pfile
-
		} else if ( $0 ~ /^@unexec/) {
-
			print $0 >> pfile
-
		} else if ( $0 ~ /^@dirrm/) {
-
			print $0 >> pfile
-
		} else if ( $0 ~ /^@dirrmtry/) {
-
			print $0 >> pfile
-
		} else if ( $0 ~ /^@conflict/) {
-
			conflict=conflict" "$2
-
		} else if ( $0 ~ /^@cwd/) {
-
			print $0 >> pfile
-
		} else if ( $0 ~ /^@ignore/) {
-
			getline
-
		}
-
	}
-
	END{
-
		print "export ORIGIN=\""origin"\""
-
		print "export CONFLICTS=\""conflict"\""
-
	}
-
	' ${DB}/+CONTENTS )
+
	MDIR=$(mktemp -d /tmp/pkg2ngXXXXX)
+
	test -f ${DB}/+DISPLAY && cp -f ${DB}/+DISPLAY ${MDIR}
+
	test -f ${DB}/+MTREE_DIRS && cp -f ${DB}/+MTREE_DIRS ${MDIR}
+
	test -f ${DB}/+DESC && cp -f ${DB}/+DESC ${MDIR}
+
	MANIFEST=${MDIR}/+MANIFEST
+
	PLIST=${MDIR}/plist
+
	cp -f ${DB}/+INSTALL ${MDIR} 2>/dev/null
+
	echo "@pkg_format_version 0.9" >> ${MANIFEST}
+
	echo "@name ${PKG%-*}" >> ${MANIFEST}
+
	echo "@version ${PKG##*-}" >> ${MANIFEST}
+
	echo "@origin ${ORIGIN}" >> ${MANIFEST}
+
	echo "@comment ${COMMENT}" >> ${MANIFEST}
+
	echo "@maintainer ${MAINTAINER}" >> ${MANIFEST}
+
	echo "@prefix /usr/local" >> ${MANIFEST}
+
	pkg_info -qr ${PKG} | while read ignore dep; do
+
		deporigin=$(pkg_info -qo ${dep})
+
		echo "@dep ${dep%-*} ${deporigin} ${dep##*-}" >> ${MANIFEST}
+
	done
+
	egrep -v "^@[pkgdep|ignore|conflicts]" ${DB}/+CONTENTS >> ${PLIST}
+
	awk '/^@conflicts/ { print "@conflict "$2 }' ${DB}/+CONTENTS >> ${MANIFEST}

	OPTIONS=""
-
	SCRIPTS=$( ls ${DB}/+*INSTALL 2>/dev/null )
-

-
	MAINTAINER=$( make -C /usr/ports/${ORIGIN} -V MAINTAINER )

-
	CMD_ARGS="-l"
-
	test -f ${DB}/+MTREE_DIRS && CMD_ARGS="${CMD_ARGS} -m ${DB}/+MTREE_DIRS"
-
	test -f ${DB}/+DISPLAY && CMD_ARGS="${CMD_ARGS} -M ${DB}/+DISPLAY"
-
	test -n "${CONFLICTS}" && CMD_ARGS="${CMD_ARGS} -C ${CONFLICTS}"
-
	test -n "${SCRIPTS}" && CMD_ARGS="${CMD_ARGS} -s ${SCRIPTS}"
-
	test -n "${OPTIONS}" && CMD_ARGS="${CMD_ARGS} -O ${OPTIONS}"
-
		
	# register a package only if it's not registered already
	if pkg info -e ${ORIGIN}; then
		echo "package is already registered."
	else
-
		pkg register -c "${COMMENT}" -d ${DESC} -p /usr/local -n ${PKG} \
-
		-P "$( cat ${DEPENDS} )" \
-
		-o ${ORIGIN} -a ${ARCH} -r ${MAINTAINER} -f ${PLIST} ${CMD_ARGS}
-

-
		test $? -ne 0 && echo "failed." || echo "done."
+
		pkg register -l -m ${MDIR} -f ${PLIST} ${CMD_ARGS}
+
		test $? -ne 0 && echo "failed." || {
+
		echo "done." ; rm -rf ${MANIFEST}
+
		}
	fi

-
	rm ${PLIST} ${DEPENDS}
+
	rm -rf ${MDIR}
done

modified tests/manifest.c
@@ -17,12 +17,9 @@ char manifest[] = ""
	"@flatsize 10000\n"
	"@dep depfoo dep/foo 1.2\n"
	"@dep depbar dep/bar 3.4\n"
+
	"@hello world\n" /* unknown keyword should not be a problem */
	"@conflict foo-*\n"
	"@conflict bar-*\n"
-
	"@exec true && echo hello\n"
-
	"@exec false || echo world\n"
-
	"@unexec true && echo good\n"
-
	"@unexec false || echo bye\n"
	"@option foo true\n"
	"@option bar false\n"
	"@file /usr/local/bin/foo "
@@ -43,8 +40,6 @@ char wrong_manifest1[] = ""
	"@dep depbar dep/bar 3.4\n"
	"@conflict foo-*\n"
	"@conflict bar-*\n"
-
	"@exec true && echo hello\n"
-
	"@exec false || echo world\n"
	"@option foo true\n"
	"@option bar false\n";

@@ -63,8 +58,6 @@ char wrong_manifest2[] = ""
	"@dep depbar dep/bar 3.4\n"
	"@conflict foo-*\n"
	"@conflict bar-*\n"
-
	"@exec true && echo hello\n"
-
	"@exec false || echo world\n"
	"@option foo true\n"
	"@option bar false\n";

@@ -83,33 +76,11 @@ char wrong_manifest3[] = ""
	"@dep depbar dep/bar 3.4\n"
	"@conflict foo-*\n"
	"@conflict \n"
-
	"@exec true && echo hello\n"
-
	"@exec false || echo world\n"
-
	"@option foo true\n"
-
	"@option bar false\n";
-

-
/* bad exec line */
-
char wrong_manifest4[] = ""
-
	"@pkg_format_version 0.9\n"
-
	"@name foobar\n"
-
	"@version 0.3\n"
-
	"@origin foo/bar\n"
-
	"@comment A dummy manifest\n"
-
	"@arch amd64\n"
-
	"@osversion 800500\n"
-
	"@www http://www.foobar.com\n"
-
	"@maintainer test@pkgng.lan\n"
-
	"@dep depfoo dep/foo 1.2\n"
-
	"@dep depbar dep/bar 3.4\n"
-
	"@conflict foo-*\n"
-
	"@conflict bar-*\n"
-
	"@exec\n"
-
	"@exec false || echo world\n"
	"@option foo true\n"
	"@option bar false\n";

/* bad option line */
-
char wrong_manifest5[] = ""
+
char wrong_manifest4[] = ""
	"@pkg_format_version 0.9\n"
	"@name foobar\n"
	"@version 0.3\n"
@@ -123,13 +94,11 @@ char wrong_manifest5[] = ""
	"@dep depbar dep/bar 3.4\n"
	"@conflict foo-*\n"
	"@conflict bar-*\n"
-
	"@exec true && echo hello\n"
-
	"@exec false || echo world\n"
	"@option \n"
	"@option bar false\n";

/* bad option line */
-
char wrong_manifest6[] = ""
+
char wrong_manifest5[] = ""
	"@pkg_format_version 0.9\n"
	"@name foobar\n"
	"@version 0.3\n"
@@ -143,42 +112,20 @@ char wrong_manifest6[] = ""
	"@dep depbar dep/bar 3.4\n"
	"@conflict foo-*\n"
	"@conflict bar-*\n"
-
	"@exec true && echo hello\n"
-
	"@exec false || echo world\n"
	"@option foo true\n"
	"@option bar\n";

-
char wrong_manifest7[] = ""
-
	"@pkg_format_version 0.9\n"
-
	"@name foobar\n"
-
	"@version 0.3\n"
-
	"@origin foo/bar\n"
-
	"@comment A dummy manifest\n"
-
	"@arch amd64\n"
-
	"@osversion 800500\n"
-
	"@www http://www.foobar.com\n"
-
	"@maintainer test@pkgng.lan\n"
-
	"@dep depfoo dep/foo 1.2\n"
-
	"@dep depbar dep/bar 3.4\n"
-
	"@conflict foo-*\n"
-
	"@conflict bar-*\n"
-
	"@keyword bla\n"
-
	"@exec true && echo hello\n"
-
	"@exec false || echo world\n"
-
	"@option foo true\n";
-

START_TEST(parse_manifest)
{
-
	struct pkg *p;
+
	struct pkg *p = NULL;
	struct pkg **deps;
	struct pkg_conflict **conflicts;
-
	struct pkg_exec **execs;
	struct pkg_option **options;
	struct pkg_file **files;
	int i;

-
	fail_unless(pkg_new(&p) == 0);
-
	fail_unless(pkg_parse_manifest(p, manifest) == 0);
+
	fail_unless(pkg_new(&p, PKG_FILE) == EPKG_OK);
+
	fail_unless(pkg_parse_manifest(p, manifest) == EPKG_OK);

	fail_unless(strcmp(pkg_get(p, PKG_NAME), "foobar") == 0);
	fail_unless(strcmp(pkg_get(p, PKG_VERSION), "0.3") == 0);
@@ -215,30 +162,6 @@ START_TEST(parse_manifest)
	}
	fail_unless(i == 2);

-
	execs = pkg_execs(p);
-
	fail_if(execs == NULL);
-
	for (i = 0; execs[i] != NULL; i++) {
-
		switch (i) {
-
			case 0:
-
				fail_unless(pkg_exec_type(execs[i]) == PKG_EXEC);
-
				fail_unless(strcmp(pkg_exec_cmd(execs[i]), "true && echo hello") == 0);
-
				break;
-
			case 1:
-
				fail_unless(pkg_exec_type(execs[i]) == PKG_EXEC);
-
				fail_unless(strcmp(pkg_exec_cmd(execs[i]), "false || echo world") == 0);
-
				break;
-
			case 2:
-
				fail_unless(pkg_exec_type(execs[i]) == PKG_UNEXEC);
-
				fail_unless(strcmp(pkg_exec_cmd(execs[i]), "true && echo good") == 0);
-
				break;
-
			case 3:
-
				fail_unless(pkg_exec_type(execs[i]) == PKG_UNEXEC);
-
				fail_unless(strcmp(pkg_exec_cmd(execs[i]), "false || echo bye") == 0);
-
				break;
-
		}
-
	}
-
	fail_unless(i == 4);
-

	options = pkg_options(p);
	fail_if(options == NULL);
	for (i = 0; options[i] != NULL; i++) {
@@ -265,9 +188,8 @@ END_TEST

START_TEST(parse_wrong_manifest1)
{
-
	struct pkg *p;
+
	struct pkg *p = NULL;

-
	fail_unless(pkg_new(&p) == EPKG_OK);
	fail_unless(pkg_parse_manifest(p, wrong_manifest1) == EPKG_FATAL);

}
@@ -275,56 +197,36 @@ END_TEST

START_TEST(parse_wrong_manifest2)
{
-
	struct pkg *p;
+
	struct pkg *p = NULL;

-
	fail_unless(pkg_new(&p) == EPKG_OK);
	fail_unless(pkg_parse_manifest(p, wrong_manifest2) == EPKG_FATAL);
}
END_TEST

START_TEST(parse_wrong_manifest3)
{
-
	struct pkg *p;
+
	struct pkg *p = NULL;

-
	fail_unless(pkg_new(&p) == EPKG_OK);
	fail_unless(pkg_parse_manifest(p, wrong_manifest3) == EPKG_FATAL);
}
END_TEST

START_TEST(parse_wrong_manifest4)
{
-
	struct pkg *p;
+
	struct pkg *p = NULL;

-
	fail_unless(pkg_new(&p) == EPKG_OK);
	fail_unless(pkg_parse_manifest(p, wrong_manifest4) == EPKG_FATAL);
}
END_TEST

START_TEST(parse_wrong_manifest5)
{
-
	struct pkg *p;
+
	struct pkg *p = NULL;

-
	fail_unless(pkg_new(&p) == EPKG_OK);
	fail_unless(pkg_parse_manifest(p, wrong_manifest5) == EPKG_FATAL);
}
END_TEST

-
START_TEST(parse_wrong_manifest6)
-
{
-
	struct pkg *p;
-
	fail_unless(pkg_new(&p) == EPKG_OK);
-
	fail_unless(pkg_parse_manifest(p, wrong_manifest6) == EPKG_FATAL);
-
}
-
END_TEST
-

-
START_TEST(parse_wrong_manifest7)
-
{
-
	struct pkg *p;
-
	fail_unless(pkg_new(&p) == EPKG_OK);
-
	fail_unless(pkg_parse_manifest(p, wrong_manifest7) == EPKG_OK);
-
}
-
END_TEST
-

TCase *
tcase_manifest(void)
{
@@ -335,8 +237,6 @@ tcase_manifest(void)
	tcase_add_test(tc, parse_wrong_manifest3);
	tcase_add_test(tc, parse_wrong_manifest4);
	tcase_add_test(tc, parse_wrong_manifest5);
-
	tcase_add_test(tc, parse_wrong_manifest6);
-
	tcase_add_test(tc, parse_wrong_manifest7);

	return (tc);
}
modified tests/pkg.c
@@ -11,17 +11,15 @@ START_TEST(pkg_null)
	fail_unless(pkg_set_from_file(p, PKG_NAME, "path") == EPKG_FATAL);
	fail_unless(pkg_scripts(p) == NULL);
	fail_unless(pkg_deps(p) == NULL);
-
	fail_unless(pkg_execs(p) == NULL);
	fail_unless(pkg_options(p) == NULL);
	fail_unless(pkg_rdeps(p) == NULL);
	fail_unless(pkg_files(p) == NULL);
	fail_unless(pkg_conflicts(p) == NULL);
	fail_unless(pkg_addscript(p, "./bla") == EPKG_FATAL);
	fail_unless(pkg_addoption(p, "foo", "bar") == EPKG_FATAL);
-
	fail_unless(pkg_addexec(p, "bal", PKG_EXEC) == EPKG_FATAL);
	fail_unless(pkg_adddep(p, "foo", "foo/bar", "123") == EPKG_FATAL);

-
	fail_unless(pkg_new(&p) == EPKG_OK);
+
	fail_unless(pkg_new(&p, PKG_FILE) == EPKG_OK);
	fail_unless(pkg_set(p, PKG_NAME, NULL) == EPKG_FATAL);
	fail_unless(pkg_set_from_file(p, PKG_NAME, NULL) == EPKG_FATAL);
	fail_unless(pkg_open(&p, NULL) == EPKG_FATAL);
@@ -30,7 +28,6 @@ START_TEST(pkg_null)
	fail_unless(pkg_addscript(p, "./bla") == EPKG_FATAL);
	fail_unless(pkg_addoption(p, NULL, "bar") == EPKG_FATAL);
	fail_unless(pkg_addoption(p, "foo", NULL) == EPKG_FATAL);
-
	fail_unless(pkg_addexec(p, NULL, PKG_EXEC) == EPKG_FATAL);
	fail_unless(pkg_adddep(p, NULL, "foo/bar", "123") == EPKG_FATAL);
	fail_unless(pkg_adddep(p, "foo", NULL, "123") == EPKG_FATAL);
	/* currently disabled until we get code to test origin format and name