*6j d Z ddlZddlZddlZddlZddlmZmZ ddlm Z ej e ZddZ defdZeej fddd Zed Zdededefd ZdS )a[ fd-based file operations for symlink-attack mitigation. All helpers in this module use O_NOFOLLOW and dir_fd-relative syscalls so that no path-based resolution can be redirected by a concurrent symlink swap. This module is intentionally kept separate from utils/__init__.py to avoid loading these OS-specific helpers into every agent component. N)contextmanagersuppress)Pathreturnc | dfg} |r$|d \ }}d}t j | 5 }|D ]}| d r`t j |j t j t j z t j z | }| ||j f d} nt j |j | ddd n# 1 swxY w Y |sN| \ }} | 5t j | |d \ } }t j | | |"dS dS # t $ r |D ]\ }} | t j | w xY w)u Remove all contents of a directory using fd-relative operations. Every entry is opened with ``O_NOFOLLOW`` so symlinks inside the tree are unlinked rather than followed. The directory referenced by *dir_fd* itself is **not** removed — the caller should ``os.rmdir()`` the parent entry after this call returns. Uses an iterative approach with an explicit stack to avoid hitting Python's recursion limit on adversarial deeply-nested trees. *dir_fd* must be an open ``O_RDONLY | O_DIRECTORY`` descriptor. NF)follow_symlinksdir_fdT)osscandiris_diropennameO_RDONLYO_DIRECTORY O_NOFOLLOWappendunlinkpopclosermdir BaseException)r stack current_fd_pushedentriesentrychild_fdfdr parent_fds Q/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/utils/fd_ops.py rmtree_fdr$ s " d^E 5!"IMJFJ'' A7$ A AE||E|:: A#%7!JK".82=H#-$ $ $ h %;<<