
    |;i#              
       (   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mZmZ ddlmZmZ  G d d	      Zedd       Zed
d
dddeeddf	d       Ze	 	 dd       Ze	 	 dd       Ze	 	 dd       Zed
d
eddfd       ZeZy)    )contextmanager)castc_void_pPOINTERcreate_string_buffer)fstatstat   )ffi)ARCHIVE_EOFOPEN_CALLBACKREAD_CALLBACKCLOSE_CALLBACKSEEK_CALLBACK
NO_OPEN_CBNO_CLOSE_CB	page_size)ArchiveEntryPassedArchiveEntryc                   J    e Zd ZddZd Zed        Zed        Zed        Zy)ArchiveReadc                      || _         || _        y N)_pointerheader_codec)self	archive_pr   s      B/home/ubuntu/myenv/lib/python3.12/site-packages/libarchive/read.py__init__zArchiveRead.__init__   s    !(    c              #      K   | j                   }| j                  }t        j                  }	 t	        ||      } |||j
                        }|t        k(  ry| t        |_        9w)z/Iterates through an archive's entries.
        N)	r   r   r   read_next_header2r   _entry_pr   r   	__class__)r   r   r   r"   entryrs         r   __iter__zArchiveRead.__iter__   sc      MM	((11 L9E!)U^^<AKK0EO s   A#A%c                 B    t        j                  | j                  d      S )N)r   filter_bytesr   r   s    r   
bytes_readzArchiveRead.bytes_read!   s    r22r    c                     t        j                  | j                        }t        |dz
        D cg c]"  }t        j                  | j                  |      $ c}S c c}w )Nr
   )r   filter_countr   rangefilter_name)r   countis      r   filter_nameszArchiveRead.filter_names%   sB      /;@;KLaq1LLLs   'Ac                 @    t        j                  | j                        S r   )r   format_namer   r+   s    r   r5   zArchiveRead.format_name*   s    t}}--r    N)utf-8)	__name__
__module____qualname__r   r'   propertyr,   r3   r5    r    r   r   r      sJ    )1 3 3 M M . .r    r   allNc              #     K   t        j                         }	 |r8t        |t              s|j	                  d      }	 t        j
                  ||        t        j                  |      |        t        j                  |       |       | t        j                  |       y# t        $ r3 t        dt        j                          dt         j                   d      w xY w# t        j                  |       w xY ww)zCreates an archive struct suitable for reading from an archive.

    Returns a pointer if successful. Raises ArchiveError on error.
    r6   z#the libarchive being used (version z, path z) doesn't support encryptionN)r   read_new
isinstancebytesencoderead_add_passphraseAttributeErrorNotImplementedErrorversion_numberlibarchive_pathget_read_filter_functionget_read_format_function	read_free)r5   r0   
passphraser   s       r   new_archive_readrK   /   s      I!j%0'..w7
''	:> 	2$$[1)<1$$[1)<i  " )9#:L:L:N9O P//00LN  	i s3   C;#C! B" :C! C;"<CC! !C88C;r6   c
              #   @  K   |rt        |      nt        }
t        |       }|rt        |      nt        }t        |      }t        |||      5 }|rt        j                  ||       t        j                  |d|
||        |||	       ddd       y# 1 sw Y   yxY ww)z-Read an archive using a custom function.
    N)
r   r   r   r   r   r   rK   r   read_set_seek_callback	read_open)	read_funcr5   r0   	open_func	seek_func
close_func
block_sizearchive_read_classrJ   r   open_cbread_cbclose_cbseek_cbr   s                  r   custom_readerrY   H   s      +4mI&GI&G-7~j)[HI&G	+{J	? :9&&y':iwB L99	: : :s   A
B=B		BBBc              #      K   t        |||      5 }	 t        |       j                  }t        j                  || |       t        ||       ddd       y# t        t        f$ r Y ?w xY w# 1 sw Y   yxY ww)z,Read an archive from a file descriptor.
    N)rK   r   
st_blksizeOSErrorrC   r   read_open_fdr   )fdr5   r0   rS   rJ   r   r   s          r   	fd_readerr_   \   s}      
+{J	? 39	r--J 	B
3)\223 3 ( 		3 3>   A6A*A%A*	A6A'$A*&A''A**A3/A6c              #      K   t        |||      5 }	 t        |       j                  }t        j                  || |       t        ||       ddd       y# t        t        f$ r Y ?w xY w# 1 sw Y   yxY ww)z!Read an archive from a file.
    N)rK   r	   r[   r\   rC   r   read_open_filename_wr   )pathr5   r0   rS   rJ   r   r   s          r   file_readerrd   l   s}      
+{J	? 39	d..J 	  D*=)\223 3 ( 		3 3r`   c              #      K   t        |||      5 }t        j                  |t        | t              t        |              t        ||       ddd       y# 1 sw Y   yxY ww)z!Read an archive from memory.
    N)rK   r   read_open_memoryr   r   lenr   )bufr5   r0   rJ   r   r   s         r   memory_readerri   |   sT      
+{J	? 39YS((;SXF)\223 3 3s   A"=A	A"AA"c              #      K   t        |      t        t               fd} fd}t        }t	        |      }	t
        }
t        |      }t        |||      5 } j                         rt        j                  ||       t        j                  |d||	|
       t        ||       ddd       y# 1 sw Y   yxY ww)zRead an archive from a stream.

    The `stream` object must support the standard `readinto` method.

    If `stream.seekable()` returns `True`, then an appropriate seek callback is
    passed to libarchive.
    c                 f    j                        }t        |t        t                    }|d<   |S )Nr   )readintor   r   r   )r   contextptrptrlengthrh   buf_pstreams       r   rO   z stream_reader.<locals>.read_func   s0    %fgh/0q	r    c                 H    j                  ||       j                         S r   )seektell)r   rm   offsetwhencerq   s       r   rQ   z stream_reader.<locals>.seek_func   s    FF#{{}r    N)r   r   r   r   r   r   r   rK   seekabler   rM   rN   r   )rq   r5   r0   rS   rJ   r   rO   rQ   rU   rV   rW   rX   r   rh   rp   s   `            @@r   stream_readerrx      s      z
*ChE
 GI&GHI&G	+{J	? 39??&&y':iwB)\22	3 3 3s   AB?AB3*	B?3B<8B?)r<   r<   N)r<   r<   i   Nr6   )r<   r<   Nr6   )
contextlibr   ctypesr   r   r   r   osr   r	    r   r   r   r   r   r   r   r   r   r%   r   r   r   rK   rY   r_   rd   ri   rx   seekable_stream_readerr;   r    r   <module>r~      s    % @ @     4. .D ! !0  edt[T	: :& JN3 3 LP3 3 :>3 3 5Y'$3 $3N ' r    