commit:     3794f21d3da7f182c85b63e87f0e073b5df3de18
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Mon May 25 10:32:27 2020 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Mon May 25 10:32:27 2020 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=3794f21d

libq/tree: have tree_read_file_binpkg populate some meta fields

The SHA1 and SIZE fields might be necessary, so psuedo fill them in
here, as we don't have a meta that contains them, except the file
itself.

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

 libq/tree.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/libq/tree.c b/libq/tree.c
index 7fbb739..d901fc6 100644
--- a/libq/tree.c
+++ b/libq/tree.c
@@ -17,6 +17,7 @@
 
 #include "atom.h"
 #include "eat_file.h"
+#include "hash.h"
 #include "rmspace.h"
 #include "scandirat.h"
 #include "set.h"
@@ -982,10 +983,48 @@ static tree_pkg_meta *
 tree_read_file_binpkg(tree_pkg_ctx *pkg_ctx)
 {
        tree_pkg_meta *m = xzalloc(sizeof(tree_pkg_meta));
+       int newfd = dup(pkg_ctx->fd);
 
        xpak_process_fd(pkg_ctx->fd, true, m, tree_read_file_binpkg_xpak_cb);
        pkg_ctx->fd = -1;  /* closed by xpak_process_fd */
 
+       /* fill in some properties which are not available, but would be in
+        * Packages, and used to verify the package ... this is somewhat
+        * fake, but allows to transparantly use a dir of binpkgs */
+       if (newfd != -1) {
+               size_t fsize;
+               size_t needlen = 40 + 1 + 19 + 1;
+               size_t pos = (size_t)m->Q__eclasses_;
+               size_t len = (size_t)m->Q__md5_;
+
+               if (len - pos < needlen) {
+                       char *old_data = m->Q__data;
+                       len += ((needlen / BUFSIZ) + 1) * BUFSIZ;
+                       m->Q__data = xrealloc(m->Q__data, len);
+                       m->Q__md5_ = (char *)len;
+
+                       /* re-position existing keys */
+                       if (old_data != NULL && m->Q__data != old_data) {
+                               char **newdata = (char **)m;
+                               int elems = sizeof(tree_pkg_meta) / sizeof(char 
*);
+                               while (elems-- > 1)  /* skip Q__data itself */
+                                       if (newdata[elems] != NULL)
+                                               newdata[elems] =
+                                                       m->Q__data + 
(newdata[elems] - old_data);
+                       }
+               }
+
+               m->Q_SHA1 = m->Q__data + pos;
+               m->Q_SIZE = m->Q_SHA1 + 40 + 1;
+               pos += needlen;
+               m->Q__eclasses_ = (char *)pos;
+
+               lseek(newfd, 0, SEEK_SET);  /* reposition at the whole file */
+               if (hash_multiple_file_fd(newfd, NULL, m->Q_SHA1, NULL, NULL,
+                               NULL, NULL, &fsize, HASH_SHA1) == 0)
+                       snprintf(m->Q_SIZE, 19 + 1, "%zu", fsize);
+       }
+
        return m;
 }
 

Reply via email to