Skip to content

Commit

Permalink
pkghash: use iterator every where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
bapt committed Sep 15, 2021
1 parent 06b59b5 commit 284d5ef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
10 changes: 6 additions & 4 deletions libpkg/pkg_manifest.c
Original file line number Diff line number Diff line change
Expand Up @@ -1099,21 +1099,23 @@ pkg_emit_object(struct pkg *pkg, short flags)
pkg_debug(4, "Emitting users");
seq = NULL;
buf = NULL;
while (pkg_users(pkg, &buf) == EPKG_OK) {
it = pkghash_iterator(pkg->users);
while (pkghash_next(&it)) {
if (seq == NULL)
seq = ucl_object_typed_new(UCL_ARRAY);
ucl_array_append(seq, ucl_object_fromstring(buf));
ucl_array_append(seq, ucl_object_fromstring(it.key));
}
if (seq)
ucl_object_insert_key(top, seq, "users", 5, false);

pkg_debug(4, "Emitting groups");
seq = NULL;
buf = NULL;
while (pkg_groups(pkg, &buf) == EPKG_OK) {
it = pkghash_iterator(pkg->users);
while (pkghash_next(&it)) {
if (seq == NULL)
seq = ucl_object_typed_new(UCL_ARRAY);
ucl_array_append(seq, ucl_object_fromstring(buf));
ucl_array_append(seq, ucl_object_fromstring(it.key));
}
if (seq)
ucl_object_insert_key(top, seq, "groups", 6, false);
Expand Down
16 changes: 8 additions & 8 deletions libpkg/pkg_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1182,21 +1182,21 @@ format_groups(xstring *buf, const void *data, struct percent_esc *p)
if (p->flags & (PP_ALTERNATE_FORM1|PP_ALTERNATE_FORM2))
return (list_count(buf, pkg_list_count(pkg, PKG_GROUPS), p));
else {
char *group = NULL;
int count;

set_list_defaults(p, "%Gn\n", "");

count = 1;
fflush(p->sep_fmt->fp);
fflush(p->item_fmt->fp);
while(pkg_groups(pkg, &group) == EPKG_OK) {
pkghash_it it = pkghash_iterator(pkg->users);
while (pkghash_next(&it)) {
if (count > 1)
iterate_item(buf, pkg, p->sep_fmt->buf,
group, count, PP_G);
it.key, count, PP_G);

iterate_item(buf, pkg,p->item_fmt->buf,
group, count, PP_G);
it.key, count, PP_G);
count++;
}
}
Expand Down Expand Up @@ -1469,21 +1469,21 @@ format_users(xstring *buf, const void *data, struct percent_esc *p)
if (p->flags & (PP_ALTERNATE_FORM1|PP_ALTERNATE_FORM2))
return (list_count(buf, pkg_list_count(pkg, PKG_USERS), p));
else {
char *user = NULL;
int count;

set_list_defaults(p, "%Un\n", "");

count = 1;
fflush(p->sep_fmt->fp);
fflush(p->item_fmt->fp);
while (pkg_users(pkg, &user) == EPKG_OK) {
pkghash_it it = pkghash_iterator(pkg->users);
while (pkghash_next(&it)) {
if (count > 1)
iterate_item(buf, pkg, p->sep_fmt->buf,
user, count, PP_U);
it.key, count, PP_U);

iterate_item(buf, pkg, p->item_fmt->buf,
user, count, PP_U);
it.key, count, PP_U);
count++;
}
}
Expand Down
18 changes: 9 additions & 9 deletions libpkg/pkgdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int forced,
struct pkg_conflict *conflict = NULL;
struct pkg_config_file *cf = NULL;
struct pkgdb_it *it = NULL;
char *buf, *msg = NULL;
char *msg = NULL;

sqlite3 *s;

Expand Down Expand Up @@ -1957,12 +1957,12 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int forced,
* Insert users
*/

buf = NULL;
while (pkg_users(pkg, &buf) == EPKG_OK) {
if (run_prstmt(USERS1, buf)
hit = pkghash_iterator(pkg->users);
while (pkghash_next(&hit)) {
if (run_prstmt(USERS1, hit.key)
!= SQLITE_DONE
||
run_prstmt(USERS2, package_id, buf)
run_prstmt(USERS2, package_id, hit.key)
!= SQLITE_DONE) {
ERROR_STMT_SQLITE(s, STMT(USERS2));
goto cleanup;
Expand All @@ -1973,12 +1973,12 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int forced,
* Insert groups
*/

buf = NULL;
while (pkg_groups(pkg, &buf) == EPKG_OK) {
if (run_prstmt(GROUPS1, buf)
hit = pkghash_iterator(pkg->groups);
while (pkghash_next(&hit)) {
if (run_prstmt(GROUPS1, hit.key)
!= SQLITE_DONE
||
run_prstmt(GROUPS2, package_id, buf)
run_prstmt(GROUPS2, package_id, hit.key)
!= SQLITE_DONE) {
ERROR_STMT_SQLITE(s, STMT(GROUPS2));
goto cleanup;
Expand Down

0 comments on commit 284d5ef

Please sign in to comment.