public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Fabian Groffen" <grobian@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/portage-utils:master commit in: libq/
Date: Thu, 25 Apr 2019 09:22:12 +0000 (UTC)	[thread overview]
Message-ID: <1556178148.7d91bbe43a2c53b1431a8529848532deebae5524.grobian@gentoo> (raw)

commit:     7d91bbe43a2c53b1431a8529848532deebae5524
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Thu Apr 25 07:42:28 2019 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Thu Apr 25 07:42:28 2019 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=7d91bbe4

libq/cache: add mode for plain ebuilds (not using any cache)

Make cache a slight misnomer by also reading ebuilds directly if no
cache appears available.  This way cache can be used to transparently
query a repository in the best possible way.

Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 libq/cache.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 libq/cache.h |  10 +++-
 2 files changed, 171 insertions(+), 14 deletions(-)

diff --git a/libq/cache.c b/libq/cache.c
index 31178e7..eec39db 100644
--- a/libq/cache.c
+++ b/libq/cache.c
@@ -12,6 +12,7 @@
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <ctype.h>
 #include <xalloc.h>
 
 #include "cache.h"
@@ -20,7 +21,7 @@
 
 #ifdef EBUG
 static void
-cache_dump(portage_cache *cache)
+cache_dump(cache_pkg_meta *cache)
 {
 	if (!cache)
 		errf("Cache is empty !");
@@ -41,11 +42,6 @@ cache_dump(portage_cache *cache)
 	printf("PROVIDE    : %s\n", cache->PROVIDE);
 	printf("EAPI       : %s\n", cache->EAPI);
 	printf("PROPERTIES : %s\n", cache->PROPERTIES);
-	if (!cache->atom) return;
-	printf("CATEGORY   : %s\n", cache->atom->CATEGORY);
-	printf("PN         : %s\n", cache->atom->PN);
-	printf("PV         : %s\n", cache->atom->PV);
-	printf("PVR        : %s\n", cache->atom->PVR);
 }
 #endif
 
@@ -59,7 +55,7 @@ cache_open(const char *sroot, const char *portdir)
 	char buf[_Q_PATH_MAX];
 
 	snprintf(buf, sizeof(buf), "%s/%s", portdir, portcachedir_md5);
-	dir = q_vdb_open(sroot, buf);
+	dir = q_vdb_open2(sroot, buf, true);
 	if (dir != NULL) {
 		ret = xmalloc(sizeof(cache_ctx));
 		ret->dir_ctx = dir;
@@ -68,7 +64,7 @@ cache_open(const char *sroot, const char *portdir)
 	}
 
 	snprintf(buf, sizeof(buf), "%s/%s", portdir, portcachedir_pms);
-	dir = q_vdb_open(sroot, buf);
+	dir = q_vdb_open2(sroot, buf, true);
 	if (dir != NULL) {
 		ret = xmalloc(sizeof(cache_ctx));
 		ret->dir_ctx = dir;
@@ -76,6 +72,17 @@ cache_open(const char *sroot, const char *portdir)
 		return ret;
 	}
 
+	dir = q_vdb_open2(sroot, portdir, true);
+	if (dir != NULL) {
+		ret = xmalloc(sizeof(cache_ctx));
+		ret->dir_ctx = dir;
+		ret->cachetype = CACHE_EBUILD;
+		return ret;
+	}
+
+	warnf("could not open repository at %s (under root %s)",
+			portdir, sroot);
+
 	return NULL;
 }
 
@@ -98,7 +105,10 @@ cache_open_cat(cache_ctx *ctx, const char *name)
 cache_cat_ctx *
 cache_next_cat(cache_ctx *ctx)
 {
-	return q_vdb_next_cat(ctx->dir_ctx);
+	cache_cat_ctx *ret = q_vdb_next_cat(ctx->dir_ctx);
+	if (ret != NULL)
+		ret->ctx = (q_vdb_ctx *)ctx;
+	return ret;
 }
 
 void
@@ -116,7 +126,48 @@ cache_open_pkg(cache_cat_ctx *cat_ctx, const char *name)
 cache_pkg_ctx *
 cache_next_pkg(cache_cat_ctx *cat_ctx)
 {
-	return q_vdb_next_pkg(cat_ctx);
+	cache_ctx *ctx = (cache_ctx *)(cat_ctx->ctx);
+	cache_pkg_ctx *ret = NULL;
+
+	if (ctx->cachetype == CACHE_EBUILD) {
+		char *p;
+
+		/* serve *.ebuild files each as separate pkg_ctx with name set
+		 * to CAT/P like in VDB and metadata */
+		do {
+			if (ctx->ebuilddir_pkg_ctx == NULL) {
+				q_vdb_ctx *pkgdir = &ctx->ebuilddir_ctx;
+
+				if ((ctx->ebuilddir_pkg_ctx = q_vdb_next_pkg(cat_ctx)) == NULL)
+					return NULL;
+
+				pkgdir->portroot_fd = -1;
+				pkgdir->vdb_fd = cat_ctx->fd;
+				pkgdir->dir = NULL;
+
+				ctx->ebuilddir_cat_ctx =
+					q_vdb_open_cat(pkgdir, ctx->ebuilddir_pkg_ctx->name);
+			}
+
+			ret = q_vdb_next_pkg(ctx->ebuilddir_cat_ctx);
+			if (ret == NULL) {
+				ctx->ebuilddir_pkg_ctx = NULL;
+			} else {
+				if ((p = strstr(ret->name, ".ebuild")) == NULL) {
+					q_vdb_close_pkg(ret);
+					ret = NULL;
+				} else {
+					/* "zap" the pkg such that it looks like CAT/P */
+					ret->cat_ctx = cat_ctx;
+					*p = '\0';
+				}
+			}
+		} while (ret == NULL);
+	} else {
+		ret = q_vdb_next_pkg(cat_ctx);
+	}
+
+	return ret;
 }
 
 static cache_pkg_meta *
@@ -292,6 +343,103 @@ err:
 	return NULL;
 }
 
+static cache_pkg_meta *
+cache_read_file_ebuild(cache_pkg_ctx *pkg_ctx)
+{
+	FILE *f;
+	struct stat s;
+	cache_pkg_meta *ret = NULL;
+	size_t len;
+	char *p;
+	char *q;
+	char *r;
+	char **key;
+
+	if ((f = fdopen(pkg_ctx->fd, "r")) == NULL)
+		goto err;
+
+	if (fstat(pkg_ctx->fd, &s) != 0)
+		goto err;
+
+	len = sizeof(*ret) + s.st_size + 1;
+	ret = xzalloc(len);
+	p = (char *)ret;
+	ret->_data = p + sizeof(*ret);
+	if ((off_t)fread(ret->_data, 1, s.st_size, f) != s.st_size)
+		goto err;
+
+	p = ret->_data;
+	do {
+		if ((p = strchr(p, '\n')) == NULL)
+			break;
+		q = p;
+		while (*p >= 'A' && *p <= 'Z')
+			p++;
+
+		key = NULL;
+		if (q < p && *p == '=') {
+			*p++ = '\0';
+			/* match variable against which ones we look for */
+#define match_key(X) else if (strcmp(q, #X) == 0) key = &ret->X
+			if (1 == 0); /* dummy for syntax */
+			match_key(DEPEND);
+			match_key(RDEPEND);
+			match_key(SLOT);
+			match_key(SRC_URI);
+			match_key(RESTRICT);
+			match_key(HOMEPAGE);
+			match_key(LICENSE);
+			match_key(DESCRIPTION);
+			match_key(KEYWORDS);
+			match_key(IUSE);
+			match_key(CDEPEND);
+			match_key(PDEPEND);
+			match_key(EAPI);
+			match_key(REQUIRED_USE);
+#undef match_key
+		}
+
+		if (key != NULL) {
+			q = p;
+			if (*q == '"' || *q == '\'') {
+				/* find matching quote */
+				q = ++p;
+				do {
+					while (*p != '\0' && *p != *q)
+						p++;
+					if (*p == *q) {
+						for (r = p - 1; r > q; r--)
+							if (*r != '\\')
+								break;
+						if (r != q && (p - 1 - r) % 2 == 1)
+							continue;
+					}
+					break;
+				} while (1);
+			} else {
+				/* find first whitespace */
+				while (!isspace((int)*p))
+					p++;
+			}
+			*p = '\0';
+			*key = q;
+		}
+	} while (p != NULL);
+
+	fclose(f);
+	pkg_ctx->fd = -1;
+
+	return ret;
+
+err:
+	if (f)
+		fclose(f);
+	pkg_ctx->fd = -1;
+	if (ret)
+		cache_close_meta(ret);
+	return NULL;
+}
+
 cache_pkg_meta *
 cache_pkg_read(cache_pkg_ctx *pkg_ctx)
 {
@@ -308,6 +456,8 @@ cache_pkg_read(cache_pkg_ctx *pkg_ctx)
 		return cache_read_file_md5(pkg_ctx);
 	} else if (ctx->cachetype == CACHE_METADATA_PMS) {
 		return cache_read_file_pms(pkg_ctx);
+	} else if (ctx->cachetype == CACHE_EBUILD) {
+		return cache_read_file_ebuild(pkg_ctx);
 	}
 
 	warn("Unknown metadata cache type!");
@@ -351,7 +501,6 @@ cache_foreach_pkg(const char *sroot, const char *portdir,
 		}
 	}
 
-	cache_close(ctx);
 	return ret;
 }
 
@@ -382,14 +531,14 @@ cache_foreach_pkg_sorted(const char *sroot, const char *portdir,
 
 	cat_cnt = scandirat(ctx->dir_ctx->vdb_fd,
 			".", &cat_de, q_vdb_filter_cat, catsortfunc);
-	for (c = 0; c < cat_cnt; ++c) {
+	for (c = 0; c < cat_cnt; c++) {
 		cat_ctx = cache_open_cat(ctx, cat_de[c]->d_name);
 		if (!cat_ctx)
 			continue;
 
 		pkg_cnt = scandirat(ctx->dir_ctx->vdb_fd,
 				cat_de[c]->d_name, &pkg_de, q_vdb_filter_pkg, pkgsortfunc);
-		for (p = 0; p < pkg_cnt; ++p) {
+		for (p = 0; p < pkg_cnt; p++) {
 			if (pkg_de[p]->d_name[0] == '-')
 				continue;
 

diff --git a/libq/cache.h b/libq/cache.h
index c16efac..a4c5803 100644
--- a/libq/cache.h
+++ b/libq/cache.h
@@ -15,7 +15,15 @@
 
 typedef struct cache_ctx {
 	q_vdb_ctx *dir_ctx;
-	enum { CACHE_UNSET = 0, CACHE_METADATA_MD5, CACHE_METADATA_PMS } cachetype;
+	enum {
+		CACHE_UNSET = 0,
+		CACHE_METADATA_MD5,
+		CACHE_METADATA_PMS,
+		CACHE_EBUILD,
+	} cachetype;
+	q_vdb_pkg_ctx *ebuilddir_pkg_ctx;
+	q_vdb_cat_ctx *ebuilddir_cat_ctx;
+	q_vdb_ctx ebuilddir_ctx;
 } cache_ctx;
 #define cache_cat_ctx q_vdb_cat_ctx
 #define cache_pkg_ctx q_vdb_pkg_ctx


             reply	other threads:[~2019-04-25  9:22 UTC|newest]

Thread overview: 207+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-25  9:22 Fabian Groffen [this message]
  -- strict thread matches above, loose matches on Subject: below --
2025-08-28 20:38 [gentoo-commits] proj/portage-utils:master commit in: libq/ Fabian Groffen
2025-08-28  7:27 Fabian Groffen
2025-08-25 11:28 Fabian Groffen
2025-08-24 16:34 Fabian Groffen
2025-08-21 19:17 Fabian Groffen
2025-08-21 14:12 Fabian Groffen
2025-08-21  9:13 Fabian Groffen
2025-08-20 16:04 Fabian Groffen
2025-06-27 14:00 Fabian Groffen
2025-06-25 15:40 Fabian Groffen
2025-04-21 14:44 Fabian Groffen
2024-07-03 19:44 Fabian Groffen
2024-04-08 19:27 Fabian Groffen
2024-02-01  8:21 Fabian Groffen
2024-02-01  8:21 Fabian Groffen
2024-01-31 20:41 Fabian Groffen
2024-01-31 19:30 Fabian Groffen
2024-01-31 19:29 Fabian Groffen
2024-01-27 13:28 Fabian Groffen
2023-04-21 19:11 Fabian Groffen
2023-01-30 14:14 Fabian Groffen
2022-05-26 14:36 Fabian Groffen
2022-05-26 14:36 Fabian Groffen
2022-05-20 17:15 Fabian Groffen
2022-05-20 17:15 Fabian Groffen
2022-05-19  8:32 Fabian Groffen
2022-05-19  8:16 Fabian Groffen
2022-05-19  7:45 Fabian Groffen
2022-02-12 17:13 Fabian Groffen
2022-02-12 17:13 Fabian Groffen
2022-02-06 14:51 Fabian Groffen
2022-02-06 14:29 Fabian Groffen
2022-02-06 13:27 Fabian Groffen
2022-02-06 13:27 Fabian Groffen
2022-02-06 12:22 Fabian Groffen
2021-12-29 12:20 Fabian Groffen
2021-12-26 13:59 Fabian Groffen
2021-12-26 13:59 Fabian Groffen
2021-12-26 13:59 Fabian Groffen
2021-12-26 13:59 Fabian Groffen
2021-12-13  8:39 Fabian Groffen
2021-12-13  8:39 Fabian Groffen
2021-11-13 14:27 Fabian Groffen
2021-10-09 12:13 Fabian Groffen
2021-10-04  6:28 Fabian Groffen
2021-10-04  6:28 Fabian Groffen
2021-10-03 10:49 Fabian Groffen
2021-06-23  7:14 Fabian Groffen
2021-06-14  9:34 Fabian Groffen
2021-06-14  9:34 Fabian Groffen
2021-06-14  9:34 Fabian Groffen
2021-06-14  9:34 Fabian Groffen
2021-06-14  9:34 Fabian Groffen
2021-06-14  9:34 Fabian Groffen
2021-06-01 19:43 Fabian Groffen
2021-05-23 10:54 Fabian Groffen
2021-05-10  9:15 Fabian Groffen
2021-04-29 15:04 Fabian Groffen
2021-04-29 13:47 Fabian Groffen
2021-04-29 13:24 Fabian Groffen
2021-03-13 12:44 Fabian Groffen
2021-02-20 12:06 Fabian Groffen
2021-02-20 11:44 Fabian Groffen
2021-02-17 20:23 Fabian Groffen
2021-02-17 20:23 Fabian Groffen
2021-01-15 20:05 Fabian Groffen
2020-06-27  9:38 Fabian Groffen
2020-06-07 10:41 Fabian Groffen
2020-05-25 18:19 Fabian Groffen
2020-05-25 18:02 Fabian Groffen
2020-05-25 13:26 Fabian Groffen
2020-05-25 11:20 Fabian Groffen
2020-05-25 11:06 Fabian Groffen
2020-05-25 10:43 Fabian Groffen
2020-05-25 10:43 Fabian Groffen
2020-05-25 10:43 Fabian Groffen
2020-05-25 10:43 Fabian Groffen
2020-05-25 10:43 Fabian Groffen
2020-05-17 12:35 Fabian Groffen
2020-05-17 12:35 Fabian Groffen
2020-02-03 13:17 Fabian Groffen
2020-02-03 13:09 Fabian Groffen
2020-01-26 19:31 Fabian Groffen
2020-01-22 19:54 Fabian Groffen
2020-01-22 19:54 Fabian Groffen
2020-01-20 19:54 Fabian Groffen
2020-01-20 19:34 Fabian Groffen
2020-01-19 19:36 Fabian Groffen
2020-01-19 19:09 Fabian Groffen
2020-01-19 19:09 Fabian Groffen
2020-01-19 19:09 Fabian Groffen
2020-01-19 19:09 Fabian Groffen
2020-01-19 16:37 Fabian Groffen
2020-01-19 12:37 Fabian Groffen
2020-01-19 10:05 Fabian Groffen
2020-01-19  9:49 Fabian Groffen
2020-01-19  9:49 Fabian Groffen
2020-01-17  8:22 Fabian Groffen
2020-01-05 16:08 Fabian Groffen
2020-01-05 16:08 Fabian Groffen
2020-01-05 16:08 Fabian Groffen
2020-01-02 15:09 Fabian Groffen
2020-01-02 14:07 Fabian Groffen
2020-01-02 14:07 Fabian Groffen
2020-01-02 14:07 Fabian Groffen
2020-01-02 11:55 Fabian Groffen
2020-01-02 11:19 Fabian Groffen
2019-12-30 17:24 Fabian Groffen
2019-12-27 21:19 Fabian Groffen
2019-12-27 16:57 Fabian Groffen
2019-12-27 16:57 Fabian Groffen
2019-11-29 13:22 Fabian Groffen
2019-11-20 17:23 Fabian Groffen
2019-11-19 20:28 Fabian Groffen
2019-11-17 15:12 Fabian Groffen
2019-11-17 15:12 Fabian Groffen
2019-11-13 18:19 Fabian Groffen
2019-11-13 15:48 Fabian Groffen
2019-11-13 15:20 Fabian Groffen
2019-11-09 10:29 Fabian Groffen
2019-09-26 14:06 Fabian Groffen
2019-09-26 14:06 Fabian Groffen
2019-09-26 14:06 Fabian Groffen
2019-09-26 14:06 Fabian Groffen
2019-09-26 13:00 Fabian Groffen
2019-09-25 15:05 Fabian Groffen
2019-09-21 19:53 Fabian Groffen
2019-09-21 19:53 Fabian Groffen
2019-07-14 18:51 Fabian Groffen
2019-07-13 15:37 Fabian Groffen
2019-07-13  9:50 Fabian Groffen
2019-07-12 18:04 Fabian Groffen
2019-06-19  7:41 Fabian Groffen
2019-06-10 10:09 Fabian Groffen
2019-06-05  7:57 Fabian Groffen
2019-05-21 14:12 Fabian Groffen
2019-05-14 20:19 Fabian Groffen
2019-05-14 20:19 Fabian Groffen
2019-05-11 11:11 Fabian Groffen
2019-05-11  7:14 Fabian Groffen
2019-05-11  7:14 Fabian Groffen
2019-05-10 15:32 Fabian Groffen
2019-05-10 15:32 Fabian Groffen
2019-05-10 15:32 Fabian Groffen
2019-05-07  6:19 Fabian Groffen
2019-05-06 16:04 Fabian Groffen
2019-05-06 16:04 Fabian Groffen
2019-05-05 20:05 Fabian Groffen
2019-05-05 18:13 Fabian Groffen
2019-05-05  8:58 Fabian Groffen
2019-05-04 11:53 Fabian Groffen
2019-05-03 11:45 Fabian Groffen
2019-05-02 15:17 Fabian Groffen
2019-05-01 19:09 Fabian Groffen
2019-04-30  8:20 Fabian Groffen
2019-04-30  7:54 Fabian Groffen
2019-04-28 17:10 Fabian Groffen
2019-04-28 16:21 Fabian Groffen
2019-04-28 16:02 Fabian Groffen
2019-04-27  8:38 Fabian Groffen
2019-04-25 17:36 Fabian Groffen
2019-04-25  9:22 Fabian Groffen
2019-04-25  9:22 Fabian Groffen
2019-04-25  9:22 Fabian Groffen
2019-04-19 11:47 Fabian Groffen
2019-03-27 10:55 Fabian Groffen
2019-03-11 20:55 Fabian Groffen
2019-03-09 18:58 Fabian Groffen
2019-02-27 20:53 Fabian Groffen
2019-02-27 20:53 Fabian Groffen
2019-02-05 14:19 Fabian Groffen
2018-12-20 20:02 Fabian Groffen
2018-12-20 20:02 Fabian Groffen
2018-12-20 18:24 Fabian Groffen
2018-04-09  7:15 Fabian Groffen
2018-04-05 13:31 Fabian Groffen
2018-04-05 12:46 Fabian Groffen
2018-04-03 20:00 Fabian Groffen
2018-03-26 18:41 Fabian Groffen
2018-03-25 14:13 Fabian Groffen
2018-03-25 14:00 Fabian Groffen
2018-03-23 20:17 Fabian Groffen
2018-03-23 11:56 Fabian Groffen
2018-03-23 11:29 Fabian Groffen
2017-12-29 11:45 Fabian Groffen
2017-12-29 11:45 Fabian Groffen
2017-12-29 11:45 Fabian Groffen
2016-12-29  2:25 Mike Frysinger
2016-11-12 17:23 Mike Frysinger
2016-02-14  1:26 Mike Frysinger
2016-02-14  1:26 Mike Frysinger
2015-11-26  8:43 Mike Frysinger
2015-10-15 22:00 Mike Frysinger
2015-10-15 22:00 Mike Frysinger
2015-05-31  8:31 Mike Frysinger
2015-05-19 17:37 Mike Frysinger
2015-02-24  1:26 Mike Frysinger
2015-02-24  1:26 Mike Frysinger
2015-02-24  1:26 Mike Frysinger
2015-02-21 18:06 Mike Frysinger
2015-02-16 11:47 Mike Frysinger
2014-03-11  4:53 Mike Frysinger
2014-03-08  5:51 Mike Frysinger
2014-03-08  5:51 Mike Frysinger
2014-03-08  5:51 Mike Frysinger
2014-03-08  5:51 Mike Frysinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1556178148.7d91bbe43a2c53b1431a8529848532deebae5524.grobian@gentoo \
    --to=grobian@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox