]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Only set --no-target-directory for cp when src is a directory
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 6 Aug 2023 13:10:07 +0000 (15:10 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 6 Aug 2023 17:30:33 +0000 (19:30 +0200)
This makes sure that when src is a file and dst is a directory that
src is copied into dst.

mkosi/tree.py

index 06e01198695533b5419d48c316919ac49b7ef3bc..952c75646e7d3f4ad3490e69192580c4f05611bd 100644 (file)
@@ -4,7 +4,7 @@ import errno
 import shutil
 import subprocess
 from pathlib import Path
-from typing import Optional, Sequence
+from typing import Optional
 
 from mkosi.archive import extract_tar
 from mkosi.config import ConfigFeature, MkosiConfig
@@ -50,15 +50,20 @@ def copy_tree(config: MkosiConfig, src: Path, dst: Path, *, preserve_owner: bool
     if config.use_subvolumes == ConfigFeature.enabled and not shutil.which("btrfs"):
         die("Subvolumes requested but the btrfs command was not found")
 
-    copy: Sequence[PathString] = [
+    copy: list[PathString] = [
         "cp",
         "--recursive",
         f"--preserve=mode,timestamps,links,xattr{',ownership' if preserve_owner else ''}",
-        "--no-target-directory",
         "--reflink=auto",
         src, dst,
     ]
 
+    # If the source and destination are both directories, we want to merge the source directory with the
+    # destination directory. If the source if a file and the destination is a directory, we want to copy
+    # the source inside the directory.
+    if src.is_dir():
+        copy += ["--no-target-directory"]
+
     # Subvolumes always have inode 256 so we can use that to check if a directory is a subvolume.
     if not subvolume or not preserve_owner or not is_subvolume(src) or (dst.exists() and any(dst.iterdir())):
         run(copy)