
    Rh.                       d Z dZdZdZddlmZ ddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlZ	 ddlZ	 ddlZeeefZg dZdZd	Zed
z  ZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2e2Z3e!e"e#e$e'e(e)e%e&e*e+e,fZ4e!e"e)e,fZ5e*e+e,fZ6dZ7h d Z8e9e9e9e:e:e:d!Z;ejx                  d"k(  rd#Z=n ej|                         Z=d$ Z?d% Z@d& ZAd'e3fd(ZBd) ZCdedfd*ZDd+ ZE G d, d-eF      ZG G d. d/eG      ZH G d0 d1eG      ZI G d2 d3eG      ZJ G d4 d5eG      ZK G d6 d7eG      ZL G d8 d9eL      ZM G d: d;eL      ZN G d< d=eL      ZO G d> d?eL      ZP G d@ dAeL      ZQ G dB dC      ZR G dD dE      ZS G dF dGeT      ZU G dH dIeT      ZV G dJ dKe	j                        ZX G dL dMeG      ZY G dN dOeY      ZZ G dP dQeY      Z[ G dR dSeY      Z\ G dT dUeY      Z] G dV dWeY      Z^ G dX dYeY      Z_eYeeHfZ`dgdZZad[ Zbd\ Zcd] Zdebecedd^Ze eT       Zf ej                  d_      Zh G d` daeT      Zi G db dceT      Zjdd Zkejj
                  Zde Zlemdfk(  r el        yy# e$ r dZY $w xY w# e$ r dZY -w xY w)hz,Read from and write to tar format archives.
z0.9.0u"   Lars Gustäbel (lars@gustaebel.de)u4   Gustavo Niemeyer, Niels Gustäbel, Richard Townsend.    )openN)TarFileTarInfo
is_tarfileTarError	ReadErrorCompressionErrorStreamErrorExtractErrorHeaderErrorENCODINGUSTAR_FORMAT
GNU_FORMAT
PAX_FORMATDEFAULT_FORMATr   fully_trusted_filterdata_filter
tar_filterFilterErrorAbsoluteLinkErrorOutsideDestinationErrorSpecialFileErrorAbsolutePathErrorLinkOutsideDestinationError    i      s   ustar   s   ustar 00d         0   1   2   3   4   5   6   7   L   K   S   x   g   X      )pathlinkpathsizemtimeuidgidunamegname>   r/   r6   r5   r0   )atimectimer2   r3   r4   r1   ntutf-8c                 v    | t        d      | j                  ||      } | d| |t        |       z
  t        z  z   S )z8Convert a string to a null-terminated bytes object.
    Nzmetadata cannot contain None)
ValueErrorencodelenNUL)slengthencodingerrorss       /usr/lib/python3.12/tarfile.pystnrE      sC     	y788	6"AWf:#a&C///    c                 \    | j                  d      }|dk7  r| d| } | j                  ||      S )z8Convert a null-terminated bytes object to a string.
    r   N)finddecode)r@   rB   rC   ps       rD   ntsrL      s4     	
uABwbqE88Hf%%rF   c                 6   | d   dv rMd}t        t        |       dz
        D ]  }|dz  }|| |dz      z  } | d   dk(  rdt        |       dz
  z  |z
   }|S 	 t        | dd      } t        | j	                         xs d	d      }|S # t
        $ r t        d
      w xY w)z/Convert a number field to a python number.
    r   )      r-      rO      asciistrict0invalid header)ranger>   rL   intstripr<   InvalidHeaderError)r@   nis      rD   ntir\      s    
 	t~s1vz" 	A!GA1q5MA	 Q45=#a&1*%)*A H	7Aw)AAGGI$a(A H  	7$%566	7s   +B BrP   c                    | }t        |       } d| cxk  rd|dz
  z  k  r n nt        d|dz
  | fz  d      t        z   }|S |t        k(  rzd|dz
  z   | cxk  rd|dz
  z  k  rbn t        d	      | dk\  rt	        dg      }nt	        dg      }d|z  | z   } t        |dz
        D ]  }|j                  d| dz         | dz  }  |S t        d	      )
z/Convert a python number to a number field.
    r   rP   r-   z%0*orR   rQ   rN   rO   zoverflow in number field)rW   bytesr?   r   	bytearrayrV   insertr<   )rZ   digitsformat
original_nr@   r[   s         rD   itnrd      s     JAAA!fqj!!&FQJ?*G4s: H 
:	36A:#6"6!"Qcfqj>Q"Q 344 65'"A5'"Av!Avz" 	AHHQE	"!GA	 H 344rF   c                     dt        t        j                  d|             z   }dt        t        j                  d|             z   }||fS )a  Calculate the checksum for a member's header by summing up all
       characters except for the chksum field which is treated as if
       it was filled with spaces. According to the GNU tar sources,
       some tars (Sun and NeXT) calculate chksum with signed char,
       which will be different if there are chars in the buffer with
       the high bit set. So we calculate two checksums, unsigned and
       signed.
    rQ   
148B8x356B
148b8x356b)sumstructunpack_from)bufunsigned_chksumsigned_chksums      rD   calc_chksumsrn      sG     C 2 2< EFFO#f00sCDDMM))rF   c                 x   |xs d}|dk(  ry|t        j                  | ||       yt        ||      \  }}t        |      D ]:  }| j	                  |      }t        |      |k  r |d      |j                  |       < |dk7  r8| j	                  |      }t        |      |k  r |d      |j                  |       y)zjCopy length bytes from fileobj src to fileobj dst.
       If length is None, copy the entire content.
    i @  r   Nunexpected end of data)shutilcopyfileobjdivmodrV   readr>   write)	srcdstrA   	exceptionbufsizeblocks	remainderbrk   s	            rD   rr   rr      s     "G{~3W-vw/FI6] hhws8g455		#	 A~hhy!s8i455		#
rF   c                     t        t        j                  dd       }|!| j                  |d      j	                  |      } t        | d       y )NrB   backslashreplace )end)getattrsysstdoutr=   rJ   print)r@   rB   s     rD   _safe_printr     s@    szz:t4HHHX1299(C	!rF   c                       e Zd ZdZy)r   zBase exception.N__name__
__module____qualname____doc__ rF   rD   r   r     s    rF   r   c                       e Zd ZdZy)r   z%General exception for extract errors.Nr   r   rF   rD   r   r     s    /rF   r   c                       e Zd ZdZy)r   z&Exception for unreadable tar archives.Nr   r   rF   rD   r   r     s    0rF   r   c                       e Zd ZdZy)r	   z.Exception for unavailable compression methods.Nr   r   rF   rD   r	   r	     s    8rF   r	   c                       e Zd ZdZy)r
   z=Exception for unsupported operations on stream-like TarFiles.Nr   r   rF   rD   r
   r
     s    GrF   r
   c                       e Zd ZdZy)r   z!Base exception for header errors.Nr   r   rF   rD   r   r     s    +rF   r   c                       e Zd ZdZy)EmptyHeaderErrorzException for empty headers.Nr   r   rF   rD   r   r      s    &rF   r   c                       e Zd ZdZy)TruncatedHeaderErrorz Exception for truncated headers.Nr   r   rF   rD   r   r   #  s    *rF   r   c                       e Zd ZdZy)EOFHeaderErrorz"Exception for end of file headers.Nr   r   rF   rD   r   r   &  s    ,rF   r   c                       e Zd ZdZy)rY   zException for invalid headers.Nr   r   rF   rD   rY   rY   )  s    (rF   rY   c                       e Zd ZdZy)SubsequentHeaderErrorz3Exception for missing and invalid extended headers.Nr   r   rF   rD   r   r   ,  s    =rF   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)_LowLevelFilezLow-level file object. Supports reading and writing.
       It is used instead of a regular file object for streaming
       access.
    c                    t         j                  t         j                  t         j                  z  t         j                  z  d|   }t        t         d      r|t         j                  z  }t        j                  ||d      | _        y )NrwO_BINARYi  )	osO_RDONLYO_WRONLYO_CREATO_TRUNChasattrr   r   fd)selfnamemodes      rD   __init__z_LowLevelFile.__init__9  sa    rzz)BJJ6
  2z"BKKD''$e,rF   c                 B    t        j                  | j                         y N)r   closer   r   s    rD   r   z_LowLevelFile.closeB  s    
rF   c                 B    t        j                  | j                  |      S r   )r   rt   r   r   r1   s     rD   rt   z_LowLevelFile.readE  s    wwtww%%rF   c                 D    t        j                  | j                  |       y r   )r   ru   r   r   r@   s     rD   ru   z_LowLevelFile.writeH  s    
!rF   N)r   r   r   r   r   r   rt   ru   r   rF   rD   r   r   3  s    
-&rF   r   c                   Z    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zdd
Zd Zd Zd Zy)_Streama  Class that serves as an adapter between TarFile and
       a stream-like object.  The stream-like object only
       needs to have a read() or write() method that works with bytes,
       and the method is accessed blockwise.
       Use of gzip or bzip2 compression is possible.
       A stream-like object could be for example: sys.stdin.buffer,
       sys.stdout.buffer, a socket, a tape device etc.

       _Stream is intended to be used only internally.
    c                    d| _         |t        ||      }d| _         |dk(  rt        |      }|j                         }|xs d| _        || _        || _        || _        || _        d| _	        d| _
        d| _        	 |dk(  r[	 ddl}|| _        |j                  d      | _        |d
k(  r"|j"                  | _        | j'                          y| j)                  |       y|dk(  rI	 ddl}|d
k(  r(d| _        |j/                         | _        t2        | _        y|j5                  |      | _        y|dk(  rN	 ddl}	|d
k(  r.d| _        |	j9                         | _        |	j:                  | _        y|	j=                         | _        y|dk7  rt        d|z        y# t        $ r t        d	      dw xY w# t        $ r t        d      dw xY w# t        $ r t        d      dw xY w#  | j                   s| j                  j?                          d| _         xY w)z$Construct a _Stream object.
        TNF* rF   r   gzzzlib module is not availabler   bz2bz2 module is not availablexzlzma module is not availabletarunknown compression type %r) _extfileobjr   _StreamProxygetcomptyper   r   comptypefileobjry   rk   posclosedzlibImportErrorr	   crc32crcerrorrx   _init_read_gz_init_write_gzr   dbufBZ2DecompressorcmpOSErrorBZ2CompressorlzmaLZMADecompressor	LZMAErrorLZMACompressorr   )
r   r   r   r   r   ry   compresslevelr   r   r   s
             rD   r   z_Stream.__init__W  s     ?#D$/G$Ds? #7+G**,H
		 -	4U !	::c?3;%)ZZDN&&(''6U"T 3; #DI"224DH%,DN"00?DHT!U 3; #DI#446DH%)^^DN#224DHU"&'Dx'OPP #E # U*+IJPTTU # T*+HItST # U*+IJPTTU	##""$DKsy   :G  F AG G G  F( $,G G (G .G 22G %G ;G F%%G (F>>G GG 0H
c                 X    t        | d      r| j                  s| j                          y y y )Nr   )r   r   r   r   s    rD   __del__z_Stream.__del__  s#    4"4;;JJL ,7"rF   c                 t   | j                   j                  || j                   j                  | j                   j                   | j                   j                  d      | _        t        j                  dt        t        j                                     }| j                  d|z   dz          | j                  j                  d      r| j                  dd | _        t        j                  j                  | j                        | _        | j                  | j                  j!                  dd	      t"        z          y)
z6Initialize for writing with gzip compression.
        r   <Ls   s   .gzNz
iso-8859-1replace)r   compressobjDEFLATED	MAX_WBITSDEF_MEM_LEVELr   ri   packrW   time_Stream__writer   endswithr   r/   basenamer=   r?   )r   r   	timestamps      rD   r   z_Stream._init_write_gz  s     99(()-););*.))*=*=)=)-)@)@)*	,
 KKc$))+&67	(94{BC99e$		#2DIGG$$TYY/	TYY%%lI>DErF   c                 *   | j                   dk(  r+| j                  j                  || j                        | _        | xj                  t        |      z  c_        | j                   dk7  r| j                  j                  |      }| j                  |       y)z&Write string s to the stream.
        r   r   N)	r   r   r   r   r   r>   r   compressr   r   s     rD   ru   z_Stream.write  sg     ==D yyq$((3DHCF==E!!!!$AQrF   c                 Z   | xj                   |z  c_         t        | j                         | j                  kD  rt| j                  j	                  | j                   d| j                          | j                   | j                  d | _         t        | j                         | j                  kD  rsyy)z]Write string s to the stream if a whole new block
           is ready to be written.
        N)rk   r>   ry   r   ru   r   s     rD   __writez_Stream.__write  sr     	A$((mdll*LLtxx67xx.DH $((mdll*rF   c                     | j                   ryd| _         	 | j                  dk(  r<| j                  dk7  r-| xj                  | j                  j                         z  c_        | j                  dk(  r| j                  r| j                  j                  | j                         d| _        | j                  dk(  ru| j                  j                  t        j                  d| j                               | j                  j                  t        j                  d| j                  dz               | j                  s| j                  j                          yy# | j                  s| j                  j                          w w xY w)	z[Close the _Stream object. No operation should be
           done on it afterwards.
        NTr   r   rF   r   r   l    )r   r   r   rk   r   flushr   ru   ri   r   r   r   r   r   r   s    rD   r   z_Stream.close  s	    ;;	%yyCDMMU$:DHHNN,,yyCDHH""488,==D(LL&&v{{4'BCLL&&v{{4J9N'OP##""$ $4##""$ $s   DE )E=c                    | j                   j                  | j                   j                         | _        d| _        | j                  d      dk7  rt        d      | j                  d      dk7  rt        d      t        | j                  d            }| j                  d       |d	z  rIt        | j                  d            d
t        | j                  d            z  z   }| j                  |       |dz  r	 | j                  d      }|r	|t        k(  rn|dz  r	 | j                  d      }|r	|t        k(  rn|dz  r| j                  d       yy)z:Initialize for reading a gzip compressed fileobj.
        rF   r.   s   not a gzip filer-      zunsupported compression method      rQ   rP      N)r   decompressobjr   r   r   _Stream__readr   r	   ordrt   r?   )r   flagxlenr@   s       rD   r   z_Stream._init_read_gz  s+    99**DII,?,?+?@	 ;;q>[(-..;;q>W$"#CDD4;;q>"A!8t{{1~&s4;;q>/B)BBDIIdO!8KKNAH  "9KKNAH  !8KKN rF   c                     | j                   S )z3Return the stream's file pointer position.
        )r   r   s    rD   tellz_Stream.tell  s     xxrF   c                    || j                   z
  dk\  rnt        || j                   z
  | j                        \  }}t        |      D ]  }| j	                  | j                          | j	                  |       | j                   S t        d      )zXSet the stream's file pointer to pos. Negative seeking
           is forbidden.
        r   z seeking backwards is not allowed)r   rs   ry   rV   rt   r
   )r   r   rz   r{   r[   s        rD   seekz_Stream.seek   sy     >Q &sTXX~t|| DFI6] (		$,,'(IIi  xx @AArF   c                 l    |J | j                  |      }| xj                  t        |      z  c_        |S )z5Return the next size number of bytes from the stream.)_readr   r>   )r   r1   rk   s      rD   rt   z_Stream.read  s3    jjCH
rF   c                 (   | j                   dk(  r| j                  |      S t        | j                        }| j                  g}||k  r| j                  r| j                  }d| _        n(| j
                  j                  | j                        }|snA	 | j                  j                  |      }|j                  |       |t        |      z  }||k  rdj                  |      }||d | _        |d| S # | j                  $ r}t        d      |d}~ww xY w)z+Return size bytes from the stream.
        r   rF   zinvalid compressed dataN)r   r   r>   r   rk   r   rt   ry   r   
decompressrx   r   appendjoin)r   r1   ctrk   es         rD   r   z_Stream._read  s     ==E!;;t$$		NYYK$hxxhhll''5Bhh))#. HHSMSMA $h HHQKdeH	$x >> B 9:ABs   C1 1D DDc                 *   t        | j                        }| j                  g}||k  rM| j                  j                  | j                        }|sn%|j                  |       |t        |      z  }||k  rMdj                  |      }||d | _        |d| S )zsReturn size bytes from stream. If internal buffer is empty,
           read another block from the stream.
        rF   N)r>   rk   r   rt   ry   r  r  )r   r1   r  r  rk   s        rD   __readz_Stream.__read/  s     MXXJ$h,,##DLL1CHHSMSMA $h HHQKTU8$xrF   N)r   )r   r   r   r   r   r   r   ru   r   r   r   r   r   rt   r   r   r   rF   rD   r   r   K  sF    	ENF"/%,>
6rF   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)r   zsSmall proxy class that enables transparent compression
       detection for the Stream interface (mode 'r|*').
    c                 Z    || _         | j                   j                  t              | _        y r   )r   rt   	BLOCKSIZErk   )r   r   s     rD   r   z_StreamProxy.__init__E  s    <<$$Y/rF   c                 P    | j                   j                  | _        | j                  S r   )r   rt   rk   r   s     rD   rt   z_StreamProxy.readI  s    LL%%	xxrF   c                     | j                   j                  d      ry| j                   dd dk(  r| j                   dd dk(  ry	| j                   j                  d
      ryy)Ns   r   r      s   BZhr   
   s   1AY&SYr   )s   ]  s   7zXZr   r   )rk   
startswithr   s    rD   r   z_StreamProxy.getcomptypeM  sV    88/XXa]f$!B9)DXX  !CDrF   c                 8    | j                   j                          y r   )r   r   r   s    rD   r   z_StreamProxy.closeW  s    rF   N)r   r   r   r   r   rt   r   r   r   rF   rD   r   r   @  s    0rF   r   c                   h    e Zd ZdZddZd Zd Zd Zd Zd Z	e
j                  fd	Zdd
Zd Zd Zy)_FileInFilezA thin wrapper around an existing file object that
       provides a part of its data as an individual file
       object.
    Nc                    || _         || _        || _        d| _        || _        d| _        |d|fg}d| _        g | _        d}| j                  }|D ]U  \  }}||kD  r| j                  j                  d||d f       | j                  j                  d|||z   |f       ||z  }||z   }W || j                  k  r*| j                  j                  d|| j                  d f       y y )Nr   FT)	r   offsetr1   positionr   r   	map_indexmapr  )r   r   r  r1   r   	blockinfolastposrealposs           rD   r   z_FileInFile.__init__d  s    		TI ++% 	$LFD >?HHOOT66D='BCtOGtmG	$ TYYHHOOUGTYY=> rF   c                      y r   r   r   s    rD   r   z_FileInFile.flush}  s    rF   c                      yNTr   r   s    rD   readablez_FileInFile.readable  s    rF   c                      yNFr   r   s    rD   writablez_FileInFile.writable  s    rF   c                 6    | j                   j                         S r   )r   seekabler   s    rD   r%  z_FileInFile.seekable  s    ||$$&&rF   c                     | j                   S )z*Return the current file position.
        )r  r   s    rD   r   z_FileInFile.tell       }}rF   c                 .   |t         j                  k(  r1t        t        |d      | j                        | _        | j
                  S |t         j                  k(  rc|dk  r*t        | j
                  |z   d      | _        | j
                  S t        | j
                  |z   | j                        | _        | j
                  S |t         j                  k(  r>t        t        | j                  |z   | j                        d      | _        | j
                  S t        d      )z(Seek to a position in the file.
        r   zInvalid argument)	ioSEEK_SETminmaxr1   r  SEEK_CURSEEK_ENDr<   )r   r  whences      rD   r   z_FileInFile.seek  s     R[[ Ha 0$))<DM }} r{{"!| #DMMH$<a @ }} !$DMMH$<dii H
 }}	 r{{"DII$8$)) DaHDM }} /00rF   c                    || j                   | j                  z
  }n#t        || j                   | j                  z
        }d}|dkD  r$	 | j                  | j                     \  }}}}|| j                  cxk  r|k  rn nn?| xj                  dz  c_        | j                  t        | j                        k(  rd| _        vt        ||| j                  z
        }|re| j                  j                  || j                  |z
  z          | j                  j                  |      }t        |      |k7  rt        d      ||z  }n|t        |z  z  }||z  }| xj                  |z  c_        |dkD  r$|S )z!Read data from the file.
        rF   r   r-   rp   )r1   r  r+  r  r  r>   r   r   rt   r   r?   )	r   r1   rk   datastartstopr  rA   r|   s	            rD   rt   z_FileInFile.read  sD    <99t}},DtTYY67DQh,0HHT^^,D)eT6DMM0D0NNa'N~~TXX6)*  tdmm34F!!&DMME,A"BCLL%%f-q6V##$<==qsV|#FNDMMV#M' Qh( 
rF   c                 h    | j                  t        |            }||d t        |       t        |      S r   )rt   r>   )r   r|   rk   s      rD   readintoz_FileInFile.readinto  s,    iiA)3s83xrF   c                     d| _         y r  )r   r   s    rD   r   z_FileInFile.close  s	    rF   r   )r   r   r   r   r   r   r   r#  r%  r   r)  r*  r   rt   r5  r   r   rF   rD   r  r  ^  sB    
?2'
 %'KK  >
rF   r  c                        e Zd Z fdZ xZS )ExFileObjectc                     t        |j                  |j                  |j                  |j                  |j
                        }t        |   |       y r   )r  r   offset_datar1   r   sparsesuperr   )r   tarfiletarinfor   	__class__s       rD   r   zExFileObject.__init__  s;    goow/B/BgllGNN<!rF   r   r   r   r   __classcell__r?  s   @rD   r8  r8    s    " "rF   r8  c                       e Zd Zy)r   N)r   r   r   r   rF   rD   r   r     s    rF   r   c                        e Zd Z fdZ xZS )r   c                 N    || _         t        | 	  d|j                  d       y )Nzmember z has an absolute pathr>  r<  r   r   r   r>  r?  s     rD   r   zAbsolutePathError.__init__  s&    77<<"22GHIrF   r@  rB  s   @rD   r   r     s    J JrF   r   c                        e Zd Z fdZ xZS )r   c                 f    || _         || _        t        |   |j                  d|ddz          y )Nz would be extracted to ,  which is outside the destinationr>  _pathr<  r   r   r   r>  r/   r?  s      rD   r   z OutsideDestinationError.__init__  s;    
GLL++B4("M=> 	?rF   r@  rB  s   @rD   r   r         ? ?rF   r   c                        e Zd Z fdZ xZS )r   c                 L    || _         t        | 	  |j                  d       y )Nz is a special filerF  rG  s     rD   r   zSpecialFileError.__init__  s$    GLL++=>?rF   r@  rB  s   @rD   r   r     s    @ @rF   r   c                        e Zd Z fdZ xZS )r   c                 L    || _         t        | 	  |j                  d       y )Nz is a link to an absolute pathrF  rG  s     rD   r   zAbsoluteLinkError.__init__  s$    GLL++IJKrF   r@  rB  s   @rD   r   r     s    L LrF   r   c                        e Zd Z fdZ xZS )r   c                 f    || _         || _        t        |   |j                  d|ddz          y )Nz would link to rJ  rK  rL  rN  s      rD   r   z$LinkOutsideDestinationError.__init__  s:    
GLL+?4("E=> 	?rF   r@  rB  s   @rD   r   r     rO  rF   r   c                        e Zd Z fdZ xZS )LinkFallbackErrorc                 j    || _         || _        t        |   d|j                  dd|dz          y )Nzlink z would be extracted as a zcopy of z, which was rejectedrL  rN  s      rD   r   zLinkFallbackError.__init__  sC    
5 00IJ%dX-ABC 	DrF   r@  rB  s   @rD   rW  rW    s    D DrF   rW  c                 2   i }| j                   }t        j                  j                  |t        j                  j                        }|j                  dt        j                  f      r1| j                  j                  dt        j                  z         x}|d<   t        j                  j                  |      rt        |       t        j                  j                  t        j                  j                  ||      t        j                  j                        }t        j                  j                  ||g      |k7  rt        | |      | j                  }|y|dz  }|r^| j                         s| j                         r|dz  s|dz  }|dz  }n.| j!                         s| j#                         rd }nt%        |       || j                  k7  r||d<   |r| j&                  d |d	<   | j(                  d |d
<   | j*                  d |d<   | j,                  d |d<   | j                         s| j#                         r]t        j                  j                  | j.                        rt1        |       t        j                  j3                  | j.                        }|| j.                  k7  r||d<   | j#                         rIt        j                  j                  |t        j                  j5                  |      | j.                        }n*t        j                  j                  || j.                        }t        j                  j                  |t        j                  j                        }t        j                  j                  ||g      |k7  rt7        | |      |S )N)rS   /r   i  @   i  r   r3   r4   r5   r6   linkname)r   r   r/   realpathALLOW_MISSINGr  seplstripisabsr   r  
commonpathr   r   isregislnkisdirissymr   r3   r4   r5   r6   r]  r   normpathdirnamer   )member	dest_pathfor_data	new_attrsr   target_pathr   
normalizeds           rD   _get_filtered_attrsrp    s   I;;D  2773H3H II RVV}%#);;#5#5cBFFl#CCy 	ww}}T  ''''""277<<	4#@*,''*?*? # AK	ww;	23y@%fk::;;De|||~e|FND6<<> 'v..6;; $If::!#Ie::!#Ie<<#!%Ig<<#!%Ig<<>V\\^ww}}V__-'//))&//:JV__,(2	*%||~ ggll9+-77??4+@+1??< !ggll9+1??<''**;24''2G2G + IKww!!;	":;yH1&+FFrF   c                     | S r   r   )rj  rk  s     rD   r   r   C  s    MrF   c                 P    t        | |d      }|r | j                  di |ddiS | S )NFdeepr   rp  r   rj  rk  rm  s      rD   r   r   F  s1    #FIu=Iv~~6	666MrF   c                 P    t        | |d      }|r | j                  di |ddiS | S )NTrs  Fr   rt  ru  s      rD   r   r   L  s1    #FIt<Iv~~6	666MrF   )fully_trustedr   r1  s   ([0-9]{1,20}) c                   B   e Zd ZdZ edTi dddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d%d'd%ZdUd(Zed)        Zej                  d*        Zed+        Z
e
j                  d,        Z
d- Zeeeeeeeed.ed/
d0Zd1 Zeed2fd3Zd4 Zd5 Zd6 Zed7        Zd8 Zed9        Zed:        Zed;        Zed<        Zed=        Zed>        Zd? Zd@ Z dA Z!dB Z"dC Z#dD Z$dE Z%dF Z&dG Z'dH Z(dI Z)dJ Z*dK Z+dL Z,dM Z-dN Z.dO Z/dP Z0dQ Z1dR Z2dS Z3y%)Vr   a  Informational class which holds the details about an
       archive member given by a tar header block.
       TarInfo objects are returned by TarFile.getmember(),
       TarFile.getmembers() and TarFile.gettarinfo() and are
       usually created internally.
    r   zName of the archive member.r   zPermission bits.r3   z6User ID of the user who originally stored this member.r4   z7Group ID of the user who originally stored this member.r1   zSize in bytes.r2   zTime of last modification.chksumzHeader checksum.typezFile type. type is usually one of these constants: REGTYPE, AREGTYPE, LNKTYPE, SYMTYPE, DIRTYPE, FIFOTYPE, CONTTYPE, CHRTYPE, BLKTYPE, GNUTYPE_SPARSE.r]  zcName of the target file name, which is only present in TarInfo objects of type LNKTYPE and SYMTYPE.r5   z
User name.r6   zGroup name.devmajorzDevice major number.devminorzDevice minor number.r  zThe tar header starts here.r:  zThe file's data starts here.pax_headerszMA dictionary containing key-value pairs of an associated pax extended header.r;  zSparse member information.r=  N_sparse_structs_link_targetc                     || _         d| _        d| _        d| _        d| _        d| _        d| _        t        | _        d| _	        d| _
        d| _        d| _        d| _        d| _        d| _        d| _        i | _        y)zXConstruct a TarInfo object. name is the optional name
           of the member.
        i  r   r   N)r   r   r3   r4   r1   r2   ry  REGTYPErz  r]  r5   r6   r{  r|  r  r:  r;  r}  r   r   s     rD   r   zTarInfo.__init__  s}     			
	

rF   c                     | j                   S )z(In pax headers, "name" is called "path".r   r   s    rD   r/   zTarInfo.path  s     yyrF   c                     || _         y r   r  r  s     rD   r/   zTarInfo.path  s	    	rF   c                     | j                   S )z0In pax headers, "linkname" is called "linkpath".r]  r   s    rD   r0   zTarInfo.linkpath  r'  rF   c                     || _         y r   r  )r   r]  s     rD   r0   zTarInfo.linkpath  s	     rF   c                 `    d| j                   j                  | j                  t        |       fz  S )Nz<%s %r at %#x>)r?  r   r   idr   s    rD   __repr__zTarInfo.__repr__  s&    4>>#:#:499RX"NNNrF   T)
r   r2   r   r]  r3   r4   r5   r6   rs  _KEEPc       
            |	rt        j                  |       }nt        j                   |       }||
ur||_        ||
ur||_        ||
ur||_        ||
ur||_        ||
ur||_        ||
ur||_        ||
ur||_        ||
ur||_	        |S )zGReturn a deep copy of self with the given attributes replaced.
        )
copydeepcopyr   r2   r   r]  r3   r4   r5   r6   )r   r   r2   r   r]  r3   r4   r5   r6   rs  r  results               rD   r   zTarInfo.replace  s     ]]4(FYYt_FuFK FLuFK5 &FOeFJeFJ FL FLrF   c                    | j                   d}n| j                   dz  }| j                  || j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  d}|d   t        k(  r!|d   j                  d      s|dxx   dz  cc<   |S )z9Return the TarInfo's attributes as a dictionary.
        N  )r   r   r3   r4   r1   r2   ry  rz  r]  r5   r6   r{  r|  rz  r   rZ  )r   r   r3   r4   r1   r2   ry  rz  r]  r5   r6   r{  r|  DIRTYPEr   )r   r   infos      rD   get_infozTarInfo.get_info  s     99D99v%D				

		




  <7"4<+@+@+ELCLrF   surrogateescapec                 .   | j                         }|j                         D ]  \  }}|	t        d|z         |t        k(  r| j	                  |||      S |t
        k(  r| j                  |||      S |t        k(  r| j                  ||      S t        d      )z<Return a tar header as a string of 512 byte blocks.
        z%s may not be Nonezinvalid format)	r  itemsr<   r   create_ustar_headerr   create_gnu_headerr   create_pax_header)r   rb   rB   rC   r  r   values          rD   tobufzTarInfo.tobuf  s     }}::< 	>KD%} !5!<==	> \!++D(FCCz!))$&AAz!))$99-..rF   c                 ,   t         |d<   t        |d   j                  ||            t        kD  rt	        d      t        |d   j                  ||            t
        kD  r| j                  |d   ||      \  |d<   |d<   | j                  |t        ||      S )z3Return the object as a ustar header block.
        magicr]  zlinkname is too longr   prefix)	POSIX_MAGICr>   r=   LENGTH_LINKr<   LENGTH_NAME_posix_split_name_create_headerr   )r   r  rB   rC   s       rD   r  zTarInfo.create_ustar_header  s     $WtJ&&x89KG344tF|""8V45C+/+A+A$v,PXZ`+a(DNDL""4xHHrF   c                 Z   t         |d<   d}t        |d   j                  ||            t        kD  r|| j	                  |d   t
        ||      z  }t        |d   j                  ||            t        kD  r|| j	                  |d   t        ||      z  }|| j                  |t        ||      z   S )z:Return the object as a GNU header block sequence.
        r  rF   r]  r   )
	GNU_MAGICr>   r=   r  _create_gnu_long_headerGNUTYPE_LONGLINKr  GNUTYPE_LONGNAMEr  r   )r   r  rB   rC   rk   s        rD   r  zTarInfo.create_gnu_header  s     "WtJ&&x89KG4//Z0@BRT\^deeCtF|""8V45C4//V>NPXZ`aaCT((z8VLLLrF   c                 \   t         |d<   | j                  j                         }ddt        fddt        fddfD ];  \  }}}||v r	 ||   j                  dd	       t        ||         |kD  s4||   ||<   = d
D ]e  \  }}d}||   }	t        |	t              }
|
rt        |	      n|	}d|cxk  rd|dz
  z  k  s
n d||<   d}n	|
r|||<   d}|sS||vsXt        |	      ||<   g |r| j                  |t        |      }nd}|| j                  |t        dd      z   S # t        $ r ||   ||<   Y w xY w)zReturn the object as a ustar header block. If it cannot be
           represented this way, prepend a pax extended header sequence
           with supplement information.
        r  r   r/   r]  r0   )r5   r5       )r6   r6   r  rR   rS   ))r3   rP   )r4   rP   )r1      )r2   r  Fr   rP   r-   TrF   r   )r  r}  r  r  r  r=   UnicodeEncodeErrorr>   
isinstancefloatroundstr_create_pax_generic_headerXHDTYPEr  r   )r   r  rB   r}  r   hnamerA   ra   	needs_paxvalval_is_floatval_intrk   s                rD   r  zTarInfo.create_pax_header  s   
 $W&&++-
 -
J/T&(>$@ 	0D% #T
!!'84
 4:'%)$ZE"!	0( R 	-LD&It*C%c51L$0eCjcG3!
"33T
 	 %T
 	 T4$'HD!%	-* 11+wQCCT((|WiPPPG & %)$ZE"s   DD+*D+c                 0    | j                  |t        d      S )zAReturn the object as a pax global header block sequence.
        r:   )r  XGLTYPE)clsr}  s     rD   create_pax_global_headerz TarInfo.create_pax_global_headerP  s     --k7GLLrF   c                 R   |j                  d      }t        dt        |            D ]t  }dj                  |d|       }dj                  ||d       }t        |j	                  ||            t
        k  sNt        |j	                  ||            t        k  sq ||fS  t        d      )zUSplit a name longer than 100 chars into a prefix
           and a name part.
        rZ  r-   Nzname is too long)splitrV   r>   r  r=   LENGTH_PREFIXr  r<   )r   r   rB   rC   
componentsr[   r  s          rD   r  zTarInfo._posix_split_nameV  s     ZZ_
q#j/* 	1AXXj!n-F88JqrN+D6==623}DHf56+E t|	1 /00rF   c                 h   | j                  d      t        t        fv }|r;t        | j                  dd      d|      }t        | j                  dd      d|      }nt	        dd||      }t	        dd||      }| j                  dt
              }|t        d      t	        | j                  d	d      d
||      t        | j                  dd      dz  d|      t        | j                  dd      d|      t        | j                  dd      d|      t        | j                  dd      d|      t        | j                  dd      d|      d|t	        | j                  dd      d
||      | j                  dt              t	        | j                  dd      d||      t	        | j                  dd      d||      ||t	        | j                  dd      d||      g}t        j                  dt        z  dj                  |            }	t        |	t         d       d   }
|	dd t        d|
z  d      z   |	dd z   }	|	S ) zReturn a header block. info is a dictionary with file
           information, format must be one of the *_FORMAT constants.
        rz  r{  r   rP   r|  r   NzTarInfo.type must not be Noner   r   r   r  r3   r4   r1   r  r2   s           r]  r  r5   r  r6   r  r   z%dsrF   iz%06o rR   i)getCHRTYPEBLKTYPErd   rE   r  r<   r  ri   r   r  r  rn   r^   )r  rb   rB   rC   has_device_fieldsr{  r|  filetypepartsrk   ry  s              rD   r  zTarInfo._create_headerf  s   
 !HHV,'0BB488J2Av>H488J2Av>H2q(F3H2q(F3H 88FG,<== $c8V<#f,a8"Av."Av.#R0!$b&1R(#x@HHWk*"%r8V<"%r8V<2&Xv>
$ kk%)+SXXe_=c9*+./2%4j5F!2G<<s45zI
rF   c                 n    t        t        |       t              \  }}|dkD  r| t        |z
  t        z  z  } | S )zdReturn the string payload filled with zero bytes
           up to the next 512 byte border.
        r   )rs   r>   r  r?   )payloadrz   r{   s      rD   _create_payloadzTarInfo._create_payload  s9    
 #3w<;	q=	I-44GrF   c                     |j                  ||      t        z   }i }d|d<   ||d<   t        |      |d<   t        |d<   | j	                  |t
        ||      | j                  |      z   S )zTReturn a GNUTYPE_LONGNAME or GNUTYPE_LONGLINK sequence
           for name.
        z././@LongLinkr   rz  r1   r  )r=   r?   r>   r  r  r   r  )r  r   rz  rB   rC   r  s         rD   r  zTarInfo._create_gnu_long_header  st    
 {{8V,s2&VV4yV!W !!$hG##D)* 	*rF   c                    d}|j                         D ]  \  }}	 |j                  dd        d}|r|dz  }|j                         D ]  \  }}|j                  d      }|r|j                  |d      }n|j                  d      }t        |      t        |      z   dz   }d	x}	}
	 |t        t	        |
            z   }	|	|
k(  rn|	}
 |t        t	        |
      d
      dz   |z   dz   |z   dz   z  } i }d|d<   ||d<   t        |      |d<   t        |d<   | j                  |t        d
d      | j                  |      z   S # t        $ r d}Y  w xY w)zReturn a POSIX.1-2008 extended or global header sequence
           that contains a list of keyword, value pairs. The values
           must be strings.
        Fr:   rS   TrF   s   21 hdrcharset=BINARY
r  r  r   rR          =   
z././@PaxHeaderr   rz  r1   r  r   )
r  r=   r  r>   r  r^   r  r  r   r  )r  r}  rz  rB   binarykeywordr  recordslrZ   rK   r  s               rD   r  z"TarInfo._create_pax_generic_header  s    )//1 	NGUWh/	 00G)//1 	VNGUnnW-G X/@AW-Gs5z)A-AIACFO6	 
 uSVW-4w>EMPUUUG!	V( 'VV7|V#W !!$gyI##G,- 	-I & s   D99E	E	c                 l   t        |      dk(  rt        d      t        |      t        k7  rt        d      |j	                  t
              t        k(  rt        d      t        |dd       }|t        |      vrt        d       |        }t        |dd ||      |_        t        |dd	       |_        t        |d	d
       |_        t        |d
d       |_        t        |dd       |_        t        |dd       |_        ||_        |dd |_        t        |dd ||      |_        t        |dd ||      |_        t        |dd ||      |_        t        |dd       |_        t        |dd       |_        t        |dd ||      }|j$                  t0        k(  r&|j                  j3                  d      rt4        |_        |j$                  t6        k(  rxd}g }t9        d      D ]@  }		 t        |||dz          }
t        ||dz   |dz          }|j=                  |
|f       |dz  }B t?        |d         }t        |dd       }|||f|_         |jC                         r |j                  jE                  d      |_        |r)|j$                  tF        vr|dz   |j                  z   |_        |S # t:        $ r Y  w xY w)zAConstruct a TarInfo object from a 512 byte bytes object.
        r   zempty headerztruncated headerzend of file header      zbad checksumr   l   t   |         i  i	  i)  iI  iQ  iY  i  rZ  i  r   r     i  i  i  )$r>   r   r  r   countr?   r   r\   rn   rY   rL   r   r   r3   r4   r1   r2   ry  rz  r]  r5   r6   r{  r|  AREGTYPEr   r  GNUTYPE_SPARSErV   r<   r  boolr~  rf  rstrip	GNU_TYPES)r  rk   rB   rC   ry  objr  r   structsr[   r  numbytes
isextendedorigsizes                 rD   frombufzTarInfo.frombuf  s    s8q=">22s8y &'9::99S>Y& !566SS\"c**$^44es1Sz8V4s3s|$c#cl#c#cl#s3s|$C%	
s3<3s3<6:Ch7	Ch7	3s3<(3s3<(SS\8V4 88xCHH$5$5c$:CH
 88~%CG1X  Sr!23F"3sRxb#9:H 12r	 c#hJ3s3<(H#*J"AC 99;xxs+CH chhi/|chh.CH
 " s   $%J&&	J32J3c                     |j                   j                  t              }| j                  ||j                  |j
                        }|j                   j                         t        z
  |_        |j                  |      S )zOReturn the next TarInfo object from TarFile object
           tarfile.
        )	r   rt   r  r  rB   rC   r   r  _proc_member)r  r=  rk   r  s       rD   fromtarfilezTarInfo.fromtarfile  s^    
 oo""9-kk#w//@__))+i7
((rF   c                    | j                   t        t        fv r| j                  |      S | j                   t        k(  r| j                  |      S | j                   t        t        t        fv r| j                  |      S | j                  |      S )zYChoose the right processing method depending on
           the type and call it.
        )rz  r  r  _proc_gnulongr  _proc_sparser  r  SOLARIS_XHDTYPE	_proc_pax_proc_builtin)r   r=  s     rD   r  zTarInfo._proc_member2  sx     99)+;<<%%g..YY.($$W--YY7G_==>>'**%%g..rF   c                    |j                   j                         | _        | j                  }| j                         s| j                  t
        vr|| j                  | j                        z  }||_        | j                  |j                  |j                  |j                         | j                         r | j                  j                  d      | _        | S )zfProcess a builtin type or an unknown type which
           will be treated as a regular file.
        rZ  )r   r   r:  rd  rz  SUPPORTED_TYPES_blockr1   r  _apply_pax_infor}  rB   rC   rf  r   r  )r   r=  r  s      rD   r  zTarInfo._proc_builtin?  s     #??//1!!::<499O;dkk$)),,F 	W00'2B2BGNNS ::<		((-DIrF   c                 F   |j                   j                  | j                  | j                              }	 | j	                  |      }| j                  |_        | j                  t        k(  r't        ||j                  |j                        |_        n9| j                  t        k(  r&t        ||j                  |j                        |_        |j#                         r |j                  j%                  d      |_        |S # t
        $ r}t        t        |            dd}~ww xY w)zSProcess the blocks that hold a GNU longname
           or longlink member.
        NrZ  )r   rt   r  r1   r  r   r   r  r  rz  r  rL   rB   rC   r   r  r]  rf  removesuffix)r   r=  rk   nextr  s        rD   r  zTarInfo._proc_gnulongU  s     oo""4;;tyy#9:	:##G,D kk99((C!1!17>>BDIYY**W%5%5w~~FDM ::<		..s3DI!  	:'A/T9	:s   C= =	D DD c                 
   | j                   \  }}}| ` |r|j                  j                  t              }d}t	        d      D ]D  }	 t        |||dz          }t        ||dz   |dz          }	|r|	r|j                  ||	f       |dz  }F t        |d         }|r|| _	        |j                  j                         | _        | j                  | j                  | j                        z   |_        || _        | S # t        $ r Y  yw xY w)z8Process a GNU sparse header plus extra headers.
        r      r  r  i  )r~  r   rt   r  rV   r\   r<   r  r  r;  r   r:  r  r1   r  )
r   r=  r  r  r  rk   r   r[   r  r  s
             rD   r  zTarInfo._proc_sparsep  s    )-(<(<%X  //&&y1CC2Y  Sr!23F"3sRxb#9:H hNNFH#56r	 c#hJ  "??//1))DKK		,BB	 " s   %C55	DDc                    |j                   j                  | j                  | j                              }| j                  t
        k(  r|j                  }n|j                  j                         }d}d}g }t        |      |kD  r||   dk7  rt        j                  ||      x}st        d      	 t        |j                  d            }|dk  rt        d      ||z   t        |      kD  rt        d      |j                  d      |z   dz
  }	||j!                  d      dz   |	 }
|
j#                  d      \  }}}|r|dk7  s||	   dk7  rt        d      |j%                  |||f       |dk(  r||d	k(  r|j&                  }nd
}||z  }t        |      |kD  r
||   dk7  r|d
}|D ]x  \  }}}| j)                  |d
d
|j*                        }|t,        v r)| j)                  |||j&                  |j*                        }n| j)                  |d
d
|j*                        }|||<   z 	 | j/                  |      }d|v r| j7                  ||       nRd|v r| j9                  ||       n;|j;                  d      dk(  r'|j;                  d      dk(  r| j=                  |||       | j                  t>        t@        fv r|jC                  ||j&                  |j*                         | jD                  |_"        d|v rS|jF                  }|jI                         s|j                  tJ        vr||j                  |j                        z  }||_"        |S # t        $ r t        d      w xY w# t0        $ r}t3        t5        |            dd}~ww xY w)zVProcess an extended or global header as described in
           POSIX.1-2008.
        r   NrU   r-      r  r  s
   hdrcharsets   BINARYr:   GNU.sparse.mapGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorrT   r1   )&r   rt   r  r1   rz  r  r}  r  r>   _header_length_prefix_rematchrY   rW   groupr<   r2  r   	partitionr  rB   _decode_pax_fieldrC   PAX_NAME_FIELDSr  r   r   r  _proc_gnusparse_01_proc_gnusparse_00r  _proc_gnusparse_10r  r  r  r  r:  rd  r  )r   r=  rk   r}  r   rB   raw_headersr  rA   header_value_end_offsetkeyword_and_valueraw_keywordequals	raw_valuer  r  r  r  r  s                      rD   r  zTarInfo._proc_pax  s   
 oo""4;;tyy#9:
 99!--K!--224K #hnST!15;;CEEEE()9::;U[[^,
 z()9::V|c#h&()9::&+kk!nv&=&A# #EIIaL1$45L M->-H-H-N*K &D.C8O4PTX4X()9::Y?@ m+0@	)&//H&H6MCM #hnST!1R H /: 	)*FK ,,['7NN$G/)..y(GDTDT( ..y'7( $)K #	)(	:##G,D
 {*##D+6+-##D+6__/0C7KOOL^<_cf<f##D+w?99/22  g.>.>O++DK$ ))::<499O#Cdkk$))44F!'y  ;()9::;|  	:'A/T9	:s$   1M "M M	N &M;;N c                 r   g }g }|D ]d  \  }}}|dk(  r*	 |j                  t        |j                                      6|dk(  s<	 |j                  t        |j                                      f t        t        ||            |_        y# t        $ r t	        d      w xY w# t        $ r t	        d      w xY w)z?Process a GNU tar extended sparse header, version 0.0.
        s   GNU.sparse.offsetrU   s   GNU.sparse.numbytesN)r  rW   rJ   r<   rY   listzipr;  )r   r  r
  offsetsr  _r  r  s           rD   r  zTarInfo._proc_gnusparse_00  s     !, 	?Aw..?NN3u||~#67 22?OOC$78	? 3w12 " ?,-=>>? " ?,-=>>?s   (B	(B!	B!B6c           	          |d   j                  d      D cg c]  }t        |       }}t        t        |ddd   |ddd               |_        yc c}w )z?Process a GNU tar extended sparse header, version 0.1.
        r  ,Nr.   r-   )r  rW   r  r  r;  )r   r  r}  xr;  s        rD   r  zTarInfo._proc_gnusparse_01  sW     #..>"?"E"Ec"JKQ#a&KK3vcc{F14a4L9: Ls   Ac           	         d}g }|j                   j                  t              }|j                  dd      \  }}t	        |      }t        |      |dz  k  rgd|vr"||j                   j                  t              z  }|j                  dd      \  }}|j                  t	        |             t        |      |dz  k  rg|j                   j                         |_        t        t        |ddd   |ddd               |_        y)z?Process a GNU tar extended sparse header, version 1.0.
        Nr  r-   r.   )r   rt   r  r  rW   r>   r  r   r:  r  r  r;  )r   r  r}  r=  fieldsr;  rk   numbers           rD   r	  zTarInfo._proc_gnusparse_10  s     oo""9-iiq)V&kFQJ&Cw++I66))E1-KFCMM#f+&	 &kFQJ&
 #??//13vcc{F14a4L9:rF   c                    |j                         D ]  \  }}|dk(  rt        | d|       |dk(  rt        | dt        |             5|dk(  rt        | dt        |             Q|t        v sZ|t        v r	 t	        |   |      }|dk(  r|j                  d      }t        | ||        |j                         | _        y# t
        $ r d}Y Hw xY w)	zoReplace fields with supplemental information from a previous
           pax extended or global header.
        zGNU.sparse.namer/   r  r1   zGNU.sparse.realsizer   rZ  N)	r  setattrrW   
PAX_FIELDSPAX_NUMBER_FIELDSr<   r  r  r}  )r   r}  rB   rC   r  r  s         rD   r  zTarInfo._apply_pax_info/  s     *//1 	.NGU++fe,--fc%j111fc%j1J&//" 1' :5 A f$!LL-Egu-	." '++- & " !"s   4B==C
Cc                 j    	 |j                  |d      S # t        $ r |j                  ||      cY S w xY w)z1Decode a single field from a pax record.
        rS   )rJ   UnicodeDecodeError)r   r  rB   fallback_encodingfallback_errorss        rD   r  zTarInfo._decode_pax_fieldF  s<    	D<<(33! 	D<< 1?CC	Ds    22c                 H    t        |t              \  }}|r|dz  }|t        z  S )z_Round up a byte count by BLOCKSIZE and return it,
           e.g. _block(834) => 1024.
        r-   )rs   r  )r   r  rz   r{   s       rD   r  zTarInfo._blockN  s,     #5)4	aKF	!!rF   c                 &    | j                   t        v S z4Return True if the Tarinfo object is a regular file.)rz  REGULAR_TYPESr   s    rD   rd  zTarInfo.isregW  s    yyM))rF   c                 "    | j                         S r%  )rd  r   s    rD   isfilezTarInfo.isfile[  s    zz|rF   c                 (    | j                   t        k(  S )z!Return True if it is a directory.)rz  r  r   s    rD   rf  zTarInfo.isdir_      yyG##rF   c                 (    | j                   t        k(  S )z%Return True if it is a symbolic link.)rz  SYMTYPEr   s    rD   rg  zTarInfo.issymc  r*  rF   c                 (    | j                   t        k(  S )z!Return True if it is a hard link.)rz  LNKTYPEr   s    rD   re  zTarInfo.islnkg  r*  rF   c                 (    | j                   t        k(  S )z(Return True if it is a character device.)rz  r  r   s    rD   ischrzTarInfo.ischrk  r*  rF   c                 (    | j                   t        k(  S )z$Return True if it is a block device.)rz  r  r   s    rD   isblkzTarInfo.isblko  r*  rF   c                 (    | j                   t        k(  S )zReturn True if it is a FIFO.)rz  FIFOTYPEr   s    rD   isfifozTarInfo.isfifos  s    yyH$$rF   c                     | j                   d uS r   )r;  r   s    rD   issparsezTarInfo.issparsew  s    {{$&&rF   c                 <    | j                   t        t        t        fv S )zCReturn True if it is one of character device, block device or FIFO.)rz  r  r  r4  r   s    rD   isdevzTarInfo.isdevz  s    yyWgx888rF   r   )r   )4r   r   r   r   dict	__slots__r   propertyr/   setterr0   r  r  r   r  r   r   r  r  r  r  classmethodr  r  staticmethodr  r  r  r  r  r  r  r  r  r  r  r  r  r	  r  r  r  rd  r(  rf  rg  re  r0  r2  r5  r7  r9  r   rF   rD   r   r   b  s     
,
!
 G
 H	

  
 -
 $
>
F
 
 
 *
  *!
" /#
$ 5%
&9'
* .+
, -
. /
0 1
I60   
[[    __! !O %eeuE88 *HEV /"IM8Qt M M
  ' 'R   * *  0- 0-d < <| ) )(/,68wr3(;; ..D"*$$$$$%'9rF   r   c                      e Zd ZdZdZdZdZdZeZ	e
ZdZeZeZdZ	 	 	 	 d;dZedddefd       Zed<d	       Zed=d
       Zed=d       Zed>d       ZdddddZd Zd Zd Zd Zd?dZd@dddZ dAdddZ!dBdZ"d Z#dCddddZ$d Z%dDdddd Z&d! Z'	 dBd"Z(d# Z)d$ Z*d% Z+	 	 dEddd&d'Z,d( Z-d) Z.d* Z/d+ Z0d, Z1d- Z2d. Z3d/ Z4d0 Z5d1 Z6d2 Z7dFd3Z8d4 Z9dBd5Z:d6 Z;d7 Z<d8 Z=d9 Z>d: Z?y)Gr   z=The TarFile Class provides an interface to tar archives.
    r   Fr-   Nr   c                    ddddd}||vrt        d      || _        ||   | _        |sZ| j                  dk(  r-t        j                  j                  |      sd| _        d| _        t        || j                        }d	| _        n^|8t        |d      r,t        |j                  t        t        f      r|j                  }t        |d      r|j                  | _        d| _        |rt        j                  j                  |      nd
| _
        || _        ||| _        ||| _        ||| _        ||| _        ||| _        |	| _        |
| j                  t*        k(  r|
| _        ni | _        ||| _        ||| _        || _        d	| _        g | _        d	| _        | j                  j;                         | _        i | _        	 | j                  dk(  rd
| _         | jC                         | _         | j                  dk(  r^	 | j                  jE                  | j<                         	 | j                   jG                  |       }| j6                  jI                  |       ]| j                  dv rd| _        | j,                  rm| j                   jQ                  | j,                  jS                               }| j                  jU                  |       | xj<                  tW        |      z  c_        y
y
y
# tJ        $ r( | j                  jE                  | j<                         Y tL        $ r}tO        t        |            d
d
}~ww xY w#  | j                  s| j                  jY                          d| _         xY w)a  Open an (uncompressed) tar archive `name'. `mode' is either 'r' to
           read from an existing archive, 'a' to append data to an existing
           file or 'w' to create a new file overwriting an existing one. `mode'
           defaults to 'r'.
           If `fileobj' is given, it is used for reading or writing data. If it
           can be determined, `mode' is overridden by `fileobj's mode.
           `fileobj' is not closed, when TarFile is closed.
        rbzr+bwbxbr   ar   r  !mode must be 'r', 'a', 'w' or 'x'rF  r   FNr   r   Tr   rF  r   r  )-r<   r   _moder   r/   exists	bltn_openr   r   r  r   r  r^   abspathr   rb   r>  dereferenceignore_zerosrB   rC   r   r}  debug
errorlevelcopybufsizer   members_loadedr   r  inodesfirstmemberr  r   r  r  r   r   r   r  r  ru   r>   r   )r   r   r   r   rb   r>  rM  rN  rB   rC   r}  rO  rP  rQ  modesr  rk   s                    rD   r   zTarFile.__init__  s    T=u@AA	4[
yyCt(<	!
djj1G$D&!97<<#u6||w'$\\
#D-1BGGOOD)t	  DK"DL"*D# ,D$DM"t{{j'@*D!DDJ!(DO 'll'')	yyC#' #'99; yyC LL%%dkk2:"&,,":":4"@++G4	  yyO+###,,??@P@P@U@U@WXCLL&&s+KK3s8+K $ , * ))$++6& :'A/T9:	##""$DKs>   >A L; 6K& BL; &.L8L; L8L33L88L; ;0M+c                     |s|st        d      |dv rz fd}g }t         j                  |      D ]:  }t          j                  |         }	||j	                         }
	  |	|d|fi |c S  d	j                  |      }t        d
|       d|v rb|j                  dd      \  }}|xs d}|xs d}| j                  v rt          j                  |         }	nt        d|z         |	|||fi |S d|v rg|j                  dd      \  }}|xs d}|xs d}|dvrt        d      |j                  dd      }t        ||||||      }	   |||fi |}d|_        |S |dv r  j                  |||fi |S t        d      # t
        t        f$ r5}|j                  d| d|       ||j                  
       Y d}~d}~ww xY w#  |j                           xY w)a  Open a tar archive for reading, writing or appending. Return
           an appropriate TarFile class.

           mode:
           'r' or 'r:*' open for reading with transparent compression
           'r:'         open for reading exclusively uncompressed
           'r:gz'       open for reading with gzip compression
           'r:bz2'      open for reading with bzip2 compression
           'r:xz'       open for reading with lzma compression
           'a' or 'a:'  open for appending, creating the file if necessary
           'w' or 'w:'  open for writing without compression
           'w:gz'       open for writing with gzip compression
           'w:bz2'      open for writing with bzip2 compression
           'w:xz'       open for writing with lzma compression

           'x' or 'x:'  create a tarfile exclusively without compression, raise
                        an exception if the file is already created
           'x:gz'       create a gzip compressed tarfile, raise an exception
                        if the file is already created
           'x:bz2'      create a bzip2 compressed tarfile, raise an exception
                        if the file is already created
           'x:xz'       create an lzma compressed tarfile, raise an exception
                        if the file is already created

           'r|*'        open a stream of tar blocks with transparent compression
           'r|'         open an uncompressed stream of tar blocks for reading
           'r|gz'       open a gzip compressed stream of tar blocks
           'r|bz2'      open a bzip2 compressed stream of tar blocks
           'r|xz'       open an lzma compressed stream of tar blocks
           'w|'         open an uncompressed stream for writing
           'w|gz'       open a gzip compressed stream for writing
           'w|bz2'      open a bzip2 compressed stream for writing
           'w|xz'       open an lzma compressed stream for writing
        znothing to open)r   r:*c                 (    j                   |    dk(  S )Ntaropen)	OPEN_METH)r   r  s    rD   not_compressedz$TarFile.open.<locals>.not_compressed4  s    }}X.);;rF   )keyNr   z	- method z: 
z'file could not be opened successfully:
:r-   r   r   |r   zmode must be 'r' or 'w'r   	   FrH  zundiscernible mode)r<   sortedr[  r   r   r   r	   r  r   r  r  popr   r   r   rZ  )r  r   r   r   ry   kwargsr\  
error_msgsr   func	saved_posr  error_msgs_summaryfilemoder   streamr  s   `                rD   r   zTarFile.open
  s9   J G.//<<J"3==nE 
sCMM($;<& 'Ic7=f==
 "&:!6FGYFZ[\\D[!%C!3Hh3H(5H 3==(sCMM($;<&'Dx'OPPh:6::D[!%C!3Hh3H(5Hz) !:;;"JJ:MT8Xw*,Fh9&9 "AMH_$3;;tT7=f==-..W "#34 %%	(2aU&CD*Y/	Ds$   "
FG G*GGGc                 6    |dvrt        d       | |||fi |S )zCOpen uncompressed tar archive name for reading or writing.
        rE  rG  )r<   )r  r   r   r   rd  s        rD   rZ  zTarFile.taropenj  s-     ++@AA4w1&11rF   c                    |dvrt        d      	 ddlm} 	  |||dz   ||      }	  | j                  |||fi |}d
|_	        |S # t        $ r t	        d      dw xY w# t
        $ r}||dk(  rt        d	      | d}~ww xY w# t
        $ r'}|j                          |dk(  rt        d	      | d}~w |j                           xY w)zkOpen gzip compressed tar archive name for reading or writing.
           Appending is not allowed.
        r   r   r  mode must be 'r', 'w' or 'x'r   )GzipFilezgzip module is not availableNr|   r   r   F)
r<   gzipro  r   r	   r   r   rZ  r   r   )	r  r   r   r   r   rd  ro  r  r  s	            rD   gzopenzTarFile.gzopenr  s    
 &;<<	M%	tTCZHG		D$:6:A +  	M"#ABL	M
  	"ts{ 129	  	MMOs{ 129	MMOs9   A A B A	B 'A;;B 	C"B..Cc                 P   |dvrt        d      	 ddlm}  ||xs |||      }	  | j
                  |||fi |}d
|_
        |S # t        $ r t	        d      dw xY w# t        t        f$ r'}|j                          |dk(  rt        d	      | d}~w |j                           xY w)zlOpen bzip2 compressed tar archive name for reading or writing.
           Appending is not allowed.
        rm  rn  r   )BZ2Filer   N)r   r   znot a bzip2 fileF)r<   r   rs  r   r	   rZ  r   EOFErrorr   r   r   )	r  r   r   r   r   rd  rs  r  r  s	            rD   bz2openzTarFile.bz2open  s    
 &;<<	L# '/T4}M		D$:6:A !  	L"#@AtK	L " 	MMOs{ 23:	MMOs"   A A AB%-"BB%c                 L   |dvrt        d      	 ddlm}m}  ||xs |||      }	  | j                  |||fi |}d
|_
        |S # t        $ r t        d      dw xY w# |t        f$ r'}	|j                          |dk(  rt        d	      |	 d}	~	w |j                           xY w)zkOpen lzma compressed tar archive name for reading or writing.
           Appending is not allowed.
        rm  rn  r   )LZMAFiler   r   N)presetr   znot an lzma fileF)r<   r   rw  r   r   r	   rZ  rt  r   r   r   )
r  r   r   r   rx  rd  rw  r   r  r  s
             rD   xzopenzTarFile.xzopen  s    
 &;<<	M0 7?dD@		D$:6:A !  	M"#ABL	M 8$ 	MMOs{ 23:	MMOs"   A A  A B#+"BB#rZ  rq  ru  ry  )r   r   r   r   c                    | j                   ryd| _         	 | j                  dv r| j                  j                  t        t
        dz  z         | xj                  t
        dz  z  c_        t        | j                  t              \  }}|dkD  r)| j                  j                  t        t        |z
  z         | j                  s| j                  j                          yy# | j                  s| j                  j                          w w xY w)zlClose the TarFile. In write-mode, two finishing zero blocks are
           appended to the archive.
        NTrH  r.   r   )r   r   r   ru   r?   r  r  rs   
RECORDSIZEr   r   )r   rz   r{   s      rD   r   zTarFile.close  s     ;;	%yyO+""3)a-#89	A. %+4;;
$C!	q=LL&&sj9.D'EF##""$ $4##""$ $s   BC )Dc                 f    | j                  |j                  d            }|t        d|z        |S )a  Return a TarInfo object for member `name'. If `name' can not be
           found in the archive, KeyError is raised. If a member occurs more
           than once in the archive, its last occurrence is assumed to be the
           most up-to-date version.
        rZ  zfilename %r not found)
_getmemberr  KeyError)r   r   r>  s      rD   	getmemberzTarFile.getmember  s6     //$++c"23?2T9::rF   c                 r    | j                          | j                  s| j                          | j                  S )zReturn the members of the archive as a list of TarInfo objects. The
           list has the same order as the members in the archive.
        )_checkrS  _loadrR  r   s    rD   
getmemberszTarFile.getmembers  s'     	||JJL||rF   c                 \    | j                         D cg c]  }|j                   c}S c c}w )zReturn the members of the archive as a list of their names. It has
           the same order as the list returned by getmembers().
        )r  r   )r   r>  s     rD   getnameszTarFile.getnames   s#     -1OO,=>>>>s   )c                 .   | j                  d       ||j                  }||}t        j                  j	                  |      \  }}|j                  t        j                  d      }|j                  d      }| j                         }| |_	        |8| j                  st        j                  |      }n9t        j                  |      }n#t        j                  |j                               }d}|j                  }t        j                   |      r|j"                  |j$                  f}	| j                  sE|j&                  dkD  r6|	| j(                  v r(|| j(                  |	   k7  rt*        }
| j(                  |	   }nt,        }
|	d   r|| j(                  |	<   nt        j.                  |      rt0        }
nt        j2                  |      rt4        }
njt        j6                  |      rt8        }
t        j:                  |      }n9t        j<                  |      rt>        }
nt        j@                  |      rtB        }
ny||_        ||_"        |jF                  |_$        |jJ                  |_&        |
t,        k(  r|jN                  |_(        nd|_(        |jR                  |_*        |
|_+        ||_,        tZ        r(	 t[        j\                  |jH                        d   |_/        tb        r(	 tc        jd                  |jL                        d   |_3        |
t>        tB        fv rhti        t        d      rXti        t        d      rHt        jj                  |jl                        |_7        t        jp                  |jl                        |_9        |S # t`        $ r Y w xY w# t`        $ r Y w xY w)	a  Create a TarInfo object from the result of os.stat or equivalent
           on an existing file. The file is either named by `name', or
           specified as a file object `fileobj' with a file descriptor. If
           given, `arcname' specifies an alternative name for the file in the
           archive, otherwise, the name is taken from the 'name' attribute of
           'fileobj', or the 'name' argument. The name should be a text
           string.
        awxNrZ  r   r-   r   majorminor):r  r   r   r/   
splitdriver   r`  ra  r>  r=  rM  lstatstatfstatfilenost_modeS_ISREGst_inost_devst_nlinkrT  r.  r  S_ISDIRr  S_ISFIFOr4  S_ISLNKr,  readlinkS_ISCHRr  S_ISBLKr  r   st_uidr3   st_gidr4   st_sizer1   st_mtimer2   rz  r]  pwdgetpwuidr5   r~  grpgetgrgidr6   r   r  st_rdevr{  r  r|  )r   r   arcnamer   drvr>  statresr]  stmdinoderz  s              rD   
gettarinfozTarFile.gettarinfo  s    	E <<D
 ?Gww))'2W//"&&#...% ,,. ?##((4.''$-hhw~~/0G<<^^W^^4E##(8(81(<T[[(WE8J-J ;;u- 8)0DKK&\\$D]]4 D\\$D{{4(H\\$D\\$D nnnn7?"??GLGL((# #W[[ 9! <  #W[[ 9! < GW%%r7#G(<#%88GOO#< #%88GOO#<   
  s$   ,'M9 'N 9	NN	NN)rR  c                   | j                          || }|D ]  }|r&|j                  t        d       n(t        t        j                  |j                               t        |j
                  xs |j                  d|j                  xs |j                         |j                         s|j                         r(t        dd|j                  |j                  fz  z         nt        d|j                  z         |j                  t        d       n.t        dt        j                   |j                        dd	 z         t        |j"                  |j%                         rdnd
z          |rP|j'                         rt        d|j(                  z          |j+                         rt        d|j(                  z          t-                 y)a  Print a table of contents to sys.stdout. If `verbose' is False, only
           the names of the members are printed. If it is True, an `ls -l'-like
           output is produced. `members' is optional and must be a subset of the
           list returned by getmembers().
        Nz
??????????rZ  z%10sz%d,%dz%10dz????-??-?? ??:??:??z%d-%02d-%02d %02d:%02d:%02dr   r   z-> zlink to )r  r   r   r  ri  r5   r3   r6   r4   r0  r2  r{  r|  r1   r2   r   	localtimer   rf  rg  r]  re  r   )r   verboserR  r>  s       rD   r  zTarFile.listi  sk    	?G 	G<<'-gll ;<w}}'C'C'.}}'C'CE F==?gmmo$(8(8':J:J'KK!M N   56==( 56 ="&.."?"C!D E w}}BGH==?(8(8 89==?
W-=-= =>G5	rF   filterc                b   | j                  d       ||}| j                  Bt        j                  j	                  |      | j                  k(  r| j                  dd|z         y| j                  d|       | j                  ||      }|| j                  dd|z         y|  ||      }|| j                  dd|z         y|j                         r(t        |d      5 }| j                  ||       ddd       y|j                         r| j                  |       |rut        t        j                  |            D ]S  }| j                  t        j                  j                  ||      t        j                  j                  ||      ||	       U yy| j                  |       y# 1 sw Y   yxY w)
a!  Add the file `name' to the archive. `name' may be any type of file
           (directory, fifo, symbolic link, etc.). If given, `arcname'
           specifies an alternative name for the file in the archive.
           Directories are added recursively by default. This can be avoided by
           setting `recursive' to False. `filter' is a function
           that expects a TarInfo object argument and returns the changed
           TarInfo object, if it returns None the TarInfo object will be
           excluded from the archive.
        r  Nr.   ztarfile: Skipped %rr-   ztarfile: Unsupported type %rtarfile: Excluded %rrB  r  )r  r   r   r/   rL  _dbgr  rd  rK  addfilerf  rb  listdiraddr  )r   r   r  	recursiver  r>  fs          rD   r  zTarFile.add  s    	E?G 99 RWW__T%:dii%GIIa.56		!T //$0?IIa7$>? WoG		!3d:; ==?4& )!Wa() ) ]]_LL!

4 01 6AHHRWW\\$2BGGLL!4L%f  66  LL!) )s   F%%F.c                    | j                  d       t        j                  |      }|j                  | j                  | j                  | j
                        }| j                  j                  |       | xj                  t        |      z  c_        | j                  }|t        || j                  |j                  |       t        |j                  t              \  }}|dkD  r.| j                  j                  t        t        |z
  z         |dz  }| xj                  |t        z  z  c_        | j                   j#                  |       y)a  Add the TarInfo object `tarinfo' to the archive. If `fileobj' is
           given, it should be a binary file, and tarinfo.size bytes are read
           from it and added to the archive. You can create TarInfo objects
           directly, or by using gettarinfo().
        r  N)ry   r   r-   )r  r  r  rb   rB   rC   r   ru   r  r>   rQ  rr   r1   rs   r  r?   rR  r  )r   r>  r   rk   ry   rz   r{   s          rD   r  zTarFile.addfile  s     	E))G$mmDKKD3s3x  w||WM &w||Y ?FI1}""3)i*?#@A!KK6I--KG$rF   c                    |K| j                   }| t        j                  dt               t        S t        |t              rt        d      |S t        |      r|S 	 t        |   S # t        $ r t        d|d      d w xY w)NzPython 3.14 will, by default, filter extracted tar archives and reject files or modify their metadata. Use the filter argument to control this behavior.zrString names are not supported for TarFile.extraction_filter. Use a function such as tarfile.data_filter directly.zfilter z
 not found)extraction_filterwarningswarnDeprecationWarningr   r  r  	TypeErrorcallable_NAMED_FILTERSr~  r<   )r   r  s     rD   _get_filter_functionzTarFile._get_filter_function  s    >++F~J '	(
 ,+&#&67 7 MFM	G!&)) 	Gwvj
;<$F	Gs   A% %A?)numeric_ownerr  c                   g }| j                  |      }|| }|D ]a  }| j                  |||      \  }}	||j                         r|j                  |	       | j	                  |||j                          ||       c |j                  d d       |D ]  }		 	  ||	|      }|| j                  |	d       #t        j                  j                  ||j                        }	 t        j                  |      }t        j                   |j"                        s| j                  |d       | j%                  |||	       | j'                  ||       | j)                  ||        y# t        $ r%}
| j                  |	t        |
             Y d}
~
d}
~
ww xY w# t        $ r | j                  |d       Y w xY w# t*        $ r}| j-                  |       Y d}~Cd}~ww xY w)
a  Extract all members from the archive to the current working
           directory and set owner, modification time and permissions on
           directories afterwards. `path' specifies a different directory
           to extract to. `members' is optional and must be a subset of the
           list returned by getmembers(). If `numeric_owner` is True, only
           the numbers for user/group names are used and not the names.

           The `filter` function will be called on each member just
           before extraction.
           It can return a changed TarInfo or None to skip the member.
           String names of common filters are accepted.
        N)	set_attrsr  filter_functionc                     | j                   S r   r  )rF  s    rD   <lambda>z$TarFile.extractall.<locals>.<lambda>	  s
    qvv rF   T)r]  reversezexcluded by filtermissingznot a directory)r  )r  _get_extract_tarinforf  r  _extract_onesort_FILTER_ERRORS_log_no_directory_fixupreprr   r/   r  r   r  FileNotFoundErrorr  r  r  chownutimechmodr   _handle_nonfatal_error)r   r/   rR  r  r  directoriesr  rj  r>  
unfilteredexcdirpathr  r  s                 rD   
extractallzTarFile.extractall  s    33F;?G 	?F"&";";#/GZ}} "":.gt7==?7J,9.=  ?	? 	-t< & 	/J/-j$?G ?001EG'',,tW\\:HHW-E ||EMM2 00:KL

7G=
I

7G,

7G,5	/ & 00T#YG ) 00)D   /++A../sl   	E$"F77*F7"F71F7)8F7$	F-FF7FF7F40F73F44F77	G GGc                 J    | j                  dd|j                  d|d       y )Nr.   z!tarfile: Not fixing up directory z ())r  r   )r   rj  reasons      rD   r  zTarFile._log_no_directory_fixup7	  s    		!;;( 	)rF   c                    | j                  |      }| j                  |||      \  }}|| j                  ||||       yy)a  Extract a member from the archive to the current working directory,
           using its full name. Its file information is extracted as accurately
           as possible. `member' may be a filename or a TarInfo object. You can
           specify a different directory using `path'. File attributes (owner,
           mtime, mode) are set unless `set_attrs' is False. If `numeric_owner`
           is True, only the numbers for user/group names are used and not
           the names.

           The `filter` function will be called before extraction.
           It can return a changed TarInfo or None to skip the member.
           String names of common filters are accepted.
        N)r  r  r  )	r   rj  r/   r  r  r  r  r>  r  s	            rD   extractzTarFile.extract;	  sO     33F;"77OT+gtYF rF   c                    t        |t              r| j                  |      }n|}d}	  |||      }| | j                  dd|j                  z         y|j                         rDt        j                  |      }t        j                  j                  ||j                        |_        ||fS # t        t        f$ r}| j                  |       Y d}~d}~wt        $ r}| j                  |       Y d}~d}~ww xY w)zGet (filtered, unfiltered) TarInfos from *member*

        *member* might be a string.

        Return (None, None) if not found.
        Nr.   r  )NN)r  r  r  r   r   _handle_fatal_errorr   r  r  r   re  r  r   r/   r  r]  r  )r   rj  r  r/   r  filteredr  s          rD   r  zTarFile._get_extract_tarinfoO	  s     fc"/JJ	+&z48H
 IIa/*//AB >>yy*H$&GGLLx7H7H$IH!## % 	($$Q'' 	+''**	+s#   	B+ +C7:CC7C22C7c                 8   | j                  d       	 | j                  |t        j                  j	                  ||j
                        ||||       y# t        $ r}| j                  |       Y d}~yd}~wt        $ r}| j                  |       Y d}~yd}~ww xY w)zExtract from filtered tarinfo to disk.

           filter_function is only used when extracting a *different*
           member (e.g. as fallback to creating a symlink)
        r   )r  r  r  extraction_rootN)
r  _extract_memberr   r/   r  r   r   r  r   r  )r   r>  r/   r  r  r  r  s          rD   r  zTarFile._extract_onem	  s     	C		+  "'',,tW\\*J+4/<1@15	 ! 7
  	($$Q'' 	+''**	+s#   ?A 	BA22B>BBc                 N    | j                   dkD  r | j                  dd|z         y)z=Handle non-fatal error (ExtractError) according to errorlevelr-   tarfile: %sN)rP  r  r   r  s     rD   r  zTarFile._handle_nonfatal_error	  s$    ??QIIa*+rF   c                 L   | j                   dkD  r t        |t              rY|j                   | j	                  dd|j
                  z         y| j	                  dd|j
                  d|j                         y| j	                  ddt        |      j                  d|       y)z1Handle "fatal" error according to self.errorlevelr   Nr-   r  z	tarfile: r   )rP  r  r   filenamer  strerrorrz  r   r  s     rD   r  zTarFile._handle_fatal_error	  st    ??Q7#zz!		!]QZZ78		!QZZHIIIaT!W-=-=qABrF   c                    | j                  d       t        |t              r| j                  |      }n|}|j	                         s|j
                  t        vr| j                  | |      S |j                         s|j                         rEt        | j                  t              rt        d      | j                  | j                  |            S y)a\  Extract a member from the archive as a file object. `member' may be
           a filename or a TarInfo object. If `member' is a regular file or
           a link, an io.BufferedReader object is returned. For all other
           existing members, None is returned. If `member' does not appear
           in the archive, KeyError is raised.
        r   z'cannot extract (sym)link as file objectN)r  r  r  r  rd  rz  r  
fileobjectre  rg  r   r   r
   extractfile_find_link_target)r   rj  r>  s      rD   r  zTarFile.extractfile	  s     	Cfc"nnV,GG==?gll/A??411]]_$,,0 ""KLL ''(>(>w(GHH rF   r  r  c                   |j                  d      }|j                  dt        j                        }t        j                  j                  |      }|r4t        j                  j                  |      st        j                  |       |j                         s|j                         r,| j                  d|j                  d|j                         n| j                  d|j                         |j                         r| j                  ||       n|j                         r| j!                  ||       n|j#                         r| j%                  ||       n|j'                         s|j)                         r| j+                  ||       nm|j                         s|j                         r| j-                  ||||       n7|j.                  t0        vr| j3                  ||       n| j                  ||       |rI| j5                  |||       |j                         s%| j7                  ||       | j9                  ||       yyy)zExtract the filtered TarInfo object tarinfo to a physical
           file called targetpath.

           filter_function is only used when extracting a *different*
           member (e.g. as fallback to creating a symlink)
        rZ  r-   z -> r  N)r  r   r   r`  r/   ri  rJ  makedirsre  rg  r  r   r]  rd  makefilerf  makedirr5  makefifor0  r2  makedevmakelink_with_filterrz  r  makeunknownr  r  r  )r   r>  
targetpathr  r  r  r  	upperdirss           rD   r  zTarFile._extract_member	  s     &&s+
''RVV4
 GGOOJ/	RWW^^I6 KK	"==?gmmoIIaw||W5E5EFGIIa&==?MM':.]]_LL*-^^MM':.]]_LL*-]]_%% / / & 1 \\0Wj1MM':.JJw
M:==?

7J/

7J/ # rF   c                     	 |j                   t        j                  |       yt        j                  |d       y# t        $ r# t        j                  j                  |      s Y yw xY w)z,Make a directory called targetpath.
        Ni  )r   r   mkdirFileExistsErrorr/   rf  r   r>  r  s      rD   r  zTarFile.makedir	  sT    
	||#$ U+ 	77==, -	s   !; ; )A'&A'c           	         | j                   }|j                  |j                         | j                  }t	        |d      5 }|j
                  d|j
                  D ])  \  }}|j                  |       t        |||t        |       + |j                  |j                         |j                          nt        |||j                  t        |       ddd       y# 1 sw Y   yxY w)z'Make a file called targetpath.
        rC  N)
r   r   r:  rQ  rK  r;  rr   r   r1   truncate)r   r>  r  sourcery   targetr  r1   s           rD   r  zTarFile.makefile	  s     G''(""z4( 	NF~~)$+NN JLFDKK'iIJ GLL)!FFGLL)WM	N 	N 	Ns    BCC c                 f    | j                  ||       | j                  dd|j                  z         y)zYMake a file from a TarInfo object with an unknown type
           at targetpath.
        r-   z9tarfile: Unknown file type %r, extracted as regular file.N)r  r  rz  r  s      rD   r  zTarFile.makeunknown
  s3     	gz*		! 24;LLA 	BrF   c                 d    t        t        d      rt        j                  |       yt        d      )z'Make a fifo called targetpath.
        mkfifozfifo not supported by systemN)r   r   r  r   r  s      rD   r  zTarFile.makefifo
  s&     2x IIj!=>>rF   c                 f   t        t        d      rt        t        d      st        d      |j                  }|d}|j	                         r|t
        j                  z  }n|t
        j                  z  }t        j                  ||t        j                  |j                  |j                               y)z<Make a character or block device called targetpath.
        mknodr  z'special devices not supported by systemNr\  )r   r   r   r   r2  r  S_IFBLKS_IFCHRr  r  r{  r|  )r   r>  r  r   s       rD   r  zTarFile.makedev
  s     r7#72y+AHII||<D==?DLL DDLL D
TG,,g.>.>?	ArF   c                 *    | j                  ||d d       S r   )r  r  s      rD   makelinkzTarFile.makelink-
  s    ((*dDIIrF   c                    d}	 |j                         rUt        j                  j                  |      rt        j                  |       t        j
                  |j                  |       yt        j                  j                  |j                        r!t        j                  |j                  |       y	 	 | j                  |      }||}n|t        d      	  |||      }|| j#                  ||||       yy# t        $ r d}Y Tw xY w# t        $ r |rt        d      d w xY w# t        $ r}t        ||j                         |d}~ww xY w)a3  Make a (symbolic) link called targetpath. If it cannot be created
          (platform limitation), we try to make a copy of the referenced file
          instead of a link.

          filter_function is only used when extracting a *different*
          member (e.g. as fallback to creating a link).
        FNTz%unable to resolve link inside archivez[makelink_with_filter: if filter_function is not None, extraction_root must also not be Noner  )rg  r   r/   lexistsunlinksymlinkr]  rJ  r  linksymlink_exceptionr  r~  r   r  rW  r   r  )	r   r>  r  r  r  keyerror_to_extracterrorr  r  causes	            rD   r  zTarFile.makelink_with_filter0
  s]    $) 	,}}77??:.IIj)

7++Z877>>'"6"67GGG00*= 8	//8J "!H&">? ?M*:G   :1@1@ ! B  / ! 	,'+$	,
  	'";=BFG 	  " M'AuLMs=   A$C; )A	C; 5D 	D( ;D	D	D%(	E1EEc                    t        t        d      rt        j                         dk(  r|j                  }|j                  }|sj	 t
        r.|j                  r"t        j                  |j                        d   }	 t        r.|j                  r"t        j                  |j                        d   }|d}|d}	 |j                         r(t        t        d      rt        j                  |||       yt        j                  |||       yyy# t        $ r Y w xY w# t        $ r Y uw xY w# t        $ r}t!        d      |d}~ww xY w)zSet owner of targetpath according to tarinfo. If numeric_owner
           is True, use .gid/.uid instead of .gname/.uname. If numeric_owner
           is False, fall back to .gid/.uid when the search based on name
           fails.
        geteuidr   r.   NrH   lchownzcould not change owner)r   r   r  r4   r3   r  r6   getgrnamr~  r  r5   getpwnamrg  r  r  r   r   )r   r>  r  r  gur  s          rD   r  zTarFile.chownb
  s    2y!bjjla&7AA w}}LL7:w}}LL7: yyD==?wr8'<IIj!Q/HHZA./ '8!   
     D"#;<!CDsB   4D 84D 57D% -D% 	DD	D"!D"%	D?.D::D?c                     |j                   y	 t        j                  ||j                          y# t        $ r}t	        d      |d}~ww xY w)zASet file permissions of targetpath according to tarinfo.
        Nzcould not change mode)r   r   r  r   r   )r   r>  r  r  s       rD   r  zTarFile.chmod
  sF     <<	?HHZ. 	?67Q>	?s    0 	A
AA
c                     |j                   }|yt        t        d      sy	 t        j                  |||f       y# t        $ r}t        d      |d}~ww xY w)zBSet modification time of targetpath according to tarinfo.
        Nr  z"could not change modification time)r2   r   r   r  r   r   )r   r>  r  r2   r  s        rD   r  zTarFile.utime
  sZ     =r7#	LHHZ%0 	LCD!K	Ls   ; 	AAAc                 L   | j                  d       | j                  | j                  }d| _        |S | j                  | j                  j	                         k7  r^| j                  dk(  ry| j                  j                  | j                  dz
         | j                  j                  d      st        d      d}	 	 | j                  j                  |       }	 || j0                  j3                  |       |S d| _        |S # t        $ rU}| j                  r?| j                  dd| j                  |fz         | xj                  t        z  c_        Y d}~Y d}~d}~wt        $ rz}| j                  r?| j                  dd| j                  |fz         | xj                  t        z  c_        Y d}~| j                  dk(  rt        t        |            dY d}~d}~wt         $ r | j                  dk(  rt        d	      dY -t"        $ r/}| j                  dk(  rt        t        |            dY d}~_d}~wt$        $ r}t        t        |            dd}~wt&        $ r@}	 ddl}t+        ||j,                        rt        d
|       d|# t.        $ r |w xY wd}~ww xY w)zReturn the next member of the archive as a TarInfo object, when
           TarFile is opened for reading. Return None if there is no more
           available.
        raNr   r-   rp   Tr.   z0x%X: %sz
empty filezzlib error: )r  rU  r  r   r   r   rt   r   r>  r  r   rN  r  r  rY   r  r   r   r   	Exceptionr   r  r   r   rR  r  rS  )r   mr>  r  r   s        rD   r  zTarFile.next
  sR   
 	D'  A#DH ;;$,,++--{{aLLdkkAo.<<$$Q' 899 ,,2248< LL(   DLI " $$IIat{{A.>!>?KK9,K % & 6$$IIat{{A.>!>?KK9,K[[A%#CF+5 &# <;;!##L1t; $' 6;;!##CF+5 $( 2A'T1 !!TZZ0',qc(:;E" Gsb   <D   	J#	AEJ#%AG0$G'J#J#$H66J#IJ#$+JJJJ#c                    | j                         }d}|	 |j                  |      }|d| }|rt        j                  j                  |      }t        |      D ]a  }|r|j                  |j                  k(  rd}!|r*t        j                  j                  |j                        }n|j                  }||k(  s_|c S  |rt        |      y# t        $ r d}Y w xY w)z}Find an archive member by name from bottom to top.
           If tarinfo is given, it is used as the starting point.
        FNT)	r  indexr<   r   r/   rh  reversedr  r   )	r   r   r>  	normalizerR  skippingr  rj  member_names	            rD   r}  zTarFile._getmember
  s    
 //# *g. "&5/77##D)Dw' 	F>>V]]2$H gg..v{{;$kk{"	 W%% 1      s   C
 
CCc                 V    | j                         	 | j                         d| _        y)zWRead through the entire archive file and look for readable
           members.
        NT)r  rS  r   s    rD   r  zTarFile._load  s(     iik% iik%rF   c                     | j                   r"t        d| j                  j                  z        |'| j                  |vrt        d| j                  z        yy)znCheck if TarFile is still open, and if the operation's mode
           corresponds to TarFile's mode.
        z%s is closedNzbad operation for mode %r)r   r   r?  r   r   )r   r   s     rD   r  zTarFile._check  sS     ;;.4>>+B+BBCC		 55		ABB !6rF   c                 ,   |j                         rQdj                  t        dt        j                  j                  |j                        |j                  f            }d}n|j                  }|}| j                  ||d      }|t        d|z        |S )zZFind the target member of a symlink or hardlink member in the
           archive.
        rZ  NT)r>  r!  zlinkname %r not found)
rg  r  r  r   r/   ri  r   r]  r}  r~  )r   r>  r]  limitrj  s        rD   r  zTarFile._find_link_target  s     ==?xxtbggoogll.KWM]M]-^ _`HE ''HE5DI>2X=>>rF   c              #   R  K   | j                   r| j                  E d{    yd}| j                  | j                         }|dz  }| 	 |t	        | j                        k  r| j                  |   }n'| j                   s| j                         }|s	d| _         yy|dz  }| Y7 w)z$Provide an iterator object.
        Nr   r-   T)rS  rR  rU  r  r>   )r   r  r>  s      rD   __iter__zTarFile.__iter__(  s      <<||##  'iikGQJEMs4<<((,,u-\\))+#'DLQJEM  $s   B'B%BB'c                 Z    || j                   k  rt        |t        j                         yy)z.Write debugging output to sys.stderr.
        fileN)rO  r   r   stderr)r   levelmsgs      rD   r  zTarFile._dbgG  s#     DJJ#CJJ' rF   c                 &    | j                          | S r   )r  r   s    rD   	__enter__zTarFile.__enter__M  s    rF   c                     || j                          y | j                  s| j                  j                          d| _        y r  )r   r   r   r   )r   rz  r  	tracebacks       rD   __exit__zTarFile.__exit__Q  s2    <JJL ##""$DKrF   )Nr   NNNNNNr  NNNN)r   N)r   Nra  )r   NN)NNNTr  r   ).N)r   T)TFr"  )@r   r   r   r   rO  rM  rN  rP  r   rb   r   rB   rC   r   r>  r8  r  r  r   r>  r{  r   rZ  rq  ru  ry  r[  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r}  r  r  r  r)  r  r1  r4  r   rF   rD   r   r     s    EK L J FHFGJAEHL>B)-b^ #tZ ]/ ]/~ 2 2  @  6  : 	I%*	?aF$D $L1" 1"f%2G.C/%C/J)GG($> &*+(,
C@ >B&+10@D(,10pN B?A$J0BdDB?L<B&&PC&>(rF   r   c                     	 t        | d      r.| j                         }t        |       }| j                  |       nt        |       }|j	                          y# t
        $ r Y yw xY w)zReturn True if name points to a tar archive that we
       are able to handle, else return False.

       'name' should be a string, file, or file-like object.
    rt   )r   TF)r   r   r   r   r   r   )r   r   r  s      rD   r   r   _  sX    
4 ))+CT"AIIcNT
A		 s   AA 	A$#A$c            	         dd l } d}| j                  |      }|j                  ddddd	       |j                  d
dt        d       |j	                  d      }|j                  dddd       |j                  ddddd       |j                  ddddd       |j                  dd dd!       |j                         }|j                  r|j                  |j                  d"d#       |j                  |j                  }t        |      rut        |d$      5 }|j                          t        |j                         t        j                  %       d d d        |j                   r=t        d&j#                  |             y |j                  d"d'j#                  |             y y |j$                  u|j$                  }t        |      r<t&        j                  |d(      5 }|j%                  |j                   )       d d d        y |j                  d"d'j#                  |             y |j                  +t)        |j                        d"k(  r |j                  d   }t*        j,                  }nHt)        |j                        d*k(  r|j                  \  }}n |j                  d"|j/                                t              r}t&        j                  |d(      5 }|j1                  |j                  +       d d d        |j                   rWd,k(  rd-j#                  |      }	nd.j#                  ||      }	t        |	       y |j                  d"d'j#                  |             y y |j2                  |j2                  j5                  d      }
t*        j6                  j9                  |
      \  }}d/d/d0d0d1d1d1d1d2}||v rd3||   z   nd4}|j2                  }t&        j                  |
|      5 }|D ]  }|j;                  |        	 d d d        |j                   rt        d5j#                  |
             y y y # 1 sw Y   xY w# 1 sw Y   y xY w# 1 sw Y   VxY w# 1 sw Y   XxY w)6Nr   z3A simple command-line interface for tarfile module.)descriptionz-vz	--verbose
store_trueFzVerbose output)actiondefaulthelpz--filterz<filtername>zFilter for extraction)metavarchoicesr=  T)requiredz-lz--list	<tarfile>zShow listing of a tarfile)r>  r=  z-ez	--extract+)rA  z<output_dir>zExtract tarfile into target dir)nargsr>  r=  z-cz--create)z<name>z<file>zCreate tarfile from sourcesz-tz--testzTest if a tarfile is validr-   z&--filter is only valid for extraction
r   r+  z{!r} is a tar archive.z{!r} is not a tar archive.
rX  )r  r.   )r/   r  r6  z{!r} file is extracted.z+{!r} file is extracted into {!r} directory.r   r   r   )r   z.tgzz.xzz.txzz.bz2z.tbzz.tbz2z.tb2zw:r   z{!r} file created.)argparseArgumentParseradd_argumentr  add_mutually_exclusive_group
parse_argsr  r  exittestr   r   r  r   r   r-  r  rb   r  r   r>   r   curdirformat_helpr  createrc  r/   splitextr  )rD  r9  parserr  argsrv   r   tfrK  r/  tar_namer  extcompressionstar_mode	tar_files	file_names                    rD   mainrX  t  s   GK$$$=F
k,-  /

N .4  6 ///>E	tX{7  9	t[<=  ? 
tZs39  ; 
tX{8  : D{{t||+A@Ayyiic?c3 93 cnn&SZZ89 ||.55c:;KK9@@EF 
 
	iic?c5) .R-. . KK9@@EF		!t||!,,q/CYYF!#,,KCKK6--/0c?c5) ?R6$++>?||S=3::3?C2396#v3F c
KK9@@EF  
	 ;;??1%!!(+3 
 03l/B4,s++KK	\\(H- 	"& "	y!"	" <<&--h78 / 
!M9 9. .? ?>	" 	"s0   :P-P:Q"Q-P7:QQQ__main__r5  )nr   version
__author____credits__builtinsr   rK  r   r   r)  rq   r  r   ri   r  rer  r  r   r  AttributeErrorNotImplementedErrorr   r  __all__r?   r  r{  r  r  r  r  r  r  r  r.  r,  r  r  r  r4  CONTTYPEr  r  r  r  r  r  r   r   r   r   r  r&  r  r  r  r  rW   r  r   r   getfilesystemencodingrE   rL   r\   rd   rn   rr   r   r  r   r   r   r	   r
   r   r   r   r   rY   r   r   r   objectr   r  BufferedReaderr8  r   r   r   r   r   r   rW  r  rp  r   r   r   r  r  compiler  r   r   r   rX  r   r   rF   rD   <module>rg     s  : 6
H
 ' 
 	 	      	  $%8'B * 	^
	





  



 HgGXWg#%5!	# (>+ /	.
 9
   77d?H(s((*H0&( N <* "&$ 2	y 		8 		 		x 		( 		( 		{ 		; 		[ 		 		K 	 0r rj6 <e& eP"2$$ "	( 	J J
?k ?@{ @
L L
?+ ?D D w5BH * 	 &2::&89 Z9f Z9zZf Z@'$ ||]9~ zF }\  
C  
Cs"   I I$ I! I!$I/.I/