B
    ![                 @   s   d dl mZmZmZ d dlmZ d dlmZmZm	Z	m
Z
 d dlmZmZ ddlmZ ddlmZmZmZmZmZmZ ddlmZmZ G d	d
 d
eZedddZeddeeeefddZedddZedddZedddZeddefddZ dS )    )divisionprint_functionunicode_literals)contextmanager)castc_void_pPOINTERcreate_string_buffer)fstatstat   )ffi)ARCHIVE_EOFOPEN_CALLBACKREAD_CALLBACKCLOSE_CALLBACKVOID_CB	page_size)ArchiveEntrynew_archive_entryc               @   s   e Zd Zdd Zdd ZdS )ArchiveReadc             C   s
   || _ d S )N)_pointer)self	archive_p r   .lib/python3.7/site-packages/libarchive/read.py__init__   s    zArchiveRead.__init__c          	   c   sN   | j }tj}t 2}t||}x |||}|tkr6dS |V  q W W dQ R X dS )z/Iterates through an archive's entries.
        N)r   r   read_next_header2r   r   r   )r   r   r   Zentry_pentryrr   r   r   __iter__   s    

zArchiveRead.__iter__N)__name__
__module____qualname__r   r    r   r   r   r   r      s   r   allc          	   c   sH   t  }tt d| | tt d|  | z
|V  W dt | X dS )zCreates an archive struct suitable for reading from an archive.

    Returns a pointer if successful. Raises ArchiveError on error.
    Zread_support_filter_Zread_support_format_N)r   Zread_newgetattrZ	read_free)format_namefilter_namer   r   r   r   new_archive_read    s    
r(   c          	   c   sN   t |}t| }t|}	t||"}
t|
d|||	 ||
V  W dQ R X dS )z-Read an archive using a custom function.
    N)r   r   r   r(   r   	read_open)	read_funcr&   r'   Z	open_funcZ
close_func
block_sizeZarchive_read_classopen_cbread_cbclose_cbr   r   r   r   custom_reader/   s    r/      c          
   c   sZ   t ||F}yt| j}W n ttfk
r2   Y nX t|| | t|V  W dQ R X dS )z,Read an archive from a file descriptor.
    N)r(   r
   
st_blksizeOSErrorAttributeErrorr   Zread_open_fdr   )fdr&   r'   r+   r   r   r   r   	fd_reader?   s    r5   c          
   c   sZ   t ||F}yt| j}W n ttfk
r2   Y nX t|| | t|V  W dQ R X dS )z!Read an archive from a file.
    N)r(   r   r1   r2   r3   r   Zread_open_filename_wr   )pathr&   r'   r+   r   r   r   r   file_readerL   s    r7   c          	   c   s<   t ||(}t|t| tt|  t|V  W dQ R X dS )z!Read an archive from memory.
    N)r(   r   Zread_open_memoryr   r   lenr   )bufr&   r'   r   r   r   r   memory_readerY   s    r:   c       	   	   #   sp   t | t t fdd}tt}t|}tt}t||"}t	|d||| t
|V  W dQ R X dS )ziRead an archive from a stream.

    The `stream` object must support the standard `readinto` method.
    c                s$     }t|tt}|d< |S )Nr   )readintor   r   r   )r   contextZptrptrlength)r9   buf_pstreamr   r   r*   l   s    
z stream_reader.<locals>.read_funcN)r	   r   r   r   r   r   r   r(   r   r)   r   )	r?   r&   r'   r+   r*   r,   r-   r.   r   r   )r9   r>   r?   r   stream_readerb   s    
	r@   N)r$   r$   )r$   r$   r0   )r$   r$   r0   )r$   r$   )!Z
__future__r   r   r   
contextlibr   Zctypesr   r   r   r	   osr
   r    r   r   r   r   r   r   r   r   r   r   objectr   r(   r/   r5   r7   r:   r@   r   r   r   r   <module>   s,    