File: //usr/lib/python3/dist-packages/mercurial/revlogutils/__pycache__/randomaccessfile.cpython-310.pyc
o
�]LbA � @ 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 �H/usr/lib/python3/dist-packages/mercurial/revlogutils/randomaccessfile.py�_is_power_of_two s r
c @ sh e Zd ZdZ ddd�Zdd� Zddd �Zejdd
d��Z ejdd
� �Z
ddd�Zddd�Zdd� Z
dS )�randomaccessfilezDAccessing arbitrary chuncks of data within a file, with some cachingNc C sN t |�sJ �|| _|| _|| _d | _d | _d| _d| _|r%|\| _| _d S d S �N� r )r
�opener�filename�default_cached_chunk_size�writing_handle�reading_handle�
_cached_chunk�_cached_chunk_position)�selfr r r �
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 sl � |dur
|V dS | j r| j V dS | jr| jV dS | �� �}|V W d � dS 1 s/w 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 sx � | j du r7| jdu r7| jdur7| �� �}|| _ zdV W d| _ nd| _ w W d � dS 1 s0w Y dS dV dS )z4Context manager that keeps the file open for readingN)r r r r )r r r r r �readingO s �
"�
zrandomaccessfile.readingc C sh || }| j }|t| j� }||kr-||kr-||kr!||kr!| 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 �end�cache_start� cache_end�relative_startr r r �
read_chunk` s
zrandomaccessfile.read_chunkc C s� || j d @ }|| | j | j d @ | }| �|��}|�|� |�|�}W d � n1 s1w Y | �||� || }t|�| } | |k rXt| j||| f }
t� |
��||ks`||krgt
�|||�S |S )Nr )r r �seek�read�_add_cached_chunkr �PARTIAL_READ_MSGr r �RevlogErrorr r! )r r# r$ r �real_offset�real_length�file_obj�data�relative_offset�got�messager r r r"