lib/package_config_files.c: avoid many small heap allocs and simplify.

This commit is contained in:
Juan RP 2014-05-22 10:52:57 +02:00
parent e2b0de8859
commit ac5aa94e58

View File

@ -1,5 +1,5 @@
/*- /*-
* Copyright (c) 2009-2013 Juan Romero Pardines. * Copyright (c) 2009-2014 Juan Romero Pardines.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -69,7 +69,7 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
xbps_object_t obj, obj2; xbps_object_t obj, obj2;
xbps_object_iterator_t iter, iter2; xbps_object_iterator_t iter, iter2;
const char *cffile, *sha256_new = NULL; const char *cffile, *sha256_new = NULL;
char *buf, *sha256_cur = NULL, *sha256_orig = NULL; char buf[PATH_MAX], *sha256_cur = NULL, *sha256_orig = NULL;
int rv = 0; int rv = 0;
assert(xbps_object_type(filesd) == XBPS_TYPE_DICTIONARY); assert(xbps_object_type(filesd) == XBPS_TYPE_DICTIONARY);
@ -101,15 +101,11 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
while ((obj2 = xbps_object_iterator_next(iter2))) { while ((obj2 = xbps_object_iterator_next(iter2))) {
xbps_dictionary_get_cstring_nocopy(obj2, xbps_dictionary_get_cstring_nocopy(obj2,
"file", &cffile); "file", &cffile);
buf = xbps_xasprintf(".%s", cffile); snprintf(buf, sizeof(buf), ".%s", cffile);
if (strcmp(entry_pname, buf) == 0) { if (strcmp(entry_pname, buf) == 0) {
xbps_dictionary_get_cstring(obj2, "sha256", xbps_dictionary_get_cstring(obj2, "sha256", &sha256_orig);
&sha256_orig);
free(buf);
break; break;
} }
free(buf);
buf = NULL;
} }
xbps_object_iterator_release(iter2); xbps_object_iterator_release(iter2);
} }
@ -128,14 +124,11 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
*/ */
while ((obj = xbps_object_iterator_next(iter))) { while ((obj = xbps_object_iterator_next(iter))) {
xbps_dictionary_get_cstring_nocopy(obj, "file", &cffile); xbps_dictionary_get_cstring_nocopy(obj, "file", &cffile);
buf = xbps_xasprintf(".%s", cffile); snprintf(buf, sizeof(buf), ".%s", cffile);
if (strcmp(entry_pname, buf)) { if (strcmp(entry_pname, buf)) {
free(buf);
buf = NULL;
continue; continue;
} }
sha256_cur = xbps_file_hash(buf); sha256_cur = xbps_file_hash(buf);
free(buf);
xbps_dictionary_get_cstring_nocopy(obj, "sha256", &sha256_new); xbps_dictionary_get_cstring_nocopy(obj, "sha256", &sha256_new);
if (sha256_cur == NULL) { if (sha256_cur == NULL) {
if (errno == ENOENT) { if (errno == ENOENT) {
@ -218,14 +211,12 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
version = xbps_pkg_version(pkgver); version = xbps_pkg_version(pkgver);
assert(version); assert(version);
buf = xbps_xasprintf(".%s.new-%s", snprintf(buf, sizeof(buf), ".%s.new-%s", cffile, version);
cffile, version);
xbps_set_cb_state(xhp, XBPS_STATE_CONFIG_FILE, xbps_set_cb_state(xhp, XBPS_STATE_CONFIG_FILE,
0, pkgver, 0, pkgver,
"Installing new configuration file to " "Installing new configuration file to "
"`%s.new-%s'.", cffile, version); "`%s.new-%s'.", cffile, version);
archive_entry_copy_pathname(entry, buf); archive_entry_copy_pathname(entry, buf);
free(buf);
rv = 1; rv = 1;
break; break;
} }