From: Daan De Meyer Date: Tue, 30 Jan 2024 13:43:12 +0000 (+0100) Subject: Introduce INVOKING_USER.mkdir() X-Git-Tag: v21~77^2~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=db706106c3918614cfafbfefa164de448b53276f;p=thirdparty%2Fmkosi.git Introduce INVOKING_USER.mkdir() This function creates a directory as the appropriate user depending on where the directory is being created. --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index dc7a5b68c..75eca8fb9 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -3538,15 +3538,14 @@ def run_build(args: Args, config: Config, *, resources: Path) -> None: ): check_tools(config, Verb.build) - # Create these as the invoking user to make sure they're owned by the user running mkosi. for p in ( config.output_dir, config.cache_dir, config.build_dir, config.workspace_dir, ): - if p: - run(["mkdir", "--parents", p], user=INVOKING_USER.uid, group=INVOKING_USER.gid) + if p and not p.exists(): + INVOKING_USER.mkdir(p) with ( acl_toggle_build(config, INVOKING_USER.uid), diff --git a/mkosi/user.py b/mkosi/user.py index a8725c663..585b51cfc 100644 --- a/mkosi/user.py +++ b/mkosi/user.py @@ -9,7 +9,7 @@ import pwd from pathlib import Path from mkosi.log import die -from mkosi.run import spawn +from mkosi.run import run, spawn from mkosi.util import flock SUBRANGE = 65536 @@ -55,6 +55,13 @@ class INVOKING_USER: return cache / "mkosi" + @classmethod + def mkdir(cls, path: Path) -> Path: + user = cls.uid if cls.is_regular_user() and path.is_relative_to(cls.home()) else os.getuid() + group = cls.gid if cls.is_regular_user() and path.is_relative_to(cls.home()) else os.getgid() + run(["mkdir", "--parents", path], user=user, group=group) + return path + def read_subrange(path: Path) -> int: uid = str(os.getuid())