From: Joerg Behrmann Date: Thu, 24 Nov 2022 11:50:24 +0000 (+0100) Subject: Move unlink_try_hard and dependents into mkosi.remove X-Git-Tag: v15~384^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=295cb8fa838ad0f169a3359cf75ed0f1353db40f;p=thirdparty%2Fmkosi.git Move unlink_try_hard and dependents into mkosi.remove Similar to mkosi.install but for removing things. --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index c5b2eb2cf..aa05629b6 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -113,6 +113,7 @@ from mkosi.install import ( ) from mkosi.manifest import Manifest from mkosi.mounts import mount, mount_api_vfs, mount_bind, mount_overlay, mount_tmpfs +from mkosi.remove import unlink_try_hard from mkosi.syscall import blkpg_add_partition, blkpg_del_partition complete_step = MkosiPrinter.complete_step @@ -561,27 +562,6 @@ def btrfs_subvol_create(path: Path, mode: int = 0o755) -> None: run(["btrfs", "subvol", "create", path]) -def btrfs_subvol_delete(path: Path) -> None: - # Extract the path of the subvolume relative to the filesystem - c = run(["btrfs", "subvol", "show", path], - stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) - subvol_path = c.stdout.splitlines()[0] - # Make the subvolume RW again if it was set RO by btrfs_subvol_delete - run(["btrfs", "property", "set", path, "ro", "false"]) - # Recursively delete the direct children of the subvolume - c = run(["btrfs", "subvol", "list", "-o", path], - stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) - for line in c.stdout.splitlines(): - if not line: - continue - child_subvol_path = line.split(" ", 8)[-1] - child_path = path / cast(str, os.path.relpath(child_subvol_path, subvol_path)) - btrfs_subvol_delete(child_path) - # Delete the subvolume now that all its descendants have been deleted - run(["btrfs", "subvol", "delete", path], - stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) - - def btrfs_subvol_make_ro(path: Path, b: bool = True) -> None: run(["btrfs", "property", "set", path, "ro", "true" if b else "false"]) @@ -5262,29 +5242,6 @@ def detect_distribution() -> Tuple[Optional[Distribution], Optional[str]]: return d, version_id -def unlink_try_hard(path: Optional[PathString]) -> None: - if path is None: - return - - path = Path(path) - try: - path.unlink() - return - except FileNotFoundError: - return - except Exception: - pass - - if shutil.which("btrfs"): - try: - btrfs_subvol_delete(path) - return - except Exception: - pass - - shutil.rmtree(path) - - def remove_glob(*patterns: PathString) -> None: pathgen = (glob.glob(str(pattern)) for pattern in patterns) paths: Set[str] = set(sum(pathgen, [])) # uniquify diff --git a/mkosi/distributions/gentoo.py b/mkosi/distributions/gentoo.py index bb262cb8b..a5964a219 100644 --- a/mkosi/distributions/gentoo.py +++ b/mkosi/distributions/gentoo.py @@ -11,7 +11,6 @@ from pathlib import Path from textwrap import dedent from typing import Dict, Generator, List, Sequence -from mkosi import unlink_try_hard from mkosi.backend import ( ARG_DEBUG, MkosiConfig, @@ -28,6 +27,7 @@ from mkosi.backend import ( ) from mkosi.distributions import DistributionInstaller from mkosi.install import copy_path, open_close +from mkosi.remove import unlink_try_hard ARCHITECTURES = { "x86_64": ("amd64", "arch/x86/boot/bzImage"), diff --git a/mkosi/remove.py b/mkosi/remove.py new file mode 100644 index 000000000..36a66a24c --- /dev/null +++ b/mkosi/remove.py @@ -0,0 +1,53 @@ +# SPDX-License-Identifier: LGPL-2.1+ + +import os +import shutil +import subprocess +from pathlib import Path +from typing import Optional, cast + +from mkosi.backend import PathString, run + + +def btrfs_subvol_delete(path: Path) -> None: + # Extract the path of the subvolume relative to the filesystem + c = run(["btrfs", "subvol", "show", path], + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) + subvol_path = c.stdout.splitlines()[0] + # Make the subvolume RW again if it was set RO by btrfs_subvol_delete + run(["btrfs", "property", "set", path, "ro", "false"]) + # Recursively delete the direct children of the subvolume + c = run(["btrfs", "subvol", "list", "-o", path], + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) + for line in c.stdout.splitlines(): + if not line: + continue + child_subvol_path = line.split(" ", 8)[-1] + child_path = path / cast(str, os.path.relpath(child_subvol_path, subvol_path)) + btrfs_subvol_delete(child_path) + # Delete the subvolume now that all its descendants have been deleted + run(["btrfs", "subvol", "delete", path], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + + +def unlink_try_hard(path: Optional[PathString]) -> None: + if path is None: + return + + path = Path(path) + try: + path.unlink() + return + except FileNotFoundError: + return + except Exception: + pass + + if shutil.which("btrfs"): + try: + btrfs_subvol_delete(path) + return + except Exception: + pass + + shutil.rmtree(path)