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")
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:
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())