晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。   林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。   见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝)   既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。   南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。 sh-3ll

HOME


sh-3ll 1.0
DIR:/proc/self/root/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/utils/
Upload File :
Current File : //proc/self/root/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/utils/_shutil.py
"""High-level file operations."""
import errno
import logging
import os
import shutil

logger = logging.getLogger(__name__)


def is_safe_subdir_name(name) -> bool:
    return (
        isinstance(name, str)
        and bool(name)
        and "\x00" not in name
        and name == os.path.basename(name)
        and name not in (".", "..")
    )


def _is_nfs_silly_rename(path):
    """NFS replaces an unlinked-but-still-open file with a sibling
    `.nfsXXXX` entry that lingers until the holder closes it. The
    name has the magic `.nfs` prefix; treating those as ignorable
    EBUSY/ENOTEMPTY sources is what every long-running process on an
    NFS-backed work dir ends up needing."""
    return os.path.basename(str(path)).startswith(".nfs")


def _only_nfs_silly_renames_inside(path):
    """Return True if the directory at `path` exists and contains
    nothing but `.nfsXXXX` survivors."""
    try:
        entries = os.listdir(path)
    except OSError:
        return False
    if not entries:
        return False
    return all(_is_nfs_silly_rename(name) for name in entries)


def _swallow_nfs(func, path, exc_info):
    """`onerror` callback for shutil.rmtree.

    NFS-backed work dirs (k8s shared volumes especially) routinely
    contain `.nfsXXXX` silly-rename files left behind by a syncer
    that still holds an open fd to a file we just unlinked. Two
    shapes appear:

      1. unlink(`<dir>/.nfsXXXX`) → EBUSY because the syncer still
         has the fd open. The file expires on its own.
      2. rmdir(`<dir>`) → ENOTEMPTY because the .nfsXXXX child is
         still there. Same root cause.

    Both are tolerable: leaving the dir + .nfs survivors in place is
    not a leak — the holder eventually closes its fd and the next
    cleanup pass succeeds.

    Re-raise everything else.
    """
    err = exc_info[1] if exc_info else None
    if err is None:
        return
    if not isinstance(err, OSError) or err.errno not in (
        errno.EBUSY,
        errno.ENOTEMPTY,
    ):
        raise err
    if _is_nfs_silly_rename(path) or _only_nfs_silly_renames_inside(path):
        logger.debug(
            "ignoring NFS silly-rename leftover at %s during rmtree"
            " (errno=%s)",
            path,
            err.errno,
        )
        return
    raise err


def rmtree(path, ignore_errors=False, onerror=None, *, max_tries=3):
    """More robust shutil.rmtree.

    Retry on "Directory not empty" race condition:
    https://github.com/ansible/ansible/issues/34335#issuecomment-362995700

    Also tolerate NFS silly-rename ``.nfsXXXX`` leftovers — they
    expire when the holder closes its fd and there is nothing for
    a synchronous cleanup to do that wouldn't race.
    """
    # Compose onerror so callers' custom handlers still see other
    # errors. We let our handler swallow only the NFS pathology.
    # Only cluster deployments put work dirs on NFS; standalone keeps
    # the caller's onerror untouched.
    # Env read (not utils.is_cluster) to avoid a circular import.
    in_cluster = os.environ.get("IS_IM_CLUSTER") == "1"
    if not in_cluster:
        effective_onerror = onerror
    elif onerror is None:
        effective_onerror = _swallow_nfs
    else:

        def _chained(func, path_, exc_info):
            try:
                _swallow_nfs(func, path_, exc_info)
            except Exception:
                onerror(func, path_, exc_info)

        effective_onerror = _chained

    retriable = [errno.EEXIST, errno.ENOTEMPTY]
    if in_cluster:
        # An EBUSY that got past _swallow_nfs is a busy path on a shared
        # volume, where the holder usually lets go within a retry.
        retriable.append(errno.EBUSY)

    for i in range(1, max_tries + 1):
        try:
            return shutil.rmtree(path, ignore_errors, effective_onerror)
        except OSError as e:
            if i == max_tries or e.errno not in retriable:
                raise

            logger.warning("Can't remove %s tree, reason: %s", path, e)