commit:     83f3df9d2df983c10b370be423606be53536a909
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Mon Mar 25 20:44:01 2019 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Mon Mar 25 20:44:01 2019 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=83f3df9d

libq: split out cache-related funcs from main/qsearch

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

 libq/Makefile.am |   1 +
 libq/Makefile.in |  24 ++++--
 libq/cache.c     | 234 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 libq/cache.h     |  50 ++++++++++++
 4 files changed, 302 insertions(+), 7 deletions(-)

diff --git a/libq/Makefile.am b/libq/Makefile.am
index 80b651c..26647ee 100644
--- a/libq/Makefile.am
+++ b/libq/Makefile.am
@@ -3,6 +3,7 @@ QFILES = \
        atom.c atom.h \
        basename.c basename.h \
        busybox.h \
+       cache.c cache.h \
        colors.c colors.h \
        copy_file.c copy_file.h \
        eat_file.c eat_file.h \

diff --git a/libq/Makefile.in b/libq/Makefile.in
index a5e43fc..6c529c2 100644
--- a/libq/Makefile.in
+++ b/libq/Makefile.in
@@ -241,13 +241,14 @@ CONFIG_CLEAN_FILES =
 CONFIG_CLEAN_VPATH_FILES =
 LTLIBRARIES = $(noinst_LTLIBRARIES)
 libq_la_LIBADD =
-am__objects_1 = libq_la-atom.lo libq_la-basename.lo libq_la-colors.lo \
-       libq_la-copy_file.lo libq_la-eat_file.lo libq_la-hash_fd.lo \
-       libq_la-human_readable.lo libq_la-md5_sha1_sum.lo \
-       libq_la-prelink.lo libq_la-profile.lo libq_la-rmspace.lo \
-       libq_la-safe_io.lo libq_la-scandirat.lo libq_la-set.lo \
-       libq_la-vdb.lo libq_la-xarray.lo libq_la-xchdir.lo \
-       libq_la-xmkdir.lo libq_la-xregex.lo libq_la-xsystem.lo
+am__objects_1 = libq_la-atom.lo libq_la-basename.lo libq_la-cache.lo \
+       libq_la-colors.lo libq_la-copy_file.lo libq_la-eat_file.lo \
+       libq_la-hash_fd.lo libq_la-human_readable.lo \
+       libq_la-md5_sha1_sum.lo libq_la-prelink.lo libq_la-profile.lo \
+       libq_la-rmspace.lo libq_la-safe_io.lo libq_la-scandirat.lo \
+       libq_la-set.lo libq_la-vdb.lo libq_la-xarray.lo \
+       libq_la-xchdir.lo libq_la-xmkdir.lo libq_la-xregex.lo \
+       libq_la-xsystem.lo
 am_libq_la_OBJECTS = $(am__objects_1)
 libq_la_OBJECTS = $(am_libq_la_OBJECTS)
 AM_V_lt = $(am__v_lt_@AM_V@)
@@ -1444,6 +1445,7 @@ QFILES = \
        atom.c atom.h \
        basename.c basename.h \
        busybox.h \
+       cache.c cache.h \
        colors.c colors.h \
        copy_file.c copy_file.h \
        eat_file.c eat_file.h \
@@ -1528,6 +1530,7 @@ distclean-compile:
 
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libq_la-atom.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ 
@am__quote@./$(DEPDIR)/libq_la-basename.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libq_la-cache.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libq_la-colors.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ 
@am__quote@./$(DEPDIR)/libq_la-copy_file.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ 
@am__quote@./$(DEPDIR)/libq_la-eat_file.Plo@am__quote@
@@ -1582,6 +1585,13 @@ libq_la-basename.lo: basename.c
 @AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) 
$(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@  $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC 
$(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) 
$(DEFAULT_INCLUDES) $(INCLUDES) $(libq_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) 
$(CFLAGS) -c -o libq_la-basename.lo `test -f 'basename.c' || echo 
'$(srcdir)/'`basename.c
 
+libq_la-cache.lo: cache.c
+@am__fastdepCC_TRUE@   $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC 
$(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) 
$(DEFAULT_INCLUDES) $(INCLUDES) $(libq_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) 
$(CFLAGS) -MT libq_la-cache.lo -MD -MP -MF $(DEPDIR)/libq_la-cache.Tpo -c -o 
libq_la-cache.lo `test -f 'cache.c' || echo '$(srcdir)/'`cache.c
+@am__fastdepCC_TRUE@   $(AM_V_at)$(am__mv) $(DEPDIR)/libq_la-cache.Tpo 
$(DEPDIR)/libq_la-cache.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      $(AM_V_CC)source='cache.c' 
object='libq_la-cache.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) 
$(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC 
$(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) 
$(DEFAULT_INCLUDES) $(INCLUDES) $(libq_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) 
$(CFLAGS) -c -o libq_la-cache.lo `test -f 'cache.c' || echo '$(srcdir)/'`cache.c
+
 libq_la-colors.lo: colors.c
 @am__fastdepCC_TRUE@   $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC 
$(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) 
$(DEFAULT_INCLUDES) $(INCLUDES) $(libq_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) 
$(CFLAGS) -MT libq_la-colors.lo -MD -MP -MF $(DEPDIR)/libq_la-colors.Tpo -c -o 
libq_la-colors.lo `test -f 'colors.c' || echo '$(srcdir)/'`colors.c
 @am__fastdepCC_TRUE@   $(AM_V_at)$(am__mv) $(DEPDIR)/libq_la-colors.Tpo 
$(DEPDIR)/libq_la-colors.Plo

diff --git a/libq/cache.c b/libq/cache.c
new file mode 100644
index 0000000..a130381
--- /dev/null
+++ b/libq/cache.c
@@ -0,0 +1,234 @@
+/*
+ * Copyright 2005-2019 Gentoo Foundation
+ * Distributed under the terms of the GNU General Public License v2
+ *
+ * Copyright 2005-2008 Ned Ludd        - <[email protected]>
+ * Copyright 2005-2014 Mike Frysinger  - <[email protected]>
+ * Copyright 2018-     Fabian Groffen  - <[email protected]>
+ */
+
+#include "main.h"
+
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <xalloc.h>
+
+#include "atom.h"
+#include "cache.h"
+
+#ifdef EBUG
+static void
+cache_dump(portage_cache *cache)
+{
+       if (!cache)
+               errf("Cache is empty !");
+
+       printf("DEPEND     : %s\n", cache->DEPEND);
+       printf("RDEPEND    : %s\n", cache->RDEPEND);
+       printf("SLOT       : %s\n", cache->SLOT);
+       printf("SRC_URI    : %s\n", cache->SRC_URI);
+       printf("RESTRICT   : %s\n", cache->RESTRICT);
+       printf("HOMEPAGE   : %s\n", cache->HOMEPAGE);
+       printf("LICENSE    : %s\n", cache->LICENSE);
+       printf("DESCRIPTION: %s\n", cache->DESCRIPTION);
+       printf("KEYWORDS   : %s\n", cache->KEYWORDS);
+       printf("INHERITED  : %s\n", cache->INHERITED);
+       printf("IUSE       : %s\n", cache->IUSE);
+       printf("CDEPEND    : %s\n", cache->CDEPEND);
+       printf("PDEPEND    : %s\n", cache->PDEPEND);
+       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
+
+static portage_cache *cache_read_file_pms(const char *file);
+static portage_cache *cache_read_file_md5(const char *file);
+
+portage_cache *
+cache_read_file(int portcachedir_type, const char *file)
+{
+       if (portcachedir_type == CACHE_METADATA_MD5)
+               return(cache_read_file_md5(file));
+       else if (portcachedir_type == CACHE_METADATA_PMS)
+               return(cache_read_file_pms(file));
+       warn("Unknown metadata cache type!");
+       return NULL;
+}
+
+static portage_cache *
+cache_read_file_pms(const char *file)
+{
+       struct stat s;
+       char *ptr;
+       FILE *f;
+       portage_cache *ret = NULL;
+       size_t len;
+
+       if ((f = fopen(file, "r")) == NULL)
+               goto err;
+
+       if (fstat(fileno(f), &s) != 0)
+               goto err;
+       len = sizeof(*ret) + s.st_size + 1;
+       ret = xzalloc(len);
+       ptr = (char*)ret;
+       ret->_data = ptr + sizeof(*ret);
+       if ((off_t)fread(ret->_data, 1, s.st_size, f) != s.st_size)
+               goto err;
+
+       ret->atom = atom_explode(file);
+       ret->DEPEND = ret->_data;
+#define next_line(curr, next) \
+       if ((ptr = strchr(ret->curr, '\n')) == NULL) { \
+               warn("Invalid cache file '%s'", file); \
+               goto err; \
+       } \
+       ret->next = ptr+1; \
+       *ptr = '\0';
+       next_line(DEPEND, RDEPEND)
+       next_line(RDEPEND, SLOT)
+       next_line(SLOT, SRC_URI)
+       next_line(SRC_URI, RESTRICT)
+       next_line(RESTRICT, HOMEPAGE)
+       next_line(HOMEPAGE, LICENSE)
+       next_line(LICENSE, DESCRIPTION)
+       next_line(DESCRIPTION, KEYWORDS)
+       next_line(KEYWORDS, INHERITED)
+       next_line(INHERITED, IUSE)
+       next_line(IUSE, CDEPEND)
+       next_line(CDEPEND, PDEPEND)
+       next_line(PDEPEND, PROVIDE)
+       next_line(PROVIDE, EAPI)
+       next_line(EAPI, PROPERTIES)
+#undef next_line
+       ptr = strchr(ptr+1, '\n');
+       if (ptr == NULL) {
+               warn("Invalid cache file '%s' - could not find end of cache 
data", file);
+               goto err;
+       }
+       *ptr = '\0';
+
+       fclose(f);
+
+       return ret;
+
+err:
+       if (f) fclose(f);
+       if (ret) cache_free(ret);
+       return NULL;
+}
+
+static portage_cache *
+cache_read_file_md5(const char *file)
+{
+       struct stat s;
+       char *ptr, *endptr;
+       FILE *f;
+       portage_cache *ret = NULL;
+       size_t len;
+
+       if ((f = fopen(file, "r")) == NULL)
+               goto err;
+
+       if (fstat(fileno(f), &s) != 0)
+               goto err;
+       len = sizeof(*ret) + s.st_size + 1;
+       ret = xzalloc(len);
+       ptr = (char*)ret;
+       ret->_data = ptr + sizeof(*ret);
+       if ((off_t)fread(ret->_data, 1, s.st_size, f) != s.st_size)
+               goto err;
+
+       ret->atom = atom_explode(file);
+
+       /* We have a block of key=value\n data.
+        * KEY=VALUE\n
+        * Where KEY does NOT contain:
+        * \0 \n =
+        * And VALUE does NOT contain:
+        * \0 \n
+        * */
+#define assign_var_cmp(keyname, cmpkey) \
+       if (strncmp(keyptr, cmpkey, strlen(cmpkey)) == 0) { \
+               ret->keyname = valptr; \
+               continue; \
+       }
+#define assign_var(keyname) \
+       assign_var_cmp(keyname, #keyname);
+
+       ptr = ret->_data;
+       endptr = strchr(ptr, '\0');
+       if (endptr == NULL) {
+                       warn("Invalid cache file '%s' - could not find end of 
cache data", file);
+                       goto err;
+       }
+
+       while (ptr != NULL && ptr != endptr) {
+               char *keyptr;
+               char *valptr;
+               keyptr = ptr;
+               valptr = strchr(ptr, '=');
+               if (valptr == NULL) {
+                       warn("Invalid cache file '%s' val", file);
+                       goto err;
+               }
+               *valptr = '\0';
+               valptr++;
+               ptr = strchr(valptr, '\n');
+               if (ptr == NULL) {
+                       warn("Invalid cache file '%s' key", file);
+                       goto err;
+               }
+               *ptr = '\0';
+               ptr++;
+
+               assign_var(CDEPEND);
+               assign_var(DEPEND);
+               assign_var(DESCRIPTION);
+               assign_var(EAPI);
+               assign_var(HOMEPAGE);
+               assign_var(INHERITED);
+               assign_var(IUSE);
+               assign_var(KEYWORDS);
+               assign_var(LICENSE);
+               assign_var(PDEPEND);
+               assign_var(PROPERTIES);
+               assign_var(PROVIDE);
+               assign_var(RDEPEND);
+               assign_var(RESTRICT);
+               assign_var(SLOT);
+               assign_var(SRC_URI);
+               assign_var(DEFINED_PHASES);
+               assign_var(REQUIRED_USE);
+               assign_var(_eclasses_);
+               assign_var(_md5_);
+               warn("Cache file '%s' with unknown key %s", file, keyptr);
+       }
+#undef assign_var
+#undef assign_var_cmp
+
+       fclose(f);
+
+       return ret;
+
+err:
+       if (f) fclose(f);
+       if (ret) cache_free(ret);
+       return NULL;
+}
+
+void
+cache_free(portage_cache *cache)
+{
+       if (!cache)
+               errf("Cache is empty !");
+       atom_implode(cache->atom);
+       free(cache);
+}

diff --git a/libq/cache.h b/libq/cache.h
new file mode 100644
index 0000000..971417c
--- /dev/null
+++ b/libq/cache.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2005-2019 Gentoo Foundation
+ * Distributed under the terms of the GNU General Public License v2
+ *
+ * Copyright 2005-2010 Ned Ludd        - <[email protected]>
+ * Copyright 2005-2014 Mike Frysinger  - <[email protected]>
+ * Copyright 2019-     Fabian Groffen  - <[email protected]>
+ */
+
+#ifndef _CACHE_H
+#define _CACHE_H 1
+
+typedef struct {
+       char *_data;
+       char *DEPEND;        /* line 1 */
+       char *RDEPEND;
+       char *SLOT;
+       char *SRC_URI;
+       char *RESTRICT;      /* line 5 */
+       char *HOMEPAGE;
+       char *LICENSE;
+       char *DESCRIPTION;
+       char *KEYWORDS;
+       char *INHERITED;     /* line 10 */
+       char *IUSE;
+       char *CDEPEND;
+       char *PDEPEND;
+       char *PROVIDE;       /* line 14 */
+       char *EAPI;
+       char *PROPERTIES;
+       depend_atom *atom;
+       /* These are MD5-Cache only */
+       char *DEFINED_PHASES;
+       char *REQUIRED_USE;
+       char *_eclasses_;
+       char *_md5_;
+} portage_cache;
+
+portage_cache *
+cache_read_file(int portcachedir_type, const char *file);
+void cache_free(portage_cache *cache);
+
+enum {
+       CACHE_EBUILD = 1,
+       CACHE_METADATA = 2,
+       CACHE_METADATA_PMS = 10,
+       CACHE_METADATA_MD5 = 11,
+};
+
+#endif

Reply via email to