summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillem Jover <guillem@debian.org>2018-03-16 04:33:31 +0100
committerGuillem Jover <guillem@debian.org>2018-03-26 15:30:18 +0200
commitcd96cca4b2cf83f46d6e289418ed06e3c2ef7066 (patch)
treeafc8f7fc4c203f1c830b8c73e6cfead109e03f74
parentac3134292ecb5ea5f203c3fa1376cbc093d35518 (diff)
downloaddpkg-cd96cca4b2cf83f46d6e289418ed06e3c2ef7066.tar.gz
libdpkg: Make pkg_name() and pkgbin_name() get const structs
Modifying the struct pkginfo and struct pkgbin complicates how the code is used, and when the function can be called. Let's just initialize the pkgname_archqual variable at parse time, so that we can use it at any time, simplifying the overall code.
-rw-r--r--lib/dpkg/dpkg-db.h7
-rw-r--r--lib/dpkg/dump.c10
-rw-r--r--lib/dpkg/parse.c10
-rw-r--r--lib/dpkg/pkg-show.c17
4 files changed, 17 insertions, 27 deletions
diff --git a/lib/dpkg/dpkg-db.h b/lib/dpkg/dpkg-db.h
index bffd317ab..56b4b5c39 100644
--- a/lib/dpkg/dpkg-db.h
+++ b/lib/dpkg/dpkg-db.h
@@ -110,8 +110,7 @@ struct pkgbin {
bool essential;
enum pkgmultiarch multiarch;
const struct dpkg_arch *arch;
- /** The following is the "pkgname:archqual" cached string, if this was a
- * C++ class this member would be mutable. */
+ /** The fully qualified package name, i.e. "pkgname:archqual". */
const char *pkgname_archqual;
const char *description;
const char *maintainer;
@@ -377,9 +376,9 @@ enum pkg_name_arch_when {
void varbuf_add_pkgbin_name(struct varbuf *vb, const struct pkginfo *pkg,
const struct pkgbin *pkgbin,
enum pkg_name_arch_when pnaw);
-const char *pkgbin_name(struct pkginfo *pkg, 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(struct pkginfo *pkg, enum pkg_name_arch_when pnaw);
+const char *pkg_name(const struct pkginfo *pkg, enum pkg_name_arch_when pnaw);
void
pkg_source_version(struct dpkg_version *version,
diff --git a/lib/dpkg/dump.c b/lib/dpkg/dump.c
index b13eb6e79..a04f673d9 100644
--- a/lib/dpkg/dump.c
+++ b/lib/dpkg/dump.c
@@ -461,16 +461,10 @@ writerecord(FILE *file, const char *filename,
varbufrecord(&vb, pkg, pkgbin);
varbuf_end_str(&vb);
- if (fputs(vb.buf,file) < 0) {
- struct varbuf pkgname = VARBUF_INIT;
- int errno_saved = errno;
- varbuf_add_pkgbin_name(&pkgname, pkg, pkgbin, pnaw_nonambig);
-
- errno = errno_saved;
+ if (fputs(vb.buf, file) < 0)
ohshite(_("failed to write details of '%.50s' to '%.250s'"),
- pkgname.buf, filename);
- }
+ pkgbin_name(pkg, pkgbin, pnaw_nonambig), filename);
varbuf_destroy(&vb);
}
diff --git a/lib/dpkg/parse.c b/lib/dpkg/parse.c
index 1b3bf13d8..d75c54458 100644
--- a/lib/dpkg/parse.c
+++ b/lib/dpkg/parse.c
@@ -219,6 +219,16 @@ 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. */
+ if (pkgbin->arch->type != DPKG_ARCH_NONE &&
+ pkgbin->arch->type != DPKG_ARCH_EMPTY) {
+ char *pkgname = nfmalloc(strlen(pkg->set->name) + 1 +
+ strlen(pkgbin->arch->name) + 1);
+
+ str_concat(pkgname, pkg->set->name, ":", pkgbin->arch->name, NULL);
+ pkgbin->pkgname_archqual = pkgname;
+ }
+
/* Initialize deps to be arch-specific unless stated otherwise. */
for (dep = pkgbin->depends; dep; dep = dep->next)
for (dop = dep->list; dop; dop = dop->next)
diff --git a/lib/dpkg/pkg-show.c b/lib/dpkg/pkg-show.c
index 6af658c42..4818675bb 100644
--- a/lib/dpkg/pkg-show.c
+++ b/lib/dpkg/pkg-show.c
@@ -93,25 +93,12 @@ varbuf_add_pkgbin_name(struct varbuf *vb,
* @return The string representation.
*/
const char *
-pkgbin_name(struct pkginfo *pkg, struct pkgbin *pkgbin,
+pkgbin_name(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;
- /* Cache the package name representation, for later reuse. */
- if (pkgbin->pkgname_archqual == NULL) {
- struct varbuf vb = VARBUF_INIT;
-
- varbuf_add_str(&vb, pkg->set->name);
- varbuf_add_archqual(&vb, pkgbin->arch);
- varbuf_end_str(&vb);
-
- pkgbin->pkgname_archqual = nfstrsave(vb.buf);
-
- varbuf_destroy(&vb);
- }
-
return pkgbin->pkgname_archqual;
}
@@ -126,7 +113,7 @@ pkgbin_name(struct pkginfo *pkg, struct pkgbin *pkgbin,
* @return The string representation.
*/
const char *
-pkg_name(struct pkginfo *pkg, enum pkg_name_arch_when pnaw)
+pkg_name(const struct pkginfo *pkg, enum pkg_name_arch_when pnaw)
{
return pkgbin_name(pkg, &pkg->installed, pnaw);
}