diff options
Diffstat (limited to 'src/context.c')
-rw-r--r-- | src/context.c | 104 |
1 files changed, 90 insertions, 14 deletions
diff --git a/src/context.c b/src/context.c index b0cfc78..677704d 100644 --- a/src/context.c +++ b/src/context.c @@ -98,6 +98,7 @@ struct syrep_db_context* db_context_open(const char *filename, int force) { struct syrep_db_context *c = NULL; const char* path; FILE *f; + int k; if (!(c = malloc(sizeof(struct syrep_db_context)))) goto fail; @@ -107,7 +108,11 @@ struct syrep_db_context* db_context_open(const char *filename, int force) { if (!(c->package = package_open(filename, force))) goto fail; - path = package_get_item(c->package, "timestamp", 1); + if ((k = package_get_item(c->package, "timestamp", 1, &path)) < 0) + goto fail; + + assert(k); + if ((f = fopen(path, "r"))) { if (fscanf(f, "%i", &c->timestamp) != 1) c->timestamp = 0; @@ -117,14 +122,22 @@ struct syrep_db_context* db_context_open(const char *filename, int force) { if (!c->timestamp) c->timestamp = time(NULL); - path = package_get_item(c->package, "version", 1); + if ((k = package_get_item(c->package, "version", 1, &path)) < 0) + goto fail; + + assert(k); + if ((f = fopen(path, "r"))) { if (fscanf(f, "%u", &c->version) != 1) c->version = 0; fclose(f); } - path = package_get_item(c->package, "origin", 1); + if ((k = package_get_item(c->package, "origin", 1, &path)) < 0) + goto fail; + + assert(k); + if ((f = fopen(path, "r"))) { char hn[256]; if (fgets(hn, sizeof(hn), f)) { @@ -150,35 +163,75 @@ struct syrep_db_context* db_context_open(const char *filename, int force) { } /* Creating database id_meta */ - if (!(c->db_id_meta = open_db(package_get_item(c->package, "id_meta", 1), 0, 0))) + if ((k = package_get_item(c->package, "id_meta", 1, &path)) < 0) + goto fail; + + assert(k); + + if (!(c->db_id_meta = open_db(path, 0, 0))) goto fail; /* Creating database md_nrecno */ - if (!(c->db_md_nrecno = open_db(package_get_item(c->package, "md_nrecno", 1), 1, 0))) + if ((k = package_get_item(c->package, "md_nrecno", 1, &path)) < 0) + goto fail; + + assert(k); + + if (!(c->db_md_nrecno = open_db(path, 1, 0))) goto fail; /* Creating database nrecno_md */ - if (!(c->db_nrecno_md = open_db(package_get_item(c->package, "nrecno_md", 1), 1, 0))) + if ((k = package_get_item(c->package, "nrecno_md", 1, &path)) < 0) + goto fail; + + assert(k); + + if (!(c->db_nrecno_md = open_db(path, 1, 0))) goto fail; /* Creating database nrecno_lastmd */ - if (!(c->db_nrecno_lastmd = open_db(package_get_item(c->package, "nrecno_lastmd", 1), 0, 0))) + if ((k = package_get_item(c->package, "nrecno_lastmd", 1, &path)) < 0) + goto fail; + + assert(k); + + if (!(c->db_nrecno_lastmd = open_db(path, 0, 0))) goto fail; /* Creating database md_lastnrecno */ - if (!(c->db_md_lastnrecno = open_db(package_get_item(c->package, "md_lastnrecno", 1), 0, 0))) + if ((k = package_get_item(c->package, "md_lastnrecno", 1, &path)) < 0) + goto fail; + + assert(k); + + if (!(c->db_md_lastnrecno = open_db(path, 0, 0))) goto fail; /* Creating database version_timestamp */ - if (!(c->db_version_timestamp = open_db(package_get_item(c->package, "version_timestamp", 1), 0, 0))) + if ((k = package_get_item(c->package, "version_timestamp", 1, &path)) < 0) + goto fail; + + assert(k); + + if (!(c->db_version_timestamp = open_db(path, 0, 0))) goto fail; /* Creating database nhash_nrecno */ - if (!(c->db_nhash_nrecno = open_db(package_get_item(c->package, "nhash_nrecno", 1), 1, 0))) + if ((k = package_get_item(c->package, "nhash_nrecno", 1, &path)) < 0) + goto fail; + + assert(k); + + if (!(c->db_nhash_nrecno = open_db(path, 1, 0))) goto fail; /* Creating database nrecno_name */ - if (!(c->db_nrecno_name = open_db(package_get_item(c->package, "nrecno_name", 1), 0, 1))) + if ((k = package_get_item(c->package, "nrecno_name", 1, &path)) < 0) + goto fail; + + assert(k); + + if (!(c->db_nrecno_name = open_db(path, 0, 1))) goto fail; return c; @@ -192,6 +245,8 @@ fail: int db_context_save(struct syrep_db_context *c, const char *filename) { FILE *f; + int k; + const char *path; assert(c && c->package); if (c->db_id_meta) @@ -217,20 +272,41 @@ int db_context_save(struct syrep_db_context *c, const char *filename) { if (c->db_nrecno_name) c->db_nrecno_name->sync(c->db_nrecno_name, 0); + + /* Saving timestamp info */ - if (!(f = fopen(package_get_item(c->package, "timestamp", 1), "w+"))) + if ((k = package_get_item(c->package, "timestamp", 1, &path)) < 0) + return -1; + + assert(k); + + if (!(f = fopen(path, "w+"))) return -1; fprintf(f, "%i\n", c->timestamp); fclose(f); - if (!(f = fopen(package_get_item(c->package, "version", 1), "w+"))) + /* Save version info */ + + if ((k = package_get_item(c->package, "version", 1, &path)) < 0) + return -1; + + assert(k); + + if (!(f = fopen(path, "w+"))) return -1; fprintf(f, "%u\n", c->version); fclose(f); - if (!(f = fopen(package_get_item(c->package, "origin", 1), "w+"))) + /* Save origin info */ + + if ((k = package_get_item(c->package, "origin", 1, &path)) < 0) + return -1; + + assert(k); + + if (!(f = fopen(path, "w+"))) return -1; fprintf(f, "%s\n", c->origin); |