This is an automated email from the git hooks/post-receive script.

guillem pushed a commit to branch master
in repository dpkg.

View the commit online:
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=92a85658bdf1e590c7d66af8047c347984db1b26

commit 92a85658bdf1e590c7d66af8047c347984db1b26
Author: Guillem Jover <guil...@debian.org>
AuthorDate: Wed Aug 15 19:31:56 2018 +0200

    libdpkg: Change pkg_name() and pkgbin_name() back to get non-const structs
    
    This partially reverts commit cd96cca4b2cf83f46d6e289418ed06e3c2ef7066.
    
    It is not safe to assume that the pkgname_arcqual member has been
    populated before. The caller might have used a local variable filled
    manually. So we should always check whether it is NULL and fill it
    ourselves, before returning it.
    
    In addition now that we have the const function variants we can turn
    these back into non-const so that we can do the archqualified package
    name generation, and cache it for later use.
---
 lib/dpkg/dpkg-db.h                      | 11 ++++++---
 lib/dpkg/parse.c                        |  2 +-
 lib/dpkg/pkg-show.c                     |  8 ++++--
 lib/dpkg/t/.gitignore                   |  1 +
 lib/dpkg/t/Makefile.am                  |  1 +
 lib/dpkg/t/{t-mod-db.c => t-pkg-show.c} | 44 ++++++++++++++++++---------------
 6 files changed, 40 insertions(+), 27 deletions(-)

diff --git a/lib/dpkg/dpkg-db.h b/lib/dpkg/dpkg-db.h
index 7b7591ebe..de8745b02 100644
--- a/lib/dpkg/dpkg-db.h
+++ b/lib/dpkg/dpkg-db.h
@@ -110,7 +110,8 @@ struct pkgbin {
   bool essential;
   enum pkgmultiarch multiarch;
   const struct dpkg_arch *arch;
-  /** The fully qualified package name, i.e. "pkgname:archqual". */
+  /** The following is the "pkgname:archqual" cached string, if this was a
+   * C++ class this member would be mutable. */
   const char *pkgname_archqual;
   const char *description;
   const char *maintainer;
@@ -383,9 +384,11 @@ void varbuf_add_pkgbin_name(struct varbuf *vb, const 
struct pkginfo *pkg,
 const char *
 pkgbin_name_archqual(const struct pkginfo *pkg, const struct pkgbin *pkgbin);
 
-const char *pkgbin_name(const struct pkginfo *pkg, const struct pkgbin *pkgbin,
-                        enum pkg_name_arch_when pnaw);
-const char *pkg_name(const struct pkginfo *pkg, enum pkg_name_arch_when pnaw);
+const char *
+pkgbin_name(struct pkginfo *pkg, struct pkgbin *pkgbin,
+            enum pkg_name_arch_when pnaw);
+const char *
+pkg_name(struct pkginfo *pkg, enum pkg_name_arch_when pnaw);
 
 const char *
 pkgbin_name_const(const struct pkginfo *pkg, const struct pkgbin *pkgbin,
diff --git a/lib/dpkg/parse.c b/lib/dpkg/parse.c
index 155f80ad2..b0db8179d 100644
--- a/lib/dpkg/parse.c
+++ b/lib/dpkg/parse.c
@@ -218,7 +218,7 @@ pkg_parse_verify(struct parsedb_state *ps,
     parse_error(ps, _("package has field '%s' but is architecture all"),
                 "Multi-Arch: same");
 
-  /* Generate the fully qualified package name representation. */
+  /* Generate the cached fully qualified package name representation. */
   pkgbin->pkgname_archqual = pkgbin_name_archqual(pkg, pkgbin);
 
   /* Initialize deps to be arch-specific unless stated otherwise. */
diff --git a/lib/dpkg/pkg-show.c b/lib/dpkg/pkg-show.c
index 84889f4a1..569cbf5c2 100644
--- a/lib/dpkg/pkg-show.c
+++ b/lib/dpkg/pkg-show.c
@@ -165,12 +165,16 @@ pkg_name_const(const struct pkginfo *pkg, enum 
pkg_name_arch_when pnaw)
  * @return The string representation.
  */
 const char *
-pkgbin_name(const struct pkginfo *pkg, const struct pkgbin *pkgbin,
+pkgbin_name(struct pkginfo *pkg, struct pkgbin *pkgbin,
             enum pkg_name_arch_when pnaw)
 {
        if (!pkgbin_name_needs_arch(pkgbin, pnaw))
                return pkg->set->name;
 
+       /* Cache the package name representation, for later reuse. */
+       if (pkgbin->pkgname_archqual == NULL)
+               pkgbin->pkgname_archqual = pkgbin_name_archqual(pkg, pkgbin);
+
        return pkgbin->pkgname_archqual;
 }
 
@@ -185,7 +189,7 @@ pkgbin_name(const struct pkginfo *pkg, const struct pkgbin 
*pkgbin,
  * @return The string representation.
  */
 const char *
-pkg_name(const struct pkginfo *pkg, enum pkg_name_arch_when pnaw)
+pkg_name(struct pkginfo *pkg, enum pkg_name_arch_when pnaw)
 {
        return pkgbin_name(pkg, &pkg->installed, pnaw);
 }
diff --git a/lib/dpkg/t/.gitignore b/lib/dpkg/t/.gitignore
index e22bcc9e4..f88d0fa1c 100644
--- a/lib/dpkg/t/.gitignore
+++ b/lib/dpkg/t/.gitignore
@@ -15,6 +15,7 @@ t-path
 t-pkginfo
 t-pkg-list
 t-pkg-queue
+t-pkg-show
 t-progname
 t-string
 t-subproc
diff --git a/lib/dpkg/t/Makefile.am b/lib/dpkg/t/Makefile.am
index 12dc4d86a..b2f227569 100644
--- a/lib/dpkg/t/Makefile.am
+++ b/lib/dpkg/t/Makefile.am
@@ -40,6 +40,7 @@ test_programs = \
        t-pkginfo \
        t-pkg-list \
        t-pkg-queue \
+       t-pkg-show \
        t-trigger \
        t-mod-db \
        $(nil)
diff --git a/lib/dpkg/t/t-mod-db.c b/lib/dpkg/t/t-pkg-show.c
similarity index 52%
copy from lib/dpkg/t/t-mod-db.c
copy to lib/dpkg/t/t-pkg-show.c
index c40d8e8ae..f5a978e81 100644
--- a/lib/dpkg/t/t-mod-db.c
+++ b/lib/dpkg/t/t-pkg-show.c
@@ -1,8 +1,8 @@
 /*
  * libdpkg - Debian packaging suite library routines
- * t-mod-db.c - test database implementation
+ * t-pkg-show.c - test pkg-show implementation
  *
- * Copyright © 2011 Guillem Jover <guil...@debian.org>
+ * Copyright © 2018 Guillem Jover <guil...@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -21,37 +21,41 @@
 #include <config.h>
 #include <compat.h>
 
-#include <stdlib.h>
-
 #include <dpkg/test.h>
 #include <dpkg/dpkg-db.h>
+#include <dpkg/arch.h>
 
 static void
-test_db_dir(void)
+test_pkg_show_name(void)
 {
-       char *dir;
+       struct dpkg_arch *arch;
+       struct pkginfo *pkg;
+       const char *pkgname;
 
-       test_str(dpkg_db_get_dir(), ==, ADMINDIR);
+       arch = dpkg_arch_find("arch");
+       test_pass(arch);
 
-       dpkg_db_set_dir("testdir");
-       test_str(dpkg_db_get_dir(), ==, "testdir");
+       pkg = pkg_db_find_pkg("test", arch);
+       test_pass(pkg);
+       test_str(pkg->set->name, ==, "test");
+       test_pass(pkg->installed.arch->type == DPKG_ARCH_UNKNOWN);
 
-       setenv("DPKG_ADMINDIR", "testenvdir", 1);
-       dpkg_db_set_dir(NULL);
-       test_str(dpkg_db_get_dir(), ==, "testenvdir");
+       pkgname = pkg_name(pkg, pnaw_never);
+       test_pass(pkgname);
+       test_str(pkgname, ==, "test");
 
-       unsetenv("DPKG_ADMINDIR");
-       dpkg_db_set_dir(NULL);
-       test_str(dpkg_db_get_dir(), ==, ADMINDIR);
+       pkgname = pkg_name(pkg, pnaw_nonambig);
+       test_pass(pkgname);
+       test_str(pkgname, ==, "test:arch");
 
-       dir = dpkg_db_get_path("testfile");
-       test_str(dir, ==, ADMINDIR "/testfile");
-       free(dir);
+       pkgname = pkg_name(pkg, pnaw_always);
+       test_pass(pkgname);
+       test_str(pkgname, ==, "test:arch");
 }
 
 TEST_ENTRY(test)
 {
-       test_plan(5);
+       test_plan(10);
 
-       test_db_dir();
+       test_pkg_show_name();
 }

-- 
Dpkg.Org's dpkg

Reply via email to