]> git.ipfire.org Git - pakfire.git/commitdiff
package: Refactor pakfire_package_join_evr and make it private
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 27 Apr 2021 12:38:00 +0000 (12:38 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 27 Apr 2021 12:38:00 +0000 (12:38 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/package.h
src/libpakfire/libpakfire.sym
src/libpakfire/package.c

index 2e3f1dfe1bf6f0312150724ddd80bd49fd97ca99..ee6f51bd533eb9ce123489d9c08c16db3d81c6bf 100644 (file)
@@ -44,7 +44,6 @@ const char* pakfire_package_get_name(PakfirePackage pkg);
 void pakfire_package_set_name(PakfirePackage pkg, const char* name);
 const char* pakfire_package_get_evr(PakfirePackage pkg);
 void pakfire_package_set_evr(PakfirePackage pkg, const char* evr);
-char* pakfire_package_join_evr(const char* e, const char* v, const char* r);
 const char* pakfire_package_get_arch(PakfirePackage pkg);
 void pakfire_package_set_arch(PakfirePackage pkg, const char* arch);
 
@@ -155,6 +154,8 @@ PakfirePackage pakfire_package_create_from_solvable(Pakfire pakfire, Id id);
 Id pakfire_package_id(PakfirePackage pkg);
 int pakfire_package_is_source(PakfirePackage pkg);
 
+char* pakfire_package_join_evr(const char* e, const char* v, const char* r);
+
 #endif
 
 #endif /* PAKFIRE_PACKAGE_H */
index c250387305a41a597e79718b546567c74c63185a..b430d38cb9a32daf5d9fe24f3dbaf037a4259aa8 100644 (file)
@@ -195,7 +195,6 @@ global:
        pakfire_package_get_uuid;
        pakfire_package_get_vendor;
        pakfire_package_is_installed;
-       pakfire_package_join_evr;
        pakfire_package_ref;
        pakfire_package_set_arch;
        pakfire_package_set_build_host;
index cb1d643175e8d4e1316beb38698547b071434a9c..fd370d464b1d94872e3e9cba41291b75a9dd6bde 100644 (file)
@@ -18,6 +18,7 @@
 #                                                                             #
 #############################################################################*/
 
+#include <errno.h>
 #include <linux/limits.h>
 #include <stdlib.h>
 #include <string.h>
@@ -231,21 +232,29 @@ PAKFIRE_EXPORT void pakfire_package_set_evr(PakfirePackage pkg, const char* evr)
        s->evr = pool_str2id(pool, evr, 1);
 }
 
-PAKFIRE_EXPORT char* pakfire_package_join_evr(const char* e, const char* v, const char* r) {
-       size_t l = strlen(v) + strlen(r) + 2;
+char* pakfire_package_join_evr(const char* e, const char* v, const char* r) {
+       char* buffer = NULL;
 
-       // Do not include epoch when it is zero
-       if (strncmp(e, "0", strlen("0")) == 0) {
-               e = NULL;
-       } else {
-               l += strlen(e);
+       // Check for valid input
+       if (!e || !v || !r) {
+               errno = EINVAL;
+               return NULL;
        }
 
-       char* buffer = malloc(l + 1);
-       if (e)
-               snprintf(buffer, l + 1, "%s:%s-%s", e, v, r);
-       else
-               snprintf(buffer, l + 1, "%s-%s", v, r);
+       // Skip any zeroes in epoch
+       while (*e && *e == '0')
+               e++;
+
+       // Format string with epoch
+       if (*e) {
+               if (asprintf(&buffer, "%s:%s-%s", e, v, r) < 0)
+                       return NULL;
+
+       // Or format it without epoch
+       } else {
+               if (asprintf(&buffer, "%s-%s", v, r) < 0)
+                       return NULL;
+       }
 
        return buffer;
 }