ruạṛ
a �+�bA � @ sL d dl Z ddlmZ ddlmZmZ dZed�Zdd� ZG d d � d e �Z dS )� N� )�_)�error�utili sC partial read of revlog %s; expected %d bytes from offset %d, got %dc C s | | d @ dko| dkS )N� r � )�nr r �L/usr/lib64/python3.9/site-packages/mercurial/revlogutils/randomaccessfile.py�_is_power_of_two s r c @ sf e Zd ZdZddd�Zdd� Zddd �Zejdd d��Z ejdd � �Z ddd�Zddd�Zdd� Z dS )�randomaccessfilezDAccessing arbitrary chuncks of data within a file, with some cachingNc C sJ t |�sJ �|| _|| _|| _d | _d | _d| _d| _|rF|\| _| _d S �N� r )r �opener�filename�default_cached_chunk_size�writing_handle�reading_handle� _cached_chunk�_cached_chunk_position)�selfr r r Z initial_cacher r r �__init__ s zrandomaccessfile.__init__c C s d| _ d| _d S r )r r )r r r r �clear_cache1 s zrandomaccessfile.clear_cache� rc C s | j | j|d�S )zReturn a file object)�mode)r r )r r r r r �_open5 s zrandomaccessfile._openc c sb |dur|V nN| j r | j V n>| jr0| jV n.| �� �}|V W d � n1 sT0 Y dS )z%File object suitable for reading dataN)r r r )r �existing_file_obj�fpr r r � _open_read9 s zrandomaccessfile._open_readc c sr | j du rh| jdu rh| jdurh| �� �0}|| _ zdV W d| _ nd| _ 0 W d � qn1 s\0 Y ndV dS )z4Context manager that keeps the file open for readingN)r r r r )r r r r r �readingO s ��� 0zrandomaccessfile.readingc C sh || }| j }|t| j� }||krZ||krZ||krB||krB| jS || }t�| j||�S | �|||�S )a� Read a chunk of bytes from the file. Accepts an absolute offset, length to read, and an optional existing file handle to read from. If an existing file handle is passed, it will be seeked and the original seek position will NOT be restored. Returns a str or buffer of raw byte data. Raises if the requested number of bytes could not be read. )r �lenr r �buffer�_read_and_update_cache)r �offset�lengthr �endZcache_startZ cache_endZrelative_startr r r � read_chunk` s zrandomaccessfile.read_chunkc C s� || j d @ }|| | j | j d @ | }| �|��$}|�|� |�|�}W d � n1 sb0 Y | �||� || }t|�| } | |k r�t| j||| f } t� | ��||ks�||kr�t �|||�S |S )Nr )r r �seek�read�_add_cached_chunkr �PARTIAL_READ_MSGr r ZRevlogErrorr r )r r"