From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1088075-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id 5E585138334
	for <garchives@archives.gentoo.org>; Sun,  5 May 2019 08:58:37 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 5CEF6E0833;
	Sun,  5 May 2019 08:58:36 +0000 (UTC)
Received: from smtp.gentoo.org (mail.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id 2E22CE0833
	for <gentoo-commits@lists.gentoo.org>; Sun,  5 May 2019 08:58:35 +0000 (UTC)
Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 2EC7C34362A
	for <gentoo-commits@lists.gentoo.org>; Sun,  5 May 2019 08:58:34 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 0A6B0566
	for <gentoo-commits@lists.gentoo.org>; Sun,  5 May 2019 08:58:32 +0000 (UTC)
From: "Fabian Groffen" <grobian@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Fabian Groffen" <grobian@gentoo.org>
Message-ID: <1557046007.0bbdb3f1328dcd0dd91c1ea502da3ba3e1fd364d.grobian@gentoo>
Subject: [gentoo-commits] proj/portage-utils:master commit in: libq/
X-VCS-Repository: proj/portage-utils
X-VCS-Files: libq/cache.c libq/cache.h
X-VCS-Directories: libq/
X-VCS-Committer: grobian
X-VCS-Committer-Name: Fabian Groffen
X-VCS-Revision: 0bbdb3f1328dcd0dd91c1ea502da3ba3e1fd364d
X-VCS-Branch: master
Date: Sun,  5 May 2019 08:58:32 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: e6a5277e-e9df-493b-a54e-86ae9941c3fe
X-Archives-Hash: ce3294cc556fcfb67af9f175b8cb0489

commit:     0bbdb3f1328dcd0dd91c1ea502da3ba3e1fd364d
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Sun May  5 08:46:47 2019 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Sun May  5 08:46:47 2019 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=0bbdb3f1

libq/cache: add support for reading metadata.xml

currently only one field is extracted: email

Bug: https://bugs.gentoo.org/685052
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 libq/cache.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 libq/cache.h |  9 ++++++
 2 files changed, 106 insertions(+), 2 deletions(-)

diff --git a/libq/cache.c b/libq/cache.c
index a8b4ede..c0ea85e 100644
--- a/libq/cache.c
+++ b/libq/cache.c
@@ -167,6 +167,9 @@ cache_next_pkg(cache_cat_ctx *cat_ctx)
 					ctx->ebuilddir_pkg_ctx = NULL;
 					return NULL;
 				}
+
+				/* "zap" the pkg such that it looks like CAT/P */
+				ctx->ebuilddir_cat_ctx->name = cat_ctx->name;
 			}
 
 			ret = q_vdb_next_pkg(ctx->ebuilddir_cat_ctx);
@@ -178,8 +181,6 @@ cache_next_pkg(cache_cat_ctx *cat_ctx)
 					cache_close_pkg(ret);
 					ret = NULL;
 				} else {
-					/* "zap" the pkg such that it looks like CAT/P */
-					ret->cat_ctx->name = cat_ctx->name;
 					*p = '\0';
 				}
 			}
@@ -529,6 +530,100 @@ cache_close_meta(cache_pkg_meta *cache)
 	free(cache);
 }
 
+cache_metadata_xml *
+cache_read_metadata(cache_pkg_ctx *pkg_ctx)
+{
+	cache_ctx *ctx = (cache_ctx *)(pkg_ctx->cat_ctx->ctx);
+	int fd;
+	FILE *f;
+	struct stat s;
+	char *xbuf;
+	char *p;
+	char *q;
+	size_t len;
+	cache_metadata_xml *ret = NULL;
+	struct elist *emailw = NULL;
+	char buf[_Q_PATH_MAX];
+
+	/* lame @$$ XML parsing, I don't want to pull in a real parser
+	 * library because we only retrieve one element for now: email
+	 * technically speaking, email may occur only once in a maintainer
+	 * tag, but practically speaking we don't care at all, so we can
+	 * just extract everything between <email> and </email> */
+
+	if (ctx->cachetype == CACHE_EBUILD) {
+		fd = openat(pkg_ctx->cat_ctx->fd, "metadata", O_RDONLY | O_CLOEXEC);
+	} else {
+		depend_atom *atom;
+		snprintf(buf, sizeof(buf), "%s/%s",
+				pkg_ctx->cat_ctx->name, pkg_ctx->name);
+		atom = atom_explode(buf);
+		snprintf(buf, sizeof(buf), "../../%s/%s/metadata.xml",
+				atom->CATEGORY, atom->PN);
+		atom_implode(atom);
+		fd = openat(ctx->vdb_fd, buf, O_RDONLY | O_CLOEXEC);
+	}
+
+	if (fd == -1)
+		return NULL;
+
+	if ((f = fdopen(fd, "r")) == NULL) {
+		close(fd);
+		return NULL;
+	}
+
+	if (fstat(fd, &s) != 0) {
+		fclose(f);
+		return NULL;
+	}
+
+	len = sizeof(*ret) + s.st_size + 1;
+	p = xbuf = xzalloc(len);
+	if ((off_t)fread(p, 1, s.st_size, f) != s.st_size) {
+		free(p);
+		fclose(f);
+		pkg_ctx->fd = -1;
+		return NULL;
+	}
+
+	ret = xmalloc(sizeof(*ret));
+	ret->email = NULL;
+
+	while ((q = strstr(p, "<email>")) != NULL) {
+		p = q + sizeof("<email>") - 1;
+		if ((q = strstr(p, "</email>")) == NULL)
+			break;
+		*q = '\0';
+		rmspace(p);
+		if (emailw == NULL) {
+			emailw = ret->email = xmalloc(sizeof(*emailw));
+		} else {
+			emailw = emailw->next = xmalloc(sizeof(*emailw));
+		}
+		emailw->next = NULL;
+		emailw->addr = xstrdup(p);
+		p = q + 1;
+	}
+
+	free(xbuf);
+	fclose(f);
+	return ret;
+}
+
+void
+cache_close_metadata(cache_metadata_xml *meta_ctx)
+{
+	struct elist *e;
+	while (meta_ctx->email != NULL) {
+		e = meta_ctx->email;
+		free(e->addr);
+		e = e->next;
+		free(meta_ctx->email);
+		meta_ctx->email = e;
+	}
+	free(meta_ctx);
+}
+
 void
 cache_close_pkg(cache_pkg_ctx *pkg_ctx)
 {

diff --git a/libq/cache.h b/libq/cache.h
index 74f45b8..2ad2e78 100644
--- a/libq/cache.h
+++ b/libq/cache.h
@@ -43,6 +43,13 @@ typedef struct {
 	char *_md5_;
 } cache_pkg_meta;
 
+typedef struct {
+	struct elist {
+		char *addr;
+		struct elist *next;
+	} *email;
+} cache_metadata_xml;
+
 typedef int (cache_pkg_cb)(cache_pkg_ctx *, void *priv);
 typedef int (cache_cat_filter)(cache_cat_ctx *, void *priv);
 
@@ -55,6 +62,8 @@ cache_pkg_ctx *cache_open_pkg(cache_cat_ctx *cat_ctx, const char *name);
 cache_pkg_ctx *cache_next_pkg(cache_cat_ctx *cat_ctx);
 cache_pkg_meta *cache_pkg_read(cache_pkg_ctx *pkg_ctx);
 void cache_close_meta(cache_pkg_meta *cache);
+cache_metadata_xml *cache_read_metadata(cache_pkg_ctx *pkg_ctx);
+void cache_close_metadata(cache_metadata_xml *meta_ctx);
 void cache_close_pkg(cache_pkg_ctx *pkg_ctx);
 int cache_foreach_pkg(const char *sroot, const char *portdir,
 		cache_pkg_cb callback, void *priv, cache_cat_filter filter);