X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=lib%2Fzpm.c;h=9eb69c286b568e01708bbb707cb36f8434ebeee4;hb=45a98e17c91c5617328859416de44fed8298f295;hp=b5c362f818fd3ae2b2a0af70c7a30bbff652934f;hpb=1c489e30fb64d735a016f051e5c40f2f9ea14a06;p=zpackage diff --git a/lib/zpm.c b/lib/zpm.c index b5c362f..9eb69c2 100644 --- 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; }