]> git.ipfire.org Git - pakfire.git/commitdiff
build: Make local repository available inside the shell
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 Sep 2021 14:55:07 +0000 (14:55 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 Sep 2021 14:55:07 +0000 (14:55 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/build.c

index 998eb86a1e9569149633100a501ff95a1727e0c1..5df592b6bc9067eb1a02f0b3d5e75665b16b9211 100644 (file)
@@ -21,6 +21,7 @@
 #include <errno.h>
 #include <glob.h>
 #include <stdlib.h>
+#include <sys/mount.h>
 #include <unistd.h>
 #include <uuid/uuid.h>
 
@@ -798,6 +799,80 @@ ERROR:
        return r;
 }
 
+/*
+       This function enables the local repository so that it can be access by
+       a pakfire instance running inside the chroot.
+*/
+static int pakfire_build_enable_repos(struct pakfire* pakfire) {
+       char repopath[PATH_MAX];
+       FILE* f = NULL;
+       int r = 1;
+
+       // Fetch the local repository
+       struct pakfire_repo* repo = pakfire_get_repo(pakfire, PAKFIRE_REPO_LOCAL);
+       if (!repo) {
+               DEBUG(pakfire, "Could not find repository '%s': %m\n", PAKFIRE_REPO_LOCAL);
+               return 0;
+       }
+
+       // Fetch repository configuration
+       char* config = pakfire_repo_get_config(repo);
+       if (!config) {
+               ERROR(pakfire, "Could not generate repository configuration: %m\n");
+               goto ERROR;
+       }
+
+       const char* path = pakfire_repo_get_path(repo);
+
+       // Make sure the source directory exists
+       r = pakfire_mkdir(path, 0);
+       if (r) {
+               ERROR(pakfire, "Could not create repository directory %s: %m\n", path);
+               goto ERROR;
+       }
+
+       // Bind-mount the repository data read-only
+       r = pakfire_bind(pakfire, path, NULL, MS_RDONLY);
+       if (r) {
+               ERROR(pakfire, "Could not bind-mount the repository at %s: %m\n", path);
+               goto ERROR;
+       }
+
+       // Make path for configuration file
+       r = pakfire_make_path(pakfire, repopath,
+               PAKFIRE_CONFIG_PATH "/repos/" PAKFIRE_REPO_LOCAL ".repo");
+       if (r < 0)
+               goto ERROR;
+
+       // Open configuration file for writing
+       f = fopen(repopath, "w");
+       if (!f) {
+               ERROR(pakfire, "Could not open %s for writing: %m\n", path);
+               r = 1;
+               goto ERROR;
+       }
+
+       // Write configuration
+       size_t bytes_written = fprintf(f, "%s", config);
+       if (bytes_written < strlen(config)) {
+               ERROR(pakfire, "Could not write repository configuration: %m\n");
+               r = 1;
+               goto ERROR;
+       }
+
+       // Success
+       r = 0;
+
+ERROR:
+       if (f)
+               fclose(f);
+       if (config)
+               free(config);
+       pakfire_repo_unref(repo);
+
+       return r;
+}
+
 PAKFIRE_EXPORT int pakfire_shell(struct pakfire* pakfire) {
        int r;
 
@@ -806,6 +881,11 @@ PAKFIRE_EXPORT int pakfire_shell(struct pakfire* pakfire) {
        if (r)
                return r;
 
+       // Export local repository if running in interactive mode
+       r = pakfire_build_enable_repos(pakfire);
+       if (r)
+               return r;
+
        return pakfire_execute_shell(pakfire);
 }