From 20e30a3dc66c5e7289991705fbe88a4672b54936 Mon Sep 17 00:00:00 2001 From: Timo Teräs Date: Wed, 12 Apr 2023 08:51:22 +0300 Subject: db, index: refactor index writing --- src/apk_database.h | 1 - src/apk_package.h | 1 + src/app_index.c | 33 ++++++++++++++++++++++++++++++--- src/database.c | 48 +++--------------------------------------------- src/package.c | 10 ++++++++-- 5 files changed, 42 insertions(+), 51 deletions(-) diff --git a/src/apk_database.h b/src/apk_database.h index 52578d7..da29ce9 100644 --- a/src/apk_database.h +++ b/src/apk_database.h @@ -260,7 +260,6 @@ struct apk_package *apk_db_get_file_owner(struct apk_database *db, apk_blob_t fi int apk_db_index_read(struct apk_database *db, struct apk_istream *is, 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_repository_check(struct apk_database *db); int apk_db_add_repository(apk_database_t db, apk_blob_t repository); diff --git a/src/apk_package.h b/src/apk_package.h index 10d3cf2..895811d 100644 --- a/src/apk_package.h +++ b/src/apk_package.h @@ -183,6 +183,7 @@ void apk_ipkg_run_script(struct apk_installed_package *ipkg, struct apk_database unsigned int type, char **argv); struct apk_package *apk_pkg_parse_index_entry(struct apk_database *db, apk_blob_t entry); +int apk_pkg_write_index_header(struct apk_package *pkg, struct apk_ostream *os); int apk_pkg_write_index_entry(struct apk_package *pkg, struct apk_ostream *os); int apk_pkg_version_compare(const struct apk_package *a, const struct apk_package *b); diff --git a/src/app_index.c b/src/app_index.c index e37d0af..56fbe7c 100644 --- a/src/app_index.c +++ b/src/app_index.c @@ -75,6 +75,33 @@ static const struct apk_option_group optgroup_applet = { .parse = option_parse_applet, }; +struct index_writer { + struct apk_ostream *os; + int count; +}; + +static int index_write_entry(struct apk_database *db, const char *match, struct apk_package *pkg, void *ctx) +{ + struct index_writer *iw = ctx; + + if (!pkg->filename) return 0; + + iw->count++; + apk_pkg_write_index_entry(pkg, iw->os); + return 0; +} + +static int index_write(struct apk_database *db, struct apk_ostream *os) +{ + struct index_writer iw = { + .os = os, + }; + + apk_db_foreach_sorted_package(db, NULL, index_write_entry, &iw); + + return iw.count; +} + static int index_read_file(struct apk_database *db, struct index_ctx *ictx) { struct apk_file_info fi; @@ -208,7 +235,7 @@ static int index_main(void *ctx, struct apk_database *db, struct apk_string_arra fi.mode = 0644 | S_IFREG; fi.name = "APKINDEX"; counter = apk_ostream_counter(&fi.size); - r = apk_db_index_write(db, counter); + r = index_write(db, counter); apk_ostream_close(counter); if (r >= 0) { @@ -223,13 +250,13 @@ static int index_main(void *ctx, struct apk_database *db, struct apk_string_arra } apk_tar_write_entry(os, &fi, NULL); - r = apk_db_index_write(db, os); + r = index_write(db, os); apk_tar_write_padding(os, &fi); apk_tar_write_entry(os, NULL, NULL); } } else { - r = apk_db_index_write(db, os); + r = index_write(db, os); } apk_ostream_close(os); diff --git a/src/database.c b/src/database.c index 13ce02f..0c91f54 100644 --- a/src/database.c +++ b/src/database.c @@ -955,7 +955,7 @@ static int apk_db_write_fdb(struct apk_database *db, struct apk_ostream *os) foreach_array_item(ppkg, pkgs) { pkg = *ppkg; ipkg = pkg->ipkg; - r = apk_pkg_write_index_entry(pkg, os); + r = apk_pkg_write_index_header(pkg, os); if (r < 0) return r; @@ -1203,35 +1203,8 @@ static int apk_db_read_state(struct apk_database *db, int flags) return 0; } -struct index_write_ctx { - struct apk_ostream *os; - int count; - int force; -}; - -static int write_index_entry(apk_hash_item item, void *ctx) -{ - struct index_write_ctx *iwctx = (struct index_write_ctx *) ctx; - struct apk_package *pkg = (struct apk_package *) item; - int r; - - if (!iwctx->force && pkg->filename == NULL) - return 0; - - r = apk_pkg_write_index_entry(pkg, iwctx->os); - if (r < 0) - return r; - - if (apk_ostream_write(iwctx->os, "\n", 1) != 1) - return -EIO; - - iwctx->count++; - return 0; -} - static int apk_db_index_write_nr_cache(struct apk_database *db) { - struct index_write_ctx ctx = { NULL, 0, TRUE }; struct apk_package_array *pkgs; struct apk_package **ppkg; struct apk_ostream *os; @@ -1247,31 +1220,16 @@ static int apk_db_index_write_nr_cache(struct apk_database *db) 0644); if (IS_ERR_OR_NULL(os)) return PTR_ERR(os); - ctx.os = os; pkgs = apk_db_sorted_installed_packages(db); foreach_array_item(ppkg, pkgs) { struct apk_package *pkg = *ppkg; if ((pkg->repos == BIT(APK_REPOSITORY_CACHED) || (pkg->repos == 0 && !pkg->installed_size))) { - r = write_index_entry(pkg, &ctx); + r = apk_pkg_write_index_entry(pkg, os); if (r != 0) return r; } } - r = apk_ostream_close(os); - if (r < 0) return r; - return ctx.count; -} - -int apk_db_index_write(struct apk_database *db, struct apk_ostream *os) -{ - struct index_write_ctx ctx = { os, 0, FALSE }; - int r; - - r = apk_hash_foreach(&db->available.packages, write_index_entry, &ctx); - if (r < 0) - return r; - - return ctx.count; + return apk_ostream_close(os); } static int add_protected_path(void *ctx, apk_blob_t blob) diff --git a/src/package.c b/src/package.c index 5aa4a67..a4d4e3a 100644 --- a/src/package.c +++ b/src/package.c @@ -1108,8 +1108,7 @@ static int write_depends(struct apk_ostream *os, const char *field, return 0; } -int apk_pkg_write_index_entry(struct apk_package *info, - struct apk_ostream *os) +int apk_pkg_write_index_header(struct apk_package *info, struct apk_ostream *os) { char buf[2048]; apk_blob_t bbuf = APK_BLOB_BUF(buf); @@ -1172,6 +1171,13 @@ int apk_pkg_write_index_entry(struct apk_package *info, return 0; } +int apk_pkg_write_index_entry(struct apk_package *pkg, struct apk_ostream *os) +{ + int r = apk_pkg_write_index_header(pkg, os); + if (r < 0) return r; + return apk_ostream_write(os, "\n", 1); +} + int apk_pkg_version_compare(const struct apk_package *a, const struct apk_package *b) { if (a->version == b->version) -- cgit v1.2.3-70-g09d2