From: Michael Tremer Date: Tue, 9 Aug 2022 17:13:59 +0000 (+0000) Subject: util: Refactor pakfire_str2deps and use it everywhere X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=83d93f3342c86bc6461abe158a062c5aa2b76d0e;p=people%2Fstevee%2Fpakfire.git util: Refactor pakfire_str2deps and use it everywhere Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index 15e0bc12..0f921da4 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -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) diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index 4044bd2d..f44a1e3a 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -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) diff --git a/src/libpakfire/parser.c b/src/libpakfire/parser.c index 16c3f34e..39e07451 100644 --- a/src/libpakfire/parser.c +++ b/src/libpakfire/parser.c @@ -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); } diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index baf4c302..1ada9d25 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -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) {