]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Simplify cache directory calculation
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 30 Jan 2024 13:38:32 +0000 (14:38 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 31 Jan 2024 13:24:43 +0000 (14:24 +0100)
mkosi/config.py
mkosi/user.py

index c7db14264e262a9d624477ae2913afe4106293d6..769acaee6c096314f52fae3d391af40bb34f03c4 100644 (file)
@@ -1296,21 +1296,8 @@ class Config:
         if self.workspace_dir:
             return self.workspace_dir
 
-        if (cache := os.getenv("XDG_CACHE_HOME")) and Path(cache).exists():
-            return Path(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(INVOKING_USER.home()) and
-            (INVOKING_USER.home() / ".cache").exists() and
-            (
-                self.cache_dir and self.cache_dir.is_relative_to(INVOKING_USER.home()) or
-                self.output_dir and self.output_dir.is_relative_to(INVOKING_USER.home())
-            )
-        ):
-            return INVOKING_USER.home() / ".cache"
+        if (cache := INVOKING_USER.cache_dir()) and cache != Path("/var/cache/mkosi"):
+            return cache
 
         return Path("/var/tmp")
 
index c468b43b3a421432ea8e7126b2021502e7a046ac..a8725c66347a091d3e02987616912af6885c7c16 100644 (file)
@@ -18,6 +18,7 @@ SUBRANGE = 65536
 class INVOKING_USER:
     uid = int(os.getenv("SUDO_UID") or os.getenv("PKEXEC_UID") or os.getuid())
     gid = int(os.getenv("SUDO_GID") or os.getgid())
+    invoked_as_root = uid == 0
 
     @classmethod
     def init(cls) -> None:
@@ -39,6 +40,21 @@ class INVOKING_USER:
     def home(cls) -> Path:
         return Path(f"~{cls.name()}").expanduser()
 
+    @classmethod
+    def is_regular_user(cls) -> bool:
+        return cls.uid >= 1000
+
+    @classmethod
+    def cache_dir(cls) -> Path:
+        if (env := os.getenv("XDG_CACHE_HOME")) or (env := os.getenv("CACHE_DIRECTORY")):
+            cache = Path(env)
+        elif cls.is_regular_user() and (Path.cwd().is_relative_to(INVOKING_USER.home()) or not cls.invoked_as_root):
+            cache = INVOKING_USER.home() / ".cache"
+        else:
+            cache = Path("/var/cache")
+
+        return cache / "mkosi"
+
 
 def read_subrange(path: Path) -> int:
     uid = str(os.getuid())