PATH:
opt
/
imunify360
/
venv
/
lib
/
python3.11
/
site-packages
/
defence360agent
/
utils
/
__pycache__
/
Editing: fd_ops.cpython-311.pyc
� ���i� � �� � d Z ddlZddlZddlZddlZddlmZmZ ddlm Z ej e� � Zdd�Z defd�Zeej fdd�d �� � 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)�contextmanager�suppress)�Path�returnc � � | 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. N���F)�follow_symlinks��dir_fdT)�os�scandir�is_dir�open�name�O_RDONLY�O_DIRECTORY� O_NOFOLLOW�append�unlink�pop�close�rmdir� BaseException)r �stack� current_fd�_�pushed�entries�entry�child_fd�fdr � parent_fds �Q/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/utils/fd_ops.py� rmtree_fdr$ s� � �"