all: various conversions to foreach_array_item, and simplifications of code

cute-signatures
Timo Teräs 2013-06-18 08:03:40 +03:00
parent 72139b82d3
commit 54509e7a1a
7 changed files with 109 additions and 189 deletions

View File

@ -154,10 +154,8 @@ void apk_blob_pull_deps(apk_blob_t *from, struct apk_database *, struct apk_depe
int apk_deps_write(struct apk_database *db, struct apk_dependency_array *deps,
struct apk_ostream *os, apk_blob_t separator);
int apk_deps_add(struct apk_dependency_array **depends,
struct apk_dependency *dep);
void apk_deps_del(struct apk_dependency_array **deps,
struct apk_name *name);
void apk_deps_add(struct apk_dependency_array **depends, struct apk_dependency *dep);
void apk_deps_del(struct apk_dependency_array **deps, struct apk_name *name);
int apk_script_type(const char *name);
struct apk_package *apk_pkg_get_installed(struct apk_name *name);

View File

@ -147,10 +147,9 @@ static int dump_packages(struct apk_changeset *changeset,
struct apk_change *change;
struct apk_name *name;
struct apk_indent indent = { .indent = 2 };
int match = 0, i;
int match = 0;
for (i = 0; i < changeset->changes->num; i++) {
change = &changeset->changes->item[i];
foreach_array_item(change, changeset->changes) {
if (!cmp(change))
continue;
if (match == 0)
@ -207,18 +206,16 @@ static int cmp_upgrade(struct apk_change *change)
static void run_triggers(struct apk_database *db, struct apk_changeset *changeset)
{
int i;
struct apk_change *change;
struct apk_installed_package *ipkg;
if (apk_db_fire_triggers(db) == 0)
return;
for (i = 0; i < changeset->changes->num; i++) {
struct apk_package *pkg = changeset->changes->item[i].new_pkg;
struct apk_installed_package *ipkg;
foreach_array_item(change, changeset->changes) {
struct apk_package *pkg = change->new_pkg;
if (pkg == NULL)
continue;
ipkg = pkg->ipkg;
if (ipkg->pending_triggers->num == 0)
continue;
@ -236,7 +233,7 @@ int apk_solver_commit_changeset(struct apk_database *db,
{
struct progress prog;
struct apk_change *change;
int i, r = 0, size_diff = 0, size_unit;
int r = 0, size_diff = 0, size_unit;
if (apk_db_check_world(db, world) != 0) {
apk_error("Not committing changes due to missing repository tags. Use --force to override.");
@ -248,8 +245,7 @@ int apk_solver_commit_changeset(struct apk_database *db,
/* Count what needs to be done */
memset(&prog, 0, sizeof(prog));
for (i = 0; i < changeset->changes->num; i++) {
change = &changeset->changes->item[i];
foreach_array_item(change, changeset->changes) {
count_change(change, &prog.total);
if (change->new_pkg)
size_diff += change->new_pkg->installed_size;
@ -291,9 +287,7 @@ int apk_solver_commit_changeset(struct apk_database *db,
/* Go through changes */
r = 0;
for (i = 0; i < changeset->changes->num; i++) {
change = &changeset->changes->item[i];
foreach_array_item(change, changeset->changes) {
if (print_change(db, change, prog.done.changes, prog.total.changes)) {
prog.pkg = change->new_pkg;
prog.flags = APK_PRINT_PROGRESS_FORCE;

View File

@ -293,10 +293,10 @@ struct apk_db_dir *apk_db_dir_get(struct apk_database *db, apk_blob_t name)
{
struct apk_db_dir *dir;
struct apk_protected_path_array *ppaths;
struct apk_protected_path *ppath;
apk_blob_t bparent;
unsigned long hash = apk_hash_from_key(&db->installed.dirs, name);
char *relative_name;
int i;
if (name.len && name.ptr[name.len-1] == '/')
name.len--;
@ -338,11 +338,8 @@ struct apk_db_dir *apk_db_dir_get(struct apk_database *db, apk_blob_t name)
return dir;
relative_name = strrchr(dir->rooted_name, '/') + 1;
for (i = 0; i < ppaths->num; i++) {
struct apk_protected_path *ppath = &ppaths->item[i];
char *slash;
slash = strchr(ppath->relative_pattern, '/');
foreach_array_item(ppath, ppaths) {
char *slash = strchr(ppath->relative_pattern, '/');
if (slash != NULL) {
*slash = 0;
if (fnmatch(ppath->relative_pattern, relative_name, FNM_PATHNAME) != 0) {
@ -529,7 +526,7 @@ static inline void add_provider(struct apk_name *name, struct apk_provider p)
struct apk_package *apk_db_pkg_add(struct apk_database *db, struct apk_package *pkg)
{
struct apk_package *idb;
int i;
struct apk_dependency *dep;
if (pkg->license == NULL)
pkg->license = apk_blob_atomize(APK_BLOB_NULL);
@ -544,10 +541,8 @@ struct apk_package *apk_db_pkg_add(struct apk_database *db, struct apk_package *
idb = pkg;
apk_hash_insert(&db->available.packages, pkg);
add_provider(pkg->name, APK_PROVIDER_FROM_PACKAGE(pkg));
for (i = 0; i < pkg->provides->num; i++) {
struct apk_dependency *dep = &pkg->provides->item[i];
foreach_array_item(dep, pkg->provides)
add_provider(dep->name, APK_PROVIDER_FROM_PROVIDES(pkg, dep));
}
apk_db_pkg_rdepends(db, pkg);
} else {
idb->repos |= pkg->repos;
@ -1057,7 +1052,7 @@ static void apk_db_triggers_write(struct apk_database *db, struct apk_ostream *o
struct apk_installed_package *ipkg;
char buf[APK_BLOB_CHECKSUM_BUF];
apk_blob_t bfn;
int i;
char **trigger;
list_for_each_entry(ipkg, &db->installed.triggers, trigger_pkgs_list) {
bfn = APK_BLOB_BUF(buf);
@ -1065,9 +1060,9 @@ static void apk_db_triggers_write(struct apk_database *db, struct apk_ostream *o
bfn = apk_blob_pushed(APK_BLOB_BUF(buf), bfn);
os->write(os, bfn.ptr, bfn.len);
for (i = 0; i < ipkg->triggers->num; i++) {
foreach_array_item(trigger, ipkg->triggers) {
os->write(os, " ", 1);
apk_ostream_write_string(os, ipkg->triggers->item[i]);
apk_ostream_write_string(os, *trigger);
}
os->write(os, "\n", 1);
}
@ -1728,6 +1723,7 @@ void apk_db_close(struct apk_database *db)
{
struct apk_installed_package *ipkg;
struct apk_db_dir_instance *diri;
struct apk_protected_path *ppath;
struct hlist_node *dc, *dn;
int i;
@ -1746,8 +1742,8 @@ void apk_db_close(struct apk_database *db)
free(db->repos[i].url);
free(db->repos[i].description.ptr);
}
for (i = 0; i < db->protected_paths->num; i++)
free(db->protected_paths->item[i].relative_pattern);
foreach_array_item(ppath, db->protected_paths)
free(ppath->relative_pattern);
apk_protected_path_array_free(&db->protected_paths);
apk_dependency_array_free(&db->world);
@ -1849,8 +1845,8 @@ static int foreach_cache_file(void *pctx, int dirfd, const char *name)
struct foreach_cache_item_ctx *ctx = (struct foreach_cache_item_ctx *) pctx;
struct apk_database *db = ctx->db;
struct apk_package *pkg = NULL;
struct apk_provider *p0;
apk_blob_t b = APK_BLOB_STR(name), bname, bver;
int i;
if (apk_pkg_parse_name(b, &bname, &bver) == 0) {
/* Package - search for it */
@ -1859,15 +1855,13 @@ static int foreach_cache_file(void *pctx, int dirfd, const char *name)
if (name == NULL)
goto no_pkg;
for (i = 0; i < name->providers->num; i++) {
struct apk_package *pkg0 = name->providers->item[i].pkg;
if (pkg0->name != name)
foreach_array_item(p0, name->providers) {
if (p0->pkg->name != name)
continue;
apk_pkg_format_cache_pkg(APK_BLOB_BUF(tmp), pkg0);
apk_pkg_format_cache_pkg(APK_BLOB_BUF(tmp), p0->pkg);
if (apk_blob_compare(b, APK_BLOB_STR(tmp)) == 0) {
pkg = pkg0;
pkg = p0->pkg;
break;
}
}
@ -1892,15 +1886,14 @@ int apk_db_permanent(struct apk_database *db)
int apk_db_check_world(struct apk_database *db, struct apk_dependency_array *world)
{
int i, bad = 0;
struct apk_dependency *dep;
int bad = 0, tag;
if (apk_flags & APK_FORCE)
return 0;
for (i = 0; i < world->num; i++) {
struct apk_dependency *dep = &world->item[i];
int tag = dep->repository_tag;
foreach_array_item(dep, world) {
tag = dep->repository_tag;
if (tag == 0 || db->repo_tags[tag].allowed_repos != 0)
continue;
@ -2213,6 +2206,7 @@ static int apk_db_install_archive_entry(void *_ctx,
struct install_ctx *ctx = (struct install_ctx *) _ctx;
struct apk_database *db = ctx->db;
struct apk_package *pkg = ctx->pkg, *opkg;
struct apk_dependency *dep;
struct apk_installed_package *ipkg = pkg->ipkg;
apk_blob_t name = APK_BLOB_STR(ae->name), bdir, bfile;
struct apk_db_dir_instance *diri = ctx->diri;
@ -2274,8 +2268,6 @@ static int apk_db_install_archive_entry(void *_ctx,
opkg = NULL;
file = apk_db_file_query(db, bdir, bfile);
if (file != NULL) {
int i;
opkg = file->diri->pkg;
do {
int opkg_prio = -1, pkg_prio = -1;
@ -2287,15 +2279,15 @@ static int apk_db_install_archive_entry(void *_ctx,
if (opkg->name == pkg->name)
break;
/* Does the original package replace the new one? */
for (i = 0; i < opkg->ipkg->replaces->num; i++) {
if (apk_dep_is_materialized(&opkg->ipkg->replaces->item[i], pkg)) {
foreach_array_item(dep, opkg->ipkg->replaces) {
if (apk_dep_is_materialized(dep, pkg)) {
opkg_prio = opkg->ipkg->replaces_priority;
break;
}
}
/* Does the new package replace the original one? */
for (i = 0; i < ctx->ipkg->replaces->num; i++) {
if (apk_dep_is_materialized(&ctx->ipkg->replaces->item[i], opkg)) {
foreach_array_item(dep, ctx->ipkg->replaces) {
if (apk_dep_is_materialized(dep, opkg)) {
pkg_prio = ctx->ipkg->replaces_priority;
break;
}

View File

@ -64,7 +64,9 @@ static void dump_name(struct dot_ctx *ctx, struct apk_name *name)
static int dump_pkg(struct dot_ctx *ctx, struct apk_package *pkg)
{
int i, j, r, ret = 0;
struct apk_dependency *dep;
struct apk_provider *p0;
int r, ret = 0;
if (pkg->state_int == S_EVALUATED)
return 0;
@ -75,8 +77,7 @@ static int dump_pkg(struct dot_ctx *ctx, struct apk_package *pkg)
}
pkg->state_int = S_EVALUATING;
for (i = 0; i < pkg->depends->num; i++) {
struct apk_dependency *dep = &pkg->depends->item[i];
foreach_array_item(dep, pkg->depends) {
struct apk_name *name = dep->name;
dump_name(ctx, name);
@ -87,9 +88,7 @@ static int dump_pkg(struct dot_ctx *ctx, struct apk_package *pkg)
continue;
}
for (j = 0; j < name->providers->num; j++) {
struct apk_provider *p0 = &name->providers->item[j];
foreach_array_item(p0, name->providers) {
if (!apk_dep_is_provided(dep, p0))
continue;
@ -124,15 +123,16 @@ static int foreach_pkg(apk_hash_item item, void *ctx)
static int dot_main(void *pctx, struct apk_database *db, int argc, char **argv)
{
struct dot_ctx *ctx = (struct dot_ctx *) pctx;
int i, j;
struct apk_provider *p;
int i;
if (argc) {
for (i = 0; i < argc; i++) {
struct apk_name *name = apk_db_get_name(db, APK_BLOB_STR(argv[i]));
if (!name)
continue;
for (j = 0; j < name->providers->num; j++)
dump_pkg(ctx, name->providers->item[j].pkg);
foreach_array_item(p, name->providers)
dump_pkg(ctx, p->pkg);
}
} else {
apk_hash_foreach(&db->available.packages, foreach_pkg, pctx);

View File

@ -29,12 +29,13 @@ void apk_hash_free(struct apk_hash *h)
int apk_hash_foreach(struct apk_hash *h, apk_hash_enumerator_f e, void *ctx)
{
struct hlist_head *bucket;
apk_hash_node *pos, *n;
ptrdiff_t offset = h->ops->node_offset;
int i, r;
int r;
for (i = 0; i < h->buckets->num; i++) {
hlist_for_each_safe(pos, n, &h->buckets->item[i]) {
foreach_array_item(bucket, h->buckets) {
hlist_for_each_safe(pos, n, bucket) {
r = e(((void *) pos) - offset, ctx);
if (r != 0 && ctx != NULL)
return r;

View File

@ -71,7 +71,8 @@ static int info_exists(struct info_ctx *ctx, struct apk_database *db,
{
struct apk_name *name;
struct apk_dependency dep;
int i, j, ok, rc = 0;
struct apk_provider *p;
int i, ok, rc = 0;
for (i = 0; i < argc; i++) {
apk_blob_t b = APK_BLOB_STR(argv[i]);
@ -85,11 +86,8 @@ static int info_exists(struct info_ctx *ctx, struct apk_database *db,
continue;
ok = 0;
for (j = 0; j < name->providers->num; j++) {
struct apk_provider *p = &name->providers->item[j];
if (p->pkg->ipkg == NULL)
continue;
if (!apk_dep_is_provided(&dep, p))
foreach_array_item(p, name->providers) {
if (p->pkg->ipkg == NULL || !apk_dep_is_provided(&dep, p))
continue;
verbose_print_pkg(p->pkg, 0);
ok = 1;
@ -173,96 +171,56 @@ static void info_print_size(struct apk_database *db, struct apk_package *pkg)
pkg->installed_size);
}
static void info_print_depends(struct apk_database *db, struct apk_package *pkg)
static void info_print_dep_array(struct apk_database *db, struct apk_package *pkg,
struct apk_dependency_array *deps, const char *dep_text)
{
struct apk_dependency *d;
apk_blob_t separator = APK_BLOB_STR(apk_verbosity > 1 ? " " : "\n");
char dep[256];
int i;
char buf[256];
if (apk_verbosity == 1)
printf(PKG_VER_FMT " depends on:\n",
PKG_VER_PRINTF(pkg));
printf(PKG_VER_FMT " %s:\n", PKG_VER_PRINTF(pkg), dep_text);
if (apk_verbosity > 1)
printf("%s: ", pkg->name->name);
for (i = 0; i < pkg->depends->num; i++) {
apk_blob_t b = APK_BLOB_BUF(dep);
apk_blob_push_dep(&b, db, &pkg->depends->item[i]);
foreach_array_item(d, deps) {
apk_blob_t b = APK_BLOB_BUF(buf);
apk_blob_push_dep(&b, db, d);
apk_blob_push_blob(&b, separator);
b = apk_blob_pushed(APK_BLOB_BUF(dep), b);
b = apk_blob_pushed(APK_BLOB_BUF(buf), b);
fwrite(b.ptr, b.len, 1, stdout);
}
}
static void info_print_depends(struct apk_database *db, struct apk_package *pkg)
{
info_print_dep_array(db, pkg, pkg->depends, "depends on");
}
static void info_print_provides(struct apk_database *db, struct apk_package *pkg)
{
apk_blob_t separator = APK_BLOB_STR(apk_verbosity > 1 ? " " : "\n");
char dep[256];
int i;
info_print_dep_array(db, pkg, pkg->provides, "provides");
}
if (apk_verbosity == 1)
printf(PKG_VER_FMT " provides:\n",
PKG_VER_PRINTF(pkg));
if (apk_verbosity > 1)
printf("%s: ", pkg->name->name);
for (i = 0; i < pkg->provides->num; i++) {
apk_blob_t b = APK_BLOB_BUF(dep);
apk_blob_push_dep(&b, db, &pkg->provides->item[i]);
apk_blob_push_blob(&b, separator);
b = apk_blob_pushed(APK_BLOB_BUF(dep), b);
fwrite(b.ptr, b.len, 1, stdout);
}
static void print_rdep_pkg(struct apk_package *pkg0, struct apk_dependency *dep0, struct apk_package *pkg, void *pctx)
{
printf(PKG_VER_FMT "%s", PKG_VER_PRINTF(pkg0), apk_verbosity > 1 ? " " : "\n");
}
static void info_print_required_by(struct apk_database *db, struct apk_package *pkg)
{
int i, j;
char *separator = apk_verbosity > 1 ? " " : "\n";
if (apk_verbosity == 1)
printf(PKG_VER_FMT " is required by:\n",
PKG_VER_PRINTF(pkg));
printf(PKG_VER_FMT " is required by:\n", PKG_VER_PRINTF(pkg));
if (apk_verbosity > 1)
printf("%s: ", pkg->name->name);
for (i = 0; i < pkg->name->rdepends->num; i++) {
struct apk_name *name0;
struct apk_package *pkg0;
/* Check only the package that is installed, and that
* it actually has this package as dependency. */
name0 = pkg->name->rdepends->item[i];
pkg0 = apk_pkg_get_installed(name0);
if (pkg0 == NULL)
continue;
for (j = 0; j < pkg0->depends->num; j++) {
if (pkg0->depends->item[j].name != pkg->name)
continue;
printf(PKG_VER_FMT "%s",
PKG_VER_PRINTF(pkg0),
separator);
break;
}
}
apk_pkg_foreach_reverse_dependency(
pkg,
APK_FOREACH_INSTALLED | APK_DEP_SATISFIES | apk_foreach_genid(),
print_rdep_pkg, NULL);
}
static void info_print_install_if(struct apk_database *db, struct apk_package *pkg)
{
apk_blob_t separator = APK_BLOB_STR(apk_verbosity > 1 ? " " : "\n");
char dep[256];
int i;
if (apk_verbosity == 1)
printf(PKG_VER_FMT " has auto-install rule:\n",
PKG_VER_PRINTF(pkg));
if (apk_verbosity > 1)
printf("%s: ", pkg->name->name);
for (i = 0; i < pkg->install_if->num; i++) {
apk_blob_t b = APK_BLOB_BUF(dep);
apk_blob_push_dep(&b, db, &pkg->install_if->item[i]);
apk_blob_push_blob(&b, separator);
b = apk_blob_pushed(APK_BLOB_BUF(dep), b);
fwrite(b.ptr, b.len, 1, stdout);
}
info_print_dep_array(db, pkg, pkg->install_if, "has auto-install rule");
}
static void info_print_rinstall_if(struct apk_database *db, struct apk_package *pkg)
@ -322,37 +280,22 @@ static void info_print_contents(struct apk_database *db, struct apk_package *pkg
static void info_print_triggers(struct apk_database *db, struct apk_package *pkg)
{
struct apk_installed_package *ipkg = pkg->ipkg;
int i;
char **trigger;
if (apk_verbosity == 1)
printf(PKG_VER_FMT " triggers:\n",
PKG_VER_PRINTF(pkg));
for (i = 0; i < ipkg->triggers->num; i++) {
foreach_array_item(trigger, ipkg->triggers) {
if (apk_verbosity > 1)
printf("%s: trigger ", pkg->name->name);
printf("%s\n", ipkg->triggers->item[i]);
printf("%s\n", *trigger);
}
}
static void info_print_replaces(struct apk_database *db, struct apk_package *pkg)
{
apk_blob_t separator = APK_BLOB_STR(apk_verbosity > 1 ? " " : "\n");
char dep[256];
int i;
if (apk_verbosity == 1)
printf(PKG_VER_FMT " replaces:\n",
PKG_VER_PRINTF(pkg));
if (apk_verbosity > 1)
printf("%s: ", pkg->name->name);
for (i = 0; i < pkg->ipkg->replaces->num; i++) {
apk_blob_t b = APK_BLOB_BUF(dep);
apk_blob_push_dep(&b, db, &pkg->ipkg->replaces->item[i]);
apk_blob_push_blob(&b, separator);
b = apk_blob_pushed(APK_BLOB_BUF(dep), b);
fwrite(b.ptr, b.len, 1, stdout);
}
info_print_dep_array(db, pkg, pkg->provides, "replaces");
}
static void info_subaction(struct info_ctx *ctx, struct apk_package *pkg)
@ -392,7 +335,8 @@ static int info_package(struct info_ctx *ctx, struct apk_database *db,
int argc, char **argv)
{
struct apk_name *name;
int i, j;
struct apk_provider *p;
int i;
for (i = 0; i < argc; i++) {
name = apk_db_query_name(db, APK_BLOB_STR(argv[i]));
@ -400,8 +344,8 @@ static int info_package(struct info_ctx *ctx, struct apk_database *db,
apk_error("Not found: %s", argv[i]);
return 1;
}
for (j = 0; j < name->providers->num; j++)
info_subaction(ctx, name->providers->item[j].pkg);
foreach_array_item(p, name->providers)
info_subaction(ctx, p->pkg);
}
return 0;
}

View File

@ -40,16 +40,12 @@ static const apk_spn_match_def apk_spn_dependency_separator = {
struct apk_package *apk_pkg_get_installed(struct apk_name *name)
{
int i;
struct apk_provider *p;
foreach_array_item(p, name->providers)
if (p->pkg->name == name && p->pkg->ipkg != NULL)
return p->pkg;
for (i = 0; i < name->providers->num; i++) {
struct apk_package *pkg = name->providers->item[i].pkg;
if (pkg->name != name)
continue;
if (pkg->ipkg == NULL)
continue;
return pkg;
}
return NULL;
}
@ -95,6 +91,7 @@ struct apk_installed_package *apk_pkg_install(struct apk_database *db,
void apk_pkg_uninstall(struct apk_database *db, struct apk_package *pkg)
{
struct apk_installed_package *ipkg = pkg->ipkg;
char **trigger;
int i;
if (ipkg == NULL)
@ -110,8 +107,8 @@ void apk_pkg_uninstall(struct apk_database *db, struct apk_package *pkg)
if (ipkg->triggers->num) {
list_del(&ipkg->trigger_pkgs_list);
list_init(&ipkg->trigger_pkgs_list);
for (i = 0; i < ipkg->triggers->num; i++)
free(ipkg->triggers->item[i]);
foreach_array_item(trigger, ipkg->triggers)
free(*trigger);
}
apk_string_array_free(&ipkg->triggers);
apk_string_array_free(&ipkg->pending_triggers);
@ -153,41 +150,35 @@ int apk_pkg_parse_name(apk_blob_t apkname,
return 0;
}
int apk_deps_add(struct apk_dependency_array **depends,
struct apk_dependency *dep)
void apk_deps_add(struct apk_dependency_array **depends, struct apk_dependency *dep)
{
struct apk_dependency_array *deps = *depends;
int i;
struct apk_dependency *d0;
if (deps != NULL) {
for (i = 0; i < deps->num; i++) {
if (deps->item[i].name == dep->name) {
deps->item[i] = *dep;
return 0;
if (*depends) {
foreach_array_item(d0, *depends) {
if (d0->name == dep->name) {
*d0 = *dep;
return;
}
}
}
*apk_dependency_array_add(depends) = *dep;
return 0;
}
void apk_deps_del(struct apk_dependency_array **pdeps,
struct apk_name *name)
void apk_deps_del(struct apk_dependency_array **pdeps, struct apk_name *name)
{
struct apk_dependency_array *deps = *pdeps;
int i;
struct apk_dependency *d0;
if (deps == NULL)
return;
for (i = 0; i < deps->num; i++) {
if (deps->item[i].name != name)
continue;
deps->item[i] = deps->item[deps->num-1];
apk_dependency_array_resize(pdeps, deps->num-1);
break;
foreach_array_item(d0, deps) {
if (d0->name == name) {
*d0 = deps->item[deps->num - 1];
apk_dependency_array_resize(pdeps, deps->num - 1);
break;
}
}
}