]> git.ipfire.org Git - pakfire.git/commitdiff
package: Refactor dumping a package
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Jun 2025 08:40:57 +0000 (08:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Jun 2025 08:40:57 +0000 (08:40 +0000)
This should be more straight forward and leak less memory.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/package.c

index f8b2919490231dd908224e4fe00e0c1a6ec456e1..fa7746142b3e3e9401c63430fa66438afa4fe7a6 100644 (file)
@@ -1816,18 +1816,24 @@ pakfire_repo* pakfire_package_get_repo(pakfire_package* pkg) {
        return pakfire_repo_ref(pkg->repo);
 }
 
-static int pakfire_package_dump_add_line(char** str, const char* key, const char* val) {
-       if (val)
-               return asprintf(str, "%s%-15s: %s\n", (*str) ? *str : "", key ? key : "", val);
+static int pakfire_package_dump_add_line(char*** s, const char* key, const char* val) {
+       // Do nothing if there is no value
+       if (!val)
+               return 0;
 
-       return 0;
+       // Replace empty keys with an empty string
+       if (!key)
+               key = "";
+
+       // Append a new line
+       return pakfire_strings_appendf(s, "%-15s: %s\n", key, val);
 }
 
-static int pakfire_package_dump_add_lines(char** str, const char* key, char** lines) {
+static int pakfire_package_dump_add_lines(char*** s, const char* key, char** lines) {
        int r = 0;
 
        for (; *lines; lines++) {
-               r = pakfire_package_dump_add_line(str, key, *lines);
+               r = pakfire_package_dump_add_line(s, key, *lines);
                if (r < 0)
                        break;
 
@@ -1838,7 +1844,7 @@ static int pakfire_package_dump_add_lines(char** str, const char* key, char** li
        return r;
 }
 
-static int pakfire_package_dump_add_long_line(char** str, const char* key, const char* val) {
+static int pakfire_package_dump_add_long_line(char*** s, const char* key, const char* val) {
        char* p = NULL;
        int r = 0;
 
@@ -1850,7 +1856,7 @@ static int pakfire_package_dump_add_long_line(char** str, const char* key, const
        const char* line = strtok_r(buffer, "\n", &p);
 
        while (line) {
-               r = pakfire_package_dump_add_line(str, key, line);
+               r = pakfire_package_dump_add_line(s, key, line);
                if (r < 0)
                        break;
 
@@ -1864,7 +1870,7 @@ static int pakfire_package_dump_add_long_line(char** str, const char* key, const
        return r;
 }
 
-static int pakfire_package_dump_add_line_date(char** str, const char* key, time_t date) {
+static int pakfire_package_dump_add_line_date(char*** s, const char* key, time_t date) {
        char buffer[1024];
        int r;
 
@@ -1873,10 +1879,10 @@ static int pakfire_package_dump_add_line_date(char** str, const char* key, time_
        if (r < 0)
                return r;
 
-       return pakfire_package_dump_add_line(str, key, buffer);
+       return pakfire_package_dump_add_line(s, key, buffer);
 }
 
-static int pakfire_package_dump_add_line_hex(char** str,
+static int pakfire_package_dump_add_line_hex(char*** s,
                const char* key, const unsigned char* buffer, const size_t length) {
        int r;
 
@@ -1884,13 +1890,13 @@ static int pakfire_package_dump_add_line_hex(char** str,
        if (!hex)
                return -errno;
 
-       r = pakfire_package_dump_add_line(str, key, hex);
+       r = pakfire_package_dump_add_line(s, key, hex);
        free(hex);
 
        return r;
 }
 
-static int pakfire_package_dump_add_line_size(char** str, const char* key, size_t size) {
+static int pakfire_package_dump_add_line_size(char*** s, const char* key, size_t size) {
        char buffer[128];
 
        // Format size in human-readable format
@@ -1898,7 +1904,7 @@ static int pakfire_package_dump_add_line_size(char** str, const char* key, size_
        if (r < 0)
                return r;
 
-       return pakfire_package_dump_add_line(str, key, buffer);
+       return pakfire_package_dump_add_line(s, key, buffer);
 }
 
 static int pakfire_sort_dependencies(const void* p1, const void* p2) {
@@ -1912,13 +1918,14 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
        enum pakfire_hash_type hash = PAKFIRE_HASH_UNDEFINED;
        const unsigned char* checksum = NULL;
        size_t checksum_length = 0;
-       char* string = NULL;
+       char* result = NULL;
+       char** s = NULL;
        int r;
 
        // Name
        const char* name = pakfire_package_get_string(pkg, PAKFIRE_PKG_NAME);
        if (name) {
-               r = pakfire_package_dump_add_line(&string, _("Name"), name);
+               r = pakfire_package_dump_add_line(&s, _("Name"), name);
                if (r < 0)
                        goto ERROR;
        }
@@ -1926,7 +1933,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
        // EVR
        const char* evr = pakfire_package_get_string(pkg, PAKFIRE_PKG_EVR);
        if (evr) {
-               r = pakfire_package_dump_add_line(&string, _("Version"), evr);
+               r = pakfire_package_dump_add_line(&s, _("Version"), evr);
                if (r < 0)
                        goto ERROR;
        }
@@ -1934,7 +1941,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
        // Arch
        const char* arch = pakfire_package_get_string(pkg, PAKFIRE_PKG_ARCH);
        if (arch) {
-               r = pakfire_package_dump_add_line(&string, _("Arch"), arch);
+               r = pakfire_package_dump_add_line(&s, _("Arch"), arch);
                if (r < 0)
                        goto ERROR;
                }
@@ -1942,7 +1949,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
        // Size
        size_t size = pakfire_package_get_size(pkg);
        if (size) {
-               r = pakfire_package_dump_add_line_size(&string, _("Size"), size);
+               r = pakfire_package_dump_add_line_size(&s, _("Size"), size);
                if (r < 0)
                        goto ERROR;
        }
@@ -1951,7 +1958,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
        if (pakfire_package_is_installed(pkg)) {
                unsigned long long installsize = pakfire_package_get_num(pkg, PAKFIRE_PKG_INSTALLSIZE, 0);
                if (installsize) {
-                       r = pakfire_package_dump_add_line_size(&string, _("Installed Size"), installsize);
+                       r = pakfire_package_dump_add_line_size(&s, _("Installed Size"), installsize);
                        if (r < 0)
                                goto ERROR;
                }
@@ -1960,7 +1967,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
        } else {
                size_t downloadsize = pakfire_package_get_num(pkg, PAKFIRE_PKG_DOWNLOADSIZE, 0);
                if (downloadsize) {
-                       r = pakfire_package_dump_add_line_size(&string, _("Download Size"), downloadsize);
+                       r = pakfire_package_dump_add_line_size(&s, _("Download Size"), downloadsize);
                        if (r < 0)
                                goto ERROR;
                }
@@ -1972,7 +1979,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
                if (!pakfire_repo_name_equals(repo, PAKFIRE_REPO_DUMMY)) {
                        const char* repo_name = pakfire_repo_get_name(repo);
 
-                       r = pakfire_package_dump_add_line(&string, _("Repo"), repo_name);
+                       r = pakfire_package_dump_add_line(&s, _("Repo"), repo_name);
                        if (r < 0)
                                goto ERROR;
                }
@@ -1983,7 +1990,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
        // Summary
        const char* summary = pakfire_package_get_string(pkg, PAKFIRE_PKG_SUMMARY);
        if (summary) {
-               r = pakfire_package_dump_add_line(&string, _("Summary"), summary);
+               r = pakfire_package_dump_add_line(&s, _("Summary"), summary);
                if (r < 0)
                        goto ERROR;
        }
@@ -1991,7 +1998,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
        // Description
        const char* description = pakfire_package_get_string(pkg, PAKFIRE_PKG_DESCRIPTION);
        if (description) {
-               r = pakfire_package_dump_add_long_line(&string, _("Description"), description);
+               r = pakfire_package_dump_add_long_line(&s, _("Description"), description);
                if (r < 0)
                        goto ERROR;
        }
@@ -1999,7 +2006,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
        // Groups
        char** groups = pakfire_package_get_strings(pkg, PAKFIRE_PKG_GROUPS);
        if (groups) {
-               r = pakfire_package_dump_add_lines(&string, _("Groups"), groups);
+               r = pakfire_package_dump_add_lines(&s, _("Groups"), groups);
                if (r < 0)
                        goto ERROR;
 
@@ -2010,7 +2017,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
        // URL
        const char* url = pakfire_package_get_string(pkg, PAKFIRE_PKG_URL);
        if (url) {
-               pakfire_package_dump_add_line(&string, _("URL"), url);
+               pakfire_package_dump_add_line(&s, _("URL"), url);
                if (r < 0)
                        goto ERROR;
        }
@@ -2018,7 +2025,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
        // License
        const char* license = pakfire_package_get_string(pkg, PAKFIRE_PKG_LICENSE);
        if (license) {
-               r = pakfire_package_dump_add_line(&string, _("License"), license);
+               r = pakfire_package_dump_add_line(&s, _("License"), license);
                if (r < 0)
                        goto ERROR;
        }
@@ -2027,7 +2034,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
                // Install Time
                time_t install_time = pakfire_package_get_num(pkg, PAKFIRE_PKG_INSTALLTIME, 0);
                if (install_time) {
-                       r = pakfire_package_dump_add_line_date(&string, _("Install Time"), install_time);
+                       r = pakfire_package_dump_add_line_date(&s, _("Install Time"), install_time);
                        if (r < 0)
                                goto ERROR;
                }
@@ -2035,7 +2042,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
                // Distribution
                const char* distro = pakfire_package_get_string(pkg, PAKFIRE_PKG_DISTRO);
                if (distro) {
-                       r = pakfire_package_dump_add_line(&string, _("Distribution"), distro);
+                       r = pakfire_package_dump_add_line(&s, _("Distribution"), distro);
                        if (r < 0)
                                goto ERROR;
                }
@@ -2043,7 +2050,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
                // Packager
                const char* packager = pakfire_package_get_string(pkg, PAKFIRE_PKG_PACKAGER);
                if (packager) {
-                       r = pakfire_package_dump_add_line(&string, _("Packager"), packager);
+                       r = pakfire_package_dump_add_line(&s, _("Packager"), packager);
                        if (r < 0)
                                goto ERROR;
                }
@@ -2051,7 +2058,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
                // Vendor
                const char* vendor = pakfire_package_get_string(pkg, PAKFIRE_PKG_VENDOR);
                if (vendor) {
-                       r = pakfire_package_dump_add_line(&string, _("Vendor"), vendor);
+                       r = pakfire_package_dump_add_line(&s, _("Vendor"), vendor);
                        if (r < 0)
                                goto ERROR;
                }
@@ -2059,7 +2066,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
                // UUID
                const char* uuid = pakfire_package_get_string(pkg, PAKFIRE_PKG_UUID);
                if (uuid) {
-                       r = pakfire_package_dump_add_line(&string, _("UUID"), uuid);
+                       r = pakfire_package_dump_add_line(&s, _("UUID"), uuid);
                        if (r < 0)
                                goto ERROR;
                }
@@ -2067,7 +2074,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
                // Build ID
                const char* build_id = pakfire_package_get_string(pkg, PAKFIRE_PKG_BUILD_ID);
                if (build_id) {
-                       r = pakfire_package_dump_add_line(&string, _("Build ID"), build_id);
+                       r = pakfire_package_dump_add_line(&s, _("Build ID"), build_id);
                        if (r < 0)
                                goto ERROR;
                }
@@ -2075,7 +2082,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
                // Build Arches
                char** build_arches = pakfire_package_get_strings(pkg, PAKFIRE_PKG_BUILD_ARCHES);
                if (build_arches) {
-                       r = pakfire_package_dump_add_lines(&string, _("Build Arch"), build_arches);
+                       r = pakfire_package_dump_add_lines(&s, _("Build Arch"), build_arches);
                        if (r < 0)
                                goto ERROR;
 
@@ -2090,14 +2097,14 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
 
                switch (hash) {
                        case PAKFIRE_HASH_SHA2_512:
-                               r = pakfire_package_dump_add_line_hex(&string,
+                               r = pakfire_package_dump_add_line_hex(&s,
                                                _("SHA2-512 Checksum"), checksum, checksum_length);
                                if (r < 0)
                                        goto ERROR;
                                break;
 
                        case PAKFIRE_HASH_SHA2_256:
-                               r = pakfire_package_dump_add_line_hex(&string,
+                               r = pakfire_package_dump_add_line_hex(&s,
                                                _("SHA2-256 Checksum"), checksum, checksum_length);
                                if (r < 0)
                                        goto ERROR;
@@ -2114,7 +2121,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
                // Source package
                const char* source_package = pakfire_package_get_string(pkg, PAKFIRE_PKG_SOURCE_PKG);
                if (source_package) {
-                       r = pakfire_package_dump_add_line(&string, _("Source Package"), source_package);
+                       r = pakfire_package_dump_add_line(&s, _("Source Package"), source_package);
                        if (r < 0)
                                goto ERROR;
                }
@@ -2122,7 +2129,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
                // Build time
                time_t build_time = pakfire_package_get_num(pkg, PAKFIRE_PKG_BUILD_TIME, 0);
                if (build_time) {
-                       r = pakfire_package_dump_add_line_date(&string, _("Build Time"), build_time);
+                       r = pakfire_package_dump_add_line_date(&s, _("Build Time"), build_time);
                        if (r < 0)
                                goto ERROR;
                }
@@ -2130,7 +2137,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
                // Build host
                const char* build_host = pakfire_package_get_string(pkg, PAKFIRE_PKG_BUILD_HOST);
                if (build_host) {
-                       r = pakfire_package_dump_add_line(&string, _("Build Host"), build_host);
+                       r = pakfire_package_dump_add_line(&s, _("Build Host"), build_host);
                        if (r < 0)
                                goto ERROR;
                }
@@ -2191,7 +2198,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
                                }
 
                                // Write it to the console
-                               r = pakfire_package_dump_add_lines(&string, name, deps);
+                               r = pakfire_package_dump_add_lines(&s, name, deps);
                                if (r < 0)
                                        goto ERROR;
 
@@ -2211,7 +2218,7 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
 
                        const char* path = pakfire_file_get_path(file);
                        if (path) {
-                               r = pakfire_package_dump_add_line(&string, prefix, path);
+                               r = pakfire_package_dump_add_line(&s, prefix, path);
                                if (r < 0)
                                        goto ERROR;
                        }
@@ -2225,13 +2232,14 @@ char* pakfire_package_dump(pakfire_package* pkg, int flags) {
                pakfire_filelist_unref(filelist);
        }
 
-       return string;
+       // Join everything together
+       result = pakfire_string_join((const char**)s, "");
 
 ERROR:
-       if (string)
-               free(string);
+       if (s)
+               pakfire_strings_free(s);
 
-       return NULL;
+       return result;
 }
 
 int pakfire_package_get_archive(pakfire_package* pkg, pakfire_archive** archive) {