a
     ÑOci  ã                   @   sZ   d Z ddlZddlmZ zddlmZ W n eyF   ddlmZ Y n0 G dd„ dƒZdS )z<For internal use only. It provides a slice-like file reader.é    N)ÚUnion)ÚLockc                   @   s”   e Zd ZdZeddœdd„Zeeef dœdd„Z	e
eed	œd
d„Zedœdd„Zddœdd„Zeedƒr~eee
dœdd„Zneee
dœdd„ZdS )Ú
FileBufferzA slice-able file readerN)ÚdatabaseÚreturnc                 C   s6   t |dƒ| _t | j ¡ ¡j| _ttdƒs2tƒ | _	d S )NÚrbÚpread)
ÚopenÚ_handleÚosÚfstatÚfilenoÚst_sizeÚ_sizeÚhasattrr   Ú_lock)Úselfr   © r   úE/var/www/html/gsl.tools/lib/python3.9/site-packages/maxminddb/file.pyÚ__init__   s    
zFileBuffer.__init__)Úkeyc                 C   sF   t |tƒr |  |j|j |j¡S t |tƒr:|  d|¡d S tdƒ‚d S )Né   r   zInvalid argument type.)Ú
isinstanceÚsliceÚ_readÚstopÚstartÚintÚ	TypeError)r   r   r   r   r   Ú__getitem__   s
    

zFileBuffer.__getitem__)Úneedler   r   c                 C   s0   |   | j| d |¡ |¡}|dkr(|S || S )zReverse find needle from startr   éÿÿÿÿ)r   r   Úrfind)r   r    r   Úposr   r   r   r"      s    zFileBuffer.rfind)r   c                 C   s   | j S )zSize of file)r   ©r   r   r   r   Úsize%   s    zFileBuffer.sizec                 C   s   | j  ¡  dS )z
Close fileN)r
   Úcloser$   r   r   r   r&   )   s    zFileBuffer.closer   )Ú
buffersizeÚoffsetr   c                 C   s   t  | j ¡ ||¡S )zread that uses pread)r   r   r
   r   ©r   r'   r(   r   r   r   r   /   s    zFileBuffer._readc                 C   sB   | j �( | j |¡ | j |¡W  d  ƒ S 1 s40    Y  dS )a¯  read with a lock

            This lock is necessary as after a fork, the different processes
            will share the same file table entry, even if we dup the fd, and
            as such the same offsets. There does not appear to be a way to
            duplicate the file table entry and we cannot re-open based on the
            original path as that file may have replaced with another or
            unlinked.
            N)r   r
   ÚseekÚreadr)   r   r   r   r   6   s    
)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   r   r   r   r   Úbytesr"   r%   r&   r   r   r   r   r   r   r   r      s   
r   )	r/   r   Útypingr   Úmultiprocessingr   ÚImportErrorÚ	threadingr   r   r   r   r   Ú<module>   s   