diff --git a/src/apk_database.h b/src/apk_database.h index 4cf8b14..0edf4f2 100644 --- a/src/apk_database.h +++ b/src/apk_database.h @@ -140,6 +140,7 @@ struct apk_package *apk_db_get_pkg(struct apk_database *db, struct apk_checksum struct apk_package *apk_db_get_file_owner(struct apk_database *db, apk_blob_t filename); int apk_db_index_read(struct apk_database *db, struct apk_bstream *bs, int repo); +int apk_db_index_read_file(struct apk_database *db, const char *file, int repo); int apk_db_index_write(struct apk_database *db, struct apk_ostream *os); int apk_db_add_repository(apk_database_t db, apk_blob_t repository); 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) diff --git a/src/index.c b/src/index.c index 2a7365e..608dea8 100644 --- a/src/index.c +++ b/src/index.c @@ -51,20 +51,15 @@ static int index_parse(void *ctx, int optch, int optindex, const char *optarg) static int index_read_file(struct apk_database *db, struct index_ctx *ictx) { - struct apk_bstream *bs; struct apk_file_info fi; - int r; + if (ictx->index == NULL) return 0; if (apk_file_get_info(ictx->index, APK_CHECKSUM_NONE, &fi) < 0) return -1; ictx->index_mtime = fi.mtime; - bs = apk_bstream_from_istream(apk_bstream_gunzip(apk_bstream_from_url(ictx->index))); - if (bs == NULL) - return -1; - r = apk_db_index_read(db, bs, 0); - bs->close(bs, NULL); - return r; + + return apk_db_index_read_file(db, ictx->index, 0); } static int warn_if_no_providers(apk_hash_item item, void *ctx)