libxbps: ABI/API break due to xbps_pkg{,pattern}_name changes.
The funcs xbps_pkg_name() and xbps_pkgpattern_name() were using malloc(3) to return the result, until now. They now have been changed to not allocate the result via malloc, the caller is responsible to provide a buffer at least of XBPS_NAME_SIZE (64). If for whatever reason the pkgname can't be guessed, returns false. This should avoid lots of small allocs around libxbps. New functions have the following prototype: bool xbps_pkg_name(char *dst, size_t len, const char *pkg) bool xbps_pkgpattern_name(char *dst, size_t len, const char *pkg) as suggested by @duncaen.
This commit is contained in:
@@ -36,7 +36,7 @@ state_cb(const struct xbps_state_cb_data *xscd, void *cbdata UNUSED)
|
||||
{
|
||||
xbps_dictionary_t pkgd;
|
||||
const char *instver, *newver;
|
||||
char *pkgname;
|
||||
char pkgname[XBPS_NAME_SIZE];
|
||||
int rv = 0;
|
||||
bool slog = false;
|
||||
|
||||
@@ -99,8 +99,9 @@ state_cb(const struct xbps_state_cb_data *xscd, void *cbdata UNUSED)
|
||||
/* empty */
|
||||
break;
|
||||
case XBPS_STATE_UPDATE:
|
||||
pkgname = xbps_pkg_name(xscd->arg);
|
||||
assert(pkgname);
|
||||
if (!xbps_pkg_name(pkgname, sizeof(pkgname), xscd->arg)) {
|
||||
abort();
|
||||
}
|
||||
newver = xbps_pkg_version(xscd->arg);
|
||||
pkgd = xbps_pkgdb_get_pkg(xscd->xhp, pkgname);
|
||||
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &instver);
|
||||
@@ -110,7 +111,6 @@ state_cb(const struct xbps_state_cb_data *xscd, void *cbdata UNUSED)
|
||||
"(rootdir: %s)\n", instver, newver,
|
||||
xscd->xhp->rootdir);
|
||||
}
|
||||
free(pkgname);
|
||||
break;
|
||||
/* success */
|
||||
case XBPS_STATE_REMOVE_FILE:
|
||||
|
||||
@@ -93,18 +93,18 @@ show_package_list(struct transaction *trans, const char *match, int cols)
|
||||
if (match && strcmp(tract, "update") == 0) {
|
||||
xbps_dictionary_t ipkgd;
|
||||
const char *ipkgver, *iversion, *version;
|
||||
char *pkgname;
|
||||
char pkgname[XBPS_NAME_SIZE];
|
||||
|
||||
/* get installed pkgver */
|
||||
pkgname = xbps_pkg_name(pkgver);
|
||||
assert(pkgname);
|
||||
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
|
||||
abort();
|
||||
}
|
||||
ipkgd = xbps_pkgdb_get_pkg(trans->xhp, pkgname);
|
||||
assert(ipkgd);
|
||||
xbps_dictionary_get_cstring_nocopy(ipkgd, "pkgver", &ipkgver);
|
||||
version = xbps_pkg_version(pkgver);
|
||||
iversion = xbps_pkg_version(ipkgver);
|
||||
buf = xbps_xasprintf("%s (%s -> %s)", pkgname, iversion, version);
|
||||
free(pkgname);
|
||||
}
|
||||
if ((match && (strcmp(match, tract) == 0)) || (!match && dload)) {
|
||||
if (buf) {
|
||||
|
||||
@@ -82,15 +82,15 @@ find_longest_pkgname(struct transaction *trans)
|
||||
{
|
||||
xbps_object_t obj;
|
||||
const char *pkgver;
|
||||
char *pkgname;
|
||||
char pkgname[XBPS_NAME_SIZE];
|
||||
unsigned int len = 0, max = 0;
|
||||
|
||||
while ((obj = xbps_object_iterator_next(trans->iter)) != NULL) {
|
||||
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
||||
pkgname = xbps_pkg_name(pkgver);
|
||||
assert(pkgname);
|
||||
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
|
||||
abort();
|
||||
}
|
||||
len = strlen(pkgname);
|
||||
free(pkgname);
|
||||
if (max == 0 || len > max)
|
||||
max = len;
|
||||
}
|
||||
@@ -125,7 +125,7 @@ print_trans_colmode(struct transaction *trans, int cols)
|
||||
while ((obj = xbps_object_iterator_next(trans->iter)) != NULL) {
|
||||
xbps_dictionary_t ipkgd;
|
||||
const char *pkgver, *ipkgver, *ver, *iver, *tract;
|
||||
char *pkgname;
|
||||
char pkgname[XBPS_NAME_SIZE];
|
||||
bool dload = false;
|
||||
|
||||
ver = iver = NULL;
|
||||
@@ -135,8 +135,9 @@ print_trans_colmode(struct transaction *trans, int cols)
|
||||
xbps_dictionary_get_uint64(obj, "filename-size", &dlsize);
|
||||
xbps_dictionary_get_bool(obj, "download", &dload);
|
||||
|
||||
pkgname = xbps_pkg_name(pkgver);
|
||||
assert(pkgname);
|
||||
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
|
||||
abort();
|
||||
}
|
||||
|
||||
if (trans->xhp->flags & XBPS_FLAG_DOWNLOAD_ONLY) {
|
||||
tract = "download";
|
||||
@@ -206,7 +207,6 @@ print_trans_colmode(struct transaction *trans, int cols)
|
||||
/* print download size */
|
||||
printf("%s ", size);
|
||||
printf("\n");
|
||||
free(pkgname);
|
||||
}
|
||||
xbps_object_iterator_reset(trans->iter);
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user