commit: 1b4c0049ff8a9297d0242c87d74946508a58c75c
Author: Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Sat Jul 13 09:39:11 2019 +0000
Commit: Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Sat Jul 13 09:39:11 2019 +0000
URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=1b4c0049
libq/tree: greatly simplify tree_get_vdb_atoms
use tree_foreach_pkg_fast() to construct the set of string atom names
using atom_format()
this function likely should be removed as the consumer should work on
the atoms instead of their string representation
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>
libq/tree.c | 91 ++++++++++++++++++-------------------------------------------
1 file changed, 27 insertions(+), 64 deletions(-)
diff --git a/libq/tree.c b/libq/tree.c
index 6bb6a89..f4314d0 100644
--- a/libq/tree.c
+++ b/libq/tree.c
@@ -1042,77 +1042,40 @@ tree_get_atom(tree_pkg_ctx *pkg_ctx, bool complete)
return pkg_ctx->atom;
}
-set *
-tree_get_vdb_atoms(const char *sroot, const char *svdb, int fullcpv)
-{
- tree_ctx *ctx;
+struct get_vdb_atoms_state {
+ set *cpf;
+ bool fullcpv;
+};
- int cfd, j;
- int dfd, i;
+static int tree_get_vdb_atoms_cb(tree_pkg_ctx *pkg_ctx, void *priv)
+{
+ struct get_vdb_atoms_state *state = (struct get_vdb_atoms_state *)priv;
+ depend_atom *atom = tree_get_atom(pkg_ctx, false);
- char buf[_Q_PATH_MAX];
- char slot[_Q_PATH_MAX];
- char *slotp = slot;
- size_t slot_len;
+ if (state->fullcpv) {
+ state->cpf = add_set(atom_format("%[CATEGORY]%[PF]", atom),
state->cpf);
+ } else {
+ state->cpf = add_set_unique(atom_format("%[CATEGORY]%[PN]",
atom),
+ state->cpf, NULL);
+ }
- struct dirent **cat;
- struct dirent **pf;
+ return 0;
+}
- depend_atom *atom = NULL;
- set *cpf = NULL;
+set *
+tree_get_vdb_atoms(const char *sroot, const char *svdb, int fullcpv)
+{
+ tree_ctx *ctx;
+ struct get_vdb_atoms_state state = {
+ .cpf = NULL,
+ .fullcpv = fullcpv != 0
+ };
ctx = tree_open_vdb(sroot, svdb);
if (!ctx)
return NULL;
-
- /* scan the cat first */
- cfd = scandirat(ctx->tree_fd, ".", &cat, tree_filter_cat, alphasort);
- if (cfd < 0)
- goto fuckit;
-
- for (j = 0; j < cfd; j++) {
- dfd = scandirat(ctx->tree_fd, cat[j]->d_name,
- &pf, tree_filter_pkg, alphasort);
- if (dfd < 0)
- continue;
- for (i = 0; i < dfd; i++) {
- int blen = snprintf(buf, sizeof(buf), "%s/%s/SLOT",
- cat[j]->d_name, pf[i]->d_name);
- if (blen < 0 || (size_t)blen >= sizeof(buf)) {
- warnf("unable to parse long package: %s/%s",
- cat[j]->d_name, pf[i]->d_name);
- continue;
- }
-
- /* Chop the SLOT for the atom parsing. */
- buf[blen - 5] = '\0';
- if ((atom = atom_explode(buf)) == NULL)
- continue;
- /* Restore the SLOT. */
- buf[blen - 5] = '/';
-
- slot_len = sizeof(slot);
- eat_file_at(ctx->tree_fd, buf, &slotp, &slot_len);
- rmspace(slot);
-
- if (fullcpv) {
- if (atom->PR_int)
- snprintf(buf, sizeof(buf),
"%s/%s-%s-r%i",
- atom->CATEGORY,
atom->PN, atom->PV, atom->PR_int);
- else
- snprintf(buf, sizeof(buf), "%s/%s-%s",
- atom->CATEGORY,
atom->PN, atom->PV);
- } else {
- snprintf(buf, sizeof(buf), "%s/%s",
atom->CATEGORY, atom->PN);
- }
- atom_implode(atom);
- cpf = add_set(buf, cpf);
- }
- scandir_free(pf, dfd);
- }
- scandir_free(cat, cfd);
-
- fuckit:
+ tree_foreach_pkg_fast(ctx, tree_get_vdb_atoms_cb, &state, NULL);
tree_close(ctx);
- return cpf;
+
+ return state.cpf;
}