Radish alpha
H
HardenedBSD Package Manager
Radicle
Git (anonymous pull)
Log in to clone via SSH
pkg_repo() now report errors.
jlaffaye committed 15 years ago
commit 436c148132cde20c18c283026810a2c548cef074
parent 99a30d627a88190e5ebb7a2a689ecc47b3840056
4 files changed +99 -63
modified libpkg/pkg.h
@@ -121,7 +121,7 @@ typedef enum {
	 * No more items available (end of the loop).
	 */
	EPKG_END,
-
	EPKG_WARNING,
+
	EPKG_WARN,
	/**
	 * The function encountered a fatal error.
	 */
modified libpkg/pkg_error.c
@@ -25,12 +25,12 @@ pkg_error_t
_pkg_error_set(pkg_error_t num, const char *fmt, ...)
{
	struct pkg_error *e;
+
	char *oldstring;
	va_list ap;

	e = pkg_error_init();

-
	if (e->string != NULL)
-
		free(e->string);
+
	oldstring = e->string;

	e->number = num;

@@ -38,6 +38,9 @@ _pkg_error_set(pkg_error_t num, const char *fmt, ...)
	vasprintf(&e->string, fmt, ap);
	va_end(ap);

+
	if (oldstring != NULL)
+
		free(oldstring);
+

	return (num);
}

modified libpkg/pkg_repo.c
@@ -8,6 +8,7 @@
#include <string.h>

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

@@ -16,16 +17,18 @@
int
pkg_create_repo(char *path, void (progress)(struct pkg *pkg, void *data), void *data)
{
-
	FTS	*fts;
-
	FTSENT	*ent;
+
	FTS *fts = NULL;
+
	FTSENT *ent = NULL;

	struct stat st;
	struct pkg *pkg = NULL;
	struct pkg **deps;
	char *ext = NULL;
-
	sqlite3 *sqlite;
-
	sqlite3_stmt *stmt_deps;
-
	sqlite3_stmt *stmt_pkg;
+
	sqlite3 *sqlite = NULL;
+
	sqlite3_stmt *stmt_deps = NULL;
+
	sqlite3_stmt *stmt_pkg = NULL;
+
	char *errmsg = NULL;
+
	int retcode = EPKG_OK;

	int i;

@@ -55,12 +58,19 @@ pkg_create_repo(char *path, void (progress)(struct pkg *pkg, void *data), void *
			"PRIMARY KEY (package_id, origin)"
		");"
		"CREATE INDEX deps_origin ON deps (origin);"
-
		"CREATE INDEX deps_package ON deps (package_id);"
-
		;
-

+
		"CREATE INDEX deps_package ON deps (package_id);";
+
	const char pkgsql[] = ""
+
		"INSERT INTO packages ("
+
				"origin, name, version, comment, desc, arch, osversion, "
+
				"maintainer, www, pkg_format_version, pkgsize, flatsize "
+
		")"
+
		"VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12);";
+
	const char depssql[] = ""
+
		"INSERT INTO deps (origin, name, version, package_id) "
+
		"VALUES (?1, ?2, ?3, ?4);";

	if (!is_dir(path))
-
		return (EPKG_FATAL);
+
		return (pkg_error_set(EPKG_FATAL, "%s is not a directory", path));

	repopath[0] = path;
	repopath[1] = NULL;
@@ -69,25 +79,36 @@ pkg_create_repo(char *path, void (progress)(struct pkg *pkg, void *data), void *

	if (stat(repodb, &st) != -1)
		if (unlink(repodb) != 0)
-
			return (EPKG_FATAL);
+
			return (pkg_error_seterrno());

	if (sqlite3_open(repodb, &sqlite) != SQLITE_OK)
		return (EPKG_FATAL);

+
	if (sqlite3_exec(sqlite, initsql, NULL, NULL, &errmsg) != SQLITE_OK) {
+
		retcode = pkg_error_set(EPKG_FATAL, "%s", errmsg);
+
		goto cleanup;
+
	}

-
	if (sqlite3_exec(sqlite, initsql, NULL, NULL, NULL) != SQLITE_OK)
-
		return (EPKG_FATAL);
+
	if (sqlite3_exec(sqlite, "BEGIN TRANSACTION;", NULL, NULL, &errmsg) !=
+
		SQLITE_OK) {
+
		retcode = pkg_error_set(EPKG_FATAL, "%s", errmsg);
+
		goto cleanup;
+
	}

-
	sqlite3_exec(sqlite, "BEGIN TRANSACTION;", NULL, NULL, NULL);
+
	if (sqlite3_prepare(sqlite, pkgsql, -1, &stmt_pkg, NULL) != SQLITE_OK) {
+
		retcode = ERROR_SQLITE(sqlite);
+
		goto cleanup;
+
	}

-
	sqlite3_prepare(sqlite, "INSERT INTO packages (origin, name, version, comment, desc, arch, osversion, maintainer, www, pkg_format_version, pkgsize, flatsize) "
-
			"values (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12);",
-
			-1, &stmt_pkg, NULL);
-
	sqlite3_prepare(sqlite, "INSERT INTO deps (origin, name, version, package_id) "
-
			"values (?1, ?2, ?3, ?4);",
-
			-1, &stmt_deps, NULL);
+
	if (sqlite3_prepare(sqlite, depssql, -1, &stmt_deps, NULL) != SQLITE_OK) {
+
		retcode = ERROR_SQLITE(sqlite);
+
		goto cleanup;
+
	}

-
	fts = fts_open(repopath, FTS_PHYSICAL, NULL);
+
	if ((fts = fts_open(repopath, FTS_PHYSICAL, NULL)) == NULL) {
+
		retcode = pkg_error_seterrno();
+
		goto cleanup;
+
	}

	while ((ent = fts_read(fts)) != NULL) {
		/* skip everything that is not a file */
@@ -95,14 +116,24 @@ pkg_create_repo(char *path, void (progress)(struct pkg *pkg, void *data), void *
			continue;

		ext = strrchr(ent->fts_name, '.');
+

+
		if (ext == NULL)
+
			continue;
+

		if (strcmp(ext, ".tgz") != 0 &&
				strcmp(ext, ".tbz") != 0 &&
				strcmp(ext, ".txz") != 0 &&
				strcmp(ext, ".tar") != 0)
			continue;

-
		if (pkg_open(&pkg, ent->fts_path) != EPKG_OK)
+
		if (pkg_open(&pkg, ent->fts_path) != EPKG_OK) {
+
			if (progress != NULL) {
+
				pkg_error_set(EPKG_WARN, "can not open %s: %s", ent->fts_name,
+
							  pkg_error_string());
+
				progress(NULL, data);
+
			}
			continue;
+
		}

		if (progress != NULL)
			progress(pkg, data);
@@ -119,34 +150,49 @@ pkg_create_repo(char *path, void (progress)(struct pkg *pkg, void *data), void *
		sqlite3_bind_int64(stmt_pkg, 11, ent->fts_statp->st_size);
		sqlite3_bind_int64(stmt_pkg, 12, pkg_flatsize(pkg));

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

-
		if ((deps = pkg_deps(pkg)) != NULL) {
-
			for (i = 0; deps[i] != NULL; i++) {
-
				sqlite3_bind_text(stmt_deps, 1, pkg_get(deps[i], PKG_ORIGIN), -1, SQLITE_STATIC);
-
				sqlite3_bind_text(stmt_deps, 2, pkg_get(deps[i], PKG_NAME), -1, SQLITE_STATIC);
-
				sqlite3_bind_text(stmt_deps, 3, pkg_get(deps[i], PKG_VERSION), -1, SQLITE_STATIC);
-
				sqlite3_bind_text(stmt_deps, 4, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);
+
		deps = pkg_deps(pkg);
+
		for (i = 0; deps[i] != NULL; i++) {
+
			sqlite3_bind_text(stmt_deps, 1, pkg_get(deps[i], PKG_ORIGIN), -1, SQLITE_STATIC);
+
			sqlite3_bind_text(stmt_deps, 2, pkg_get(deps[i], PKG_NAME), -1, SQLITE_STATIC);
+
			sqlite3_bind_text(stmt_deps, 3, pkg_get(deps[i], PKG_VERSION), -1, SQLITE_STATIC);
+
			sqlite3_bind_text(stmt_deps, 4, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);

-
				sqlite3_step(stmt_deps);
-
				sqlite3_reset(stmt_deps);
+
			if (sqlite3_step(stmt_deps) != SQLITE_DONE) {
+
				retcode = ERROR_SQLITE(sqlite);
+
				goto cleanup;
			}
+
			sqlite3_reset(stmt_deps);
		}

	}
-
	fts_close(fts);
-
	pkg_free(pkg);

-
	sqlite3_finalize(stmt_pkg);
-
	sqlite3_finalize(stmt_deps);
+
	if (sqlite3_exec(sqlite, "COMMIT;", NULL, NULL, &errmsg) != SQLITE_OK)
+
		retcode = pkg_error_set(EPKG_FATAL, "%s", errmsg);
+

+
	cleanup:
+
	if (fts != NULL)
+
		fts_close(fts);
+

+
	if (pkg != NULL)
+
		pkg_free(pkg);
+

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

-
	sqlite3_exec(sqlite, "COMMIT;", NULL, NULL, NULL);
+
	if (stmt_deps != NULL)
+
		sqlite3_finalize(stmt_deps);

-
	sqlite3_close(sqlite);
+
	if (sqlite != NULL)
+
		sqlite3_close(sqlite);

-
	if (progress != NULL)
-
		progress(NULL, data);
+
	if (errmsg != NULL)
+
		sqlite3_free(errmsg);

-
	return (EPKG_OK);
+
	return (retcode);
}
modified pkg/repo.c
@@ -14,40 +14,27 @@ usage_repo(void)

static void
progress(struct pkg *pkg, void *data) {
+
	(void)data;

-
	char buf[BUFSIZ], pattern[BUFSIZ];
-
	int *len = (int *)data;
-
	int newlen;
-

-
	if (pkg != NULL) {
-
		snprintf(buf, BUFSIZ, "%s->%s", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
-
		newlen = strlen(buf);
-

-
		if (newlen > *len) {
-
			*len = newlen;
-
		}
-

-
		snprintf(pattern, BUFSIZ, "\rAdding: %%-%ds", *len);
-

-
		printf(pattern, buf);
-
	} else {
-
		snprintf(pattern, BUFSIZ, "\rdone: %%-%ds", *len);
-
		printf(pattern, "");
-
	}
+
	if (pkg != NULL)
+
		printf("%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
+
	else
+
		pkg_error_warn("");
}

int
exec_repo(int argc, char **argv)
{
	int ret;
-
	int len = 0;

	if (argc != 2) {
		usage_repo();
		return (EX_USAGE);
	}

-
	ret = pkg_create_repo(argv[1], progress, &len);
+
	printf("Generating repo.db in %s\n", argv[1]);
+
	ret = pkg_create_repo(argv[1], progress, NULL);
+
	printf("Done!\n");

	if (ret != EPKG_OK)
		pkg_error_warn("can not create repository");