diff --git a/libpkg/pkg.c b/libpkg/pkg.c index 2c47eaa..1a8efe4 100644 --- a/libpkg/pkg.c +++ b/libpkg/pkg.c @@ -47,6 +47,9 @@ pkg_new(struct pkg **pkg, pkg_t type) return EPKG_FATAL; } + (*pkg)->deps = kh_init_pkg_deps(); + (*pkg)->rdeps = kh_init_pkg_deps(); + (*pkg)->files = kh_init_pkg_files(); (*pkg)->type = type; (*pkg)->rootfd = -1; @@ -514,7 +517,7 @@ pkg_deps(const struct pkg *pkg, struct pkg_dep **d) { assert(pkg != NULL); - HASH_NEXT(pkg->deps, (*d)); + kh_next(pkg_deps, pkg->deps, (*d), name); } int @@ -522,7 +525,7 @@ pkg_rdeps(const struct pkg *pkg, struct pkg_dep **d) { assert(pkg != NULL); - HASH_NEXT(pkg->rdeps, (*d)); + kh_next(pkg_deps, pkg->rdeps, (*d), name); } int @@ -530,7 +533,7 @@ pkg_files(const struct pkg *pkg, struct pkg_file **f) { assert(pkg != NULL); - HASH_NEXT(pkg->files, (*f)); + kh_next(pkg_files, pkg->files, (*f), path); } int @@ -678,14 +681,15 @@ int pkg_adddep(struct pkg *pkg, const char *name, const char *origin, const char *version, bool locked) { struct pkg_dep *d = NULL; + int ret; + khint_t k; assert(pkg != NULL); assert(name != NULL && name[0] != '\0'); assert(origin != NULL && origin[0] != '\0'); pkg_debug(3, "Pkg: add a new dependency origin: %s, name: %s", origin, name); - HASH_FIND_STR(pkg->deps, name, d); - if (d != NULL) { + if (kh_get_pkg_deps(pkg->deps, name) != kh_end(pkg->deps)) { if (developer_mode) { pkg_emit_error("%s: duplicate dependency listing: %s, fatal (developer mode)", pkg->name, name); @@ -706,7 +710,9 @@ pkg_adddep(struct pkg *pkg, const char *name, const char *origin, const char *ve d->uid = strdup(name); d->locked = locked; - HASH_ADD_KEYPTR(hh, pkg->deps, d->name, strlen(d->name), d); + k = kh_put_pkg_deps(pkg->deps, d->name, &ret); + if (ret != 0) + kh_value(pkg->deps, k) = d; return (EPKG_OK); } @@ -715,6 +721,8 @@ int pkg_addrdep(struct pkg *pkg, const char *name, const char *origin, const char *version, bool locked) { struct pkg_dep *d; + int ret; + khint_t k; assert(pkg != NULL); assert(name != NULL && name[0] != '\0'); @@ -730,7 +738,9 @@ pkg_addrdep(struct pkg *pkg, const char *name, const char *origin, const char *v d->uid = strdup(name); d->locked = locked; - HASH_ADD_KEYPTR(hh, pkg->rdeps, d->origin, strlen(d->origin), d); + k = kh_put_pkg_deps(pkg->rdeps, d->name, &ret); + if (ret != 0) + kh_value(pkg->rdeps, k) = d; return (EPKG_OK); } @@ -746,6 +756,8 @@ pkg_addfile_attr(struct pkg *pkg, const char *path, const char *sum, const char *uname, const char *gname, mode_t perm, u_long fflags, bool check_duplicates) { + khint_t k; + int ret; struct pkg_file *f = NULL; char abspath[MAXPATHLEN]; @@ -756,13 +768,13 @@ pkg_addfile_attr(struct pkg *pkg, const char *path, const char *sum, pkg_debug(3, "Pkg: add new file '%s'", path); if (check_duplicates) { - HASH_FIND_STR(pkg->files, path, f); - if (f != NULL) { + k = kh_get_pkg_files(pkg->files, path); + if (k != kh_end(pkg->files)) { if (developer_mode) { - pkg_emit_error("duplicate file listing: %s, fatal (developer mode)", f->path); + pkg_emit_error("duplicate file listing: %s, fatal (developer mode)", path); return (EPKG_FATAL); } else { - pkg_emit_error("duplicate file listing: %s, ignoring", f->path); + pkg_emit_error("duplicate file listing: %s, ignoring", path); return (EPKG_OK); } } @@ -786,7 +798,9 @@ pkg_addfile_attr(struct pkg *pkg, const char *path, const char *sum, if (fflags != 0) f->fflags = fflags; - HASH_ADD_STR(pkg->files, path, f); + k = kh_put_pkg_files(pkg->files, f->path, &ret); + if (ret != 0) + kh_value(pkg->files, k) = f; return (EPKG_OK); } @@ -1318,13 +1332,13 @@ pkg_list_count(const struct pkg *pkg, pkg_list list) { switch (list) { case PKG_DEPS: - return (HASH_COUNT(pkg->deps)); + return (kh_size(pkg->deps)); case PKG_RDEPS: - return (HASH_COUNT(pkg->rdeps)); + return (kh_size(pkg->rdeps)); case PKG_OPTIONS: return (HASH_COUNT(pkg->options)); case PKG_FILES: - return (HASH_COUNT(pkg->files)); + return (kh_size(pkg->files)); case PKG_DIRS: return (HASH_COUNT(pkg->dirs)); case PKG_USERS: @@ -1349,14 +1363,15 @@ pkg_list_count(const struct pkg *pkg, pkg_list list) } void -pkg_list_free(struct pkg *pkg, pkg_list list) { +pkg_list_free(struct pkg *pkg, pkg_list list) +{ switch (list) { case PKG_DEPS: - HASH_FREE(pkg->deps, pkg_dep_free); + kh_free(pkg_deps, pkg->deps, struct pkg_dep, pkg_dep_free); pkg->flags &= ~PKG_LOAD_DEPS; break; case PKG_RDEPS: - HASH_FREE(pkg->rdeps, pkg_dep_free); + kh_free(pkg_deps, pkg->rdeps, struct pkg_dep, pkg_dep_free); pkg->flags &= ~PKG_LOAD_RDEPS; break; case PKG_OPTIONS: @@ -1365,7 +1380,7 @@ pkg_list_free(struct pkg *pkg, pkg_list list) { break; case PKG_FILES: case PKG_CONFIG_FILES: - HASH_FREE(pkg->files, pkg_file_free); + kh_free(pkg_files, pkg->files, struct pkg_file, pkg_file_free); HASH_FREE(pkg->config_files, pkg_config_file_free); pkg->flags &= ~PKG_LOAD_FILES; break; @@ -1732,21 +1747,21 @@ pkg_is_config_file(struct pkg *p, const char *path, const struct pkg_file **file, struct pkg_config_file **cfile) { - struct pkg_file *f; + khint_t k; struct pkg_config_file *cf; *file = NULL; *cfile = NULL; - HASH_FIND_STR(p->files, path, f); - if (f == NULL) + k = kh_get_pkg_files(p->files, path); + if (k == kh_end(p->files)) return (false); HASH_FIND_STR(p->config_files, path, cf); if (cf == NULL) return (false); - *file = f; + *file = kh_value(p->files, k); *cfile = cf; return (true); @@ -1755,11 +1770,8 @@ pkg_is_config_file(struct pkg *p, const char *path, bool pkg_has_file(struct pkg *p, const char *path) { - struct pkg_file *f; - - HASH_FIND_STR(p->files, path, f); - return (f != NULL ? true : false); + return (kh_get_pkg_files(p->files, path) != kh_end(p->files)); } bool diff --git a/libpkg/pkg_add.c b/libpkg/pkg_add.c index 4cfacea..fad188e 100644 --- a/libpkg/pkg_add.c +++ b/libpkg/pkg_add.c @@ -647,7 +647,7 @@ pkg_add_common(struct pkgdb *db, const char *path, unsigned flags, /* add the user and group if necessary */ - nfiles = HASH_COUNT(pkg->files); + nfiles = kh_size(pkg->files); /* * Extract the files on disk. */ diff --git a/libpkg/pkg_create.c b/libpkg/pkg_create.c index 6ba4a15..13b3327 100644 --- a/libpkg/pkg_create.c +++ b/libpkg/pkg_create.c @@ -75,7 +75,7 @@ pkg_create_from_dir(struct pkg *pkg, const char *root, * Get / compute size / checksum if not provided in the manifest */ - nfiles = HASH_COUNT(pkg->files); + nfiles = kh_size(pkg->files); counter_init("file sizes/checksums", nfiles); hardlinks = kh_init_hardlinks(); diff --git a/libpkg/pkg_cudf.c b/libpkg/pkg_cudf.c index 6a1cb0e..448fdda 100644 --- a/libpkg/pkg_cudf.c +++ b/libpkg/pkg_cudf.c @@ -111,7 +111,7 @@ static int cudf_emit_pkg(struct pkg *pkg, int version, FILE *f, struct pkg_job_universe_item *conflicts_chain) { - struct pkg_dep *dep, *dtmp; + struct pkg_dep *dep; struct pkg_provide *prov, *ptmp; struct pkg_conflict *conflict, *ctmp; struct pkg_job_universe_item *u; @@ -126,15 +126,15 @@ cudf_emit_pkg(struct pkg *pkg, int version, FILE *f, if (fprintf(f, "\nversion: %d\n", version) < 0) return (EPKG_FATAL); - if (HASH_COUNT(pkg->deps) > 0) { + if (kh_size(pkg->deps) > 0) { if (fprintf(f, "depends: ") < 0) return (EPKG_FATAL); - HASH_ITER(hh, pkg->deps, dep, dtmp) { + kh_foreach_value(pkg->deps, dep, { if (cudf_print_element(f, dep->name, - (dep->hh.next != NULL), &column) < 0) { + column + 1 == kh_size(pkg->deps), &column) < 0) { return (EPKG_FATAL); } - } + }); } column = 0; @@ -246,13 +246,13 @@ int pkg_jobs_cudf_emit_file(struct pkg_jobs *j, pkg_jobs_t t, FILE *f) { struct pkg *pkg; - struct pkg_job_universe_item *it, *itmp, *icur; + struct pkg_job_universe_item *it, *icur; int version; if (fprintf(f, "preamble: \n\n") < 0) return (EPKG_FATAL); - HASH_ITER(hh, j->universe->items, it, itmp) { + kh_foreach_value(j->universe->items, it, { /* XXX * Here are dragons: * after sorting it we actually modify the head of the list, but there is @@ -278,7 +278,7 @@ pkg_jobs_cudf_emit_file(struct pkg_jobs *j, pkg_jobs_t t, FILE *f) return (EPKG_FATAL); } } - } + }); if (fprintf(f, "request: \n") < 0) return (EPKG_FATAL); diff --git a/libpkg/pkg_delete.c b/libpkg/pkg_delete.c index c2e8a5a..8c8c569 100644 --- a/libpkg/pkg_delete.c +++ b/libpkg/pkg_delete.c @@ -327,7 +327,7 @@ pkg_delete_files(struct pkg *pkg, unsigned force) int nfiles, cur_file = 0; - nfiles = HASH_COUNT(pkg->files); + nfiles = kh_size(pkg->files); if (nfiles == 0) return (EPKG_OK); diff --git a/libpkg/pkg_jobs.c b/libpkg/pkg_jobs.c index b82ec42..e5aca09 100644 --- a/libpkg/pkg_jobs.c +++ b/libpkg/pkg_jobs.c @@ -1252,11 +1252,11 @@ pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp) static void pkg_jobs_propagate_automatic(struct pkg_jobs *j) { - struct pkg_job_universe_item *unit, *utmp, *cur, *local; + struct pkg_job_universe_item *unit, *cur, *local; struct pkg_job_request *req; bool automatic; - HASH_ITER(hh, j->universe->items, unit, utmp) { + kh_foreach_value(j->universe->items, unit, { if (unit->next == NULL) { /* * For packages that are alone in the installation list @@ -1293,7 +1293,7 @@ pkg_jobs_propagate_automatic(struct pkg_jobs *j) if (cur->pkg->type != PKG_INSTALLED) cur->pkg->automatic = automatic; } - } + }); } static struct pkg_job_request * diff --git a/libpkg/pkg_jobs_conflicts.c b/libpkg/pkg_jobs_conflicts.c index 560331e..bb78925 100644 --- a/libpkg/pkg_jobs_conflicts.c +++ b/libpkg/pkg_jobs_conflicts.c @@ -207,7 +207,7 @@ pkg_conflicts_item_cmp(struct pkg_jobs_conflict_item *a, static bool pkg_conflicts_need_conflict(struct pkg_jobs *j, struct pkg *p1, struct pkg *p2) { - struct pkg_file *fcur, *ftmp, *ff; + struct pkg_file *fcur; struct pkg_dir *df; struct pkg_conflict *c1, *c2; @@ -235,14 +235,13 @@ pkg_conflicts_need_conflict(struct pkg_jobs *j, struct pkg *p1, struct pkg *p2) /* * We need to check all files and dirs and find the similar ones */ - HASH_ITER(hh, p1->files, fcur, ftmp) { - HASH_FIND_STR(p2->files, fcur->path, ff); - if (ff != NULL) + kh_foreach_value(p1->files, fcur, { + if (pkg_has_file(p2, fcur->path)) return (true); HASH_FIND_STR(p2->dirs, fcur->path, df); if (df != NULL) return (true); - } + }); /* XXX pkg dirs are terribly broken */ /* No common paths are found in p1 and p2 */ @@ -466,20 +465,19 @@ static void pkg_conflicts_check_chain_conflict(struct pkg_job_universe_item *it, struct pkg_job_universe_item *local, struct pkg_jobs *j) { - struct pkg_file *fcur, *ftmp, *ff; + struct pkg_file *fcur; struct pkg *p; struct pkg_job_universe_item *cun; struct sipkey *k; - HASH_ITER(hh, it->pkg->files, fcur, ftmp) { + kh_foreach_value(it->pkg->files, fcur, { k = pkg_conflicts_sipkey_init(); /* Check in hash tree */ cun = pkg_conflicts_check_all_paths(j, fcur->path, it, k); if (local != NULL) { /* Filter only new files for remote packages */ - HASH_FIND_STR(local->pkg->files, fcur->path, ff); - if (ff != NULL) + if (pkg_has_file(local->pkg, fcur->path)) continue; } /* Check for local conflict in db */ @@ -492,7 +490,7 @@ pkg_conflicts_check_chain_conflict(struct pkg_job_universe_item *it, assert(cun != NULL); pkg_conflicts_register_chain(j, it, cun, fcur->path); } - } + }); /* XXX: dirs are currently broken terribly */ #if 0 struct pkg_dir *dcur, *dtmp, *df; diff --git a/libpkg/pkg_jobs_universe.c b/libpkg/pkg_jobs_universe.c index 31afd1f..6d59828 100644 --- a/libpkg/pkg_jobs_universe.c +++ b/libpkg/pkg_jobs_universe.c @@ -53,6 +53,69 @@ struct pkg_chain { struct pkg_chain *next; }; +struct pkg_job_universe_item * +pkg_jobs_universe_find(struct pkg_jobs_universe *universe, const char *key) +{ + khint_t k; + + if (universe->items == NULL) + return(NULL); + k = kh_get_pkg_job_universe_items(universe->items, key); + if (k == kh_end(universe->items)) + return (NULL); + return (kh_value(universe->items, k)); +} + +static void +pkg_jobs_universe_add(struct pkg_jobs_universe *universe, struct pkg_job_universe_item *it, bool replace) +{ + khint_t k; + int ret; + + if (universe->items == NULL) + universe->items = kh_init_pkg_job_universe_items(); + k = kh_put_pkg_job_universe_items(universe->items, it->pkg->uid, &ret); + if (ret != 0 || replace) + kh_value(universe->items, k) = it; +} + +static void +pkg_jobs_universe_del(struct pkg_jobs_universe *universe, struct pkg_job_universe_item *it) +{ + khint_t k; + + k = kh_get_pkg_job_universe_items(universe->items, it->pkg->uid); + if (k != kh_end(universe->items)) + kh_del_pkg_job_universe_items(universe->items, k); +} + +static struct pkg_job_universe_item * +pkg_jobs_seen_find(struct pkg_jobs_universe *universe, const char *digest) +{ + khint_t k; + + if (universe->seen == NULL) + return (NULL); + k = kh_get_pkg_jobs_seen(universe->seen, digest); + if (k == kh_end(universe->seen)) + return (NULL); + return (kh_value(universe->seen, k)); +} + +static void +pkg_jobs_seen_add(struct pkg_jobs_universe *universe, struct pkg_job_universe_item *it, + bool replace) +{ + khint_t k; + int ret; + + if (universe->seen == NULL) + universe->seen = kh_init_pkg_jobs_seen(); + k = kh_put_pkg_jobs_seen(universe->seen, it->pkg->digest, &ret); + if (ret != 0 || replace) + kh_value(universe->seen, k) = it; +} + struct pkg * pkg_jobs_universe_get_local(struct pkg_jobs_universe *universe, const char *uid, unsigned flag) @@ -71,7 +134,7 @@ pkg_jobs_universe_get_local(struct pkg_jobs_universe *universe, flag = PKG_LOAD_BASIC|PKG_LOAD_RDEPS|PKG_LOAD_DEPS|PKG_LOAD_ANNOTATIONS; } - HASH_FIND(hh, universe->items, uid, strlen(uid), unit); + unit = pkg_jobs_universe_find(universe, uid); if (unit != NULL) { /* Search local in a universe chain */ cur = unit; @@ -129,7 +192,7 @@ pkg_jobs_universe_get_remote(struct pkg_jobs_universe *universe, PKG_LOAD_ANNOTATIONS|PKG_LOAD_CONFLICTS; } - HASH_FIND(hh, universe->items, uid, strlen(uid), unit); + unit = pkg_jobs_universe_find(universe, uid); if (unit != NULL && unit->pkg->type != PKG_INSTALLED) { /* Search local in a universe chain */ cur = unit; @@ -172,8 +235,7 @@ int pkg_jobs_universe_add_pkg(struct pkg_jobs_universe *universe, struct pkg *pkg, bool force, struct pkg_job_universe_item **found) { - struct pkg_job_universe_item *item, *tmp = NULL; - struct pkg_job_seen *seen; + struct pkg_job_universe_item *item, *seen, *tmp = NULL; pkg_validate(pkg); if (pkg->digest == NULL) { @@ -185,7 +247,7 @@ pkg_jobs_universe_add_pkg(struct pkg_jobs_universe *universe, struct pkg *pkg, } } - HASH_FIND_STR(universe->seen, pkg->digest, seen); + seen = pkg_jobs_seen_find(universe, pkg->digest); if (seen != NULL && !force) { /* * For remote packages we could have the same digest but different repos @@ -193,16 +255,16 @@ pkg_jobs_universe_add_pkg(struct pkg_jobs_universe *universe, struct pkg *pkg, */ bool other_candidate = false; - if (seen->un->pkg->type != PKG_INSTALLED && pkg->type != PKG_INSTALLED) { - if (pkg->reponame && seen->un->pkg->reponame) { + if (seen->pkg->type != PKG_INSTALLED && pkg->type != PKG_INSTALLED) { + if (pkg->reponame && seen->pkg->reponame) { other_candidate = - (strcmp(pkg->reponame, seen->un->pkg->reponame) != 0); + (strcmp(pkg->reponame, seen->pkg->reponame) != 0); } } if (!other_candidate) { if (found != NULL) - *found = seen->un; + *found = seen; return (EPKG_END); } @@ -220,26 +282,12 @@ pkg_jobs_universe_add_pkg(struct pkg_jobs_universe *universe, struct pkg *pkg, item->pkg = pkg; - - HASH_FIND_STR(universe->items, pkg->uid, tmp); - if (tmp == NULL) - HASH_ADD_KEYPTR(hh, universe->items, pkg->uid, strlen(pkg->uid), item); + pkg_jobs_universe_add(universe, item, false); DL_APPEND(tmp, item); - if (seen == NULL) { - seen = calloc(1, sizeof(struct pkg_job_seen)); - if (seen == NULL) { - pkg_emit_errno("pkg_jobs_universe_add_pkg", "calloc: struct pkg_job_seen)"); - return (EPKG_FATAL); - } - seen->digest = pkg->digest; - seen->un = item; - HASH_ADD_KEYPTR(hh, universe->seen, seen->digest, strlen(seen->digest), - seen); - } - - universe->nitems++; + if (seen == NULL) + pkg_jobs_seen_add(universe, item, false); if (found != NULL) *found = item; @@ -269,7 +317,7 @@ pkg_jobs_universe_process_deps(struct pkg_jobs_universe *universe, deps_func = pkg_deps; while (deps_func(pkg, &d) == EPKG_OK) { - HASH_FIND_STR(universe->items, d->uid, unit); + unit = pkg_jobs_universe_find(universe, d->uid); if (unit != NULL) continue; @@ -334,7 +382,7 @@ pkg_jobs_universe_handle_provide(struct pkg_jobs_universe *universe, prhead = NULL; while (pkgdb_it_next(it, &rpkg, flags) == EPKG_OK) { /* Check for local packages */ - HASH_FIND_STR(universe->items, rpkg->uid, unit); + unit = pkg_jobs_universe_find(universe, rpkg->uid); if (unit != NULL) { /* Remote provide is newer, so we can add it */ if (pkg_jobs_universe_process_item(universe, rpkg, @@ -359,8 +407,6 @@ pkg_jobs_universe_handle_provide(struct pkg_jobs_universe *universe, /* Skip seen packages */ if (unit == NULL) { - struct pkg_job_seen *seen; - if (rpkg->digest == NULL) { pkg_debug(3, "no digest found for package %s", rpkg->uid); if (pkg_checksum_calculate(rpkg, universe->j->db) != EPKG_OK) { @@ -632,7 +678,7 @@ pkg_jobs_update_universe_item_priority(struct pkg_jobs_universe *universe, } while (deps_func(it->pkg, &d) == EPKG_OK) { - HASH_FIND_STR(universe->items, d->uid, found); + found = pkg_jobs_universe_find(universe, d->uid); if (found != NULL) { LL_FOREACH(found, cur) { if (cur->priority < priority + 1) @@ -645,7 +691,7 @@ pkg_jobs_update_universe_item_priority(struct pkg_jobs_universe *universe, d = NULL; maxpri = priority; while (rdeps_func(it->pkg, &d) == EPKG_OK) { - HASH_FIND_STR(universe->items, d->uid, found); + found = pkg_jobs_universe_find(universe, d->uid); if (found != NULL) { LL_FOREACH(found, cur) { if (cur->priority >= maxpri) { @@ -661,7 +707,7 @@ pkg_jobs_update_universe_item_priority(struct pkg_jobs_universe *universe, } if (it->pkg->type != PKG_INSTALLED) { while (pkg_conflicts(it->pkg, &c) == EPKG_OK) { - HASH_FIND_STR(universe->items, c->uid, found); + found = pkg_jobs_universe_find(universe, c->uid); if (found != NULL) { LL_FOREACH(found, cur) { if (cur->pkg->type == PKG_INSTALLED) { @@ -689,7 +735,7 @@ pkg_jobs_update_conflict_priority(struct pkg_jobs_universe *universe, while (pkg_conflicts(lp, &c) == EPKG_OK) { rit = NULL; - HASH_FIND_STR(universe->items, c->uid, found); + found = pkg_jobs_universe_find(universe, c->uid); assert(found != NULL); LL_FOREACH(found, cur) { @@ -741,17 +787,16 @@ pkg_jobs_universe_replacement_free(struct pkg_job_replace *r) void pkg_jobs_universe_free(struct pkg_jobs_universe *universe) { - struct pkg_job_universe_item *un, *untmp, *cur, *curtmp; - - HASH_ITER(hh, universe->items, un, untmp) { - HASH_DEL(universe->items, un); + struct pkg_job_universe_item *un, *cur, *curtmp; + kh_foreach_value(universe->items, un, { LL_FOREACH_SAFE(un, cur, curtmp) { pkg_free(cur->pkg); free(cur); } - } - HASH_FREE(universe->seen, free); + }); + kh_destroy_pkg_job_universe_items(universe->items); + kh_destroy_pkg_jobs_seen(universe->seen); HASH_FREE(universe->provides, pkg_jobs_universe_provide_free); LL_FREE(universe->uid_replaces, pkg_jobs_universe_replacement_free); } @@ -773,26 +818,6 @@ pkg_jobs_universe_new(struct pkg_jobs *j) return (universe); } -struct pkg_job_universe_item * -pkg_jobs_universe_find(struct pkg_jobs_universe *universe, const char *uid) -{ - struct pkg_job_universe_item *unit; - - HASH_FIND_STR(universe->items, uid, unit); - - return (unit); -} - -struct pkg_job_seen * -pkg_jobs_universe_seen(struct pkg_jobs_universe *universe, const char *digest) -{ - struct pkg_job_seen *seen; - - HASH_FIND_STR(universe->seen, digest, seen); - - return (seen); -} - void pkg_jobs_universe_change_uid(struct pkg_jobs_universe *universe, struct pkg_job_universe_item *unit, @@ -833,15 +858,15 @@ pkg_jobs_universe_change_uid(struct pkg_jobs_universe *universe, LL_PREPEND(universe->uid_replaces, replacement); } - HASH_DELETE(hh, universe->items, unit); + pkg_jobs_universe_del(universe, unit); free(unit->pkg->uid); unit->pkg->uid = strdup(new_uid); - HASH_FIND(hh, universe->items, new_uid, uidlen, found); + found = pkg_jobs_universe_find(universe, new_uid); if (found != NULL) DL_APPEND(found, unit); else - HASH_ADD_KEYPTR(hh, universe->items, new_uid, uidlen, unit); + pkg_jobs_universe_add(universe, unit, false); } @@ -998,14 +1023,14 @@ pkg_jobs_universe_select_candidate(struct pkg_job_universe_item *chain, void pkg_jobs_universe_process_upgrade_chains(struct pkg_jobs *j) { - struct pkg_job_universe_item *unit, *tmp, *cur, *local; + struct pkg_job_universe_item *unit, *cur, *local; struct pkg_job_request *req; struct pkg_job_request_item *rit, *rtmp; bool conservative = false; conservative = pkg_object_bool(pkg_config_get("CONSERVATIVE_UPGRADE")); - HASH_ITER(hh, j->universe->items, unit, tmp) { + kh_foreach_value(j->universe->items, unit, { unsigned vercnt = 0; HASH_FIND_STR(j->request_add, unit->pkg->uid, req); @@ -1078,7 +1103,7 @@ pkg_jobs_universe_process_upgrade_chains(struct pkg_jobs *j) HASH_ADD_KEYPTR(hh, j->request_add, selected->pkg->uid, strlen (selected->pkg->uid), req); } - } + }); } struct pkg_job_universe_item* @@ -1095,7 +1120,7 @@ pkg_jobs_universe_get_upgrade_candidates(struct pkg_jobs_universe *universe, UT_array *candidates; struct pkg **p = NULL; - HASH_FIND(hh, universe->items, uid, strlen(uid), unit); + unit = pkg_jobs_universe_find(universe, uid); if (unit != NULL) { /* * If a unit has been found, we have already found the potential @@ -1161,7 +1186,7 @@ pkg_jobs_universe_get_upgrade_candidates(struct pkg_jobs_universe *universe, return (NULL); } - HASH_FIND(hh, universe->items, uid, strlen(uid), unit); + unit = pkg_jobs_universe_find(universe, uid); utarray_free(candidates); return (unit); diff --git a/libpkg/pkg_solve.c b/libpkg/pkg_solve.c index eb4beb7..a672ab3 100644 --- a/libpkg/pkg_solve.c +++ b/libpkg/pkg_solve.c @@ -613,7 +613,7 @@ pkg_solve_add_request_rule(struct pkg_solve_problem *problem, cnt ++; } - if (cnt > 1 && var->unit->hh.keylen != 0) { + if (cnt > 1 && var->unit->pkg->uid != NULL) { kv_prepend(typeof(rule), problem->rules, rule); /* Also need to add pairs of conflicts */ LL_FOREACH(req->item, item) { @@ -715,7 +715,7 @@ static int pkg_solve_process_universe_variable(struct pkg_solve_problem *problem, struct pkg_solve_variable *var) { - struct pkg_dep *dep, *dtmp; + struct pkg_dep *dep; struct pkg_conflict *conflict, *ctmp; struct pkg *pkg; struct pkg_solve_variable *cur_var; @@ -729,10 +729,10 @@ pkg_solve_process_universe_variable(struct pkg_solve_problem *problem, pkg = cur_var->unit->pkg; /* Depends */ - HASH_ITER(hh, pkg->deps, dep, dtmp) { + kh_foreach_value(pkg->deps, dep, { if (pkg_solve_add_depend_rule(problem, cur_var, dep) != EPKG_OK) continue; - } + }); /* Conflicts */ HASH_ITER(hh, pkg->conflicts, conflict, ctmp) { @@ -816,7 +816,7 @@ struct pkg_solve_problem * pkg_solve_jobs_to_sat(struct pkg_jobs *j) { struct pkg_solve_problem *problem; - struct pkg_job_universe_item *un, *utmp; + struct pkg_job_universe_item *un; size_t i = 0; problem = calloc(1, sizeof(struct pkg_solve_problem)); @@ -827,7 +827,7 @@ pkg_solve_jobs_to_sat(struct pkg_jobs *j) } problem->j = j; - problem->nvars = j->universe->nitems; + problem->nvars = kh_size(j->universe->items); problem->variables = calloc(problem->nvars, sizeof(struct pkg_solve_variable)); problem->sat = picosat_init(); kv_init(problem->rules); @@ -845,15 +845,15 @@ pkg_solve_jobs_to_sat(struct pkg_jobs *j) picosat_adjust(problem->sat, problem->nvars); /* Parse universe */ - HASH_ITER(hh, j->universe->items, un, utmp) { + kh_foreach_value(j->universe->items, un, { /* Add corresponding variables */ if (pkg_solve_add_variable(un, problem, &i) == EPKG_FATAL) goto err; - } + }); /* Add rules for all conflict chains */ - HASH_ITER(hh, j->universe->items, un, utmp) { + kh_foreach_value(j->universe->items, un, { struct pkg_solve_variable *var; HASH_FIND_STR(problem->variables_by_uid, un->pkg->uid, var); @@ -864,7 +864,7 @@ pkg_solve_jobs_to_sat(struct pkg_jobs *j) } if (pkg_solve_process_universe_variable(problem, var) != EPKG_OK) goto err; - } + }); if (kv_size(problem->rules) == 0) { pkg_debug(1, "problem has no requests"); @@ -891,6 +891,8 @@ pkg_solve_picosat_iter(struct pkg_solve_problem *problem, int iter) var = &problem->variables[i]; is_installed = false; + if (var == NULL) + printf("sucks\n"); LL_FOREACH(var, cur) { if (cur->unit->pkg->type == PKG_INSTALLED) { is_installed = true; diff --git a/libpkg/private/pkg.h b/libpkg/private/pkg.h index 7c7c718..bdbd657 100644 --- a/libpkg/private/pkg.h +++ b/libpkg/private/pkg.h @@ -121,6 +121,31 @@ return (EPKG_OK); \ } while (0) +#define kh_next(name, head, data, attrib) do { \ + khint_t k; \ + if (head == NULL) \ + return (EPKG_END); \ + if (data == NULL) { \ + k = kh_begin(head); \ + } else { \ + k = kh_get_##name(head, (data)->attrib); \ + k++; \ + } \ + while (k != kh_end(head) && !kh_exist(head, k)) \ + k++; \ + if (k == kh_end(head)) \ + return (EPKG_END); \ + data = kh_value(head, k); \ + return (EPKG_OK); \ +} while (0) + +#define kh_free(name, head, type, free_func) do { \ + type *_todelete; \ + kh_foreach_value(head, _todelete, free_func(_todelete)); \ + kh_destroy_##name(head); \ + head = NULL; \ +} while (0) + extern int eventpipe; extern int64_t debug_level; extern bool developer_mode; @@ -129,6 +154,9 @@ extern const char *pkg_rootdir; struct pkg_repo_it; struct pkg_repo; +KHASH_MAP_INIT_STR(pkg_deps, struct pkg_dep *); +KHASH_MAP_INIT_STR(pkg_files, struct pkg_file *); + struct pkg { bool direct; bool locked; @@ -161,11 +189,11 @@ struct pkg { int64_t flatsize; int64_t old_flatsize; int64_t timestamp; - struct pkg_dep *deps; - struct pkg_dep *rdeps; + kh_pkg_deps_t *deps; + kh_pkg_deps_t *rdeps; struct pkg_strel *categories; struct pkg_strel *licenses; - struct pkg_file *files; + kh_pkg_files_t *files; struct pkg_dir *dirs; struct pkg_option *options; struct pkg_user *users; @@ -195,7 +223,6 @@ struct pkg_dep { char *version; char *uid; bool locked; - UT_hash_handle hh; }; enum pkg_conflict_type { @@ -225,7 +252,6 @@ struct pkg_file { char gname[MAXLOGNAME]; mode_t perm; u_long fflags; - UT_hash_handle hh; }; struct pkg_dir { diff --git a/libpkg/private/pkg_jobs.h b/libpkg/private/pkg_jobs.h index f3e7fcc..86cba85 100644 --- a/libpkg/private/pkg_jobs.h +++ b/libpkg/private/pkg_jobs.h @@ -44,10 +44,11 @@ struct pkg_job_universe_item { struct pkg *pkg; int priority; bool processed; - UT_hash_handle hh; struct pkg_job_universe_item *next, *prev; }; +KHASH_MAP_INIT_STR(pkg_job_universe_items, struct pkg_job_universe_item *); + struct pkg_job_request_item { struct pkg *pkg; struct pkg_job_universe_item *unit; @@ -69,11 +70,7 @@ struct pkg_solved { struct pkg_solved *prev, *next; }; -struct pkg_job_seen { - struct pkg_job_universe_item *un; - const char *digest; - UT_hash_handle hh; -}; +KHASH_MAP_INIT_STR(pkg_jobs_seen, struct pkg_job_universe_item *); struct pkg_job_provide { struct pkg_job_universe_item *un; @@ -89,14 +86,12 @@ struct pkg_job_replace { struct pkg_job_replace *next; }; - struct pkg_jobs_universe { - struct pkg_job_universe_item *items; - struct pkg_job_seen *seen; + kh_pkg_job_universe_items_t *items; + kh_pkg_jobs_seen_t *seen; struct pkg_job_provide *provides; struct pkg_job_replace *uid_replaces; struct pkg_jobs *j; - size_t nitems; }; struct pkg_jobs_conflict_item { @@ -173,12 +168,6 @@ int pkg_jobs_universe_process_item(struct pkg_jobs_universe *universe, struct pkg *pkg, struct pkg_job_universe_item **result); /* - * Check if the specified digest was seen in the universe - */ -struct pkg_job_seen* pkg_jobs_universe_seen(struct pkg_jobs_universe *universe, - const char *digest); - -/* * Search for an entry corresponding to the uid in the universe */ struct pkg_job_universe_item* pkg_jobs_universe_find(struct pkg_jobs_universe diff --git a/src/audit.c b/src/audit.c index d4a0c85..4cdb1a1 100644 --- a/src/audit.c +++ b/src/audit.c @@ -129,7 +129,6 @@ exec_audit(int argc, char **argv) int ret = EX_OK; const char *portaudit_site = NULL; struct sbuf *sb; - const char *key; kh_pkgs_t *check = NULL; db_dir = pkg_object_string(pkg_config_get("PKG_DBDIR")); @@ -273,7 +272,7 @@ exec_audit(int argc, char **argv) #endif if (pkg_audit_process(audit) == EPKG_OK) { - kh_foreach(check, key, pkg, { + kh_foreach_value(check, pkg, { if (pkg_audit_is_vulnerable(audit, pkg, quiet, &sb)) { vuln ++; printf("%s", sbuf_data(sb)); diff --git a/src/clean.c b/src/clean.c index af99dc8..51d72f9 100644 --- a/src/clean.c +++ b/src/clean.c @@ -301,7 +301,7 @@ exec_clean(int argc, char **argv) } } if (sumlist != NULL) { - kh_foreach(sumlist, sum, cksum, free(cksum)); + kh_foreach_value(sumlist, cksum, free(cksum)); kh_destroy_sum(sumlist); } diff --git a/src/version.c b/src/version.c index f05b8f3..3be0322 100644 --- a/src/version.c +++ b/src/version.c @@ -349,13 +349,12 @@ hash_indexfile(const char *indexfilename) static void free_categories(void) { - const char *key, *k; char *v; struct category *cat; - kh_foreach(categories, key, cat, { + kh_foreach_value(categories, cat, { free(cat->name); - kh_foreach(cat->ports, k, v, free(v)); + kh_foreach_value(cat->ports, v, free(v)); kh_destroy_ports(cat->ports); free(cat); }); @@ -365,13 +364,12 @@ free_categories(void) static void free_index(kh_index_t *index) { - const char *key __unused; struct index_entry *entry; if (index == NULL) return; - kh_foreach(index, key, entry, { + kh_foreach_value(index, entry, { free(entry->origin); free(entry->version); free(entry);