]> git.ipfire.org Git - pakfire.git/commitdiff
jail: Return any output as string
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Aug 2022 15:50:21 +0000 (15:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Aug 2022 15:50:21 +0000 (15:50 +0000)
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 <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c
src/libpakfire/build.c
src/libpakfire/include/pakfire/jail.h
src/libpakfire/jail.c
src/libpakfire/libpakfire.sym
tests/libpakfire/jail.c

index 5e313fb04d096d9d3e41604bc7c055d3452f6166..58101b7acd8d686e5b8dac03e2c041f462bef0e7 100644 (file)
@@ -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);
 
index 453284a343b187755cc3b305799f4520c9d81dcf..15e0bc126f41fbd4c134dbdf086c15900eb74ff0 100644 (file)
@@ -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;
 }
 
index 8ec1da523dc74b3ea63ef5f41fa4e6090b103572..299f69c3e1da98640b4ee7f0b71d1f507190b34a 100644 (file)
@@ -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);
index fad6bdf0cab8b88115c9d0a37acd4379ffa1747a..3d196623efc0cdce6d90d7e918f682b74ba39b40 100644 (file)
@@ -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;
-}
index 2bc802e3536632c470726bdc5e035c0032ba0305..ed88ce63da5e8bbbf71cf958f717b4b8cb65ad4a 100644 (file)
@@ -138,7 +138,6 @@ global:
        pakfire_key_unref;
 
        # jail
-       pakfire_jail_concat_output;
        pakfire_jail_create;
        pakfire_jail_exec;
        pakfire_jail_exec_script;
index 958951d0a06dd09964e4e3adc41045e8631501c8..6cd15fe1261222de6ce1a064d4d578e4a56ec3d6 100644 (file)
@@ -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;
 }