]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
util: Refactor pakfire_str2deps and use it everywhere
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Aug 2022 17:13:59 +0000 (17:13 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Aug 2022 17:13:59 +0000 (17:13 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/build.c
src/libpakfire/include/pakfire/util.h
src/libpakfire/parser.c
src/libpakfire/util.c

index 15e0bc126f41fbd4c134dbdf086c15900eb74ff0..0f921da48b27bbb2e747a7730acec0c726beebb2 100644 (file)
@@ -119,23 +119,6 @@ ERROR:
        return r;
 }
 
-static int find_dependency(char** haystack, const char* needle) {
-       if (!needle) {
-               errno = EINVAL;
-               return -1;
-       }
-
-       if (!haystack)
-               return 0;
-
-       for (char** element = haystack; *element; element++) {
-               if (strcmp(needle, *element) == 0)
-                       return 1;
-       }
-
-       return 0;
-}
-
 static int pakfire_build_find_dependencies(struct pakfire_build* build,
                struct pakfire_package* pkg, struct pakfire_filelist* filelist, const char* buildroot) {
        char* provides = NULL;
@@ -181,28 +164,25 @@ static int pakfire_build_find_dependencies(struct pakfire_build* build,
                goto ERROR;
        }
 
-#warning TODO parse dependencies
-#if 0
        // Add all provides to the package
        if (provides) {
-               for (char** element = provides; *element; element++) {
-                       DEBUG(build->pakfire, "Adding provides: %s\n", *element);
-                       pakfire_package_add_provides(pkg, *element);
+               r = pakfire_str2deps(build->pakfire, pkg,
+                       pakfire_package_add_provides, provides);
+               if (r) {
+                       ERROR(build->pakfire, "Could not add provides: %m\n");
+                       goto ERROR;
                }
        }
 
        // Add all requires to the package
        if (requires) {
-               for (char** element = requires; *element; element++) {
-                       // Skip adding this requirement if also provided by this package
-                       if (find_dependency(provides, *element))
-                               continue;
-
-                       DEBUG(build->pakfire, "Adding requires: %s\n", *element);
-                       pakfire_package_add_requires(pkg, *element);
+               r = pakfire_str2deps(build->pakfire, pkg,
+                       pakfire_package_add_requires, requires);
+               if (r) {
+                       ERROR(build->pakfire, "Could not add provides: %m\n");
+                       goto ERROR;
                }
        }
-#endif
 
        // Success
        r = 0;
@@ -379,15 +359,14 @@ static int pakfire_build_add_scriptlet_requires(struct pakfire_build* build,
                goto ERROR;
 
        // Add all pre-requires to the package
-#warning TODO parse dependenices
-#if 0
        if (prerequires) {
-               for (char** element = prerequires; *element; element++) {
-                       DEBUG(build->pakfire, "Adding pre-requires: %s\n", *element);
-                       pakfire_package_add_prerequires(pkg, *element);
+               r = pakfire_str2deps(build->pakfire, pkg,
+                       pakfire_package_add_prerequires, prerequires);
+               if (r) {
+                       ERROR(build->pakfire, "Could not add pre-requires: %m\n");
+                       goto ERROR;
                }
        }
-#endif
 
 ERROR:
        if (r && *path)
index 4044bd2d2d0fea741f56a1ce4e22e586b6e8f108..f44a1e3a8eee8ebe0fa320619b32106a15c0e16f 100644 (file)
@@ -32,8 +32,8 @@
 
 const char* pakfire_dep2str(struct pakfire* pakfire, Id id);
 Id pakfire_str2dep(struct pakfire* pakfire, const char* s);
-void pakfire_str2deps(struct pakfire* pakfire, struct pakfire_package* pkg,
-       void (*func)(struct pakfire_package* pkg, const char* dep), const char* deps);
+int pakfire_str2deps(struct pakfire* pakfire, struct pakfire_package* pkg,
+       void (*callback)(struct pakfire_package* pkg, const char* dep), const char* deps);
 
 #define pakfire_string_format(s, fmt, ...) snprintf(s, sizeof(s) - 1, fmt, __VA_ARGS__)
 #define pakfire_string_set(s, value) pakfire_string_format(s, "%s", value)
index 16c3f34e14fb183ff5f10e6993d322635913e492..39e074514ab6560b109cde4ed1b24d3f053fc878 100644 (file)
@@ -449,18 +449,15 @@ static int pakfire_parser_expand_commands(struct pakfire_parser* parser, char**
                argv[2] = (const char*)command;
 
                // The output of the command
-               char** stdout = NULL;
+               char* output = NULL;
 
                // Execute the command inside the Pakfire environment
-               r = pakfire_jail_run(parser->pakfire, argv, 0, &stdout);
+               r = pakfire_jail_run(parser->pakfire, argv, 0, &output);
                if (r) {
                        // Just log this and continue
                        DEBUG(parser->pakfire, "Command '%s' failed with return code %d\n", command, r);
                }
 
-               // Join all output together
-               char* output = pakfire_string_join(stdout, "");
-
                // Strip newline from output
                if (output)
                        pakfire_remove_trailing_newline(output);
@@ -494,12 +491,6 @@ static int pakfire_parser_expand_commands(struct pakfire_parser* parser, char**
                pcre2_substring_free(pattern);
                pattern = NULL;
 
-               if (stdout) {
-                       for (char** line = stdout; *line; line++)
-                               free(*line);
-                       free(stdout);
-               }
-
                if (output)
                        free(output);
        }
index baf4c3020075422c5d7a458a8ddde93d1e936b2d..1ada9d25ec962f337faef4dfc984620dbb984a59 100644 (file)
@@ -321,27 +321,36 @@ Id pakfire_str2dep(struct pakfire* pakfire, const char* s) {
        return id;
 }
 
-void pakfire_str2deps(struct pakfire* pakfire, struct pakfire_package* pkg,
-               void (*func)(struct pakfire_package* pkg, const char* dep), const char* deps) {
-       char* p = strdupa(deps);
+int pakfire_str2deps(struct pakfire* pakfire, struct pakfire_package* pkg,
+               void (callback)(struct pakfire_package* pkg, const char* dep), const char* deps) {
+       char* p = NULL;
 
-       while (*p) {
-               char* e = strchr(p, '\n');
+       // Check for valid inputs
+       if (!callback || !deps) {
+               errno = EINVAL;
+               return 1;
+       }
 
-               // Terminate the string
-               if (e)
-                       *e = '\0';
+       // Copy deps into a working buffer
+       char* buffer = strdup(deps);
+       if (!buffer)
+               return 1;
 
-               // Add the dependency
-               func(pkg, p);
+       char* dep = strtok_r(buffer, "\n", &p);
 
-               // End loop when we reached the end
-               if (!e)
-                       break;
+       // Walk through the buffer line by line
+       while (dep) {
+               DEBUG(pakfire, "Found dep '%s'\n", dep);
 
-               // Or continue at the next line
-               p = e + 1;
+               // Add the dependency
+               callback(pkg, dep);
+
+               // Move on to next token
+               dep = strtok_r(NULL, "\n", &p);
        }
+
+       free(buffer);
+       return 0;
 }
 
 int pakfire_string_startswith(const char* s, const char* prefix) {