lib/package_msg: don't roundtrip data with fmemopen

Using fmemopen here wasn't necessary, since memcpy could have been used
with way lower overhead. We don't use a dedicated function, because
turning a data field into a string is an inefficient operation and
shouldn't be encouraged.

Also don't initialize data when it's declared, it isn't necessary.
This commit is contained in:
Érico Nogueira 2021-05-12 10:19:55 -03:00 committed by Duncan Overbruck
parent 4a5eb8dc87
commit 08ad0c2a9b
No known key found for this signature in database
GPG Key ID: 335C1D17EC3D6E35

View File

@ -34,8 +34,7 @@ int HIDDEN
xbps_cb_message(struct xbps_handle *xhp, xbps_dictionary_t pkgd, const char *key)
{
xbps_data_t msg;
FILE *f = NULL;
const void *data = NULL;
const void *data;
const char *pkgver = NULL;
size_t len;
char *buf = NULL;
@ -52,35 +51,22 @@ xbps_cb_message(struct xbps_handle *xhp, xbps_dictionary_t pkgd, const char *key
if (xbps_object_type(msg) != XBPS_TYPE_DATA)
goto out;
/* turn data from msg into a string */
data = xbps_data_data_nocopy(msg);
len = xbps_data_size(msg);
if ((f = fmemopen(__UNCONST(data), len, "r")) == NULL) {
rv = errno;
xbps_dbg_printf(xhp, "[%s] %s: fmemopen %s\n", __func__, pkgver, strerror(rv));
goto out;
};
buf = malloc(len+1);
assert(buf);
if (fread(buf, len, 1, f) != len) {
if (ferror(f)) {
rv = errno;
xbps_dbg_printf(xhp, "[%s] %s: fread %s\n", __func__, pkgver, strerror(rv));
goto out;
}
}
/* terminate buffer and notify client to show the post-install message */
memcpy(buf, data, len);
/* terminate string */
buf[len] = '\0';
/* notify client to show the post-install message */
if (strcmp(key, "install-msg") == 0)
xbps_set_cb_state(xhp, XBPS_STATE_SHOW_INSTALL_MSG, 0, pkgver, "%s", buf);
else
xbps_set_cb_state(xhp, XBPS_STATE_SHOW_REMOVE_MSG, 0, pkgver, "%s", buf);
out:
if (f != NULL)
fclose(f);
if (buf != NULL)
free(buf);
return rv;
}