Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
add ability to attache the remote database at pkgdb_open
Baptiste Daroussin committed 15 years ago
commit 046ef408163af908162a877500f0d89ebc9600ea
parent ea478f0
10 files changed +50 -34
modified libpkg/pkg.h
@@ -13,6 +13,11 @@ struct pkg_script;
struct pkgdb;
struct pkgdb_it;

+
typedef enum {
+
	PKGDB_DEFAULT=0,
+
	PKGDB_REMOTE
+
} pkgdb_t;
+

/**
 * Specify how an argument should be used by matching functions.
 */
@@ -389,7 +394,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);
+
int pkgdb_open(struct pkgdb **db, pkgdb_t remote);

/**
 * Close and free the struct pkgdb.
modified libpkg/pkgdb.c
@@ -226,25 +226,45 @@ pkgdb_init(sqlite3 *sdb)
}

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

-
	snprintf(fpath, sizeof(fpath), "%s/local.sqlite", pkgdb_get_dir());
+
	snprintf(localpath, sizeof(localpath), "%s/local.sqlite", pkgdb_get_dir());

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

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

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

-
	if (sqlite3_open(fpath, &(*db)->sqlite) != SQLITE_OK)
+
	if (remote == PKGDB_REMOTE) {
+
		snprintf(remotepath, sizeof(localpath), "%s/repo.sqlite", pkgdb_get_dir());
+
		if ((retcode = stat(remotepath, &st)) == -1 && errno != ENOENT)
+
			return (pkg_error_set(EPKG_FATAL, "can not stat %s: %s", remotepath,
+
						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 (sqlite3_exec((*db)->sqlite, sql, NULL, NULL, &errmsg) != SQLITE_OK){
+
			sqlite3_free(errmsg);
+
			return ERROR_SQLITE((*db)->sqlite);
+
		}
+
	}
+

	/* If the database is missing we have to initialize it */
	if (retcode == -1)
		if ((retcode = pkgdb_init((*db)->sqlite)) != EPKG_OK)
@@ -279,8 +299,12 @@ pkgdb_close(struct pkgdb *db)
	if (db == NULL)
		return;

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

		sqlite3_close(db->sqlite);
+
	}

	free(db);
}
@@ -374,9 +398,6 @@ pkgdb_it_next(struct pkgdb_it *it, struct pkg **pkg_p, int flags)

		return (EPKG_OK);
	case SQLITE_DONE:
-
		if (flags & PKG_LOAD_NEWVERSION)
-
			sqlite3_exec(it->db->sqlite, "DETACH remote;", NULL, NULL, NULL);
-

		return (EPKG_END);
	default:
		return (ERROR_SQLITE(it->db->sqlite));
@@ -1245,9 +1266,10 @@ pkgdb_compact(struct pkgdb *db)
struct pkgdb_it *
pkgdb_query_upgrades(struct pkgdb *db)
{
+
	if (db->remote != PKGDB_REMOTE)
+
		return (NULL);

	sqlite3_stmt *stmt;
-
	char *errmsg;

	const char sql[] = ""
		"SELECT l.id, l.origin, l.name, l.version, l.comment, l.desc, "
@@ -1258,13 +1280,6 @@ pkgdb_query_upgrades(struct pkgdb *db)
		"WHERE l.origin = r.origin "
		"AND PKGLT(l.version, r.version)";

-

-
	if (sqlite3_exec(db->sqlite, "ATTACH \"/var/db/pkg/repo.sqlite\" as remote;", NULL, NULL, &errmsg) != SQLITE_OK){
-
		pkg_error_set(EPKG_FATAL, "%s", errmsg);
-
		sqlite3_free(errmsg);
-
		return (NULL);
-
	}
-

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

@@ -1276,7 +1291,9 @@ pkgdb_query_downgrades(struct pkgdb *db)
{

	sqlite3_stmt *stmt;
-
	char *errmsg;
+

+
	if (db->remote != PKGDB_REMOTE)
+
		return (NULL);

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

-

-
	if (sqlite3_exec(db->sqlite, "ATTACH \"/var/db/pkg/repo.sqlite\" as remote;", NULL, NULL, &errmsg) != SQLITE_OK){
-
		pkg_error_set(EPKG_FATAL, "%s", errmsg);
-
		sqlite3_free(errmsg);
-
		return (NULL);
-
	}
-

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

modified libpkg/pkgdb.h
@@ -7,6 +7,7 @@

struct pkgdb {
	sqlite3 *sqlite;
+
	pkgdb_t remote;
};

struct pkgdb_it {
modified pkg/add.c
@@ -67,7 +67,7 @@ exec_add(int argc, char **argv)
		return (EX_NOPERM);
	}

-
	if (pkgdb_open(&db) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		pkg_error_warn("can not open database");
		return (1);
	}
modified pkg/create.c
@@ -107,7 +107,7 @@ exec_create(int argc, char **argv)

	if (manifestdir == NULL) {
		/* create package from local db */
-
		if (pkgdb_open(&db) != 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) != 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
@@ -164,7 +164,7 @@ exec_info(int argc, char **argv)

	pkg_new(&pkg);

-
	if (pkgdb_open(&db) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		pkg_error_warn("can not open database");
		pkgdb_close(db);
		return (-1);
modified pkg/register.c
@@ -198,7 +198,7 @@ exec_register(int argc, char **argv)
	if (plist != NULL)
		free(plist);

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


-
	if (pkgdb_open(&db) != 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) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
		pkg_error_warn("can not open database");
		pkgdb_close(db);
		return (-1);