]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Make default workspace directory logic more robust
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 9 Nov 2023 13:26:35 +0000 (14:26 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 9 Nov 2023 13:52:18 +0000 (14:52 +0100)
When running with sudo, $HOME won't be set, so let's rework the
logic to not rely on that.

mkosi/config.py

index 168b5aa59f62e9ba7e7bff3f1b928307f0045cf9..487a5274999ae2ef1cba6f6ac8cc790803e66d0a 100644 (file)
@@ -893,17 +893,18 @@ class MkosiConfig:
         if (cache := os.getenv("XDG_CACHE_HOME")) and Path(cache).exists():
             return Path(cache)
 
-        # If there's a cache directory and we're running from /home, we want to use a workspace directory in /home as
-        # well as /home might be on a separate partition or subvolume which means that to take advantage of reflinks
-        # and such, the workspace directory has to be on the same partition/subvolume.
-        if (
-            (home := os.getenv("HOME")) and
-            "/home" in home and
-            self.cache_dir and
-            self.cache_dir.is_relative_to(home) and
-            (Path(home) / ".cache").exists()
-        ):
-            return Path(home) / ".cache"
+        # If we're running from /home and there's a cache or output directory in /home, we want to use a workspace
+        # directory in /home as well as /home might be on a separate partition or subvolume which means that to take
+        # advantage of reflinks and such, the workspace directory has to be on the same partition/subvolume.
+        if Path.cwd().is_relative_to("/home") and len(Path.cwd().parents) >= 3:
+            home = Path.cwd().parents[-3]
+
+            if (
+                (self.cache_dir and self.cache_dir.is_relative_to(home) or
+                self.output_dir and self.output_dir.is_relative_to(home)) and
+                (home / ".cache").exists()
+            ):
+                return home / ".cache"
 
         return Path("/var/tmp")