summaryrefslogtreecommitdiff
path: root/plugins/claude-security/scripts/lib/removal.py
blob: a4eeefe7964f0f155f675d05b0b408bbe9c06ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Removing a directory tree the plugin made, whose files or directories may be read-only."""

from __future__ import annotations

import functools
import os
import shutil
import stat
from contextlib import suppress
from pathlib import Path
from typing import TYPE_CHECKING

from .link import is_link

if TYPE_CHECKING:
    from collections.abc import Callable
    from types import TracebackType


def writable_then(
    root: str,
    function: Callable[..., object],
    name: str,
    exc_info: tuple[type[BaseException], BaseException, TracebackType],
) -> None:
    """rmtree's error handler for the tree at `root`: retry a refused unlink or rmdir once, after
    making the entry writable on Windows (a link never touched) or, on POSIX, its parent when that
    is a real directory inside `root`; an entry already gone counts as removed; raise anything else,
    or a retry that fails. `root` is absolute and normalised, as os.path.abspath writes it."""
    if isinstance(exc_info[1], FileNotFoundError):
        return
    if function not in {os.unlink, os.rmdir}:
        raise exc_info[1]
    with suppress(FileNotFoundError):
        if os.name == "nt":
            if not is_link(name):
                Path(name).chmod(stat.S_IWRITE)
        else:
            parent = Path(os.path.abspath(name)).parent
            if os.path.commonpath([root, parent]) == root and not is_link(parent):
                mode = parent.stat().st_mode
                if not mode & stat.S_IWUSR:
                    parent.chmod(mode | stat.S_IWUSR)
        function(name)


def remove_tree(path: Path) -> None:
    """Remove the tree at `path`, first making writable a read-only entry (Windows) or a
    read-only directory in it (POSIX) that blocks the removal; an OSError names what could still
    not be removed."""
    root = os.path.abspath(path)
    shutil.rmtree(root, onerror=functools.partial(writable_then, root))