|
7 | 7 | import os
|
8 | 8 | import platform
|
9 | 9 | import shutil
|
| 10 | +import subprocess |
10 | 11 | import sys
|
11 | 12 | import tempfile
|
12 | 13 | from pathlib import Path
|
|
18 | 19 | from mkosi.log import log_notice, log_setup
|
19 | 20 | from mkosi.run import find_binary, run, uncaught_exception_handler
|
20 | 21 | from mkosi.sandbox import __version__, umask
|
21 |
| -from mkosi.tree import copy_tree |
| 22 | +from mkosi.tree import copy_tree, move_tree, rmtree |
22 | 23 | from mkosi.util import PathString, mandatory_variable, resource_path
|
23 | 24 |
|
24 | 25 |
|
@@ -141,12 +142,22 @@ def initrd_finalize(staging_dir: str, output: str, output_dir: str) -> None:
|
141 | 142 | else:
|
142 | 143 | output_dir = os.fspath(Path.cwd())
|
143 | 144 |
|
144 |
| - log_notice(f"Copying {staging_dir}/{output} to {output_dir}/{output}") |
| 145 | + staging = Path(f"{staging_dir}/{output}") |
| 146 | + tmp = Path(f"{output_dir}/{output}.new") |
| 147 | + final = Path(f"{output_dir}/{output}") |
| 148 | + |
| 149 | + log_notice(f"Copying {staging} to {tmp}") |
145 | 150 | # mkosi symlinks the expected output image, so dereference it
|
146 |
| - copy_tree( |
147 |
| - Path(f"{staging_dir}/{output}").resolve(), |
148 |
| - Path(f"{output_dir}/{output}"), |
149 |
| - ) |
| 151 | + try: |
| 152 | + copy_tree(staging.resolve(), tmp) |
| 153 | + except subprocess.CalledProcessError: |
| 154 | + rmtree(tmp) |
| 155 | + raise |
| 156 | + |
| 157 | + log_notice(f"Moving {output_dir}/{output}.new to {output_dir}/{output}") |
| 158 | + if final.is_dir(): |
| 159 | + rmtree(final) |
| 160 | + move_tree(tmp, final) |
150 | 161 |
|
151 | 162 |
|
152 | 163 | def initrd_common_args(parser: argparse.ArgumentParser) -> None:
|
|
0 commit comments