a
    ze                    @   s  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zddlZW n ey   dZY n0 zddlZW n ey   dZY n0 eefZzeef7 ZW n ey   Y n0 g dZdZdZe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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j<d!krd"Z=ne> Z=d#d$ Z?d%d& Z@d'd( ZAd)e3fd*d+ZBd,d- ZCdedfd.d/ZDd0d1 ZEG d2d3 d3eFZGG d4d5 d5eGZHG d6d7 d7eGZIG d8d9 d9eGZJG d:d; d;eGZKG d<d= d=eGZLG d>d? d?eLZMG d@dA dAeLZNG dBdC dCeLZOG dDdE dEeLZPG dFdG dGeLZQG dHdI dIZRG dJdK dKZSG dLdM dMeTZUG dNdO dOeTZVG dPdQ dQejWZXG dRdS dSeGZYG dTdU dUeYZZG dVdW dWeYZ[G dXdY dYeYZ\G dZd[ d[eYZ]G d\d] d]eYZ^dqd_d`Z_dadb Z`dcdd Zadedf Zbe`eaebdgZceT ZdG dhdi dieTZeG djdk dkeTZfdldm ZgefjZdndo Zheidpkreh  dS )rz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       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&   r+   r,   r%   )Zatimectimer(   r)   r*   r'   ntutf-8c                 C   s8   | d u rt d| ||} | d | |t|  t  S )Nzmetadata cannot contain None)
ValueErrorencodelenNUL)slengthencodingerrors r8   /usr/lib/python3.9/tarfile.pystn   s    r:   c                 C   s*   |  d}|dkr| d | } | ||S )Nr   )finddecode)r4   r6   r7   pr8   r8   r9   nts   s    
r?   c                 C   s   | d dv rbd}t t| d D ]}|dK }|| |d  7 }q | d dkrdt| d  |  }n>z"t| dd} t|  p|d	d}W n ty   td
Y n0 |S )Nr   )      r#      rA      asciistrict0invalid header)ranger2   r?   intstripr0   InvalidHeaderError)r4   nir8   r8   r9   nti   s    rN   rB   c                 C   s   t | } d|   kr$d|d  k rDn ntd|d | f dt }n|tkrd|d   |   krrd|d  k rn nV| dkrtdg}ntdg}d| |  } t|d D ]}|d| d@  | dL } qntd	|S )
Nr   rB   r#   z%0*orD   rC   r@   rA   zoverflow in number field)rI   bytesr3   r   	bytearrayrH   insertr0   )rL   digitsformatr4   rM   r8   r8   r9   itn   s     2
rT   c                 C   s0   dt td|  }dt td|  }||fS )NrC   Z
148B8x356BZ
148b8x356b)sumstructunpack_from)bufZunsigned_chksumZsigned_chksumr8   r8   r9   calc_chksums   s    	rY   c           	      C   s   |pd}|dkrd S |d u r.t | || d S t||\}}t|D ],}| |}t||k rf|d|| qD|dkr| |}t||k r|d|| d S )Ni @  r   unexpected end of data)shutilcopyfileobjdivmodrH   readr2   write)	srcdstr5   	exceptionbufsizeblocks	remainderbrX   r8   r8   r9   r\      s$    


r\   c                 C   s8   t tjdd }|d ur(| |d|} t| dd d S )Nr6   backslashreplace )end)getattrsysstdoutr1   r=   print)r4   r6   r8   r8   r9   _safe_print  s    rn   c                   @   s   e Zd ZdS )r   N__name__
__module____qualname__r8   r8   r8   r9   r     s   r   c                   @   s   e Zd ZdS )r
   Nro   r8   r8   r8   r9   r
     s   r
   c                   @   s   e Zd ZdS )r   Nro   r8   r8   r8   r9   r     s   r   c                   @   s   e Zd ZdS )r   Nro   r8   r8   r8   r9   r     s   r   c                   @   s   e Zd ZdS )r	   Nro   r8   r8   r8   r9   r	     s   r	   c                   @   s   e Zd ZdS )r   Nro   r8   r8   r8   r9   r     s   r   c                   @   s   e Zd ZdS )EmptyHeaderErrorNro   r8   r8   r8   r9   rs      s   rs   c                   @   s   e Zd ZdS )TruncatedHeaderErrorNro   r8   r8   r8   r9   rt   #  s   rt   c                   @   s   e Zd ZdS )EOFHeaderErrorNro   r8   r8   r8   r9   ru   &  s   ru   c                   @   s   e Zd ZdS )rK   Nro   r8   r8   r8   r9   rK   )  s   rK   c                   @   s   e Zd ZdS )SubsequentHeaderErrorNro   r8   r8   r8   r9   rv   ,  s   rv   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
_LowLevelFilec                 C   sF   t jt jt jB t jB d| }tt dr2|t jO }t ||d| _d S )NrwO_BINARYi  )	osO_RDONLYO_WRONLYO_CREATO_TRUNChasattrr{   r   fd)selfnamemoder8   r8   r9   __init__9  s    

z_LowLevelFile.__init__c                 C   s   t | j d S N)r|   closer   r   r8   r8   r9   r   B  s    z_LowLevelFile.closec                 C   s   t | j|S r   )r|   r^   r   r   r'   r8   r8   r9   r^   E  s    z_LowLevelFile.readc                 C   s   t | j| d S r   )r|   r_   r   r   r4   r8   r8   r9   r_   H  s    z_LowLevelFile.writeN)rp   rq   rr   r   r   r^   r_   r8   r8   r8   r9   rw   3  s   	rw   c                   @   sn   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dddZdd Zdd Zdd ZdS )_Streamc           	      C   s  d| _ |d u rt||}d| _ |dkr6t|}| }|p<d| _|| _|| _|| _|| _d| _	d| _
d| _zF|dkrzdd l}W n ty   tdY n0 || _|d| _|d	kr|   |j| _n|   n|d
kr6zdd l}W n ty   tdY n0 |d	kr*d| _| | _t| _n
| | _nz|dkrzdd l}W n tyh   tdY n0 |d	krd| _| | _|j| _n
| | _n|dkrtd| W n&   | j s| j  d| _ Y n0 d S )NTF*     r   gzzzlib module is not availablery   bz2bz2 module is not availablexzlzma module is not availabletarunknown compression type %r) _extfileobjrw   _StreamProxygetcomptyper   r   comptypefileobjrc   rX   posclosedzlibImportErrorr   crc32crc_init_read_gzerrorrb   _init_write_gzr   dbufBZ2DecompressorcmpOSErrorBZ2CompressorlzmaLZMADecompressor	LZMAErrorLZMACompressorr   )	r   r   r   r   r   rc   r   r   r   r8   r8   r9   r   V  sl    












z_Stream.__init__c                 C   s   t | dr| js|   d S )Nr   )r   r   r   r   r8   r8   r9   __del__  s    z_Stream.__del__c                 C   s   | j d| j j| j j | j jd| _tdtt		 }| 
d| d  | jdrf| jd d | _tj| j| _| 
| jdd	t  d S )
N	   r   <Ls   s   .gzz
iso-8859-1replace)r   compressobjDEFLATED	MAX_WBITSDEF_MEM_LEVELr   rV   packrI   time_Stream__writer   endswithr|   r%   basenamer1   r3   )r   Z	timestampr8   r8   r9   r     s    z_Stream._init_write_gzc                 C   sR   | j dkr| j|| j| _|  jt|7  _| j dkrD| j|}| | d S )Nr   r   )	r   r   r   r   r   r2   r   compressr   r   r8   r8   r9   r_     s    

z_Stream.writec                 C   sN   |  j |7  _ t| j | jkrJ| j| j d | j  | j | jd  | _ qd S r   )rX   r2   rc   r   r_   r   r8   r8   r9   Z__write  s    z_Stream.__writec                 C   s   | j r
d S d| _ z| jdkr:| jdkr:|  j| j 7  _| jdkr| jr| j| j d| _| jdkr| jt	d| j
 | jt	d| jd@  W | js| j  n| js| j  0 d S )NTrz   r   r   r   r   l    )r   r   r   rX   r   flushr   r_   rV   r   r   r   r   r   r   r8   r8   r9   r     s     
z_Stream.closec                 C   s   | j | j j | _d| _| ddkr0td| ddkrFtdt| d}| d |d	@ rt| dd
t| d  }| 	| |d@ r| d}|r|t
krqq|d@ r| d}|r|t
krqq|d@ r| d d S )Nr   r$   s   not a gzip filer#      zunsupported compression method      rC   rB      )r   decompressobjr   r   r   _Stream__readr   r   ordr^   r3   )r   flagZxlenr4   r8   r8   r9   r     s*    
 


z_Stream._init_read_gzc                 C   s   | j S r   )r   r   r8   r8   r9   tell  s    z_Stream.tellr   c                 C   sX   || j  dkrJt|| j  | j\}}t|D ]}| | j q,| | ntd| j S )Nr   z seeking backwards is not allowed)r   r]   rc   rH   r^   r	   )r   r   rd   re   rM   r8   r8   r9   seek  s    z_Stream.seekc                 C   s    |  |}|  jt|7  _|S r   )_readr   r2   )r   r'   rX   r8   r8   r9   r^   
  s    
z_Stream.readc                 C   s   | j dkr| |S t| j}| jg}||k r| jrB| j}d| _n| j| j}|sVqz| j	|}W n | j
y   tdY n0 || |t|7 }q&d|}||d  | _|d | S )Nr   r   zinvalid compressed data)r   r   r2   r   rX   r   r^   rc   r   
decompressrb   r   appendjoinr   r'   ctrX   r8   r8   r9   r     s(    




z_Stream._readc                 C   sj   t | j}| jg}||k rF| j| j}|s.qF|| |t |7 }qd|}||d  | _|d | S )Nr   )r2   rX   r   r^   rc   r   r   r   r8   r8   r9   Z__read,  s    


z_Stream.__readN)r   )rp   rq   rr   r   r   r   r_   r   r   r   r   r   r^   r   r   r8   r8   r8   r9   r   K  s   F
	
r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
r   c                 C   s   || _ | j t| _d S r   )r   r^   	BLOCKSIZErX   )r   r   r8   r8   r9   r   B  s    z_StreamProxy.__init__c                 C   s   | j j| _| jS r   )r   r^   rX   r   r8   r8   r9   r^   F  s    
z_StreamProxy.readc                 C   sP   | j drdS | j dd dkr8| j dd dkr8d	S | j d
rHdS dS d S )Ns   r   r      s   BZhr   
   s   1AY&SYr   )s   ]  s   7zXZr   r   )rX   
startswithr   r8   r8   r9   r   J  s    $z_StreamProxy.getcomptypec                 C   s   | j   d S r   )r   r   r   r8   r8   r9   r   T  s    z_StreamProxy.closeN)rp   rq   rr   r   r^   r   r   r8   r8   r8   r9   r   =  s   
r   c                   @   sf   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Ze	j
fddZdddZdd Zdd ZdS )_FileInFileNc                 C   s   || _ || _|| _d| _t|dd | _d| _|d u r>d|fg}d| _g | _d}| j}|D ]L\}}||kr|| j	d||d f | j	d||| |f ||7 }|| }qX|| jk r| j	d|| jd f d S )Nr   r   FT)
r   offsetr'   positionrj   r   r   	map_indexmapr   )r   r   r   r'   Z	blockinfoZlastposZrealposr8   r8   r9   r   a  s(    


z_FileInFile.__init__c                 C   s   d S r   r8   r   r8   r8   r9   r   z  s    z_FileInFile.flushc                 C   s   dS NTr8   r   r8   r8   r9   readable}  s    z_FileInFile.readablec                 C   s   dS )NFr8   r   r8   r8   r9   writable  s    z_FileInFile.writablec                 C   s
   | j  S r   )r   seekabler   r8   r8   r9   r     s    z_FileInFile.seekablec                 C   s   | j S r   )r   r   r8   r8   r9   r     s    z_FileInFile.tellc                 C   s   |t jkr tt|d| j| _nj|t jkr\|dk rFt| j| d| _qt| j| | j| _n.|t jkrtt| j| | jd| _ntd| jS )Nr   zInvalid argument)	ioSEEK_SETminmaxr'   r   SEEK_CURSEEK_ENDr0   )r   r   whencer8   r8   r9   r     s    


z_FileInFile.seekc           	      C   s
  |d u r| j | j }nt|| j | j }d}|dkr| j| j \}}}}|| j  kr`|k rhq nqq6|  jd7  _| jt| jkr6d| _q6t||| j }|r| j|| j|   | j|}t||krt	d||7 }n|t
| 7 }||8 }|  j|7  _q,|S )Nr   r   r#   rZ   )r'   r   r   r   r   r2   r   r   r^   r   r3   )	r   r'   rX   datastartstopr   r5   rf   r8   r8   r9   r^     s,    

z_FileInFile.readc                 C   s&   |  t|}||d t|< t|S r   )r^   r2   )r   rf   rX   r8   r8   r9   readinto  s    z_FileInFile.readintoc                 C   s
   d| _ d S r   )r   r   r8   r8   r9   r     s    z_FileInFile.close)N)N)rp   rq   rr   r   r   r   r   r   r   r   r   r   r^   r   r   r8   r8   r8   r9   r   [  s   

r   c                       s   e Zd Z fddZ  ZS )ExFileObjectc                    s&   t |j|j|j|j}t | d S r   )r   r   offset_datar'   sparsesuperr   )r   tarfiletarinfor   	__class__r8   r9   r     s    
zExFileObject.__init__rp   rq   rr   r   __classcell__r8   r8   r   r9   r     s   r   c                   @   s   e Zd ZdS )FilterErrorNro   r8   r8   r8   r9   r     s   r   c                       s   e Zd Z fddZ  ZS )AbsolutePathErrorc                    s    || _ t d|jd d S )Nzmember z has an absolute pathr   r   r   r   r   r   r   r8   r9   r     s    zAbsolutePathError.__init__r   r8   r8   r   r9   r     s   r   c                       s   e Zd Z fddZ  ZS )OutsideDestinationErrorc                    s.   || _ || _t |jd|dd  d S )Nz would be extracted to ,  which is outside the destinationr   _pathr   r   r   r   r   r%   r   r8   r9   r     s
    z OutsideDestinationError.__init__r   r8   r8   r   r9   r     s   r   c                       s   e Zd Z fddZ  ZS )SpecialFileErrorc                    s   || _ t |jd d S )Nz is a special filer   r   r   r8   r9   r     s    zSpecialFileError.__init__r   r8   r8   r   r9   r     s   r   c                       s   e Zd Z fddZ  ZS )AbsoluteLinkErrorc                    s   || _ t |jd d S )Nz is a link to an absolute pathr   r   r   r8   r9   r     s    zAbsoluteLinkError.__init__r   r8   r8   r   r9   r     s   r   c                       s   e Zd Z fddZ  ZS )LinkOutsideDestinationErrorc                    s.   || _ || _t |jd|dd  d S )Nz would link to r   r   r   r   r   r8   r9   r     s
    z$LinkOutsideDestinationError.__init__r   r8   r8   r   r9   r     s   r   Tc                 C   s  i }| j }tj|}|dtjfr@| jdtj  }|d< tj|rTt| tjtj	||}tj
||g|krt| || j}|d ur|d@ }|r|  s|  r|d@ s|dM }|dO }n|  s|  rd }nt| || jkr||d< |r| jd urd |d< | jd ur*d |d	< | jd ur>d |d
< | jd urRd |d< |  sf|  rtj| jr~t| |  rtj	|tj|| j}ntj	|| j}tj|}tj
||g|krt| ||S )N/r   i  @   i  r   r)   r*   r+   r,   )r   r|   r%   realpathr   seplstripisabsr   r   
commonpathr   r   isregislnkisdirissymr   r)   r*   r+   r,   linknamer   dirnamer   )member	dest_pathZfor_data	new_attrsr   Ztarget_pathr   r8   r8   r9   _get_filtered_attrs  s^    





r  c                 C   s   | S r   r8   )r  r  r8   r8   r9   fully_trusted_filter/  s    r  c                 C   s,   t | |d}|r(| jf i |ddiS | S )NFdeepr  r   r  r  r  r8   r8   r9   
tar_filter2  s    r  c                 C   s,   t | |d}|r(| jf i |ddiS | S )NTr  Fr  r  r8   r8   r9   data_filter8  s    r  )Zfully_trustedr   r   c                   @   s  e Zd Zeddddddddd	d
dddddddddddZdhddZedd Zejdd Zedd Z	e	jdd Z	dd Z
eeeeeeeeded 
d!d"Zd#d$ Zeed%fd&d'Zd(d) Zd*d+ Zd,d- Zed.d/ Zd0d1 Zed2d3 Zed4d5 Zed6d7 Zed8d9 Zed:d; Zed<d= Zd>d? Zd@dA ZdBdC Z dDdE Z!dFdG Z"dHdI Z#dJdK Z$dLdM Z%dNdO Z&dPdQ Z'dRdS Z(dTdU Z)dVdW Z*dXdY Z+dZd[ Z,d\d] Z-d^d_ Z.d`da Z/dbdc Z0ddde Z1dfdg Z2dS )ir   zName of the archive member.zPermission bits.z6User ID of the user who originally stored this member.z7Group ID of the user who originally stored this member.zSize in bytes.zTime of last modification.zHeader checksum.zFile type. type is usually one of these constants: REGTYPE, AREGTYPE, LNKTYPE, SYMTYPE, DIRTYPE, FIFOTYPE, CONTTYPE, CHRTYPE, BLKTYPE, GNUTYPE_SPARSE.zcName of the target file name, which is only present in TarInfo objects of type LNKTYPE and SYMTYPE.z
User name.zGroup name.zDevice major number.zDevice minor number.zThe tar header starts here.zThe file's data starts here.zMA dictionary containing key-value pairs of an associated pax extended header.zSparse member information.N)r   r   r)   r*   r'   r(   chksumtyper  r+   r,   devmajordevminorr   r   pax_headersr   r   _sparse_structs_link_targetr   c                 C   sj   || _ d| _d| _d| _d| _d| _d| _t| _d| _	d| _
d| _d| _d| _d| _d| _d | _i | _d S )Ni  r   r   )r   r   r)   r*   r'   r(   r  REGTYPEr  r  r+   r,   r  r  r   r   r   r  r   r   r8   r8   r9   r   n  s"    zTarInfo.__init__c                 C   s   | j S r   r   r   r8   r8   r9   r%     s    zTarInfo.pathc                 C   s
   || _ d S r   r!  r   r8   r8   r9   r%     s    c                 C   s   | j S r   r  r   r8   r8   r9   r&     s    zTarInfo.linkpathc                 C   s
   || _ d S r   r"  )r   r  r8   r8   r9   r&     s    c                 C   s   d| j j| jt| f S )Nz<%s %r at %#x>)r   rp   r   idr   r8   r8   r9   __repr__  s    zTarInfo.__repr__T)
r   r(   r   r  r)   r*   r+   r,   r  _KEEPc       
         C   s   |	rt | }n
t  | }||
ur(||_||
ur6||_||
urD||_||
urR||_||
ur`||_||
urn||_||
ur|||_||
ur||_	|S r   )
copydeepcopyr   r(   r   r  r)   r*   r+   r,   )r   r   r(   r   r  r)   r*   r+   r,   r  r%  resultr8   r8   r9   r     s(    
zTarInfo.replacec                 C   s   | j d u rd }n
| j d@ }| j|| j| j| j| j| j| j| j| j	| j
| j| jd}|d tkr||d ds||d  d7  < |S )N  )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  DIRTYPEr   )r   r   infor8   r8   r9   get_info  s(    

zTarInfo.get_infosurrogateescapec                 C   sz   |   }| D ]\}}|d u rtd| q|tkrD| |||S |tkrZ| |||S |tkrn| ||S tdd S )Nz%s may not be Nonezinvalid format)	r,  itemsr0   r   create_ustar_headerr   create_gnu_headerr   create_pax_header)r   rS   r6   r7   r+  r   valuer8   r8   r9   tobuf  s    zTarInfo.tobufc                 C   sn   t |d< t|d ||tkr(tdt|d ||tkr^| |d ||\|d< |d< | |t||S )Nmagicr  zlinkname is too longr   prefix)	POSIX_MAGICr2   r1   LENGTH_LINKr0   LENGTH_NAME_posix_split_name_create_headerr   )r   r+  r6   r7   r8   r8   r9   r/    s    zTarInfo.create_ustar_headerc                 C   s   t |d< d}t|d ||tkr<|| |d t||7 }t|d ||tkrl|| |d t||7 }|| |t	|| S )Nr4  r   r  r   )
	GNU_MAGICr2   r1   r7  _create_gnu_long_headerGNUTYPE_LONGLINKr8  GNUTYPE_LONGNAMEr:  r   )r   r+  r6   r7   rX   r8   r8   r9   r0    s    zTarInfo.create_gnu_headerc              	   C   sL  t |d< | j }ddtfddtfddfD ]h\}}}||v r>q*z|| dd	 W n" tyt   || ||< Y q*Y n0 t|| |kr*|| ||< q*d
D ]\}}d}|| }	t|	t	}
|
rt
|	n|	}d|  krd|d  k sn d||< d}n|
r|||< d}|r||vrt|	||< q|r4| |t|}nd}|| |tdd S )Nr4  r   r%   r  r&   )r+   r+       )r,   r,   r?  rD   rE   ))r)   rB   )r*   rB   )r'      )r(   r@  Fr   rB   r#   Tr   r   )r6  r  r&  r8  r7  r1   UnicodeEncodeErrorr2   
isinstancefloatroundstr_create_pax_generic_headerXHDTYPEr:  r   )r   r+  r6   r  r   Zhnamer5   rR   Z	needs_paxvalZval_is_floatZval_intrX   r8   r8   r9   r1    s>    


zTarInfo.create_pax_headerc                 C   s   |  |tdS )Nr/   )rF  XGLTYPE)clsr  r8   r8   r9   create_pax_global_header9  s    z TarInfo.create_pax_global_headerc                 C   s~   | d}tdt|D ]T}d|d | }d||d  }t|||tkrt|||tkr qvqtd||fS )Nr   r#   zname is too long)splitrH   r2   r   r1   LENGTH_PREFIXr8  r0   )r   r   r6   r7   
componentsrM   r5  r8   r8   r9   r9  ?  s    
zTarInfo._posix_split_namec                 C   s  |  dttfv }|r@t|  ddd|}t|  ddd|}ntdd||}tdd||}|  dt}|d u rxtdt|  ddd	||t|  d
dd@ d|t|  ddd|t|  ddd|t|  ddd|t|  ddd|d|t|  ddd	|||  dtt|  ddd||t|  ddd||||t|  ddd||g}t	dt
 d|}	t|	t
 d  d }
|	d d td|
 d |	dd   }	|	S )Nr  r  r   rB   r  r   zTarInfo.type must not be Noner   r   r   r)  r)   r*   r'   r@  r(   s           r  r4  r+   r?  r,   r5  r   z%dsr   iz%06o rD   i)getCHRTYPEBLKTYPErT   r:   r  r0   r6  rV   r   r   r   rY   rO   )r+  rS   r6   r7   Zhas_device_fieldsr  r  ZfiletypepartsrX   r  r8   r8   r9   r:  O  s:    
&zTarInfo._create_headerc                 C   s.   t t| t\}}|dkr*| t| t 7 } | S )Nr   )r]   r2   r   r3   )payloadrd   re   r8   r8   r9   _create_payloady  s    zTarInfo._create_payloadc                 C   sR   | ||t }i }d|d< ||d< t||d< t|d< | |t||| | S )Nz././@LongLinkr   r  r'   r4  )r1   r3   r2   r;  r:  r   rT  )rJ  r   r  r6   r7   r+  r8   r8   r9   r<    s    zTarInfo._create_gnu_long_headerc              	   C   s0  d}|  D ]6\}}z|dd W q ty@   d}Y  qDY q0 qd}|rT|d7 }|  D ]\}}|d}|r||d}n
|d}t|t| d }d	 }	}
|tt|
 }	|	|
krq|	}
q|tt|
d
d | d | d 7 }q\i }d|d< ||d< t||d< t|d< | |td
d| 	| S )NFr/   rE   Tr   s   21 hdrcharset=BINARY
r-  r   r   rD          =   
z././@PaxHeaderr   r  r'   r4  r   )
r.  r1   rA  r2   rE  rO   r6  r:  r   rT  )rJ  r  r  r6   binarykeywordr2  recordslrL   r>   r+  r8   r8   r9   rF    s<    

(z"TarInfo._create_pax_generic_headerc              	   C   st  t |dkrtdt |tkr(td|ttkr>tdt|dd }|t|vrbt	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tkr|jdrt|_|jtkr6d}g }tdD ]j}	z0t|||d  }
t||d |d  }W n ty   Y  qY n0 ||
|f |d7 }qt|d }t|dd }|||f|_ |! rN|j"d|_|rp|jt#vrp|d |j |_|S )Nr   zempty headerztruncated headerzend of file header      zbad checksumr   l   t   |         i  i	  i)  iI  iQ  iY  i  r   i  r   r@     i  i  i  )$r2   rs   r   rt   countr3   ru   rN   rY   rK   r?   r   r   r)   r*   r'   r(   r  r  r  r+   r,   r  r  AREGTYPEr   r*  GNUTYPE_SPARSErH   r0   r   boolr  r
  rstrip	GNU_TYPES)rJ  rX   r6   r7   r  objr5  r   structsrM   r   numbytes
isextendedorigsizer8   r8   r9   frombuf  sZ    
zTarInfo.frombufc                 C   s8   |j t}| ||j|j}|j  t |_||S r   )	r   r^   r   ro  r6   r7   r   r   _proc_member)rJ  r   rX   rj  r8   r8   r9   fromtarfile  s    zTarInfo.fromtarfilec                 C   sT   | j ttfv r| |S | j tkr,| |S | j tttfv rF| 	|S | 
|S d S r   )r  r>  r=  _proc_gnulongrf  _proc_sparserG  rI  SOLARIS_XHDTYPE	_proc_pax_proc_builtin)r   r   r8   r8   r9   rp    s    



zTarInfo._proc_memberc                 C   sR   |j  | _| j}|  s$| jtvr4|| | j7 }||_| 	|j
|j|j | S r   )r   r   r   r  r  SUPPORTED_TYPES_blockr'   r   _apply_pax_infor  r6   r7   )r   r   r   r8   r8   r9   rv  (  s    zTarInfo._proc_builtinc                 C   s   |j | | j}z| |}W n ty<   tdY n0 | j|_| jt	krdt
||j|j|_n| jtkrt
||j|j|_|S )N missing or bad subsequent header)r   r^   rx  r'   rq  r   rv   r   r  r>  r?   r6   r7   r   r=  r  )r   r   rX   nextr8   r8   r9   rr  9  s    

zTarInfo._proc_gnulongc           
   	   C   s   | j \}}}| ` |r|jt}d}tdD ]l}z0t|||d  }t||d |d  }	W n tyx   Y  qY n0 |r|	r|||	f |d7 }q,t|d }q|| _	|j
 | _| j| | j |_|| _| S )Nr      r@  rc  i  )r  r   r^   r   rH   rN   r0   r   rg  r   r   r   rx  r'   r   )
r   r   rk  rm  rn  rX   r   rM   r   rl  r8   r8   r9   rs  O  s(    
zTarInfo._proc_sparsec                 C   s,  |j | | j}| jtkr&|j}n
|j }t	d|}|d urX|
dd|d< |d}|dkrr|j}nd}td}d}|||}|sq6| \}	}
t|	}	|	dkrtd||d	d |d|	 d  }| |
dd|j}
|
tv r| |||j|j}n| |dd|j}|||
< ||	7 }qz| |}W n ty`   td
Y n0 d|v rz| || nHd|v r| ||| n.|ddkr|ddkr| ||| | jttfv r(| ||j|j | j!|_!d|v r(|j"}|# s|jt$vr"|||j7 }||_!|S )Ns   \d+ hdrcharset=([^\n]+)\nr#   r/   
hdrcharsetZBINARYs   (\d+) ([^=]+)=r   rG   r$   rz  GNU.sparse.mapGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorrF   r'   )%r   r^   rx  r'   r  rI  r  r&  researchgroupr=   rO  r6   compilematchgroupsrI   rK   ri   r   _decode_pax_fieldr7   PAX_NAME_FIELDSrq  r   rv   _proc_gnusparse_01_proc_gnusparse_00_proc_gnusparse_10rG  rt  ry  r   r   r  rw  )r   r   rX   r  r  r}  r6   Zregexr   r5   rY  r2  r{  r   r8   r8   r9   ru  k  sh    



$	





 
zTarInfo._proc_paxc                 C   sh   g }t d|D ]}|t|d qg }t d|D ]}|t|d q:tt|||_d S )Ns   \d+ GNU.sparse.offset=(\d+)\nr#   s   \d+ GNU.sparse.numbytes=(\d+)\n)r  finditerr   rI   r  listzipr   )r   r{  r  rX   Zoffsetsr  rl  r8   r8   r9   r    s    zTarInfo._proc_gnusparse_00c                 C   s@   dd |d  dD }tt|d d d |dd d |_d S )Nc                 S   s   g | ]}t |qS r8   )rI   ).0xr8   r8   r9   
<listcomp>  r   z.TarInfo._proc_gnusparse_01.<locals>.<listcomp>r~  ,r$   r#   )rL  r  r  r   )r   r{  r  r   r8   r8   r9   r    s    zTarInfo._proc_gnusparse_01c                 C   s   d }g }|j t}|dd\}}t|}t||d k rtd|vrT||j t7 }|dd\}}|t| q,|j  |_t	t
|d d d |dd d |_d S )NrW  r#   r$   )r   r^   r   rL  rI   r2   r   r   r   r  r  r   )r   r{  r  r   fieldsr   rX   Znumberr8   r8   r9   r    s    zTarInfo._proc_gnusparse_10c              	   C   s   |  D ]\}}|dkr&t| d| q|dkr@t| dt| q|dkrZt| dt| q|tv r|tv rzt| |}W n ty   d}Y n0 |dkr|d}t| || q| | _d S )NzGNU.sparse.namer%   r  r'   zGNU.sparse.realsizer   r   )	r.  setattrrI   
PAX_FIELDSPAX_NUMBER_FIELDSr0   rh  r&  r  )r   r  r6   r7   rY  r2  r8   r8   r9   ry    s"    

zTarInfo._apply_pax_infoc                 C   s2   z| |dW S  ty,   | || Y S 0 d S )NrE   )r=   UnicodeDecodeError)r   r2  r6   Zfallback_encodingZfallback_errorsr8   r8   r9   r    s    zTarInfo._decode_pax_fieldc                 C   s"   t |t\}}|r|d7 }|t S )Nr#   )r]   r   )r   rd  rd   re   r8   r8   r9   rx    s    zTarInfo._blockc                 C   s
   | j tv S r   )r  REGULAR_TYPESr   r8   r8   r9   r    s    zTarInfo.isregc                 C   s   |   S r   )r  r   r8   r8   r9   isfile   s    zTarInfo.isfilec                 C   s
   | j tkS r   )r  r*  r   r8   r8   r9   r
  $  s    zTarInfo.isdirc                 C   s
   | j tkS r   )r  SYMTYPEr   r8   r8   r9   r  (  s    zTarInfo.issymc                 C   s
   | j tkS r   )r  LNKTYPEr   r8   r8   r9   r	  ,  s    zTarInfo.islnkc                 C   s
   | j tkS r   )r  rP  r   r8   r8   r9   ischr0  s    zTarInfo.ischrc                 C   s
   | j tkS r   )r  rQ  r   r8   r8   r9   isblk4  s    zTarInfo.isblkc                 C   s
   | j tkS r   )r  FIFOTYPEr   r8   r8   r9   isfifo8  s    zTarInfo.isfifoc                 C   s
   | j d uS r   )r   r   r8   r8   r9   issparse<  s    zTarInfo.issparsec                 C   s   | j tttfv S r   )r  rP  rQ  r  r   r8   r8   r9   isdev?  s    zTarInfo.isdev)r   )3rp   rq   rr   dict	__slots__r   propertyr%   setterr&   r$  r%  r   r,  r   r   r3  r/  r0  r1  classmethodrK  r9  staticmethodr:  rT  r<  rF  ro  rq  rp  rv  rr  rs  ru  r  r  r  ry  r  rx  r  r  r
  r  r	  r  r  r  r  r  r8   r8   r8   r9   r   K  s   




:

)
	

2
>
h	r   c                   @   s  e Zd ZdZdZdZdZeZe	Z
dZeZeZdZdeddZedddefd	d
ZedfddZedgddZedhddZediddZdddddZdd Zdd Zdd Zdd  Zdjd!d"Zdkdd$d%d&Zdldd'd(d)Z dmd*d+Z!d,d- Z"dnddd/d0d1Z#doddd/d3d4Z$d5d6 Z%d7d8 Z&d9d: Z'd;d< Z(d=d> Z)dpd?d@Z*dAdB Z+dCdD Z,dEdF Z-dGdH Z.dIdJ Z/dKdL Z0dMdN Z1dOdP Z2dQdR Z3dSdT Z4dqdUdVZ5dWdX Z6drdYdZZ7d[d\ Z8d]d^ Z9d_d` Z:dadb Z;dcdd Z<dS )sr   r   Fr#   Nry   r-  c              
   C   s  ddddd}||vrt d|| _|| | _|sh| jdkrTtj|sTd| _d| _t|| j}d	| _n@|d u rt|d
rt	|j
ttfr|j
}t|dr|j| _d| _|rtj|nd | _
|| _|d ur|| _|d ur|| _|d ur|| _|d ur|| _|d ur|| _|	| _|
d ur0| jtkr0|
| _ni | _|d urF|| _|d urV|| _|| _d	| _g | _d	| _| j | _i | _z| jdkrd | _ | ! | _ | jdkr2| j"| j z| j#| }| j$| W nX t%y   | j"| j Y q2Y n2 t&y, } zt't|W Y d }~n
d }~0 0 q| jdv r|d| _| jr|| j(| j) }| j*| |  jt+|7  _W n&   | js| j,  d| _ Y n0 d S )Nrbzr+bwbxbry   arz   r  !mode must be 'r', 'a', 'w' or 'x'r  rz   Fr   r   Try   r  rz   r  )-r0   r   _moder|   r%   exists	bltn_openr   r   rB  r   rE  rO   abspathr   rS   r   dereferenceignore_zerosr6   r7   r   r  debug
errorlevelcopybufsizer   members_loadedr   r   inodesfirstmemberr{  r   rq  r   ru   r   r   rK  r&  r_   r2   r   )r   r   r   r   rS   r   r  r  r6   r7   r  r  r  r  ZmodeserX   r8   r8   r9   r   `  s    






&
zTarFile.__init__c              
      s  |s|st d|dv r fdd}t j|dD ]l}t  j| }|d urV| }	z||d|fi |W   S  ttfy   |d ur||	 Y q2Y q20 q2tdnd|v r|dd	\}
}|
pd}
|pd
}| jv rt  j| }ntd| |||
|fi |S d|v r|dd	\}
}|
p4d}
|p>d
}|
dvrRt dt	||
|||}z ||
|fi |}W n   |
   Y n0 d|_|S |dv r j|||fi |S t dd S )Nznothing to open)ry   r:*c                    s    j |  dkS )Ntaropen)	OPEN_METH)r   rJ  r8   r9   not_compressed  s    z$TarFile.open.<locals>.not_compressed)keyry   z%file could not be opened successfully:r#   r   r   |rx   zmode must be 'r' or 'w'Fr  zundiscernible mode)r0   sortedr  rj   r   r   r   r   rL  r   r   r   r  )rJ  r   r   r   rc   kwargsr  r   funcZ	saved_posfilemodestreamr   r8   r  r9   r     sP    %







zTarFile.openc                 K   s$   |dvrt d| |||fi |S )Nr  r  )r0   )rJ  r   r   r   r  r8   r8   r9   r  *  s    zTarFile.taropenr   c                 K   s   |dvrt dzddlm} W n ty:   tdY n0 z|||d ||}W n, ty~   |d urx|dkrxtd Y n0 z| j|||fi |}W n@ ty   |  |dkrtd Y n   |   Y n0 d	|_	|S )
Nry   rz   r  mode must be 'r', 'w' or 'x'r   )GzipFilezgzip module is not availablerf   ry   r   F)
r0   gzipr  r   r   r   r   r  r   r   )rJ  r   r   r   compresslevelr  r  r   r8   r8   r9   gzopen2  s0    zTarFile.gzopenc              	   K   s   |dvrt dzddlm} W n ty:   tdY n0 ||pD|||d}z| j|||fi |}W nD ttfy   |  |dkrt	d Y n   |   Y n0 d	|_
|S )
Nr  r  r   )BZ2Filer   )r  ry   znot a bzip2 fileF)r0   r   r  r   r   r  r   EOFErrorr   r   r   )rJ  r   r   r   r  r  r  r   r8   r8   r9   bz2openS  s&    zTarFile.bz2openc           	   	   K   s   |dvrt dzddlm}m} W n ty>   tdY n0 ||pH|||d}z| j|||fi |}W nD |tfy   |  |dkrt	d Y n   |   Y n0 d	|_
|S )
Nr  r  r   )LZMAFiler   r   )presetry   znot an lzma fileF)r0   r   r  r   r   r   r  r  r   r   r   )	rJ  r   r   r   r  r  r  r   r   r8   r8   r9   xzopeno  s&    zTarFile.xzopenr  r  r  r  )r   r   r   r   c                 C   s   | j r
d S d| _ zp| jdv rn| jttd   |  jtd 7  _t| jt\}}|dkrn| jtt|   W | j	s| j
  n| j	s| j
  0 d S )NTr  r$   r   )r   r   r   r_   r3   r   r   r]   
RECORDSIZEr   r   )r   rd   re   r8   r8   r9   r     s    
zTarFile.closec                 C   s(   |  |d}|d u r$td| |S )Nr   zfilename %r not found)
_getmemberrh  KeyError)r   r   r   r8   r8   r9   	getmember  s    zTarFile.getmemberc                 C   s   |    | js|   | jS r   )_checkr  _loadr  r   r8   r8   r9   
getmembers  s    zTarFile.getmembersc                 C   s   dd |   D S )Nc                 S   s   g | ]
}|j qS r8   r!  )r  r   r8   r8   r9   r    r   z$TarFile.getnames.<locals>.<listcomp>)r  r   r8   r8   r9   getnames  s    zTarFile.getnamesc                 C   sZ  |  d |d ur|j}|d u r$|}tj|\}}|tjd}|d}|  }| |_	|d u r| j
stt|}qt|}nt| }d}|j}t|r|j|jf}	| j
s|jdkr|	| jv r|| j|	 krt}
| j|	 }nt}
|	d r|| j|	< nht|rt}
nVt|r"t}
nDt|r>t}
t|}n(t|rPt}
nt |rbt!}
nd S ||_||_"|j#|_$|j%|_&|
tkr|j'|_(nd|_(|j)|_*|
|_+||_,t-rzt-.|j$d |_/W n t0y   Y n0 t1rzt12|j&d |_3W n t0y   Y n0 |
tt!fv rVt4tdrVt4tdrVt5|j6|_7t8|j6|_9|S )Nawxr   r   r#   r   majorminor):r  r   r|   r%   
splitdriver   r  r  r   r   r  lstatstatfstatfilenost_modeS_ISREGst_inost_devst_nlinkr  r  r  S_ISDIRr*  S_ISFIFOr  S_ISLNKr  readlinkS_ISCHRrP  S_ISBLKrQ  r   st_uidr)   st_gidr*   st_sizer'   st_mtimer(   r  r  pwdgetpwuidr+   r  grpgetgrgidr,   r   r  st_rdevr  r  r  )r   r   arcnamer   drvr   Zstatresr  Zstmdinoder  r8   r8   r9   
gettarinfo  s    	




zTarFile.gettarinfoT)r  c                C   s*  |    |d u r| }|D ]
}|r|jd u r6td ntt|j td|jpT|j|jp^|jf  |	 sv|
 rtdd|j|jf   ntd|j  |jd u rtd ntdt|jd d   t|j| rd	nd
  |r| rtd|j  | rtd|j  t  qd S )Nz
??????????z%s/%sz%10sz%d,%dz%10dz????-??-?? ??:??:??z%d-%02d-%02d %02d:%02d:%02dr   r   r   z-> zlink to )r  r   rn   r  r  r+   r)   r,   r*   r  r  r  r  r'   r(   r   	localtimer   r
  r  r  r	  rm   )r   verboser  r   r8   r8   r9   r  )  s8    







zTarFile.listfilterc                C   sL  |  d |d u r|}| jd urFtj|| jkrF| dd|  d S | d| | ||}|d u rz| dd|  d S |d ur||}|d u r| dd|  d S | rt|d}| 	|| W d    n1 s0    Y  nb|
 r>| 	| |rHtt|D ],}| jtj||tj||||d qn
| 	| d S )	Nr  r$   ztarfile: Skipped %rr#   ztarfile: Unsupported type %rtarfile: Excluded %rr  r   )r  r   r|   r%   r  _dbgr  r  r  addfiler
  r  listdiraddr   )r   r   r  	recursiver  r   fr8   r8   r9   r  O  s6    

,

zTarFile.addc                 C   s   |  d t|}|| j| j| j}| j| |  jt	|7  _| j
}|d urt|| j|j|d t|jt\}}|dkr| jtt|   |d7 }|  j|t 7  _| j| d S )Nr  )rc   r   r#   )r  r&  r3  rS   r6   r7   r   r_   r   r2   r  r\   r'   r]   r   r3   r  r   )r   r   r   rX   rc   rd   re   r8   r8   r9   r    s    

zTarFile.addfilec                 C   sp   |d u r0| j }|d u rtS t|tr,td|S t|r<|S z
t| W S  tyj   td|dd Y n0 d S )NzrString names are not supported for TarFile.extraction_filter. Use a function such as tarfile.data_filter directly.zfilter z
 not found)	extraction_filterr  rB  rE  	TypeErrorcallable_NAMED_FILTERSr  r0   )r   r  r8   r8   r9   _get_filter_function  s    

zTarFile._get_filter_function.)numeric_ownerr  c                C   s   g }|  |}|d u r| }|D ]F}| |||}|d u r:q| rL|| | j|||  |d q|jdd dd |D ]p}tj||j	}	z,| j
||	|d | ||	 | ||	 W q| ty }
 z| |
 W Y d }
~
q|d }
~
0 0 q|d S )N	set_attrsr  c                 S   s   | j S r   r!  )r  r8   r8   r9   <lambda>  r   z$TarFile.extractall.<locals>.<lambda>T)r  reverse)r  )r  _get_extract_tarinfor
  r   _extract_onesortr|   r%   r   r   chownutimechmodr
   _handle_nonfatal_error)r   r%   r  r  r  Zdirectoriesfilter_functionr  r   dirpathr  r8   r8   r9   
extractall  s,    

zTarFile.extractallr   c                C   s4   |  |}| |||}|d ur0| |||| d S r   )r  r  r  )r   r  r%   r  r  r  r  r   r8   r8   r9   extract  s    
zTarFile.extractc              
   C   s   t |tr| |}n|}|}z|||}W n^ ttfy^ } z| | W Y d }~n6d }~0  ty } z| | W Y d }~n
d }~0 0 |d u r| dd|j	  d S |
 rt|}tj||j|_|S )Nr$   r  )rB  rE  r  r   r   _handle_fatal_errorr
   r  r  r   r	  r&  r|   r%   r   r  r  )r   r  r  r%   r   Z
unfilteredr  r8   r8   r9   r    s"    
 
zTarFile._get_extract_tarinfoc              
   C   s   |  d z"| j|tj||j||d W nZ tyZ } z| | W Y d }~n6d }~0  ty } z| 	| W Y d }~n
d }~0 0 d S )Nry   r  )
r  _extract_memberr|   r%   r   r   r   r  r
   r  )r   r   r%   r  r  r  r8   r8   r9   r  	  s    

zTarFile._extract_onec                 C   s"   | j dkr n| dd|  d S )Nr#   tarfile: %s)r  r  r   r  r8   r8   r9   r  	  s    
zTarFile._handle_nonfatal_errorc                 C   sn   | j dkr n\t|trP|jd u r6| dd|j  qj| dd|j|jf  n| ddt|j|f  d S )Nr   r#   r!  ztarfile: %s %rztarfile: %s %s)r  rB  r   filenamer  strerrorr  rp   r"  r8   r8   r9   r  	  s    


zTarFile._handle_fatal_errorc                 C   s   |  d t|tr | |}n|}| s6|jtvrB| | |S | sR|	 rzt| j
trhtdq~| | |S nd S d S )Nry   z'cannot extract (sym)link as file object)r  rB  rE  r  r  r  rw  
fileobjectr	  r  r   r   r	   extractfile_find_link_target)r   r  r   r8   r8   r9   r&  $	  s    


zTarFile.extractfilec                 C   sT  | d}|dtj}tj|}|r>tj|s>t| | sN|	 rh| 
dd|j|jf  n| 
d|j | r| || n| r| || nx| r| || nb| s| r| || nD| s|	 r| || n&|jtvr| || n| || |rP| ||| |	 sP| || | || d S )Nr   r#   z%s -> %s)rh  r   r|   r  r%   r  r  makedirsr	  r  r  r   r  r  makefiler
  makedirr  makefifor  r  makedevmakelinkr  rw  makeunknownr  r  r  )r   r   
targetpathr  r  Z	upperdirsr8   r8   r9   r   D	  s4    


zTarFile._extract_memberc                 C   s>   z&|j d u rt| nt|d W n ty8   Y n0 d S )Ni  )r   r|   mkdirFileExistsErrorr   r   r/  r8   r8   r9   r*  u	  s    
zTarFile.makedirc                 C   s   | j }||j | j}t|dl}|jd urn|jD ]"\}}|| t|||t| q4||j |	  nt|||jt| W d    n1 s0    Y  d S )Nr  )
r   r   r   r  r  r   r\   r   r'   truncate)r   r   r/  sourcerc   targetr   r'   r8   r8   r9   r)  	  s    


zTarFile.makefilec                 C   s"   |  || | dd|j  d S )Nr#   z9tarfile: Unknown file type %r, extracted as regular file.)r)  r  r  r2  r8   r8   r9   r.  	  s    zTarFile.makeunknownc                 C   s"   t tdrt| ntdd S )Nmkfifozfifo not supported by system)r   r|   r6  r
   r2  r8   r8   r9   r+  	  s    
zTarFile.makefifoc                 C   sj   t tdrt tdstd|j}|d u r.d}| rB|tjO }n
|tjO }t||t	|j
|j d S )Nmknodr,  z'special devices not supported by systemr  )r   r|   r
   r   r  r  S_IFBLKS_IFCHRr7  r,  r  r  )r   r   r/  r   r8   r8   r9   r,  	  s    
zTarFile.makedevc                 C   s   zb|  r0tj|r t| t|j| n0tj|jrNt	|j| n| 
| || W nD ty   z| 
| || W n ty   tdY n0 Y n0 d S )Nz%unable to resolve link inside archive)r  r|   r%   lexistsunlinksymlinkr  r  r  linkr   r'  symlink_exceptionr  r
   r2  r8   r8   r9   r-  	  s"    
zTarFile.makelinkc                 C   s   t tdrt dkr|j}|j}|sztrB|jrBt|jd }W n tyV   Y n0 zt	rt|j
rtt	|j
d }W n ty   Y n0 |d u rd}|d u rd}z4| rt tdrt||| nt||| W n ty   tdY n0 d S )Ngeteuidr   r$   r;   lchownzcould not change owner)r   r|   r?  r*   r)   r  r,   getgrnamr  r  r+   getpwnamr  r@  r  r   r
   )r   r   r/  r  gur8   r8   r9   r  	  s0    

zTarFile.chownc                 C   s@   |j d u rd S zt||j  W n ty:   tdY n0 d S )Nzcould not change mode)r   r|   r  r   r
   r2  r8   r8   r9   r  	  s    
zTarFile.chmodc                 C   sT   |j }|d u rd S ttds d S zt|||f W n tyN   tdY n0 d S )Nr  z"could not change modification time)r(   r   r|   r  r   r
   )r   r   r/  r(   r8   r8   r9   r  	  s    
zTarFile.utimec                 C   sl  |  d | jd ur$| j}d | _|S | j| j krZ| j| jd  | jdsZtdd }z| j	| }W qJ t
y } zD| jr| dd| j|f  |  jt7  _W Y d }~q^W Y d }~qJd }~0  tyD } z`| jr| dd| j|f  |  jt7  _W Y d }~q^n| jdkr.tt|W Y d }~qJd }~0  tyj   | jdkrftdY n ty } z$| jdkrtt|W Y d }~nd }~0  ty } ztt|W Y d }~nvd }~0  tyB } zTz.dd l}t||jrtd| n|W n ty,   |Y n0 W Y d }~n
d }~0 0 qJq^|d urb| j| nd	| _|S )
NZrar#   rZ   r$   z0x%X: %sr   z
empty filezzlib error: T)r  r  r   r   r   r   r^   r   r   rq  ru   r  r  r   rK   rE  rs   rt   rv   	Exceptionr   rB  r   r   r  r   r  )r   mr   r  r   r8   r8   r9   r{  
  sZ    

""   
zTarFile.nextc           	      C   s   |   }d}|d urFz||}W n ty8   d}Y n0 |d | }|rVtj|}t|D ]D}|rx|j|jkr^d}q^|rtj|j}n|j}||kr^|  S q^|rt|d S )NFT)	r  indexr0   r|   r%   normpathreversedr   r   )	r   r   r   	normalizer  ZskippingrG  r  member_namer8   r8   r9   r  F
  s,    

zTarFile._getmemberc                 C   s   |   }|d u r qq d| _d S r   )r{  r  r   r8   r8   r9   r  n
  s    zTarFile._loadc                 C   s:   | j rtd| jj |d ur6| j|vr6td| j d S )Nz%s is closedzbad operation for mode %r)r   r   r   rp   r   )r   r   r8   r8   r9   r  x
  s    zTarFile._checkc                 C   s`   |  r.dtd tj|j|jf}d }n
|j}|}| j||dd}|d u r\t	d| |S )Nr   T)r   rJ  zlinkname %r not found)
r  r   r  r|   r%   r  r   r  r  r  )r   r   r  limitr  r8   r8   r9   r'  
  s     zTarFile._find_link_targetc                 c   s   | j r| jE d H  d S d}| jd ur:|  }|d7 }|V  |t| jk rT| j| }n"| j sr|  }|svd| _ d S nd S |d7 }|V  q:d S )Nr   r#   T)r  r  r  r{  r2   )r   rG  r   r8   r8   r9   __iter__
  s$    
zTarFile.__iter__c                 C   s   || j krt|tjd d S )Nfile)r  rm   rk   stderr)r   levelmsgr8   r8   r9   r  
  s    
zTarFile._dbgc                 C   s   |    | S r   )r  r   r8   r8   r9   	__enter__
  s    zTarFile.__enter__c                 C   s,   |d u r|    n| js"| j   d| _d S r   )r   r   r   r   )r   r  r2  	tracebackr8   r8   r9   __exit__
  s
    

zTarFile.__exit__)Nry   NNNNNNr-  NNNN)ry   N)ry   Nr   )ry   Nr   )ry   NN)NNN)T)NT)N)r  N)r   T)TF)NF)N)=rp   rq   rr   r  r  r  r  r   rS   r   r6   r7   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,  r-  r  r  r  r{  r  r  r  r'  rM  r  rS  rU  r8   r8   r8   r9   r   D  s       
oZ 


c&3
-   
1!
?
(

	r   c                 C   sF   z,t | drt| d}nt| }|  W dS  ty@   Y dS 0 d S )Nr^   )r   TF)r   r   r   r   )r   r   r8   r8   r9   r   
  s    
r   c               	   C   s`  dd l } d}| j|d}|jdddddd	 |jd
dtdd |jdd}|jddddd |jdddddd |jdddddd |jdd dd!d | }|jr|jd u r|d"d# |j	d urJ|j	}t
|r4t|d$*}|  t| tjd% W d    n1 s0    Y  |jrFtd&| n|d"d'| n|jd ur|j}t
|rt|d(}|j|jd) W d    n1 s0    Y  n|d"d'| n|jd urt|jd"kr|jd }tj}n,t|jd*kr|j\}}n|d"|  t
|rt|d( }|j||jd+ W d    n1 sR0    Y  |jr|d,krzd-|}	nd.||}	t|	 n|d"d'| n|jd ur\|jd}
tj|
\}}d/d/d0d0d1d1d1d1d2}||v rd3||  nd4}|j}t|
|&}|D ]}|| qW d    n1 s<0    Y  |jr\td5|
 d S )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choicesrZ  T)requiredz-lz--list	<tarfile>zShow listing of a tarfile)r[  rZ  z-ez	--extract+)r^  z<output_dir>zExtract tarfile into target dir)nargsr[  rZ  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
ry   rN  z{!r} is a tar archive.z{!r} is not a tar archive.
r  )r  r$   )r%   r  r  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:rz   z{!r} file created.)argparseArgumentParseradd_argumentr  add_mutually_exclusive_group
parse_argsr  r  exittestr   r   r  rm   rk   rP  r  rS   r  r   r2   r|   curdirformat_helpr  Zcreatepopr%   splitextr  )ra  rV  parserr  argsr`   r   Ztfrh  rR  Ztar_name_extZcompressionsZtar_modeZ	tar_files	file_namer8   r8   r9   main
  s    




2
0

0

.rq  __main__)T)jversion
__author____credits__builtinsr   r  rk   r|   r   r[   r  r   rV   r&  r  warningsr  r   r  AttributeErrorNotImplementedErrorr>  r   	NameError__all__r3   r   r  r;  r6  r8  r7  rM  r  re  r  r  rP  rQ  r*  r  ZCONTTYPEr>  r=  rf  rG  rI  rt  r   r   r   r   rw  r  ri  r  r  rC  rI   r  r   r   getfilesystemencodingr:   r?   rN   rT   rY   r\   rn   rE  r   r
   r   r   r	   r   rs   rt   ru   rK   rv   rw   r   objectr   r   BufferedReaderr   r   r   r   r   r   r   r  r  r  r  r  r%  r   r   r   rq  rp   r8   r8   r8   r9   <module>    s  

	 sh
?     ~         _
