
    OOIdP                     F   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
mZmZmZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ  ee
e	ee
e	          Z ej        d	ej        ege	ej                  Z eed
          Zeddej        fd            Z  G d de          Z!dS )    )absolute_importdivisionprint_function)contextmanager)	CFUNCTYPEPOINTERc_int
c_longlongc_void_pcastcreate_string_bufferN)
open_files)AbstractArchiveFileSystem)
MemoryFile)DEFAULT_BLOCK_SIZEread_set_seek_callback
NO_OPEN_CBallc              #      K   t          |          t          t                     fd} fd}t          j        |          }t          |          }t          rt          j        }t          j        }	nRt          j
                            t          j                  }t          j
                            t          j                  }	t          j
                            ||          5 }
t          |
|           t          j        |
d|||	           t          j
                            |
          V  ddd           dS # 1 swxY w Y   dS )zRead an archive from a seekable file-like object.

    The `file` object must support the standard `readinto` and 'seek' methods.
    c                                                    }t          |t          t                              }|d<   |S )Nr   )readintor   r   r   )	archive_pcontextptrptrlengthbufbuf_pfiles       Alib/python3.11/site-packages/fsspec/implementations/libarchive.py	read_funcz custom_reader.<locals>.read_func,   s9    s##fgh//00q	    c                 X                         ||                                           S )N)seektell)r   r   offsetwhencer   s       r   	seek_funcz custom_reader.<locals>.seek_func5   s%    		&&!!!yy{{r!   N)r   r   r   ffiREAD_CALLBACKSEEK_CALLBACKnew_apir   NO_CLOSE_CB
libarchivereadOPEN_CALLBACKVOID_CBCLOSE_CALLBACKnew_archive_readr   	read_openArchiveRead)r   format_namefilter_name
block_sizer    r'   read_cbseek_cbopen_cbclose_cbr   r   r   s   `          @@r   custom_readerr<   #   s      z
*
*ChE          
 	**GI&&G ?.?///<<?11#+>>		)	)+{	C	C 5yy'222iwBBBo)))444445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5s   +A
EE	Ec                        e Zd ZdZdZdZdZddddef fd	Ze	d             Z
e fd	            Zd
 Z	 	 	 	 ddZ xZS )LibArchiveFileSystemay  Compressed archives as a file-system (read-only)

    Supports the following formats:
    tar, pax , cpio, ISO9660, zip, mtree, shar, ar, raw, xar, lha/lzh, rar
    Microsoft CAB, 7-Zip, WARC

    See the libarchive documentation for further restrictions.
    https://www.libarchive.org/

    Keeps file object open while instance lives. It only works in seekable
    file-like objects. In case the filesystem does not support this kind of
    file object, it is recommended to cache locally.

    This class is pickleable, but not necessarily thread-safe (depends on the
    platform). See libarchive documentation for details.
     r-   FrNc                     t                      j        | fi | |dk    rt          d          t          |t                    rOt          |fd|i|pi }t          |          dk    r#t          d                    ||                    |d         }|| _        |	                                | _
        || _        d| _        dS )aB  
        Parameters
        ----------
        fo: str or file-like
            Contains ZIP, and must exist. If a str, will fetch file using
            :meth:`~fsspec.open_files`, which must return one file exactly.
        mode: str
            Currently, only 'r' accepted
        target_protocol: str (optional)
            If ``fo`` is a string, this value can be used to override the
            FS protocol inferred from a URL
        target_options: dict (optional)
            Kwargs passed when instantiating the target FS, if ``fo`` is
            a string.
        r@   z%Only read from archive files acceptedprotocol   z2Path "{}" did not resolve to exactlyone file: "{}"r   N)super__init__
ValueError
isinstancestrr   lenformatof	__enter__for7   	dir_cache)	selfrM   modetarget_protocoltarget_optionsr7   kwargsfiles	__class__s	           r   rE   zLibArchiveFileSystem.__init__`   s    0 	(((((3;;DEEEb# 	rVVOV@TRTVVE5zzQ %%+VB%6%6   qB,,..$r!   c              #      K   | j                             d           t          | j         | j                  5 }|V  d d d            d S # 1 swxY w Y   d S )Nr   )r7   )rM   r#   r<   r7   )rO   arcs     r   _open_archivez"LibArchiveFileSystem._open_archive   s      Q47t??? 	3III	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A

AAc                 l    t                                          |                              d          S )N/)rD   _strip_protocollstrip)clspathrU   s     r   r[   z$LibArchiveFileSystem._strip_protocol   s+     ww&&t,,33C888r!   c           
      R   dddddddd| j         d S i | _         g }|                                 5 }|D ]j        sj        s| j                             d	 |                     t          j                            D                        fd
D             }j        rdnd|d<   |                    j                   || j         |d         <   	 d d d            n# 1 swxY w Y   | j                             d |                     |          D                        d S )NpathnamesizectimerP   uidgidmtime)namera   createdrP   rc   rd   re   c                 (    i | ]}|d z   |d z   dddS rZ   r   	directory)rf   ra   type .0dirnames     r   
<dictcomp>z2LibArchiveFileSystem._get_dirs.<locals>.<dictcomp>   sC        $  (/#q+VV  r!   c                 >    i | ]}|t          |                   S rl   )getattr)rn   keyentryfieldss     r   rp   z2LibArchiveFileSystem._get_dirs.<locals>.<dictcomp>   s)    HHH#S'%55HHHr!   rj   r   rk   rf   c                 (    i | ]}|d z   |d z   dddS ri   rl   rm   s     r   rp   z2LibArchiveFileSystem._get_dirs.<locals>.<dictcomp>   s<        ##q+VV  r!   )	rN   rX   isdirisfileupdate_all_dirnamessetrf   append)rO   
list_namesrW   frt   ru   s       @@r   	_get_dirszLibArchiveFileSystem._get_dirs   s   
 
 >%F
!! 	.S . .{ 5< %%  (,'9'9#ej//'J'J     IHHHHHHH+0;BKKF&	!!%*---,-qy)).	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.& 	 #11*==  	
 	
 	
 	
 	
s   B%C%%C),C)rbTc                 h   |                      |          }|dk    rt          t                      }|                                 5 }|D ]B}	|	j        |k    r|	j        dk    r n(|	                    |	j                  D ]}
|
} nt          Cd d d            n# 1 swxY w Y   t          | ||          S )Nr   r   )fsr^   data)	r[   NotImplementedErrorbytesrX   r`   ra   
get_blocksrF   r   )rO   r^   rP   r7   
autocommitcache_optionsrS   r   rW   rt   blocks              r   _openzLibArchiveFileSystem._open   s    ##D))4<<%%ww!! 	%S % %>T)):??E"--ej99 % %E DE$$ 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% T48888s   ABBB)r   NTN)__name__
__module____qualname____doc__root_markerrB   cachabler   rE   r   rX   classmethodr[   r   r   __classcell__)rU   s   @r   r>   r>   J   s         " KHH %& & & & & &P   ^
 9 9 9 9 [9(
 (
 (
Z 9 9 9 9 9 9 9 9r!   r>   )"
__future__r   r   r   
contextlibr   ctypesr   r   r	   r
   r   r   r   r-   libarchive.ffir(   fsspecr   fsspec.archiver   fsspec.implementations.memoryr   fsspec.utilsr   r*   c_archive_p	check_intr   hasattrr+   	page_sizer<   r>   rl   r!   r   <module>r      s   @ @ @ @ @ @ @ @ @ @ % % % % % %                                  4 4 4 4 4 4 4 4 4 4 4 4 + + + + + + 	*eXz5II s>s}   '#|
$
$ $)u #5 #5 #5 #5LO9 O9 O9 O9 O94 O9 O9 O9 O9 O9r!   