]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Move IPFire check
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 Mar 2021 19:10:28 +0000 (19:10 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 Mar 2021 19:10:28 +0000 (19:10 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/pakfire.c
src/pakfire/base.py
src/pakfire/errors.py

index ff6dc69dcacc88b17ae66522518a84e45da89878..5c27032cef155f2a44a3b215f256bb7c5c003b72 100644 (file)
@@ -147,6 +147,25 @@ static void pakfire_free(Pakfire pakfire) {
        free(pakfire);
 }
 
+// Safety check in case this is being launched on the host system
+static int pakfire_safety_checks(Pakfire pakfire) {
+       // Nothing to do if we are not working on root
+       if (strcmp(pakfire->path, "/") != 0)
+               return 0;
+
+       char* path = pakfire_make_path(pakfire, "/etc/ipfire-release");
+       if (!path)
+               return 1;
+
+       if (!pakfire_path_exists(path)) {
+               ERROR(pakfire, "Not an IPFire system\n");
+               errno = EPERM;
+               return 1;
+       }
+
+       return 0;
+}
+
 PAKFIRE_EXPORT int pakfire_create(Pakfire* pakfire, const char* path, const char* arch) {
        int r;
 
@@ -205,6 +224,11 @@ PAKFIRE_EXPORT int pakfire_create(Pakfire* pakfire, const char* path, const char
        DEBUG(p, "  arch = %s\n", pakfire_get_arch(p));
        DEBUG(p, "  path = %s\n", pakfire_get_path(p));
 
+       // Perform some safety checks
+       r = pakfire_safety_checks(p);
+       if (r)
+               goto ERROR;
+
        // Make sure that our private directory exists
        char* private_dir = pakfire_make_path(p, PAKFIRE_PRIVATE_DIR);
        r = pakfire_mkdir(p, private_dir, 0);
index e59b1501a0574eec6678d20662fd1287f68125da..0d0a0dab7ecd9e5a8f43a934a1044d43fc61aa8d 100644 (file)
@@ -49,10 +49,6 @@ class Pakfire(_pakfire.Pakfire):
                # Default to system distribution
                self.distro = distro or system.distro
 
-               # check if we are actually running on an ipfire system
-               if self.path == "/":
-                       self.check_is_ipfire()
-
                # Load configuration
                self.config = config or Config("general.conf")
 
@@ -106,12 +102,6 @@ class Pakfire(_pakfire.Pakfire):
                for repo in self.repos:
                        repo.refresh(force=force)
 
-       def check_is_ipfire(self):
-               ret = os.path.exists("/etc/ipfire-release")
-
-               if not ret:
-                       raise NotIPFireError("You can run pakfire only on an IPFire system")
-
        def clean(self):
                # Clean up repository caches
                for repo in self.repos:
index ad33975e4d608a2b91530731006f48e176fb4f0d..bc7ecfb2796a0b70bc64fa9e26ff0628b8747410 100644 (file)
@@ -65,9 +65,6 @@ class FileError(Error):
 class FileNotFoundError(Error):
        pass
 
-class NotIPFireError(Error):
-       pass
-
 class OfflineModeError(Error):
        message = _("The requested action cannot be done on offline mode.\n"
                "Please connect your system to the network, remove --offline from the"