diff options
Diffstat (limited to 'src/database.c')
-rw-r--r-- | src/database.c | 59 |
1 files changed, 38 insertions, 21 deletions
diff --git a/src/database.c b/src/database.c index d5f83e6..b9eef79 100644 --- a/src/database.c +++ b/src/database.c @@ -1152,6 +1152,40 @@ static int load_apkindex(void *sctx, const struct apk_file_info *fi, return 0; } +static int load_index(struct apk_database *db, struct apk_bstream *bs, + int targz, int repo) +{ + if (targz) { + struct apk_istream *is; + struct apkindex_ctx ctx; + + ctx.db = db; + ctx.repo = repo; + apk_sign_ctx_init(&ctx.sctx, APK_SIGN_VERIFY, NULL); + is = apk_bstream_gunzip_mpart(bs, apk_sign_ctx_mpart_cb, &ctx.sctx); + apk_tar_parse(is, load_apkindex, &ctx); + is->close(is); + apk_sign_ctx_free(&ctx.sctx); + if (!ctx.sctx.data_verified) + return -1; + } else { + bs = apk_bstream_from_istream(apk_bstream_gunzip(bs)); + apk_db_index_read(db, bs, repo); + bs->close(bs, NULL); + } + return 0; +} + +int apk_db_index_read_file(struct apk_database *db, const char *file, int repo) +{ + int targz = 1; + + if (strstr(file, ".tar.gz") == NULL && strstr(file, ".gz") != NULL) + targz = 0; + + return load_index(db, apk_bstream_from_file(file), targz, repo); +} + int apk_db_add_repository(apk_database_t _db, apk_blob_t repository) { struct apk_database *db = _db.db; @@ -1195,28 +1229,11 @@ int apk_db_add_repository(apk_database_t _db, apk_blob_t repository) apk_warning("Failed to open index for %s", repo->url); return -1; } - if (targz) { - struct apk_istream *is; - struct apkindex_ctx ctx; - - ctx.db = db; - ctx.repo = r; - apk_sign_ctx_init(&ctx.sctx, APK_SIGN_VERIFY, NULL); - is = apk_bstream_gunzip_mpart(bs, apk_sign_ctx_mpart_cb, &ctx.sctx); - r = apk_tar_parse(is, load_apkindex, &ctx); - is->close(is); - apk_sign_ctx_free(&ctx.sctx); - if (!ctx.sctx.data_verified) { - apk_error("Bad repository signature: %s", repo->url); - return -1; - } - } else { - bs = apk_bstream_from_istream(apk_bstream_gunzip(bs)); - apk_db_index_read(db, bs, r); - bs->close(bs, NULL); - } - return 0; + r = load_index(db, bs, targz, r); + if (r != 0) + apk_error("%s: Bad repository signature", repo->url); + return r; } static void extract_cb(void *_ctx, size_t progress) |