Radish alpha
H
HardenedBSD Package Manager
Radicle
Git (anonymous pull)
Log in to clone via SSH
Merge branch 'master' of etoilebsd.net:pkgng
Baptiste Daroussin committed 15 years ago
commit d96caa554a05fcf84803141797a8017eeb09e6cb
parent 6fb84eaae422d793cb29de4fdb64b2b4ee167f02
10 files changed +92 -106
modified libpkg/pkg.c
@@ -216,8 +216,8 @@ pkg_conflicts(struct pkg *pkg)
int
pkg_open(struct pkg **pkg_p, const char *path)
{
-
	struct archive *a;
-
	struct archive_entry *ae;
+
	struct archive *a = NULL;
+
	struct archive_entry *ae = NULL;
	int retcode;

	retcode = pkg_open2(pkg_p, &a, &ae, path);
@@ -255,10 +255,8 @@ pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae, con
	pkg = *pkg_p;
	pkg->type = PKG_FILE;

-
	if (archive_read_open_filename(*a, path, 4096) != ARCHIVE_OK) {
-
		archive_read_finish(*a);
+
	if (archive_read_open_filename(*a, path, 4096) != ARCHIVE_OK)
		return (pkg_error_set(EPKG_FATAL, "%s", archive_error_string(*a)));
-
	}

	array_init(&pkg->scripts, 10);
	array_init(&pkg->files, 10);
@@ -328,7 +326,7 @@ int
pkg_new(struct pkg **pkg)
{
	if ((*pkg = calloc(1, sizeof(struct pkg))) == NULL)
-
		return(pkg_error_seterrno());
+
		return(pkg_error_set(EPKG_FATAL, "%s", strerror(errno)));

	(*pkg)->fields[PKG_MESSAGE].optional = 1;
	(*pkg)->fields[PKG_WWW].optional = 1;
modified libpkg/pkg_add.c
@@ -34,8 +34,8 @@ extract(struct archive *a, struct archive_entry *ae)
int
pkg_add(struct pkgdb *db, const char *path)
{
-
	struct archive *a;
-
	struct archive_entry *ae;
+
	struct archive *a = NULL;
+
	struct archive_entry *ae = NULL;
	struct pkgdb_it *it;
	struct pkg *pkg = NULL;
	struct pkg *p = NULL;
modified libpkg/pkg_error.c
@@ -45,12 +45,6 @@ _pkg_error_set(pkg_error_t num, const char *fmt, ...)
}

pkg_error_t
-
pkg_error_seterrno(void)
-
{
-
	return (pkg_error_set(EPKG_FATAL, "%s", strerror(errno)));
-
}
-

-
pkg_error_t
pkg_error_number(void)
{
	struct pkg_error *e;
modified libpkg/pkg_error.h
@@ -15,6 +15,5 @@
	pkg_error_set(EPKG_FATAL, "%s (sqlite)", sqlite3_errmsg(db))

pkg_error_t _pkg_error_set(pkg_error_t, const char *, ...);
-
pkg_error_t pkg_error_seterrno(void);

#endif
modified libpkg/pkg_ports.c
@@ -25,6 +25,7 @@ ports_parse_plist(struct pkg *pkg, char *plist)
	struct stat st;
	int ret = EPKG_OK;
	off_t sz = 0;
+
	int64_t flatsize = 0;

	buf = NULL;
	p = NULL;
@@ -89,9 +90,10 @@ ports_parse_plist(struct pkg *pkg, char *plist)
			else
				snprintf(path, MAXPATHLEN, "%s/%s", prefix, buf);

-
			if (lstat(path, &st) >= 0)
+
			if (lstat(path, &st) >= 0) {
				p = S_ISLNK(st.st_mode) ? NULL : SHA256_File(path, sha256);
-
			else {
+
				flatsize += st.st_size;
+
			} else {
				warn("lstat(%s)", path);
				p = NULL;
			}
@@ -105,6 +107,8 @@ ports_parse_plist(struct pkg *pkg, char *plist)
		}
	}

+
	pkg_setflatsize(pkg, flatsize);
+

	free(plist_buf);

	return (ret);
modified libpkg/pkg_repo.c
@@ -1,5 +1,7 @@
#include <sys/types.h>
#include <sys/stat.h>
+

+
#include <errno.h>
#include <fnmatch.h>
#include <sqlite3.h>
#include <fts.h>
@@ -77,7 +79,8 @@ pkg_create_repo(char *path, void (progress)(struct pkg *pkg, void *data), void *

	if (stat(repodb, &st) != -1)
		if (unlink(repodb) != 0)
-
			return (pkg_error_seterrno());
+
			return (pkg_error_set(EPKG_FATAL, "can not unlink %s: %s", repodb,
+
								  strerror(errno)));

	if (sqlite3_open(repodb, &sqlite) != SQLITE_OK)
		return (EPKG_FATAL);
@@ -104,7 +107,8 @@ pkg_create_repo(char *path, void (progress)(struct pkg *pkg, void *data), void *
	}

	if ((fts = fts_open(repopath, FTS_PHYSICAL, NULL)) == NULL) {
-
		retcode = pkg_error_seterrno();
+
		retcode = pkg_error_set(EPKG_FATAL, "can not open %s: %s", repopath,
+
								strerror(errno));
		goto cleanup;
	}

modified libpkg/pkg_util.c
@@ -5,6 +5,7 @@
#include <assert.h>
#include <dirent.h>
#include <err.h>
+
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
@@ -125,22 +126,23 @@ file_to_buffer(const char *path, char **buffer, off_t *sz)
		return (ERROR_BAD_ARG("buffer"));

	if ((fd = open(path, O_RDONLY)) == -1) {
-
		return (pkg_error_seterrno());
+
		return (pkg_error_set(EPKG_FATAL, "can not open %s: %s", path,
+
				strerror(errno)));
	}

	if (fstat(fd, &st) == -1) {
		close(fd);
-
		return (pkg_error_seterrno());
+
		return (pkg_error_set(EPKG_FATAL, "fstat(): %s", strerror(errno)));
	}

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

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

	close(fd);
modified libpkg/pkgdb.c
@@ -199,10 +199,11 @@ pkgdb_open(struct pkgdb **db)
	snprintf(fpath, sizeof(fpath), "%s/pkg.db", pkgdb_get_dir());

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

	if ((retcode = stat(fpath, &st)) == -1 && errno != ENOENT)
-
		return (pkg_error_seterrno());
+
		return (pkg_error_set(EPKG_FATAL, "can not stat %s: %s", fpath,
+
							  strerror(errno)));

	if (sqlite3_open(fpath, &(*db)->sqlite) != SQLITE_OK)
		return (ERROR_SQLITE((*db)->sqlite));
@@ -249,7 +250,7 @@ pkgdb_it_new(struct pkgdb *db, sqlite3_stmt *s, pkgdb_it_t t)
	struct pkgdb_it *it;

	if ((it = malloc(sizeof(struct pkgdb_it))) == NULL) {
-
		pkg_error_seterrno();
+
		pkg_error_set(EPKG_FATAL, "malloc(): %s", strerror(errno));
		sqlite3_finalize(s);
		return (NULL);
	}
@@ -916,14 +917,14 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
	struct pkg_option **options;

	sqlite3 *s;
-
	sqlite3_stmt *stmt_pkg;
-
	sqlite3_stmt *stmt_mtree;
-
	sqlite3_stmt *stmt_dep;
-
	sqlite3_stmt *stmt_conflict;
-
	sqlite3_stmt *stmt_file;
-
	sqlite3_stmt *stmt_exec;
-
	sqlite3_stmt *stmt_script;
-
	sqlite3_stmt *stmt_option;
+
	sqlite3_stmt *stmt_pkg = NULL;
+
	sqlite3_stmt *stmt_mtree = NULL;
+
	sqlite3_stmt *stmt_dep = NULL;
+
	sqlite3_stmt *stmt_conflict = NULL;
+
	sqlite3_stmt *stmt_file = NULL;
+
	sqlite3_stmt *stmt_exec = NULL;
+
	sqlite3_stmt *stmt_script = NULL;
+
	sqlite3_stmt *stmt_option = NULL;

	int i;
	int ret;
@@ -977,7 +978,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
	if (sqlite3_prepare(s, sql_mtree, -1, &stmt_mtree, NULL) !=
						SQLITE_OK) {
		retcode = ERROR_SQLITE(s);
-
		goto error;
+
		goto cleanup;
	}

	sqlite3_bind_text(stmt_mtree, 1, mtree_id, -1, SQLITE_STATIC);
@@ -987,15 +988,15 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
	sqlite3_finalize(stmt_mtree);
	if (ret != SQLITE_DONE) {
		retcode = ERROR_SQLITE(s);
-
		goto error;
+
		goto cleanup;
	}

	/*
	 * Insert package record
	 */
	if (sqlite3_prepare(s, sql_pkg, -1, &stmt_pkg, NULL) != SQLITE_OK) {
-
		retcode = ERROR_SQLITE(db->sqlite);
-
		goto error;
+
		retcode = ERROR_SQLITE(s);
+
		goto cleanup;
	}

	sqlite3_bind_text(stmt_pkg, 1, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);
@@ -1012,8 +1013,10 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
	sqlite3_bind_text(stmt_pkg, 12, pkg_get(pkg, PKG_PREFIX), -1, SQLITE_STATIC);
	sqlite3_bind_int64(stmt_pkg, 13, pkg_flatsize(pkg));

-
	sqlite3_step(stmt_pkg);
-
	sqlite3_finalize(stmt_pkg);
+
	if (sqlite3_step(stmt_pkg) != SQLITE_DONE) {
+
		retcode = ERROR_SQLITE(s);
+
		goto cleanup;
+
	}

	/*
	 * Insert dependencies list
@@ -1021,7 +1024,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)

	if (sqlite3_prepare(s, sql_dep, -1, &stmt_dep, NULL) != SQLITE_OK) {
		retcode = ERROR_SQLITE(s);
-
		goto error;
+
		goto cleanup;
	}

	deps = pkg_deps(pkg);
@@ -1032,14 +1035,12 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
		sqlite3_bind_text(stmt_dep, 4, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);

		if (sqlite3_step(stmt_dep) != SQLITE_DONE) {
-
			sqlite3_finalize(stmt_dep);
			retcode = ERROR_SQLITE(s);
-
			goto error;
+
			goto cleanup;
		}

		sqlite3_reset(stmt_dep);
	}
-
	sqlite3_finalize(stmt_dep);

	/*
	 * Insert conflicts list
@@ -1048,7 +1049,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
	if (sqlite3_prepare(s, sql_conflict, -1, &stmt_conflict, NULL) !=
						SQLITE_OK) {
		retcode = ERROR_SQLITE(s);
-
		goto error;
+
		goto cleanup;
	}

	conflicts = pkg_conflicts(pkg);
@@ -1057,23 +1058,20 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
		sqlite3_bind_text(stmt_conflict, 2, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);

		if (sqlite3_step(stmt_conflict) != SQLITE_DONE) {
-
			sqlite3_finalize(stmt_conflict);
			retcode = ERROR_SQLITE(s);
-
			goto error;
+
			goto cleanup;
		}

		sqlite3_reset(stmt_conflict);
	}

-
	sqlite3_finalize(stmt_conflict);
-

	/*
	 * Insert file
	 */

	if (sqlite3_prepare(s, sql_file, -1, &stmt_file, NULL) != SQLITE_OK) {
		retcode = ERROR_SQLITE(s);
-
		goto error;
+
		goto cleanup;
	}

	files = pkg_files(pkg);
@@ -1082,24 +1080,25 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
		sqlite3_bind_text(stmt_file, 2, pkg_file_sha256(files[i]), -1, SQLITE_STATIC);
		sqlite3_bind_text(stmt_file, 3, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);

-
		if (sqlite3_step(stmt_file) != SQLITE_DONE) {
-
			sqlite3_finalize(stmt_file);
-
			retcode = ERROR_SQLITE(s);
-
			goto error;
+
		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);
	}

-
	sqlite3_finalize(stmt_file);
-

	/*
	 * Insert scripts
	 */

	if (sqlite3_prepare(s, sql_script, -1, &stmt_script, NULL) != SQLITE_OK) {
		retcode = ERROR_SQLITE(s);
-
		goto error;
+
		goto cleanup;
	}

	scripts = pkg_scripts(pkg);
@@ -1109,23 +1108,20 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
		sqlite3_bind_text(stmt_script, 3, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);

		if (sqlite3_step(stmt_script) != SQLITE_DONE) {
-
			sqlite3_finalize(stmt_script);
			retcode = ERROR_SQLITE(s);
-
			goto error;
+
			goto cleanup;
		}

		sqlite3_reset(stmt_script);
	}

-
	sqlite3_finalize(stmt_script);
-

	/*
	 * Insert execs
	 */

	if (sqlite3_prepare(s, sql_exec, -1, &stmt_exec, NULL) != SQLITE_OK) {
		retcode = ERROR_SQLITE(s);
-
		goto error;
+
		goto cleanup;
	}

	execs = pkg_execs(pkg);
@@ -1135,16 +1131,13 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
		sqlite3_bind_text(stmt_exec, 3, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);

		if (sqlite3_step(stmt_exec) != SQLITE_DONE) {
-
			sqlite3_finalize(stmt_exec);
			retcode = ERROR_SQLITE(s);
-
			goto error;
+
			goto cleanup;
		}

		sqlite3_reset(stmt_exec);
	}

-
	sqlite3_finalize(stmt_exec);
-

	/*
	 * Insert options
	 */
@@ -1152,7 +1145,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
	options = pkg_options(pkg);
	if (sqlite3_prepare(s, sql_option, -1, &stmt_option, NULL) != SQLITE_OK) {
		retcode = ERROR_SQLITE(s);
-
		goto error;
+
		goto cleanup;
	}

	for (i = 0; options[i] != NULL; i++) {
@@ -1161,32 +1154,48 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
		sqlite3_bind_text(stmt_option, 3, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);

		if (sqlite3_step(stmt_option) != SQLITE_DONE) {
-
			sqlite3_finalize(stmt_option);
			retcode = ERROR_SQLITE(s);
-
			goto error;
+
			goto cleanup;
		}

		sqlite3_reset(stmt_option);
	}

-
	sqlite3_finalize(stmt_option);
-

	/*
	 * Register the package for real
	 */
	if (sqlite3_exec(s, "COMMIT;", NULL, NULL, &errmsg) != SQLITE_OK) {
-
		pkg_error_set(EPKG_FATAL, "sqlite: %s", errmsg);
+
		retcode = pkg_error_set(EPKG_FATAL, "sqlite: %s", errmsg);
		sqlite3_free(errmsg);
-
		return (EPKG_FATAL);
	}

-
	return (EPKG_OK);
+
	cleanup:
+
	if (retcode != EPKG_OK && sqlite3_exec(db->sqlite, "ROLLBACK;", NULL, NULL, &errmsg) !=
+
		SQLITE_OK)
+
		err(1, "Can not rollback: %s", errmsg);
+

+
	if (stmt_pkg != NULL)
+
		sqlite3_finalize(stmt_pkg);
+

+
	if (stmt_dep != NULL)
+
		sqlite3_finalize(stmt_dep);
+

+
	if (stmt_conflict != NULL)
+
		sqlite3_finalize(stmt_conflict);

-
	error:
-
		if (sqlite3_exec(db->sqlite, "ROLLBACK;", NULL, NULL, &errmsg) !=
-
						 SQLITE_OK)
-
			err(1, "Critical error: %s", errmsg);
-
		return (retcode);
+
	if (stmt_file != NULL)
+
		sqlite3_finalize(stmt_file);
+

+
	if (stmt_script != NULL)
+
		sqlite3_finalize(stmt_script);
+

+
	if (stmt_exec != NULL)
+
		sqlite3_finalize(stmt_exec);
+

+
	if (stmt_option != NULL)
+
		sqlite3_finalize(stmt_option);
+

+
	return (retcode);
}

int
modified pkg/register.c
@@ -13,8 +13,6 @@

#include "register.h"

-
static void compute_flatsize(struct pkg *pkg);
-

static struct {
	pkg_attr attr;
	const char *flag;
@@ -25,7 +23,6 @@ static struct {
	{ PKG_COMMENT, "-c"},
	{ PKG_DESC, "-d"},
	{ PKG_PREFIX, "-p"},
-
	{ PKG_MTREE, "-m"},
	{ PKG_MAINTAINER, "-r"},
	{ 0, NULL}
};
@@ -195,8 +192,6 @@ exec_register(int argc, char **argv)
	if (heuristic)
		pkg_analyse_files(db, pkg);

-
	compute_flatsize(pkg);
-

	if (pkgdb_register_pkg(db, pkg) != EPKG_OK) {
		pkg_error_warn("can not register package");
		retcode = 1;
@@ -207,22 +202,3 @@ exec_register(int argc, char **argv)

	return (retcode);
}
-

-
static void
-
compute_flatsize(struct pkg *pkg)
-
{
-
	struct pkg_file **files;
-
	struct stat st;
-
	int64_t size = 0;
-

-
	files = pkg_files(pkg);
-
	for (int i = 0; files[i] != NULL; i++) {
-
		if (stat(pkg_file_path(files[i]), &st) != 0) {
-
			warn("stat(%s)", pkg_file_path(files[i]));
-
			continue;
-
		}
-
		size += st.st_size;
-
	}
-

-
	pkg_setflatsize(pkg, size);
-
}
modified ports/pkg2ng
@@ -58,12 +58,12 @@ do
	test -n "${DEPENDS}" && CMD_ARGS="${CMD_ARGS} -P ${DEPENDS}"
	test -n "${SCRIPTS}" && CMD_ARGS="${CMD_ARGS} -s ${SCRIPTS}"
	test -n "${OPTIONS}" && CMD_ARGS="${CMD_ARGS} -O ${OPTIONS}"
+
	test -f "${MTREE}" && CMD_ARGS="${CMD_ARGS} -m ${MTREE}"
	test -f "${MESSAGE}" && CMD_ARGS="${CMD_ARGS} -M ${MESSAGE}"
	test -n "${CONFLICTS}" && CMD_ARGS="${CMD_ARGS} -C ${CONFLICTS}"

-
	pkg register -c "${COMMENT}" -d ${DESC} -p /usr/local -m ${MTREE} \
-
	-n ${PKG} -o ${ORIGIN} -a ${ARCH} \
-
	-r ${MAINTAINER} -f ${PLIST} ${CMD_ARGS}
+
	pkg register -c "${COMMENT}" -d ${DESC} -p /usr/local -n ${PKG} \
+
	-o ${ORIGIN} -a ${ARCH} -r ${MAINTAINER} -f ${PLIST} ${CMD_ARGS}

	rm ${PLIST}