]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Make sure we create parent directories as well
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 26 Mar 2024 10:44:38 +0000 (11:44 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 26 Mar 2024 11:02:42 +0000 (12:02 +0100)
mkosi/__init__.py
mkosi/kmod.py
mkosi/util.py

index 3046e5adc79ec7c5d7ffbd334a0e3eee7f79e9d6..51303c8696cc95fa991d4c583effcacd00a15ca6 100644 (file)
@@ -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:
index 4477b3b91ada2e156418e3d11cb076036777f195..e572fdc1325229791e13bd89d7d940330f5f12e0 100644 (file)
@@ -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,
index 812c33e53464cb939cd9fabdb77d33c488061290..3f9c1c6a235e972dab6d8616a7833fc3ee8a2e41 100644 (file)
@@ -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]: