diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2019-11-26 14:00:30 +0300 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2019-11-26 14:00:30 +0300 |
commit | 414ea1706306e061fc44a8b5ce3042d4f0728489 (patch) | |
tree | ef0b2c4eac79e479ed686a5d88d7b3b954717824 /lib/dpkg/pkg-show.c | |
parent | ed2b463626bd721942143baa6207f2ccac67a616 (diff) | |
parent | 89afa9af7cd589eb8384ed96b6d86dd59d56bdf5 (diff) | |
download | dpkg-414ea1706306e061fc44a8b5ce3042d4f0728489.tar.gz |
Merge https://salsa.debian.org/dpkg-team/dpkg
Diffstat (limited to 'lib/dpkg/pkg-show.c')
-rw-r--r-- | lib/dpkg/pkg-show.c | 128 |
1 files changed, 112 insertions, 16 deletions
diff --git a/lib/dpkg/pkg-show.c b/lib/dpkg/pkg-show.c index 6af658c42..67e5af404 100644 --- a/lib/dpkg/pkg-show.c +++ b/lib/dpkg/pkg-show.c @@ -34,6 +34,10 @@ static bool pkgbin_name_needs_arch(const struct pkgbin *pkgbin, enum pkg_name_arch_when pnaw) { + if (pkgbin->arch->type == DPKG_ARCH_NONE || + pkgbin->arch->type == DPKG_ARCH_EMPTY) + return false; + switch (pnaw) { case pnaw_never: break; @@ -43,8 +47,7 @@ pkgbin_name_needs_arch(const struct pkgbin *pkgbin, /* Fall through. */ case pnaw_foreign: if (pkgbin->arch->type == DPKG_ARCH_NATIVE || - pkgbin->arch->type == DPKG_ARCH_ALL || - pkgbin->arch->type == DPKG_ARCH_NONE) + pkgbin->arch->type == DPKG_ARCH_ALL) break; /* Fall through. */ case pnaw_always: @@ -76,12 +79,33 @@ varbuf_add_pkgbin_name(struct varbuf *vb, varbuf_end_str(vb); } +const char * +pkgbin_name_archqual(const struct pkginfo *pkg, const struct pkgbin *pkgbin) +{ + char *pkgname; + + if (pkgbin->arch->type == DPKG_ARCH_NONE || + pkgbin->arch->type == DPKG_ARCH_EMPTY) + return pkg->set->name; + + pkgname = nfmalloc(strlen(pkg->set->name) + 1 + + strlen(pkgbin->arch->name) + 1); + str_concat(pkgname, pkg->set->name, ":", + pkgbin->arch->name, NULL); + + return pkgname; +} + /** * 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. * + * 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), @@ -93,24 +117,63 @@ varbuf_add_pkgbin_name(struct varbuf *vb, * @return The string representation. */ const char * -pkgbin_name(struct pkginfo *pkg, struct pkgbin *pkgbin, +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; - /* Cache the package name representation, for later reuse. */ - if (pkgbin->pkgname_archqual == NULL) { - struct varbuf vb = VARBUF_INIT; + /* 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); - varbuf_add_str(&vb, pkg->set->name); - varbuf_add_archqual(&vb, pkgbin->arch); - varbuf_end_str(&vb); + return pkgbin->pkgname_archqual; +} - pkgbin->pkgname_archqual = nfstrsave(vb.buf); +/** + * 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); +} - varbuf_destroy(&vb); - } +/** + * 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), + * + * @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(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; } @@ -132,22 +195,22 @@ pkg_name(struct pkginfo *pkg, enum pkg_name_arch_when pnaw) } /** - * Return a string representation of the package summary. + * Return a string representation of the package synopsis. * * 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 package summary is the short description, but it is not NUL terminated, + * The package synopsis is the short description, but it is not NUL terminated, * so the output len argument should be used to limit the string length. * * @param pkg The package to consider. * @param pkgbin The binary package instance to consider. - * @param[out] len The length of the summary string within the description. + * @param[out] len The length of the synopsis string within the description. * * @return The string representation. */ const char * -pkgbin_summary(const struct pkginfo *pkg, const struct pkgbin *pkgbin, int *len) +pkgbin_synopsis(const struct pkginfo *pkg, const struct pkgbin *pkgbin, int *len) { const char *pdesc; @@ -161,6 +224,39 @@ pkgbin_summary(const struct pkginfo *pkg, const struct pkgbin *pkgbin, int *len) } /** + * Return a string representation of the package synopsis. + * + * 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. + * + * It will try to use the installed version, otherwise it will fallback to + * use the available version. + * + * The package synopsis is the short description, but it is not NUL terminated, + * so the output len argument should be used to limit the string length. + * + * @param pkg The package to consider. + * @param[out] len The length of the synopsis string within the description. + * + * @return The string representation. + */ +const char * +pkg_synopsis(const struct pkginfo *pkg, int *len) +{ + const char *pdesc; + + pdesc = pkg->installed.description; + if (!pdesc) + pdesc = pkg->available.description; + if (!pdesc) + pdesc = _("(no description available)"); + + *len = strcspn(pdesc, "\n"); + + return pdesc; +} + +/** * Return a character abbreviated representation of the package want status. * * @param pkg The package to consider. |