From: Daan De Meyer Date: Tue, 26 Mar 2024 10:44:38 +0000 (+0100) Subject: Make sure we create parent directories as well X-Git-Tag: v23~61^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4af4500097967dc42541c9fc9ea93d89c172be08;p=thirdparty%2Fmkosi.git Make sure we create parent directories as well --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 3046e5adc..51303c869 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -77,6 +77,7 @@ from mkosi.util import ( format_rlimit, make_executable, one_zero, + parents_below, read_env_file, read_os_release, round_up, @@ -4129,12 +4130,16 @@ def run_build(args: Args, config: Config, *, resources: Path) -> None: if not p or p.exists(): continue - p.mkdir() + p.mkdir(parents=True, exist_ok=True) - # If we created the directory in a parent directory owned by the invoking user, make sure the directory itself - # is owned by the invoking user as well. - if INVOKING_USER.is_regular_user() and p.parent.stat().st_uid == INVOKING_USER.uid: - os.chown(p, INVOKING_USER.uid, INVOKING_USER.gid) + # If we created the directory in a parent directory owned by the invoking user, make sure the directories we + # just created are owned by the invoking user as well. + if ( + INVOKING_USER.is_regular_user() and + (q := next((parent for parent in p.parents if parent.stat().st_uid == INVOKING_USER.uid), None)) + ): + for parent in parents_below(p, q): + os.chown(parent, INVOKING_USER.uid, INVOKING_USER.gid) # Discard setuid/setgid bits as these are inherited and can leak into the image. if config.build_dir: diff --git a/mkosi/kmod.py b/mkosi/kmod.py index 4477b3b91..e572fdc13 100644 --- a/mkosi/kmod.py +++ b/mkosi/kmod.py @@ -11,6 +11,7 @@ from pathlib import Path from mkosi.log import complete_step, log_step from mkosi.run import run from mkosi.sandbox import Mount, SandboxProtocol, nosandbox +from mkosi.util import parents_below def loaded_modules() -> list[str]: @@ -150,11 +151,6 @@ def resolve_module_dependencies( return set(nametofile[m] for m in mods if m in nametofile), set(firmware) -def parents_below(path: Path, below: Path) -> list[Path]: - parents = list(path.parents) - return parents[:parents.index(below)] - - def gen_required_kernel_modules( root: Path, kver: str, diff --git a/mkosi/util.py b/mkosi/util.py index 812c33e53..3f9c1c6a2 100644 --- a/mkosi/util.py +++ b/mkosi/util.py @@ -197,6 +197,11 @@ def umask(mask: int) -> Iterator[None]: os.umask(old) +def parents_below(path: Path, below: Path) -> list[Path]: + parents = list(path.parents) + return parents[:parents.index(below)] + + @contextlib.contextmanager def resource_path(mod: ModuleType) -> Iterator[Path]: