]> git.ipfire.org Git - pakfire.git/commitdiff
execute: Add function to execute a script
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 1 Mar 2021 19:47:19 +0000 (19:47 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 1 Mar 2021 19:47:19 +0000 (19:47 +0000)
This has been moved from step

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/execute.c
src/libpakfire/include/pakfire/execute.h
src/libpakfire/step.c

index 1e4de1e75ce04a03a692b0e3201eb5fb38f183da..daa79d64bb769957f1f480b6ea9e58888e602e75 100644 (file)
@@ -35,6 +35,7 @@
 #include <pakfire/logging.h>
 #include <pakfire/private.h>
 #include <pakfire/types.h>
+#include <pakfire/util.h>
 
 #define BUFFER_SIZE      1024 * 64
 #define EPOLL_MAX_EVENTS 2
@@ -425,3 +426,72 @@ PAKFIRE_EXPORT int pakfire_execute_command(Pakfire pakfire, const char* command,
 
        return pakfire_execute(pakfire, argv, envp, flags, logging_callback);
 }
+
+PAKFIRE_EXPORT int pakfire_execute_script(Pakfire pakfire, const char* data, const size_t size,
+               char* envp[], int flags, pakfire_execute_logging_callback logging_callback) {
+       const char* root = pakfire_get_path(pakfire);
+
+       // Write the scriptlet to disk
+       char* path = pakfire_path_join(root, "tmp/.pakfire-script.XXXXXX");
+       int r;
+
+       DEBUG(pakfire, "Writing script to %s\n", path);
+
+       // Open a temporary file
+       int fd = mkstemp(path);
+       if (fd < 0) {
+               ERROR(pakfire, "Could not open a temporary file: %s\n",
+                       strerror(errno));
+
+               r = errno;
+       }
+
+       // Write data
+       ssize_t bytes_written = write(fd, data, size);
+       if (bytes_written < (ssize_t)size) {
+               ERROR(pakfire, "Could not write script to file %s: %s\n",
+                       path, strerror(errno));
+
+               r = errno;
+               goto out;
+       }
+
+       // Make the script executable
+       r = fchmod(fd, S_IRUSR|S_IWUSR|S_IXUSR);
+       if (r) {
+               ERROR(pakfire, "Could not set executable permissions on %s: %s\n",
+                       path, strerror(errno));
+
+               r = errno;
+               goto out;
+       }
+
+       // Close file
+       r = close(fd);
+       if (r) {
+               ERROR(pakfire, "Could not close script file %s: %s\n",
+                       path, strerror(errno));
+
+               r = errno;
+               goto out;
+       }
+
+       const char* command = path;
+       if (root)
+               command = pakfire_path_relpath(root, path);
+
+       // Run the script
+       r = pakfire_execute_command(pakfire, command, envp, flags, logging_callback);
+       if (r) {
+               DEBUG(pakfire, "Script return code: %d\n", r);
+       }
+
+out:
+       // Remove script from disk
+       unlink(path);
+
+       // Cleanup
+       free(path);
+
+       return r;
+}
index edc216365493cf2d4478bde1c6cd87195b40d5db..e1d534c4111f26a710f4e0d3c75d264fdb05c31c 100644 (file)
@@ -29,6 +29,8 @@ int pakfire_execute(Pakfire pakfire, const char* argv[], char* envp[],
        int flags, pakfire_execute_logging_callback logging_callback);
 int pakfire_execute_command(Pakfire pakfire, const char* command, char* envp[],
        int flags, pakfire_execute_logging_callback logging_callback);
+int pakfire_execute_script(Pakfire pakfire, const char* data, const size_t size,
+       char* envp[], int flags, pakfire_execute_logging_callback logging_callback);
 
 enum {
        PAKFIRE_EXECUTE_NONE                    = 0,
index c88d10b90578429b54041516f2cda3e5a4140b60..4faaad9f143d97d5b39251692e293363309defa3 100644 (file)
@@ -234,71 +234,7 @@ static int pakfire_script_check_shell(struct pakfire_scriptlet* scriptlet) {
 }
 
 static int pakfire_step_run_shell_script(PakfireStep step, struct pakfire_scriptlet* scriptlet) {
-       const char* root = pakfire_get_path(step->pakfire);
-
-       // Write the scriptlet to disk
-       char* path = pakfire_path_join(root, "tmp/.pakfire-scriptlet.XXXXXX");
-       int r;
-
-       DEBUG(step->pakfire, "Writing script to %s\n", path);
-
-       // Open a temporary file
-       int fd = mkstemp(path);
-       if (fd < 0) {
-               ERROR(step->pakfire, "Could not open a temporary file: %s\n",
-                       strerror(errno));
-
-               r = errno;
-       }
-
-       // Write data
-       ssize_t bytes_written = write(fd, scriptlet->data, scriptlet->size);
-       if (bytes_written < (ssize_t)scriptlet->size) {
-               ERROR(step->pakfire, "Could not write script to file %s: %s\n",
-                       path, strerror(errno));
-
-               r = errno;
-               goto out;
-       }
-
-       // Make the script executable
-       r = fchmod(fd, S_IRUSR|S_IWUSR|S_IXUSR);
-       if (r) {
-               ERROR(step->pakfire, "Could not set executable permissions on %s: %s\n",
-                       path, strerror(errno));
-
-               r = errno;
-               goto out;
-       }
-
-       // Close file
-       r = close(fd);
-       if (r) {
-               ERROR(step->pakfire, "Could not close script file %s: %s\n",
-                       path, strerror(errno));
-
-               r = errno;
-               goto out;
-       }
-
-       const char* command = path;
-       if (root)
-               command = pakfire_path_relpath(root, path);
-
-       // Run the script
-       r = pakfire_execute_command(step->pakfire, command, NULL, 0, NULL);
-       if (r) {
-               DEBUG(step->pakfire, "Script return code: %d\n", r);
-       }
-
-out:
-       // Remove script from disk
-       unlink(path);
-
-       // Cleanup
-       free(path);
-
-       return r;
+       return pakfire_execute_script(step->pakfire, scriptlet->data, scriptlet->size, NULL, 0, NULL);
 }
 
 static int pakfire_step_run_script(PakfireStep step,