]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: Reorganise initialization
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 17 Oct 2024 18:11:23 +0000 (18:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 17 Oct 2024 18:11:23 +0000 (18:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/pakfire.c

index c31fffb52268e658f76e873f0984aac0750bcf82..d892cdac41a46fd5cd428fc46e437a6858cded33 100644 (file)
@@ -443,14 +443,7 @@ static int pakfire_safety_checks(struct pakfire* pakfire) {
        // We must be root in order to operate in /
        if (pakfire->user.uid) {
                CTX_ERROR(pakfire->ctx, "Must be running as root on /\n");
-               errno = EPERM;
-               return 1;
-       }
-
-       if (strcmp(pakfire->distro.id, "ipfire") != 0) {
-               CTX_ERROR(pakfire->ctx, "Not an IPFire system\n");
-               errno = EPERM;
-               return 1;
+               return -EPERM;
        }
 
        return 0;
@@ -642,45 +635,28 @@ static int pakfire_config_import_distro(struct pakfire* pakfire) {
        return 0;
 }
 
-static int pakfire_read_config(struct pakfire* pakfire, FILE* f) {
-       int r;
-
-       CTX_DEBUG(pakfire->ctx, "Reading configuration\n");
-
-       // Read configuration from file
-       if (f) {
-               r = pakfire_config_read(pakfire->config, f);
-               if (r) {
-                       CTX_ERROR(pakfire->ctx, "Could not parse configuration: %m\n");
-                       return r;
-               }
-       }
-
-       // Import distro configuration
-       r = pakfire_config_import_distro(pakfire);
-       if (r)
-               return r;
-
-#if 0
-       // Read repository configuration
-       r = pakfire_read_repo_config(pakfire);
-       if (r)
-               return r;
-#endif
-
-       return 0;
-}
-
 static int pakfire_read_os_release(struct pakfire* pakfire) {
        char path[PATH_MAX];
        int r;
 
        // Compose path
        r = pakfire_path(pakfire, path, "%s", "/etc/os-release");
-       if (r)
+       if (r < 0)
                return r;
 
-       return pakfire_distro(&pakfire->distro, path);
+       // Read the distro
+       r = pakfire_distro(&pakfire->distro, path);
+       if (r < 0) {
+               switch (-r) {
+                       case ENOENT:
+                               return 0;
+
+                       default:
+                               break;
+               }
+       }
+
+       return r;
 }
 
 static int pakfire_set_cache_path(struct pakfire* pakfire) {
@@ -838,16 +814,41 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx*
        if (r < 0)
                goto ERROR;
 
+       // Store if we have a path
+       if (path)
+               p->internal_flags |= PAKFIRE_HAS_PATH;
+
        // Initialise configuration
        r = pakfire_config_create(&p->config);
        if (r < 0)
                goto ERROR;
 
-       // Read configuration file
-       r = pakfire_read_config(p, conf);
+       // Read the configuration file
+       if (conf) {
+               r = pakfire_config_read(p->config, conf);
+               if (r < 0) {
+                       CTX_ERROR(p->ctx, "Could not read configuration: %s\n", strerror(-r));
+                       goto ERROR;
+               }
+       }
+
+       // Import distro configuration
+       r = pakfire_config_import_distro(p);
        if (r < 0)
                goto ERROR;
 
+       // Read /etc/os-release
+       if (p->internal_flags & PAKFIRE_HAS_PATH) {
+               r = pakfire_read_os_release(p);
+               if (r < 0)
+                       goto ERROR;
+
+               // Perform some safety checks
+               r = pakfire_safety_checks(p);
+               if (r < 0)
+                       goto ERROR;
+       }
+
        // Set cache path
        r = pakfire_set_cache_path(p);
        if (r < 0) {
@@ -865,12 +866,8 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx*
                }
        }
 
-       // Store if we have a path
-       if (path) {
-               p->internal_flags |= PAKFIRE_HAS_PATH;
-
-       // Otherwise we create our own temporary path
-       } else {
+       // If we don't have a path, create something temporary...
+       if (!path) {
                // Template when running Pakfire in a temporary environment
                char tmppath[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-root-XXXXXX";
 
@@ -909,16 +906,23 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx*
        if (r < 0)
                goto ERROR;
 
-       // Read /etc/os-release
-       r = pakfire_read_os_release(p);
-       if (r && errno != ENOENT)
+       // Populate pool
+       r = pakfire_populate_pool(p);
+       if (r < 0)
                goto ERROR;
 
-       // Bump RLIMIT_NOFILE to maximum
-       r = pakfire_rlimit_set(p, PAKFIRE_RLIMIT_NOFILE_MAX);
-       if (r)
+       // Create repositories
+       r = pakfire_repo_import(p, p->config);
+       if (r < 0)
                goto ERROR;
 
+       // Read repository configuration
+       if (p->internal_flags & PAKFIRE_HAS_PATH) {
+               r = pakfire_read_repo_config(p);
+               if (r < 0)
+                       goto ERROR;
+       }
+
        CTX_DEBUG(p->ctx, "Pakfire initialized at %p\n", p);
        CTX_DEBUG(p->ctx, "  user   = %s (%u)\n", p->user.name, p->user.uid);
        CTX_DEBUG(p->ctx, "  group  = %s (%u)\n", p->group.name, p->group.gid);
@@ -929,11 +933,6 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx*
        if (p->group.subgids.id)
                CTX_DEBUG(p->ctx, "  subgid = %u (%zu)\n", p->group.subgids.id, p->group.subgids.length);
 
-       // Perform some safety checks
-       r = pakfire_safety_checks(p);
-       if (r)
-               goto ERROR;
-
        // Dump distribution configuration
        CTX_DEBUG(p->ctx, "  Distribution: %s\n", p->distro.pretty_name);
        CTX_DEBUG(p->ctx, "    name       = %s\n", p->distro.name);
@@ -959,14 +958,9 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx*
                goto ERROR;
        }
 
-       // Populate pool
-       r = pakfire_populate_pool(p);
-       if (r < 0)
-               goto ERROR;
-
-       // Create repositories
-       r = pakfire_repo_import(p, p->config);
-       if (r < 0)
+       // Bump RLIMIT_NOFILE to maximum
+       r = pakfire_rlimit_set(p, PAKFIRE_RLIMIT_NOFILE_MAX);
+       if (r)
                goto ERROR;
 
        // Return the pointer