From: Michael Tremer Date: Tue, 9 Aug 2022 15:50:21 +0000 (+0000) Subject: jail: Return any output as string X-Git-Tag: 0.9.28~539 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=12b9b39ff1a6253966bb5bbb4609a3fb0d304f08;p=pakfire.git jail: Return any output as string I thought it would be beneficial to return the output as an array, but that makes things way too complicated later on. Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 5e313fb04..58101b7ac 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -832,7 +832,6 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject* int flags = 0; int r; PyObject* ret = NULL; - char** buffer = NULL; char* output = NULL; PyObject* command = NULL; @@ -944,7 +943,7 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject* } // Execute command - r = pakfire_jail_exec(jail, argv, (return_output) ? &buffer : NULL); + r = pakfire_jail_exec(jail, argv, (return_output) ? &output : NULL); // If the return code was negative, we had some internal error if (r < 0) { @@ -966,19 +965,8 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject* // Did the user request the output? if (return_output) { - size_t length = 0; - - // Join everything together into a long string - if (buffer) { - output = pakfire_jail_concat_output(jail, (const char**)buffer, &length); - if (!output) { - PyErr_SetFromErrno(PyExc_OSError); - goto ERROR; - } - } - // Return the buffer as bytes - ret = PyBytes_FromStringAndSize(output, length); + ret = PyBytes_FromString(output); // Otherwise just return None } else { @@ -989,12 +977,6 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject* ERROR: if (argv) free(argv); - if (buffer) { - for (char** line = buffer; *line; line++) { - free(*line); - } - free(buffer); - } if (output) free(output); diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index 453284a34..15e0bc126 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -82,7 +82,7 @@ static const char* stages[] = { "exit 0\n" static int pakfire_build_run_script(struct pakfire_build* build, const char* filename, - const char* args[], char*** output) { + const char* args[], char** output) { char* script = NULL; size_t size = 0; char path[PATH_MAX]; @@ -138,8 +138,8 @@ static int find_dependency(char** haystack, const char* needle) { static int pakfire_build_find_dependencies(struct pakfire_build* build, struct pakfire_package* pkg, struct pakfire_filelist* filelist, const char* buildroot) { - char** provides = NULL; - char** requires = NULL; + char* provides = NULL; + char* requires = NULL; char path[PATH_MAX]; // Allocate path to write the filelist to @@ -181,6 +181,8 @@ 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++) { @@ -200,21 +202,16 @@ static int pakfire_build_find_dependencies(struct pakfire_build* build, pakfire_package_add_requires(pkg, *element); } } +#endif // Success r = 0; ERROR: - if (provides) { - for (char** element = provides; *element; element++) - free(*element); + if (provides) free(provides); - } - if (requires) { - for (char** element = requires; *element; element++) - free(*element); + if (requires) free(requires); - } unlink(path); return r; @@ -339,7 +336,7 @@ ERROR: static int pakfire_build_add_scriptlet_requires(struct pakfire_build* build, struct pakfire_package* pkg, struct pakfire_scriptlet* scriptlet) { - char** prerequires = NULL; + char* prerequires = NULL; char path[PATH_MAX]; size_t size; int r; @@ -382,21 +379,21 @@ 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); } } +#endif ERROR: if (r && *path) unlink(path); - if (prerequires) { - for (char** element = prerequires; *element; element++) - free(*element); + if (prerequires) free(prerequires); - } return r; } diff --git a/src/libpakfire/include/pakfire/jail.h b/src/libpakfire/include/pakfire/jail.h index 8ec1da523..299f69c3e 100644 --- a/src/libpakfire/include/pakfire/jail.h +++ b/src/libpakfire/include/pakfire/jail.h @@ -51,13 +51,9 @@ int pakfire_jail_set_env(struct pakfire_jail* jail, const char* key, const char* int pakfire_jail_import_env(struct pakfire_jail* jail, const char* env[]); // Execute -int pakfire_jail_exec(struct pakfire_jail* jail, const char* argv[], char*** output); +int pakfire_jail_exec(struct pakfire_jail* jail, const char* argv[], char** output); int pakfire_jail_exec_script(struct pakfire_jail* jail, - const char* script, const size_t size, const char* args[], char*** output); - -// Utility functions -char* pakfire_jail_concat_output(struct pakfire_jail* jail, - const char** input, size_t* length); + const char* script, const size_t size, const char* args[], char** output); #ifdef PAKFIRE_PRIVATE @@ -67,9 +63,9 @@ char* pakfire_jail_concat_output(struct pakfire_jail* jail, int pakfire_jail_set_cgroup(struct pakfire_jail* jail, struct pakfire_cgroup* cgroup); // Convenience functions -int pakfire_jail_run(struct pakfire* pakfire, const char* argv[], int flags, char*** output); +int pakfire_jail_run(struct pakfire* pakfire, const char* argv[], int flags, char** output); int pakfire_jail_run_script(struct pakfire* pakfire, - const char* script, const size_t length, const char* argv[], int flags, char*** output); + const char* script, const size_t length, const char* argv[], int flags, char** output); int pakfire_jail_shell(struct pakfire* pakfire); int pakfire_jail_ldconfig(struct pakfire* pakfire); diff --git a/src/libpakfire/jail.c b/src/libpakfire/jail.c index fad6bdf0c..3d196623e 100644 --- a/src/libpakfire/jail.c +++ b/src/libpakfire/jail.c @@ -683,32 +683,14 @@ ERROR: static int pakfire_jail_capture_stdout(struct pakfire* pakfire, void* data, int priority, const char* line, size_t length) { - char*** array = (char***)data; + char** output = (char**)data; + int r; - // Append everything from stdout to an array + // Append everything from stdout to a buffer if (priority == LOG_INFO) { - length = 0; - - // Create a copy of line - char* message = strdup(line); - if (!message) + r = asprintf(output, "%s%s", (output && *output) ? *output : "", line); + if (r < 0) return 1; - - // Determine the length of the existing array - if (*array) { - for (char** element = *array; *element; element++) - length++; - } - - // Allocate space - *array = reallocarray(*array, length + 2, sizeof(**array)); - if (!*array) - return 1; - - // Append message and terminate the array - (*array)[length] = message; - (*array)[length + 1] = NULL; - return 0; } @@ -1388,7 +1370,7 @@ ERROR: } PAKFIRE_EXPORT int pakfire_jail_exec(struct pakfire_jail* jail, - const char* argv[], char*** output) { + const char* argv[], char** output) { int r; // Store logging callback @@ -1409,7 +1391,7 @@ PAKFIRE_EXPORT int pakfire_jail_exec(struct pakfire_jail* jail, } PAKFIRE_EXPORT int pakfire_jail_exec_script(struct pakfire_jail* jail, - const char* script, const size_t size, const char* args[], char*** output) { + const char* script, const size_t size, const char* args[], char** output) { char path[PATH_MAX]; const char** argv = NULL; int r; @@ -1492,7 +1474,7 @@ ERROR: A convenience function that creates a new jail, runs the given command and destroys the jail again. */ -int pakfire_jail_run(struct pakfire* pakfire, const char* argv[], int flags, char*** output) { +int pakfire_jail_run(struct pakfire* pakfire, const char* argv[], int flags, char** output) { struct pakfire_jail* jail = NULL; int r; @@ -1512,7 +1494,7 @@ ERROR: } int pakfire_jail_run_script(struct pakfire* pakfire, - const char* script, const size_t length, const char* argv[], int flags, char*** output) { + const char* script, const size_t length, const char* argv[], int flags, char** output) { struct pakfire_jail* jail = NULL; int r; @@ -1565,24 +1547,3 @@ int pakfire_jail_ldconfig(struct pakfire* pakfire) { // Run ldconfig return pakfire_jail_run(pakfire, argv, 0, NULL); } - -// Utility functions - -PAKFIRE_EXPORT char* pakfire_jail_concat_output(struct pakfire_jail* jail, - const char** input, size_t* length) { - // Return nothing on no input - if (!input) - return NULL; - - // XXX Maybe there is a more efficient way to do this - - char* output = pakfire_string_join((char**)input, ""); - if (!output) - return NULL; - - // Store the length of the result - if (length) - *length = strlen(output); - - return output; -} diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 2bc802e35..ed88ce63d 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -138,7 +138,6 @@ global: pakfire_key_unref; # jail - pakfire_jail_concat_output; pakfire_jail_create; pakfire_jail_exec; pakfire_jail_exec_script; diff --git a/tests/libpakfire/jail.c b/tests/libpakfire/jail.c index 958951d0a..6cd15fe12 100644 --- a/tests/libpakfire/jail.c +++ b/tests/libpakfire/jail.c @@ -126,17 +126,13 @@ FAIL: static int test_nice(const struct test* t) { struct pakfire_jail* jail = NULL; - char** output = NULL; + char* output = NULL; int r = EXIT_FAILURE; const char* argv[] = { "/command", "print-nice", NULL, }; - char* expected_output[] = { - "5\n", NULL, - }; - // Create a new jail ASSERT_SUCCESS(pakfire_jail_create(&jail, t->pakfire, 0)); @@ -147,8 +143,9 @@ static int test_nice(const struct test* t) { // Set something sane ASSERT_SUCCESS(pakfire_jail_nice(jail, 5)); + // Check if the nice level has been set ASSERT_SUCCESS(pakfire_jail_exec(jail, argv, &output)); - ASSERT_STRING_ARRAY_EQUALS(output, expected_output); + ASSERT_STRING_EQUALS(output, "5\n"); // Success r = EXIT_SUCCESS; @@ -156,11 +153,8 @@ static int test_nice(const struct test* t) { FAIL: if (jail) pakfire_jail_unref(jail); - if (output) { - for (char** line = output; *line; line++) - free(*line); + if (output) free(output); - } return r; }