]> pd.if.org Git - zpackage/commitdiff
separate zpm library database create and open
authorNathan Wagner <nw@hydaspes.if.org>
Mon, 17 Sep 2018 11:31:53 +0000 (11:31 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Mon, 24 Sep 2018 10:39:43 +0000 (10:39 +0000)
use new init function in zpm-init

exit with error if addfile db open fails

lib/zpm.c
zpm-addfile.c
zpm-init.c
zpm.h

index b5c362f818fd3ae2b2a0af70c7a30bbff652934f..9eb69c286b568e01708bbb707cb36f8434ebeee4 100644 (file)
--- a/lib/zpm.c
+++ b/lib/zpm.c
@@ -284,18 +284,63 @@ int zpm_db_initialize(struct zpm *pkg) {
        return 1;
 }
 
-/* NULL?  Create? */
-int zpm_open(struct zpm *pkg, char *path) {
-       int rc;
+
+/* assumes pkg->db is set */
+static int setupdb(struct zpm *zpm) {
        char *errstr = 0;
-       sqlite3 *db = 0;
        int appid, dbver;
 
-       pkg->db = 0;
-       pkg->path = 0;
-       pkg->current_package = 0;
-       pkg->pkgid = 0;
-       pkg->errmsg = 0;
+       zpm->error = 0;
+
+       appid = zpm_db_pragma(zpm, 1);
+       dbver = zpm_db_pragma(zpm, 2);
+
+       if (appid != 0x5a504442) {
+               fprintf(stderr, "unknown database type\n");
+               zpm->error = 1;
+               return 0;
+       }
+
+       if (dbver > 1) {
+               fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
+               zpm->error = 1;
+               return 0;
+       }
+
+       sqlite3_exec(zpm->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
+
+       if (errstr) {
+               free(zpm->errmsg);
+               zpm->errmsg = strdup(errstr);
+               fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
+               sqlite3_free(errstr);
+               zpm->error = 1;
+               return 0;
+       }
+
+       /* TODO add vercmp */
+
+       return 1;
+}
+
+struct zpm *zpm_clearmem(struct zpm *zpm) {
+       if (!zpm) {
+               zpm = malloc(sizeof *zpm);
+       }
+
+       if (zpm) {
+               *zpm = (struct zpm){0};
+       }
+
+       return zpm;
+}
+
+int zpm_init(struct zpm *pkg, char *path) {
+       int rc;
+       sqlite3 *db = 0;
+       int appid;
+
+       zpm_clearmem(pkg);
 
        rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
        if (rc) {
@@ -304,43 +349,59 @@ int zpm_open(struct zpm *pkg, char *path) {
                return 0;
        }
        pkg->db   = db;
-       pkg->path = dupstr(path);
+       pkg->path = strdup(path);
 
        appid = zpm_db_pragma(pkg, 1);
-       dbver = zpm_db_pragma(pkg, 2);
 
-       //fprintf(stderr, "db appid = %x, dbver = %d\n", appid, dbver);
        switch (appid) {
                case 0: if (!zpm_db_initialize(pkg)) {
                                sqlite3_close(db);
                                return 1;
                        };
                        break;
-               case 0x5a504442: break;
+               case 0x5a504442:
+                       /* already initialized */
+                       break;
                default:
                        fprintf(stderr, "unknown database type\n");
                        sqlite3_close(db);
                        return 0;
                        break;
        }
-       if (dbver > 1) {
-               fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
-                       sqlite3_close(db);
-                       return 0;
-       }
 
-       sqlite3_exec(pkg->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
-       if (errstr) {
-               fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
-               sqlite3_free(errstr);
+       if (!setupdb(pkg)) {
                sqlite3_close(db);
+               pkg->db = 0;
                return 0;
        }
 
+       return 1;
+}
+
+int zpm_open(struct zpm *zpm, char *path) {
+       int rc;
+       sqlite3 *db = 0;
+
+       zpm_clearmem(zpm);
+
+       rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL);
+       if (rc) {
+               SQLERROR(sqlite3_errmsg(db));
+               sqlite3_close(db);
+               zpm->error = 1;
+               fprintf(stderr, "path = %s\n", path);
+               return 0;
+       }
+       zpm->db   = db;
+       zpm->path = strdup(path);
 
-       /* TODO if this is a new database, create structures */
+       if (!setupdb(zpm)) {
+               sqlite3_close(db);
+               zpm->db = 0;
+               zpm->error = 1;
+               return 0;
+       }
 
-       /* get a package. what if more than one? what if none? */
        return 1;
 }
 
index 160d84f7e115009b062f154e69edc2a13efb43a3..fd8d67b4c50231331d6e721f301fa26b5216e6ca 100644 (file)
@@ -28,7 +28,11 @@ int main(int ac, char **av){
                                exit(1);
                        }
                }
+       } else {
+               fprintf(stderr, "failed to open database %s\n", av[1]);
+               exit(EXIT_FAILURE);
        }
+
        zpm_commit(&pkg);
        zpm_close(&pkg);
        return 0;
index 03c539cabcd5f9202b5fea19c9765ca1434f4b48..b63007f4f2227941ff641ee1db79af0826776a7c 100644 (file)
@@ -10,7 +10,7 @@ int main(int ac, char **av){
                fprintf(stderr, "usage: path\n");
                return 1;
        }
-       rv = zpm_open(&pkg, av[1]);
+       rv = zpm_init(&pkg, av[1]);
        if (rv) {
                zpm_close(&pkg);
        }
diff --git a/zpm.h b/zpm.h
index 43d394cdc11affde6911744a54a410bf6106415c..43c6687a1208af219dd6dcc0f75715c8000675e4 100644 (file)
--- a/zpm.h
+++ b/zpm.h
@@ -29,6 +29,8 @@ struct zpm {
        sqlite3 *db;
        char *path; /* path to db file */
        int error;
+       char *errmsg;
+       char *pkgid;
        struct zpm_package *current_package;
 };
 
@@ -45,26 +47,36 @@ struct zpm_tag {
 
 struct zpm_package {
        struct zpm *zpm;
+       struct jsw_hash *ht;
+
+       /* char pointers are just pointers into the hash table */
+       /* integers/times and such are passed through atoi */
+       /* tags and licenses are trees, NULL if not fetched */
        char *name;
        char *version;
        int release;
        char *id;
-       char *tags;
+       /* null if tags not collected */
+       //struct zpm_tree *tags;
        char *description;
        char *architecture;
        char *url;
-       char *status;
-       char *licenses;
-       char *package;
+       char *status; /* integer code ? */
+       //struct zpm_tree *licenses;
        time_t build_time;
        time_t install_time;
        char checksum[ZPM_HASH_STRLEN+1];
+       struct zpm_package *next;
 };
 
+int zpm_parse_package(char *pstr, char *name, char *ver, int *rel);
+char *zpm_findpkg(struct zpm *zpm, char *pkgstr);
+int zpm_quote(char *value, char *dest, size_t n);
+
 struct zpm_file {
        char path[ZPM_PATH_MAX];
        int mode;
-       char tags[64];
+       //struct zpm_tree *tags;
        char owner[32];
        char group[32];
        time_t mtime;
@@ -72,9 +84,8 @@ struct zpm_file {
        struct zpm_file *next; /* so you can make a linked list */
 };
 
-
-/* NULL?  Create? */
 int zpm_open(struct zpm *pkg, char *path);
+int zpm_init(struct zpm *pkg, char *path);
 int zpm_pkgname(char *base, char *version, int release); /* construct a package file name */
 
 /* flags for preserving mode, owner, etc */
@@ -160,5 +171,8 @@ int zpm_foreach_path(struct zpm *zpm, char *pkgid,
 int (*callback)(void *f, int ncols, char **vals, char **cols),
 void *data, char **errmsg);
 
+int zpm_script_hash(struct zpm *zpm, char *pkgstr, char *phase, char *hash);
+
+sqlite3_stmt *zpm_dbquery(struct zpm *zpm, char *query, ...);
 
 #endif