
    wg                     :   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j6                  fd       Z G d de      Zy)    )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                  }	nZt        j                  j                  t        j                        }t        j                  j                  t        j                        }	t        j                  j                  ||      5 }
t        |
|       t        j                   |
d|||	       t        j                  j#                  |
       ddd       y# 1 sw Y   yxY ww)zRead an archive from a seekable file-like object.

    The `file` object must support the standard `readinto` and 'seek' methods.
    c                 f    j                        }t        |t        t                    }|d<   |S )Nr   )readintor	   r   r   )	archive_pcontextptrptrlengthbufbuf_pfiles       f/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/fsspec/implementations/libarchive.py	read_funcz custom_reader.<locals>.read_func*   s0    s#fgh/0q	    c                 H    j                  ||       j                         S )N)seektell)r   r   offsetwhencer   s       r   	seek_funcz custom_reader.<locals>.seek_func3   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_readerr9   !   s      z
*ChE
 	*GI&G..??////<??11#++>		)	)+{	C 5yy'2iwBoo)))445 5 5s   C)E
.AD>5	E
>EE
c                   t     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        |   | fi | |dk7  rt        d      t        |t              r7t        |fd|i|xs i }t        |      dk7  rt        d| d| d      |d   }|| _        |j                         | _	        || _
        d	| _        y	)
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   zPath "z(" did not resolve to exactly one file: ""r   N)super__init__
ValueError
isinstancestrr   lenof	__enter__for4   	dir_cache)	selfrJ   modetarget_protocoltarget_optionsr4   kwargsfiles	__class__s	           r   rC   zLibArchiveFileSystem.__init__^   s    0 	((3;DEEb#rVOV@TRTVE5zQ RD HqQ  qB,,.$r   c              #      K   | j                   j                  d       t        | j                   | j                        5 }| d d d        y # 1 sw Y   y xY ww)Nr   )r4   )rJ   r    r9   r4   )rL   arcs     r   _open_archivez"LibArchiveFileSystem._open_archive   sB     Q477t? 	3I	 	 	s   =AA	AAAc                 @    t         |   |      j                  d      S )N/)rB   _strip_protocollstrip)clspathrR   s     r   rX   z$LibArchiveFileSystem._strip_protocol   s      w&t,33C88r   c                    dddddddd}| j                   y i | _         g }| j                         5 }|D ]  }|j                  s|j                  s| j                   j	                  | j                  t        |j                              D ci c]	  }||d	d
d c}       |D ci c]  }|t        |||          }}|j                  rd
nd|d<   |j                  |j                         || j                   |d   <    	 d d d        | j                   j	                  | j                  |      D ci c]	  }||d	d
d c}       y c c}w c c}w # 1 sw Y   QxY wc c}w )NpathnamesizectimerM   uidgidmtime)namer^   createdrM   r`   ra   rb   r   	directory)rc   r^   typer   rf   rc   )
rK   rU   isdirisfileupdate_all_dirnamessetrc   getattrappend)rL   fields
list_namesrT   entrydirnamekeyfs           r   	_get_dirszLibArchiveFileSystem._get_dirs   sr   
 >>%
! 	.S .{{5<<%% (,'9'9#ejj/'J#  '1k!RR BHH#S'%55HH+0;;KF&	!!%**-,-qy).	.$ 	  $11*= '1kJJ	

 I	. 	.&s1   AEEE$E;AE0E
EEc                 T   | j                  |      }|dk7  rt        t               }| j                         5 }|D ]K  }	|	j                  |k7  r|	j
                  dk(  r n*|	j                  |	j
                        D ]  }
|
} F t         d d d        t        | ||      S # 1 sw Y   xY w)Nrbr   )fsr[   data)	rX   NotImplementedErrorbytesrU   r]   r^   
get_blocksrD   r   )rL   r[   rM   r4   
autocommitcache_optionsrP   rx   rT   rp   blocks              r   _openzLibArchiveFileSystem._open   s     ##D)4<%%w! 	%S %>>T)::?"--ejj9 %E D% %$%	% T488	% 	%s   ABB')rv   NTN)__name__
__module____qualname____doc__root_markerr?   cachabler   rC   r   rU   classmethodrX   rt   r   __classcell__)rR   s   @r   r;   r;   H   sq    " KHH %%N  
 9 9'
X 9r   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_sizer9   r;    r   r   <module>r      s    %      4 4 + *eXz5I s>s}}  #|
$ $)u #5 #5LM94 M9r   