summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimo Teräs <timo.teras@iki.fi>2023-04-12 08:51:22 +0300
committerTimo Teräs <timo.teras@iki.fi>2023-04-12 08:57:45 +0300
commit7bd2046757f3d678d591c31fe6c206c8f5deb431 (patch)
tree2e6ce0e2c0a3143d8f6ab68de11a9b1ba800cb00
parentd76213e643c39da7d3b4e5534b721238ad7e8baf (diff)
downloadapk-tools-7bd2046757f3d678d591c31fe6c206c8f5deb431.tar.gz
apk-tools-7bd2046757f3d678d591c31fe6c206c8f5deb431.tar.bz2
apk-tools-7bd2046757f3d678d591c31fe6c206c8f5deb431.tar.xz
apk-tools-7bd2046757f3d678d591c31fe6c206c8f5deb431.zip
db, index: refactor index writing
-rw-r--r--src/apk_database.h1
-rw-r--r--src/apk_package.h1
-rw-r--r--src/app_index.c30
-rw-r--r--src/database.c47
-rw-r--r--src/package.c10
5 files changed, 40 insertions, 49 deletions
diff --git a/src/apk_database.h b/src/apk_database.h
index e53e13e..7aa6883 100644
--- a/src/apk_database.h
+++ b/src/apk_database.h
@@ -235,7 +235,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 6cd6f91..d54e9cf 100644
--- a/src/apk_package.h
+++ b/src/apk_package.h
@@ -157,6 +157,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 640541f..1e1e19a 100644
--- a/src/app_index.c
+++ b/src/app_index.c
@@ -75,6 +75,32 @@ 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++;
+ return apk_pkg_write_index_entry(pkg, iw->os);
+}
+
+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;
@@ -204,7 +230,7 @@ static int index_main(void *ctx, struct apk_ctx *ac, struct apk_string_array *ar
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) {
@@ -219,7 +245,7 @@ static int index_main(void *ctx, struct apk_ctx *ac, struct apk_string_array *ar
}
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);
diff --git a/src/database.c b/src/database.c
index 781a443..3c50389 100644
--- a/src/database.c
+++ b/src/database.c
@@ -974,7 +974,7 @@ static int apk_db_fdb_write(struct apk_database *db, struct apk_installed_packag
if (IS_ERR(os)) return PTR_ERR(os);
- r = apk_pkg_write_index_entry(pkg, os);
+ r = apk_pkg_write_index_header(pkg, os);
if (r < 0) goto err;
if (ipkg->replaces->num) {
@@ -1238,34 +1238,8 @@ static int apk_db_read_layer(struct apk_database *db, unsigned layer)
return ret;
}
-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;
-
- r = apk_ostream_write(iwctx->os, "\n", 1);
- if (r < 0) return r;
-
- 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;
@@ -1278,31 +1252,16 @@ static int apk_db_index_write_nr_cache(struct apk_database *db)
os = apk_ostream_to_file(db->cache_fd, "installed", 0644);
if (IS_ERR(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 049f6ef..29ca299 100644
--- a/src/package.c
+++ b/src/package.c
@@ -906,8 +906,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);
@@ -967,6 +966,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)