]> pd.if.org Git - zpackage/blobdiff - lib/zpm.c
rollback on initialize failure
[zpackage] / lib / zpm.c
index 6827caf629a13693cea6fb3bc73741f5d928b3b3..b7d97776659c5c7b22bc6aec59462b2b2e58a138 100644 (file)
--- a/lib/zpm.c
+++ b/lib/zpm.c
@@ -258,7 +258,6 @@ static
 #include "newdb.c"
 
 int zpm_db_initialize(struct zpm *pkg) {
-       //fprintf(stderr, "initializing zpm database\n");
        char *error;
        switch (sqlite3_exec(pkg->db, createdb, (int (*)(void *,int,char **,char **))0, NULL, &error)) {
                case SQLITE_OK: break;
@@ -266,6 +265,7 @@ int zpm_db_initialize(struct zpm *pkg) {
                        SQLERROR(sqlite3_errmsg(pkg->db));
                        fprintf(stderr, "error: %s\n", error);
                        sqlite3_free(error);
+                       zpm_rollback(pkg);
                        return 0;
                        break;
        }
@@ -323,6 +323,20 @@ struct zpm *zpm_clearmem(struct zpm *zpm) {
        return zpm;
 }
 
+static void zpm_set_db_errmsg(struct zpm *zpm, const char *msg) {
+       if (zpm) {
+               if (zpm->dberrmsg) {
+                       free(zpm->dberrmsg);
+               }
+               if (msg) {
+                       zpm->dberrmsg = strdup(msg);
+                       if (!zpm->dberrmsg) {
+                               zpm->error = 1;
+                       }
+               }
+       }
+}
+
 int zpm_init(struct zpm *pkg, char *path) {
        int rc;
        sqlite3 *db = 0;
@@ -336,8 +350,15 @@ int zpm_init(struct zpm *pkg, char *path) {
 
        rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
        if (rc) {
-               SQLERROR(sqlite3_errmsg(db));
-               sqlite3_close(db);
+               pkg->error = 1;
+               pkg->dbresult = rc;
+               zpm_set_db_errmsg(pkg, sqlite3_errstr(rc));
+               if (db) {
+                       sqlite3_close(db);
+               }
+               fprintf(stderr, "error (%d): %s: %s\n", rc,
+                               pkg->dberrmsg, path);
+
                return 0;
        }
        pkg->db   = db;