commit: 16ff9817f7d68d37e39302dac6f632c306cdce4f Author: Fabian Groffen <grobian <AT> gentoo <DOT> org> AuthorDate: Wed Feb 17 20:14:01 2021 +0000 Commit: Fabian Groffen <grobian <AT> gentoo <DOT> org> CommitDate: Wed Feb 17 20:14:01 2021 +0000 URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=16ff9817
libq/tree: make tree_match_atom return path for each match Along with some changes to make sure returned atoms remain valid (e.g. not getting an expired pointer for category name), build a patch to the returned objects, that should allow opening the ebuild or something. Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org> libq/tree.c | 41 +++++++++++++++++++++++++++++++++-------- libq/tree.h | 5 ++++- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/libq/tree.c b/libq/tree.c index e41a0cb..293d7f7 100644 --- a/libq/tree.c +++ b/libq/tree.c @@ -1,5 +1,5 @@ /* - * Copyright 2005-2020 Gentoo Foundation + * Copyright 2005-2021 Gentoo Foundation * Distributed under the terms of the GNU General Public License v2 * * Copyright 2005-2008 Ned Ludd - <so...@gentoo.org> @@ -55,6 +55,10 @@ tree_open_int(const char *sroot, const char *tdir, bool quiet) goto cp_error; } + if (sroot[0] == '/' && sroot[1] == '\0') + sroot = ""; + snprintf(ctx->path, sizeof(ctx->path), "%s/%s", sroot, tdir); + ctx->dir = fdopendir(ctx->tree_fd); if (ctx->dir == NULL) goto cv_error; @@ -128,6 +132,25 @@ tree_open_vdb(const char *sroot, const char *svdb) return ret; } +tree_ctx * +tree_open_ebuild(const char *sroot, const char *portdir) +{ + tree_ctx *ret = tree_open_int(sroot, portdir, true); + if (ret != NULL) { + char buf[_Q_PATH_MAX]; + char *repo = NULL; + size_t repolen = 0; + + snprintf(buf, sizeof(buf), "%s%s/%s", sroot, portdir, portrepo_name); + if (eat_file(buf, &repo, &repolen)) { + (void)rmspace(repo); + ret->repo = repo; + } + ret->cachetype = CACHE_EBUILD; + } + return ret; +} + static const char binpkg_packages[] = "Packages"; tree_ctx * tree_open_binpkg(const char *sroot, const char *spkg) @@ -1491,10 +1514,10 @@ depend_atom * tree_get_atom(tree_pkg_ctx *pkg_ctx, bool complete) { if (pkg_ctx->atom == NULL) { - pkg_ctx->atom = atom_explode(pkg_ctx->name); + pkg_ctx->atom = + atom_explode_cat(pkg_ctx->name, (char *)pkg_ctx->cat_ctx->name); if (pkg_ctx->atom == NULL) return NULL; - pkg_ctx->atom->CATEGORY = (char *)pkg_ctx->cat_ctx->name; } if (complete) { @@ -1708,14 +1731,15 @@ tree_match_atom(tree_ctx *ctx, depend_atom *query, int flags) while ((pkg_ctx = tree_next_pkg(C)) != NULL) { \ atom = tree_get_atom(pkg_ctx, \ query->SLOT != NULL || flags & TREE_MATCH_FULL_ATOM); \ -fprintf(stderr, "fbg: %s\n", atom_to_string(atom)); \ if (flags & TREE_MATCH_VIRTUAL || \ - strcmp(atom->CATEGORY, "virtual") != 0) \ + strcmp(atom->CATEGORY, "virtual") != 0) { \ if (atom_compare(atom, query) == EQUAL) { \ - tree_match_ctx *n; \ - n = xzalloc(sizeof(tree_match_ctx)); \ - n->free_atom = false; \ + tree_match_ctx *n = xzalloc(sizeof(tree_match_ctx)); \ n->atom = atom; \ + snprintf(n->path, sizeof(n->path), "%s/%s/%s%s", \ + (char *)C->ctx->path, C->name, pkg_ctx->name, \ + C->ctx->cachetype == CACHE_EBUILD ? ".ebuild" : \ + C->ctx->cachetype == CACHE_BINPKGS ? ".tbz2" : ""); \ if (flags & TREE_MATCH_METADATA) \ n->meta = tree_pkg_read(pkg_ctx); \ n->next = ret; \ @@ -1723,6 +1747,7 @@ fprintf(stderr, "fbg: %s\n", atom_to_string(atom)); \ } \ if (flags & TREE_MATCH_FIRST && ret != NULL) \ break; \ + } \ } \ C->pkg_cur = 0; /* reset to allow another traversal */ \ } diff --git a/libq/tree.h b/libq/tree.h index 900b998..f756fd4 100644 --- a/libq/tree.h +++ b/libq/tree.h @@ -1,5 +1,5 @@ /* - * Copyright 2005-2020 Gentoo Foundation + * Copyright 2005-2021 Gentoo Foundation * Distributed under the terms of the GNU General Public License v2 */ @@ -24,6 +24,7 @@ typedef struct tree_match_ctx tree_match_ctx; struct tree_ctx { int portroot_fd; int tree_fd; + char path[_Q_PATH_MAX]; DIR *dir; struct dirent **cat_de; size_t cat_cnt; @@ -122,6 +123,7 @@ struct tree_metadata_xml { struct tree_match_ctx { depend_atom *atom; tree_pkg_meta *meta; + char path[_Q_PATH_MAX + 48]; tree_match_ctx *next; int free_atom; }; @@ -131,6 +133,7 @@ typedef int (tree_pkg_cb)(tree_pkg_ctx *, void *priv); tree_ctx *tree_open(const char *sroot, const char *portdir); tree_ctx *tree_open_vdb(const char *sroot, const char *svdb); +tree_ctx *tree_open_ebuild(const char *sroot, const char *portdir); tree_ctx *tree_open_binpkg(const char *sroot, const char *spkg); void tree_close(tree_ctx *ctx); tree_cat_ctx *tree_open_cat(tree_ctx *ctx, const char *name);