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=8cbd195ff7227b7e628b378ae2961a793d53bdcc

commit 8cbd195ff7227b7e628b378ae2961a793d53bdcc
Author: Guillem Jover <[email protected]>
AuthorDate: Wed Aug 15 22:09:56 2018 +0200

    libdpkg: Add new pkg_name() and pkgbin_name() const variants
    
    Because C does not have a C++ equivalent for the mutable keyword, we
    cannot mark the pkgname_archqual member as such. Instead we will add a
    new couple of const functions that will fallback to return a non-freeing
    string to be used mostly in error handling code paths in case there is
    no cached member, as otherwise we'd be "leaking" those strings (just
    generating new instances that will be released as part of the pool)
    every time we call these functions.
---
 debian/changelog     |  1 +
 lib/dpkg/dpkg-db.h   |  6 ++++++
 lib/dpkg/libdpkg.map |  2 ++
 lib/dpkg/pkg-show.c  | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 61 insertions(+)

diff --git a/debian/changelog b/debian/changelog
index 094971925..7f5c17720 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -174,6 +174,7 @@ dpkg (1.19.1) UNRELEASED; urgency=medium
       of varbuf_add_archqual().
     - libdpkg: Factor out cached arch-qualified package name generation into
       new pkgbin_name_archqual() function.
+    - libdpkg: Add new pkg_name() and pkgbin_name() const variants.
   * Build system:
     - Set distribution tarball format to ustar, instead of default v7 format.
     - Mark PO4A and POD2MAN as precious variables.
diff --git a/lib/dpkg/dpkg-db.h b/lib/dpkg/dpkg-db.h
index a7b77aaa9..7b7591ebe 100644
--- a/lib/dpkg/dpkg-db.h
+++ b/lib/dpkg/dpkg-db.h
@@ -387,6 +387,12 @@ 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_const(const struct pkginfo *pkg, const struct pkgbin *pkgbin,
+                  enum pkg_name_arch_when pnaw);
+const char *
+pkg_name_const(const struct pkginfo *pkg, enum pkg_name_arch_when pnaw);
+
 void
 pkg_source_version(struct dpkg_version *version,
                    const struct pkginfo *pkg, const struct pkgbin *pkgbin);
diff --git a/lib/dpkg/libdpkg.map b/lib/dpkg/libdpkg.map
index 8dd62ccc9..6260f7456 100644
--- a/lib/dpkg/libdpkg.map
+++ b/lib/dpkg/libdpkg.map
@@ -279,6 +279,8 @@ LIBDPKG_PRIVATE {
        varbuf_add_source_version;
        pkgbin_name;
        pkg_name;
+       pkgbin_name_const;
+       pkg_name_const;
        pkg_source_version;
        pkgbin_synopsis;
        pkg_abbrev_want;
diff --git a/lib/dpkg/pkg-show.c b/lib/dpkg/pkg-show.c
index 47bd55cab..84889f4a1 100644
--- a/lib/dpkg/pkg-show.c
+++ b/lib/dpkg/pkg-show.c
@@ -102,6 +102,58 @@ pkgbin_name_archqual(const struct pkginfo *pkg, const 
struct pkgbin *pkgbin)
  * The returned string must not be freed, and it's permanently allocated so
  * can be used as long as the non-freeing memory pool has not been freed.
  *
+ * Note, that this const variant will "leak" a new non-freeing string on
+ * each call if the internal cache has not been previously initialized,
+ * so it is advised to use it only in error reporting code paths.
+ *
+ * The pnaw parameter should be one of pnaw_never (never print arch),
+ * pnaw_foreign (print arch for foreign packages only), pnaw_nonambig (print
+ * arch for non ambiguous cases) or pnaw_always (always print arch),
+ *
+ * @param pkg     The package to consider.
+ * @param pkgbin  The binary package instance to consider.
+ * @param pnaw    When to display the architecture qualifier.
+ *
+ * @return The string representation.
+ */
+const char *
+pkgbin_name_const(const struct pkginfo *pkg, const struct pkgbin *pkgbin,
+            enum pkg_name_arch_when pnaw)
+{
+       if (!pkgbin_name_needs_arch(pkgbin, pnaw))
+               return pkg->set->name;
+
+       /* Return a non-freeing package name representation, which
+        * is intended to be used in error-handling code, as we will keep
+        * "leaking" them until the next memory pool flush. */
+       if (pkgbin->pkgname_archqual == NULL)
+               return pkgbin_name_archqual(pkg, pkgbin);
+
+       return pkgbin->pkgname_archqual;
+}
+
+/**
+ * Return a string representation of the installed package name.
+ *
+ * This is equivalent to pkgbin_name_const() but just for its installed pkgbin.
+ *
+ * @param pkg   The package to consider.
+ * @param pnaw  When to display the architecture qualifier.
+ *
+ * @return The string representation.
+ */
+const char *
+pkg_name_const(const struct pkginfo *pkg, enum pkg_name_arch_when pnaw)
+{
+       return pkgbin_name_const(pkg, &pkg->installed, pnaw);
+}
+
+/**
+ * Return a string representation of the package name.
+ *
+ * The returned string must not be freed, and it's permanently allocated so
+ * can be used as long as the non-freeing memory pool has not been freed.
+ *
  * The pnaw parameter should be one of pnaw_never (never print arch),
  * pnaw_foreign (print arch for foreign packages only), pnaw_nonambig (print
  * arch for non ambiguous cases) or pnaw_always (always print arch),

-- 
Dpkg.Org's dpkg

Reply via email to