Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Fix malformed database in case pkg update is interrupted
Baptiste Daroussin committed 12 years ago
commit d36fbe6434b8e7d1b45a7ac9cc19b5492e5cb083
parent 492184f
4 files changed +19 -15
modified libpkg/pkgdb.c
@@ -367,7 +367,7 @@ pkgdb_upgrade(struct pkgdb *db)

	assert(db != NULL);

-
	ret = get_pragma(db->sqlite, "PRAGMA user_version;", &db_version);
+
	ret = get_pragma(db->sqlite, "PRAGMA user_version;", &db_version, false);
	if (ret != EPKG_OK)
		return (EPKG_FATAL);

@@ -3071,7 +3071,7 @@ pkgdb_detach_remotes(sqlite3 *s)
}

int
-
get_pragma(sqlite3 *s, const char *sql, int64_t *res)
+
get_pragma(sqlite3 *s, const char *sql, int64_t *res, bool silence)
{
	sqlite3_stmt	*stmt;
	int		 ret;
@@ -3080,7 +3080,8 @@ get_pragma(sqlite3 *s, const char *sql, int64_t *res)

	pkg_debug(4, "Pkgdb: running '%s'", sql);
	if (sqlite3_prepare_v2(s, sql, -1, &stmt, NULL) != SQLITE_OK) {
-
		ERROR_SQLITE(s);
+
		if (!silence)
+
			ERROR_SQLITE(s);
		return (EPKG_OK);
	}

@@ -3092,7 +3093,8 @@ get_pragma(sqlite3 *s, const char *sql, int64_t *res)
	sqlite3_finalize(stmt);

	if (ret != SQLITE_ROW) {
-
		ERROR_SQLITE(s);
+
		if (!silence)
+
			ERROR_SQLITE(s);
		return (EPKG_FATAL);
	}

@@ -3143,12 +3145,12 @@ pkgdb_compact(struct pkgdb *db)

	assert(db != NULL);

-
	ret = get_pragma(db->sqlite, "PRAGMA page_count;", &page_count);
+
	ret = get_pragma(db->sqlite, "PRAGMA page_count;", &page_count, false);
	if (ret != EPKG_OK)
		return (EPKG_FATAL);

	ret = get_pragma(db->sqlite, "PRAGMA freelist_count;",
-
			 &freelist_count);
+
			 &freelist_count, false);
	if (ret != EPKG_OK)
		return (EPKG_FATAL);

modified libpkg/private/pkg.h
@@ -379,7 +379,7 @@ int pkg_set_mtree(struct pkg *, const char *mtree);

/* pkgdb commands */
int sql_exec(sqlite3 *, const char *, ...);
-
int get_pragma(sqlite3 *, const char *sql, int64_t *res);
+
int get_pragma(sqlite3 *, const char *sql, int64_t *res, bool silence);
int get_sql_string(sqlite3 *, const char *sql, char **res);

int pkgdb_load_deps(struct pkgdb *db, struct pkg *pkg);
modified libpkg/private/utils.h
@@ -41,7 +41,7 @@
#define STARTS_WITH(string, needle) (strncasecmp(string, needle, strlen(needle)) == 0)

#define ERROR_SQLITE(db) \
-
	pkg_emit_error("sqlite: %s (%s:%d)", sqlite3_errmsg(db), __FILE__, __LINE__)
+
	pkg_emit_error("sqlite: %s", sqlite3_errmsg(db), __FILE__, __LINE__)

#define HASH_FIND_INO(head,ino,out)                                          \
	HASH_FIND(hh,head,ino,sizeof(ino_t),out)
modified libpkg/update.c
@@ -321,6 +321,7 @@ pkg_update_full(const char *repofile, struct pkg_repo *repo, time_t *mtime)
	req = sqlite3_mprintf("select group_concat(arch, ', ') from "
			"(select distinct arch from packages "
			"where arch not GLOB '%q')", myarch);
+

	if (get_sql_string(sqlite, req, &bad_abis) != EPKG_OK) {
		sqlite3_free(req);
		pkg_emit_error("Unable to query repository");
@@ -646,7 +647,7 @@ pkg_update(struct pkg_repo *repo, bool force)
		}

		if (get_pragma(sqlite, "SELECT count(name) FROM sqlite_master "
-
		    "WHERE type='table' AND name='repodata';", &res) != EPKG_OK) {
+
		    "WHERE type='table' AND name='repodata';", &res, false) != EPKG_OK) {
			pkg_emit_error("Unable to query repository");
			sqlite3_close(sqlite);
			return (EPKG_FATAL);
@@ -666,12 +667,13 @@ pkg_update(struct pkg_repo *repo, bool force)
		req = sqlite3_mprintf("select count(key) from repodata "
		    "WHERE key = \"packagesite\" and value = '%q'", pkg_repo_url(repo));

-
		if (get_pragma(sqlite, req, &res) != EPKG_OK) {
-
			sqlite3_free(req);
-
			pkg_emit_error("Unable to query repository");
-
			sqlite3_close(sqlite);
-
			return (EPKG_FATAL);
-
		}
+
		res = 0;
+
		/*
+
		 * Ignore error here:
+
		 * if an error occure it means the database is unusable
+
		 * therefor it is better to rebuild it from scratch
+
		 */
+
		get_pragma(sqlite, req, &res, true);
		sqlite3_free(req);
		if (res != 1) {
			t = 0;