From: Daan De Meyer Date: Tue, 30 Jan 2024 13:38:32 +0000 (+0100) Subject: Simplify cache directory calculation X-Git-Tag: v21~77^2~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cf7f79cb69579ec5df5e8788f0f67bb78a621ecd;p=thirdparty%2Fmkosi.git Simplify cache directory calculation --- diff --git a/mkosi/config.py b/mkosi/config.py index c7db14264..769acaee6 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -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") diff --git a/mkosi/user.py b/mkosi/user.py index c468b43b3..a8725c663 100644 --- a/mkosi/user.py +++ b/mkosi/user.py @@ -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())