
    =2g=                        d Z ddlmZ ddl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mZ d	d
gZ eed      Z	 	 	 	 ddZddZy)zh
Extract package to directory, with checks against tar members extracting outside
the target directory.
    )annotationsN)ELOOP)Path)	Generator   )
exceptionspackage_streamingextract_streamextract
tar_filterc                V   t         j                  j                        fd| D ]C  \  }fd}	  |       d}t        rd|d<    j                  di | | j                          E y# t
        $ r.}|j                  t        k(  rt        j                         | d}~ww xY w)z
    Pipe ``stream_conda_component`` output here to extract every member into
    dest_dir.

    For ``.conda`` will need to be called twice (for info and pkg components);
    for ``.tar.bz2`` every member is extracted.
    c                    t         j                  j                  t         j                  j                  |             }t         j                  j	                  |f      }|k(  S )N)ospathrealpathjoin
commonpath)name
abs_targetprefixdest_dirs      ?lib/python3.12/site-packages/conda_package_streaming/extract.pyis_within_dest_dirz*extract_stream.<locals>.is_within_dest_dir!   sJ    WW%%bggll8T&BC
##Xz$:;!!    c               3     K   D ]:  }  | j                         s"t        j                  d| j                          |  < y w)Nzcontains unsafe path: )r   r   SafetyError)memberr   tar_files    r   checked_membersz'extract_stream.<locals>.checked_members(   s?     ")&++6$003I&++1WXX #s   AA)r   membersfully_trustedfilterN )r   r   r   HAS_TAR_FILTER
extractallOSErrorerrnor   r   CaseInsensitiveFileSystemErrorclose)streamr   _r   tar_argser   r   s    `    @@r   r
   r
      s     ww)H"
 !		 !)_5FGH%4"H+(+ 	/ "  	ww% ??AqH	s   'A11	B(:)B##B(c                   |sJ d       t        |       j                  d      r5t        j                  j                  t        j                  j
                  g}nt        j                  j                  g}d}|st        | d      }d}	 |D ]&  }t        j                  | ||      }t        ||       ( 	 |r|j                          yy# |r|j                          w w xY w)z
    Extract all components of conda package to dest_dir.

    fileobj: must be seekable if provided, if a ``.conda`` package.
    zdest_dir is requiredz.condaFrbT)	componentN)
strendswithr	   CondaComponentpkginfoopenstream_conda_componentr
   r)   )filenamer   fileobj
componentsclosefdr0   r*   s          r   r   r   @   s     +++8
8}h',,00,,11


 (66::;
Gx&#I&=='YF 68,	 $ MMO 7MMO s   +C C)r*   z2Generator[tuple[tarfile.TarFile, tarfile.TarInfo]]r   z
Path | str)NN)__doc__
__future__r   r   tarfiler'   r   pathlibr   typingr    r   r	   __all__hasattrr$   r
   r   r#   r   r   <module>rD      sR   
 # 	     +Y
',/)>))Xr   