U &�.e��@sdZdZdZdZddlmZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZz ddlZWnek r�dZYnXz ddlZWnek r�dZYnXeefZzeef7ZWnek r�YnXddd d d d d ddddddddgZdZdZdZdZedZdZdZ dZ!dZ"dZ#dZ$dZ%d Z&d!Z'd"Z(d#Z)d$Z*d%Z+d&Z,d'Z-d(Z.d)Z/d*Z0d+Z1d,Z2dZ3d-Z4d.Z5e5Z6e$e%e&e'e*e+e,e(e)e-e.e/f Z7e$e%e,e/fZ8e-e.e/fZ9d/Z:d0d1d2d3hZ;eej?d5k�r�d6Z@ne�A�Z@d7d8�ZBd9d:�ZCd;d<�ZDd=e6fd>d?�ZEd@dA�ZFdedfdBdC�ZGdDdE�ZHGdFd �d eI�ZJGdGd�deJ�ZKGdHd �d eJ�ZLGdId �d eJ�ZMGdJd �d eJ�ZNGdKd�deJ�ZOGdLdM�dMeO�ZPGdNdO�dOeO�ZQGdPdQ�dQeO�ZRGdRdS�dSeO�ZSGdTdU�dUeO�ZTGdVdW�dW�ZUGdXdY�dY�ZVGdZd[�d[eW�ZXGd\d]�d]eW�ZYGd^d_�d_e jZ�Z[Gd`da�daeJ�Z\Gdbdc�dce\�Z]Gddde�dee\�Z^Gdfdg�dge\�Z_Gdhdi�die\�Z`Gdjdk�dke\�Zad{dldm�Zbdndo�Zcdpdq�Zddrds�Zeecedeedt�ZfeW�ZgGdud�deW�ZhGdvd�deW�Zidwd �ZjeijZdxdy�Zkeldzk�r ek�dS)|z,Read from and write to tar format archives. z0.9.0u"Lars Gustäbel ([email protected])u4Gustavo 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_FORMATrTz/etc/python/tarfile.cfg�i�sustar sustar00�d��0�1�2�3�4�5�6�7�L�K�S�x�g�X��)�path�linkpath�size�mtime�uid�gid�uname�gnamer%r&r+r,)Zatime�ctimer(r)r*r'�nt�utf-8cCs8|dkrtd��|�||�}|d|�|t|�tS)z8Convert a string to a null-terminated bytes object. Nzmetadata cannot contain None)� ValueError�encode�len�NUL)�s�length�encoding�errors�r8�/usr/lib64/python3.8/tarfile.py�stn�s r:cCs*|�d�}|dkr|d|�}|�||�S)z8Convert a null-terminated bytes object to a string. r�����N)�find�decode)r4r6r7�pr8r8r9�nts�s  r?cCs�|ddkrbd}tt|�d�D]}|dK}|||d7}q |ddkr�dt|�d| }n@z"t|dd�}t|��p|d d�}Wntk r�td ��YnX|S) z/Convert a number field to a python number. r)���r#�rA��ascii�strict�0�invalid header)�ranger2r?�int�stripr0�InvalidHeaderError)r4�n�ir8r8r9�nti�s   rNrBcCs�t|�}d|kr$d|dkrDnntd|d|fd�t}n�|tkr�d|d |krrd|dkr�nnV|dkr�tdg�}ntdg�}d||}t|d�D]}|�d|d@�|dL}q�ntd ��|S) z/Convert a python number to a number field. rrBr#z%0*orDrCr@rAzoverflow in number field)rI�bytesr3r� bytearrayrH�insertr0)rL�digits�formatr4rMr8r8r9�itn�s  2    rTcCs0dtt�d|��}dtt�d|��}||fS)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. rCZ 148B8x356BZ 148b8x356b)�sum�structZ unpack_from)�bufZunsigned_chksumZ signed_chksumr8r8r9� calc_chksums�s rXc Cs�|pd}|dkrdS|dkr.t�|||�dSt||�\}}t|�D],}|�|�}t|�|krf|d��|�|�qD|dkr�|�|�}t|�|kr�|d��|�|�dS)zjCopy length bytes from fileobj src to fileobj dst. If length is None, copy the entire content. i@rN�unexpected end of data)�shutil� copyfileobj�divmodrH�readr2�write) �srcZdstr5� exception�bufsize�blocks� remainder�brWr8r8r9r[�s$       r[cCs8ttjdd�}|dk r(|�|d��|�}t|dd�dS)Nr6�backslashreplace� )�end)�getattr�sys�stdoutr1r=�print)r4r6r8r8r9� _safe_printsrlc@seZdZdZdS)rzBase exception.N��__name__� __module__� __qualname__�__doc__r8r8r8r9rsc@seZdZdZdS)r z%General exception for extract errors.Nrmr8r8r8r9r sc@seZdZdZdS)rz&Exception for unreadable tar archives.Nrmr8r8r8r9rsc@seZdZdZdS)rz.Exception for unavailable compression methods.Nrmr8r8r8r9rsc@seZdZdZdS)r z=Exception for unsupported operations on stream-like TarFiles.Nrmr8r8r8r9r !sc@seZdZdZdS)r z!Base exception for header errors.Nrmr8r8r8r9r $sc@seZdZdZdS)�EmptyHeaderErrorzException for empty headers.Nrmr8r8r8r9rr'srrc@seZdZdZdS)�TruncatedHeaderErrorz Exception for truncated headers.Nrmr8r8r8r9rs*srsc@seZdZdZdS)�EOFHeaderErrorz"Exception for end of file headers.Nrmr8r8r8r9rt-srtc@seZdZdZdS)rKzException for invalid headers.Nrmr8r8r8r9rK0srKc@seZdZdZdS)�SubsequentHeaderErrorz3Exception for missing and invalid extended headers.Nrmr8r8r8r9ru3sruc@s0eZdZdZdd�Zdd�Zdd�Zdd �Zd S) � _LowLevelFilez�Low-level file object. Supports reading and writing. It is used instead of a regular file object for streaming access. cCsFtjtjtjBtjBd�|}ttd�r2|tjO}t�||d�|_dS)N��r�w�O_BINARYi�) �os�O_RDONLY�O_WRONLY�O_CREAT�O_TRUNC�hasattrrzr�fd)�self�name�moder8r8r9�__init__@s��  z_LowLevelFile.__init__cCst�|j�dS�N)r{�closer��r�r8r8r9r�Isz_LowLevelFile.closecCst�|j|�Sr�)r{r]r��r�r'r8r8r9r]Lsz_LowLevelFile.readcCst�|j|�dSr�)r{r^r��r�r4r8r8r9r^Osz_LowLevelFile.writeN)rnrorprqr�r�r]r^r8r8r8r9rv:s  rvc@sreZdZdZdd�Zdd�Zdd�Zdd �Zd d �Zd d �Z dd�Z dd�Z ddd�Z dd�Z dd�Zdd�ZdS)�_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 and is accessed blockwise. Use of gzip or bzip2 compression is possible. A stream-like object could be for example: sys.stdin, sys.stdout, a socket, a tape device etc. _Stream is intended to be used only internally. c Cs�d|_|dkrt||�}d|_|dkr6t|�}|��}|pd|fg}d|_g|_d}|j}|D]L\}}||kr||j� d||df�|j� d||||f�||7}||}qX||jkr�|j� d||jdf�dS)Nrr�FT) r��offsetr'�positionrhr�r�� map_index�mapr�)r�r�r�r'Z blockinfoZlastposZrealposr8r8r9r�hs(    z_FileInFile.__init__cCsdSr�r8r�r8r8r9r��sz_FileInFile.flushcCsdS�NTr8r�r8r8r9�readable�sz_FileInFile.readablecCsdS)NFr8r�r8r8r9�writable�sz_FileInFile.writablecCs |j��Sr�)r��seekabler�r8r8r9r��sz_FileInFile.seekablecCs|jS)z*Return the current file position. )r�r�r8r8r9r��sz_FileInFile.tellcCs�|tjkr tt|d�|j�|_nj|tjkr\|dkrFt|j|d�|_q�t|j||j�|_n.|tjkr�tt|j||j�d�|_ntd��|jS)z(Seek to a position in the file. rzInvalid argument) �io�SEEK_SET�min�maxr'r��SEEK_CUR�SEEK_ENDr0)r�r��whencer8r8r9r��s   z_FileInFile.seekc Cs |dkr|j|j}nt||j|j�}d}|dk�r|j|j\}}}}||jkr`|krhq�nq�q6|jd7_|jt|j�kr6d|_q6t|||j�}|r�|j�||j|�|j�|�}t|�|kr�t d��||7}n |t |7}||8}|j|7_q,|S)z!Read data from the file. Nr�rr#rY) r'r�r�r�r�r2r�r�r]rr3) r�r'rW�data�start�stopr�r5rdr8r8r9r]�s,     z_FileInFile.readcCs&|�t|��}||dt|��<t|�Sr�)r]r2)r�rdrWr8r8r9�readinto�sz_FileInFile.readintocCs d|_dSr�)r�r�r8r8r9r��sz_FileInFile.close)N)N)rnrorprqr�r�r�r�r�r�r�r�r�r]r�r�r8r8r8r9r�bs  r�cseZdZ�fdd�Z�ZS)� ExFileObjectcs&t|j|j|j|j�}t��|�dSr�)r�r�� offset_datar'�sparse�superr�)r��tarfile�tarinfor��� __class__r8r9r��s  �zExFileObject.__init__�rnrorpr�� __classcell__r8r8r�r9r��sr�c@s eZdZdS)� FilterErrorN)rnrorpr8r8r8r9r��sr�cseZdZ�fdd�Z�ZS)�AbsolutePathErrorcs ||_t��d|j�d��dS)Nzmember z has an absolute path�r�r�r�r��r�r�r�r8r9r��szAbsolutePathError.__init__r�r8r8r�r9r��sr�cseZdZ�fdd�Z�ZS)�OutsideDestinationErrorcs.||_||_t��|j�d|�d�d�dS)Nz would be extracted to �, � which is outside the destination�r��_pathr�r�r��r�r�r%r�r8r9r��s �z OutsideDestinationError.__init__r�r8r8r�r9r��sr�cseZdZ�fdd�Z�ZS)�SpecialFileErrorcs||_t��|j�d��dS)Nz is a special filer�r�r�r8r9r��szSpecialFileError.__init__r�r8r8r�r9r��sr�cseZdZ�fdd�Z�ZS)�AbsoluteLinkErrorcs||_t��|j�d��dS)Nz! is a symlink to an absolute pathr�r�r�r8r9r��szAbsoluteLinkError.__init__r�r8r8r�r9r��sr�cseZdZ�fdd�Z�ZS)�LinkOutsideDestinationErrorcs.||_||_t��|j�d|�d�d�dS)Nz would link to r�r�r�r�r�r8r9r��s �z$LinkOutsideDestinationError.__init__r�r8r8r�r9r��sr�cCs�i}|j}tj�|�}|�dtjf�r@|j�dtj�}|d<tj�|�rTt|��tj�tj� ||��}tj� ||g�|kr�t ||��|j }|dk r�|d@}|r�|� �s�|��r�|d@s�|dM}|dO}n|��s�|��r�d}nt|��||j kr�||d<|�r�|jdk �rd|d<|jdk �r*d|d <|jdk �r>d|d <|jdk �rRd|d <|���sf|���r�tj�|j��r~t|��|���r�tj� |tj�|�|j�}ntj� ||j�}tj�|�}tj� ||g�|k�r�t||��|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_pathZfor_data� new_attrsr�Z target_pathr�r8r8r9�_get_filtered_attrs�s^           ��  rcCs|Sr�r8)rrr8r8r9�fully_trusted_filter6sr cCs(t||d�}|r$|jf|ddi��S|S)NF�deep�rr��rrrr8r8r9� tar_filter9s r cCs(t||d�}|r$|jf|ddi��S|S)NTr Fr r r8r8r9� data_filter?s r)Z fully_trustedr�r�c@s�eZdZdZedddddddd d d d d ddddddddd�Zdidd�Zedd��Zej dd��Zedd��Z e j dd��Z dd�Z e e e e e e e e d e d!� d"d#�Z d$d%�Zeed&fd'd(�Zd)d*�Zd+d,�Zd-d.�Zed/d0��Zd1d2�Zed3d4��Zed5d6��Zed7d8��Zed9d:��Zed;d<��Zed=d>��Zd?d@�ZdAdB�Z dCdD�Z!dEdF�Z"dGdH�Z#dIdJ�Z$dKdL�Z%dMdN�Z&dOdP�Z'dQdR�Z(dSdT�Z)dUdV�Z*dWdX�Z+dYdZ�Z,d[d\�Z-d]d^�Z.d_d`�Z/dadb�Z0dcdd�Z1dedf�Z2dgdh�Z3dS)jraInformational 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. zName of the archive member.zPermission bits.z6User ID of the user who originally stored this member.z7Group ID of the user who originally stored this member.zSize in bytes.zTime of last modification.zHeader checksum.z�File 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.zDevice major number.zDevice minor number.zThe tar header starts here.zThe file's data starts here.zMA dictionary containing key-value pairs of an associated pax extended header.zSparse member information.N)r�r�r)r*r'r(�chksum�typerr+r,�devmajor�devminorr�r�� pax_headersr�r��_sparse_structs� _link_targetr�cCsj||_d|_d|_d|_d|_d|_d|_t|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_i|_dS)zXConstruct a TarInfo object. name is the optional name of the member. i�rr�N)r�r�r)r*r'r(r�REGTYPErrr+r,rrr�r�r�r�r�r�r8r8r9r�us"zTarInfo.__init__cCs|jS)z(In pax headers, "name" is called "path".�r�r�r8r8r9r%�sz TarInfo.pathcCs ||_dSr�rrr8r8r9r%�scCs|jS)z0In pax headers, "linkname" is called "linkpath".�rr�r8r8r9r&�szTarInfo.linkpathcCs ||_dSr�r)r�rr8r8r9r&�scCsd|jj|jt|�fS)Nz<%s %r at %#x>)r�rnr��idr�r8r8r9�__repr__�szTarInfo.__repr__T) r�r(r�rr)r*r+r,r �_KEEPc Cs�| rt�|�} n t�|�} || k r(|| _|| k r6|| _|| k rD|| _|| k rR|| _|| k r`|| _|| k rn|| _|| k r||| _|| k r�|| _ | S)zGReturn a deep copy of self with the given attributes replaced. ) �copyZdeepcopyr�r(r�rr)r*r+r,) r�r�r(r�rr)r*r+r,r r�resultr8r8r9r��s(  zTarInfo.replacecCs�|jdkrd}n |jd@}|j||j|j|j|j|j|j|j|j |j |j |j d� }|dt kr||d�d�s||dd7<|S)z9Return the TarInfo's attributes as a dictionary. N��) r�r�r)r*r'r(rrrr+r,rrrr�r�)r�r�r)r*r'r(rrrr+r,rr�DIRTYPEr�)r�r��infor8r8r9�get_info�s(  �zTarInfo.get_info�surrogateescapecCsz|��}|��D]\}}|dkrtd|��q|tkrD|�|||�S|tkrZ|�|||�S|tkrn|�||�Std��dS)zq*z||�dd �Wn$tk rv||||<Yq*YnXt||�|kr*||||<q*d D]`\}}||kr�d ||<q�||}d |kr�d |d kr�nn t|t �r�t |�||<d ||<q�|�r|� |t |�} nd} | |� |tdd�S)z�Return 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%rr&)r+r+� )r,r,r5rDrE))r)rB)r*rB)r'� )r(r6rrBr#r�r�)r,rrr.r-r1�UnicodeEncodeErrorr2� isinstance�float�str�_create_pax_generic_header�XHDTYPEr0r ) r�r!r6rr�Zhnamer5rR�valrWr8r8r9r's8 �   *  zTarInfo.create_pax_headercCs|�|td�S)zAReturn the object as a pax global header block sequence. r/)r;�XGLTYPE)�clsrr8r8r9�create_pax_global_header7sz TarInfo.create_pax_global_headercCs~|�d�}tdt|��D]T}d�|d|��}d�||d��}t|�||��tkrt|�||��tkrqvqtd��||fS)zUSplit a name longer than 100 chars into a prefix and a name part. r�r#Nzname is too long)�splitrHr2r�r1� LENGTH_PREFIXr.r0)r�r�r6r7Z componentsrMr+r8r8r9r/=s �zTarInfo._posix_split_namec Cs�|�d�ttfk}|r@t|�dd�d|�}t|�dd�d|�}ntdd||�}tdd||�}|�dt�}|dkrxtd��t|�d d�d ||�t|�d d�d @d|�t|�d d�d|�t|�dd�d|�t|�dd�d|�t|�dd�d|�d|t|�dd�d ||�|�dt�t|�dd�d||�t|�dd�d||�t|�dd�d|�t|�dd�d|�t|�dd�d||�g}t� dt d� |��} t | t d��d} | dd�t d| d�| dd�} | S) z�Return a header block. info is a dictionary with file information, format must be one of the *_FORMAT constants. rrrrBrr�NzTarInfo.type must not be Noner�rr�rr)r*r'r6r(s rr*r+r5r,r+rz%dsr�i����z%06orDi����)�get�CHRTYPE�BLKTYPErTr:rr0r,rVr�r�r�rXrO) r!rSr6r7Zhas_device_fieldsrrZfiletype�partsrWrr8r8r9r0Ms:  �&zTarInfo._create_headercCs.tt|�t�\}}|dkr*|t|t7}|S)zdReturn the string payload filled with zero bytes up to the next 512 byte border. r)r\r2r�r3)Zpayloadrbrcr8r8r9�_create_payloadwszTarInfo._create_payloadcCsR|�||�t}i}d|d<||d<t|�|d<t|d<|�|t||�|�|�S)zTReturn a GNUTYPE_LONGNAME or GNUTYPE_LONGLINK sequence for name. z ././@LongLinkr�rr'r*)r1r3r2r1r0r rG)r?r�rr6r7r!r8r8r9r2�s �zTarInfo._create_gnu_long_headerc Cs2d}|��D]8\}}z|�dd�Wq tk rBd}YqFYq Xq d}|rV|d7}|��D]�\}}|�d�}|r�|�|d�}n |�d�}t|�t|�d}d } } |tt| ��} | | kr�q�| } q�|tt| �d �d |d |d 7}q^i} d| d<|| d<t|�| d<t| d<|�| td d�|� |�S)z�Return a POSIX.1-2008 extended or global header sequence that contains a list of keyword, value pairs. The values must be strings. Fr/rETr�s21 hdrcharset=BINARY r#r�rrD� �=� z././@PaxHeaderr�rr'r*r�) r$r1r7r2r:rOr,r0r rG) r?rrr6Zbinary�keywordr(Zrecords�lrLr>r!r8r8r9r;�s<  ( �z"TarInfo._create_pax_generic_headerc Csvt|�dkrtd��t|�tkr(td��|�t�tkr>td��t|dd��}|t|�krbt d��|�}t |dd�||�|_ t|dd ��|_ t|d d ��|_ t|d d ��|_t|d d ��|_t|d d��|_||_|dd �|_t |d d�||�|_t |dd�||�|_t |dd�||�|_t|dd��|_t|dd��|_t |dd�||�}|jtk�r�|j �d��r�t|_|jtk�r8d}g}td�D]l} z0t|||d��} t||d|d��} Wntk �r�Y�qYnX|�| | f�|d7}�q�t|d�} t|dd��} || | f|_ |�!��rP|j �"d�|_ |�rr|jt#k�rr|d|j |_ |S)zAConstruct a TarInfo object from a 512 byte bytes object. rz empty headerztruncated headerzend of file header��z bad checksumr�l�t�|��ii i)iIiQiYi�r�i�r�r6�i�i�i�)$r2rrr�rs�countr3rtrNrXrKr?r�r�r)r*r'r(rrrr+r,rr�AREGTYPEr�r �GNUTYPE_SPARSErHr0r��boolrr�rstrip� GNU_TYPES)r?rWr6r7r�objr+r��structsrMr��numbytes� isextended�origsizer8r8r9�frombuf�sZ         zTarInfo.frombufcCs8|j�t�}|�||j|j�}|j��t|_|�|�S)zOReturn the next TarInfo object from TarFile object tarfile. ) r�r]r�r`r6r7r�r�� _proc_member)r?r�rWr[r8r8r9� fromtarfiles zTarInfo.fromtarfilecCsT|jttfkr|�|�S|jtkr,|�|�S|jtttfkrF|� |�S|� |�SdS)zYChoose the right processing method depending on the type and call it. N) rr4r3� _proc_gnulongrW� _proc_sparser<r>�SOLARIS_XHDTYPE� _proc_pax� _proc_builtin)r�r�r8r8r9ras    zTarInfo._proc_membercCsR|j��|_|j}|��s$|jtkr4||�|j�7}||_|� |j |j |j �|S)zfProcess a builtin type or an unknown type which will be treated as a regular file. ) r�r�r�r�r�SUPPORTED_TYPES�_blockr'r��_apply_pax_inforr6r7)r�r�r�r8r8r9rg&s zTarInfo._proc_builtincCs�|j�|�|j��}z|�|�}Wntk r>td��YnX|j|_|jt krft ||j |j �|_ n|jtkr�t ||j |j �|_|S)zSProcess the blocks that hold a GNU longname or longlink member. � missing or bad subsequent header)r�r]rir'rbr rur�rr4r?r6r7r�r3r)r�r�rW�nextr8r8r9rc7s  zTarInfo._proc_gnulongc Cs�|j\}}}|`|r�|j�t�}d}td�D]n}z0t|||d��}t||d|d��} Wntk rzYq�YnX|r�| r�|�|| f�|d7}q,t|d�}q||_ |j� �|_ |j |� |j �|_||_ |S)z8Process a GNU sparse header plus extra headers. r�r6rTi�)rr�r]r�rHrNr0r�rXr�r�r�rir'r�) r�r�r\r^r_rWr�rMr�r]r8r8r9rdMs(      zTarInfo._proc_sparsecCs.|j�|�|j��}|jtkr&|j}n |j��}t� d|�}|dk rX|� d�� d�|d<|� d�}|dkrr|j }nd}t�d�}d}|�||�}|s��q6|��\} } t| �} | dkr�td ��||�d �d|�d�| d�} |�| dd|j�} | tk�r|�| ||j |j�} n|�| dd|j�} | || <|| 7}q�z|�|�} Wntk �rbtd ��YnXd |k�r||�| |�nHd |k�r�|�| ||�n.|� d�dk�r�|� d�dk�r�|�| ||�|jttfk�r*| � ||j |j�|j!| _!d|k�r*| j"} | �#��s| jt$k�r$| | �| j�7} | |_!| S)zVProcess an extended or global header as described in POSIX.1-2008. s\d+ hdrcharset=([^\n]+)\nNr#r/� hdrcharsetZBINARYs(\d+) ([^=]+)=rrGr$rk�GNU.sparse.map�GNU.sparse.sizezGNU.sparse.major�1zGNU.sparse.minorrFr')%r�r]rir'rr>rr�re�search�groupr=rCr6�compile�match�groupsrIrKrgr��_decode_pax_fieldr7�PAX_NAME_FIELDSrbr ru�_proc_gnusparse_01�_proc_gnusparse_00�_proc_gnusparse_10r<rerjr�r�r�rh)r�r�rWrrvrnr6Zregexr�r5rKr(rlr�r8r8r9rfish       $ �  � �     zTarInfo._proc_paxcCshg}t�d|�D]}|�t|�d���qg}t�d|�D]}|�t|�d���q:tt||��|_dS)z?Process a GNU tar extended sparse header, version 0.0. s\d+ GNU.sparse.offset=(\d+)\nr#s\d+ GNU.sparse.numbytes=(\d+)\nN)rr�finditerr�rIrt�list�zipr�)r�rlrrWZoffsetsrvr]r8r8r9r{�szTarInfo._proc_gnusparse_00cCs@dd�|d�d�D�}tt|ddd�|ddd���|_dS)z?Process a GNU tar extended sparse header, version 0.1. cSsg|] }t|��qSr8)rI)�.0�xr8r8r9� �sz.TarInfo._proc_gnusparse_01..ro�,Nr$r#)rAr~rr�)r�rlrr�r8r8r9rz�szTarInfo._proc_gnusparse_01cCs�d}g}|j�t�}|�dd�\}}t|�}t|�|dkrtd|krT||j�t�7}|�dd�\}}|�t|��q,|j��|_t t |ddd�|ddd���|_ dS)z?Process a GNU tar extended sparse header, version 1.0. NrJr#r$) r�r]r�rArIr2r�r�r�r~rr�)r�rlrr�Zfieldsr�rWZnumberr8r8r9r|�s  zTarInfo._proc_gnusparse_10c Cs�|��D]�\}}|dkr&t|d|�q|dkr@t|dt|��q|dkrZt|dt|��q|tkr|tkr�zt||�}Wntk r�d}YnX|dkr�|�d�}t|||�q|��|_dS) zoReplace fields with supplemental information from a previous pax extended or global header. zGNU.sparse.namer%rpr'zGNU.sparse.realsizerr�N) r$�setattrrI� PAX_FIELDS�PAX_NUMBER_FIELDSr0rYrr)r�rr6r7rKr(r8r8r9rj�s"  zTarInfo._apply_pax_infocCs4z|�|d�WStk r.|�||�YSXdS)z1Decode a single field from a pax record. rEN)r=�UnicodeDecodeError)r�r(r6Zfallback_encodingZfallback_errorsr8r8r9rx szTarInfo._decode_pax_fieldcCs"t|t�\}}|r|d7}|tS)z_Round up a byte count by BLOCKSIZE and return it, e.g. _block(834) => 1024. r#)r\r�)r�rUrbrcr8r8r9riszTarInfo._blockcCs |jtkS�z4Return True if the Tarinfo object is a regular file.)r� REGULAR_TYPESr�r8r8r9r�sz TarInfo.isregcCs|��Sr�)r�r�r8r8r9�isfileszTarInfo.isfilecCs |jtkS)z!Return True if it is a directory.)rr r�r8r8r9r"sz TarInfo.isdircCs |jtkS)z%Return True if it is a symbolic link.)r�SYMTYPEr�r8r8r9r&sz TarInfo.issymcCs |jtkS)z!Return True if it is a hard link.)r�LNKTYPEr�r8r8r9r*sz TarInfo.islnkcCs |jtkS)z(Return True if it is a character device.)rrDr�r8r8r9�ischr.sz TarInfo.ischrcCs |jtkS)z$Return True if it is a block device.)rrEr�r8r8r9�isblk2sz TarInfo.isblkcCs |jtkS)zReturn True if it is a FIFO.)r�FIFOTYPEr�r8r8r9�isfifo6szTarInfo.isfifocCs |jdk Sr�)r�r�r8r8r9�issparse:szTarInfo.issparsecCs|jtttfkS)zCReturn True if it is one of character device, block device or FIFO.)rrDrEr�r�r8r8r9�isdev=sz TarInfo.isdev)r�)4rnrorprq�dict� __slots__r��propertyr%�setterr&rrr�r"rr r)r%r&r'� classmethodr@r/� staticmethodr0rGr2r;r`rbrargrcrdrfr{rzr|rjrxrir�r�rrrr�r�r�r�r�r8r8r8r9rRs��     �  1  )   2 >  h  c @s�eZdZdZdZdZdZdZeZ e Z dZ e ZeZdZdfdd �Zedddefd d ��Zedgd d ��Zedhdd��Zedidd��Zedjdd��Zddddd�Zdd�Zdd�Zdd�Zd d!�Zdkd"d#�Zdldd%�d&d'�Z dmdd(�d)d*�Z!dnd+d,�Z"d-d.�Z#doddd0�d1d2�Z$dpddd0�d4d5�Z%d6d7�Z&d8d9�Z'd:d;�Z(dd?�Z*dqd@dA�Z+dBdC�Z,dDdE�Z-dFdG�Z.dHdI�Z/dJdK�Z0dLdM�Z1dNdO�Z2dPdQ�Z3dRdS�Z4dTdU�Z5drdVdW�Z6dXdY�Z7dsdZd[�Z8d\d]�Z9d^d_�Z:d`da�Z;dbdc�Z.not_compressed)�keyNrxz%file could not be opened successfully�:r#r�r��|rwzmode must be 'r' or 'w'Fr�zundiscernible mode) r0�sortedr�rhr�rrr�rAr�r�r�r�) r?r�r�r�ra�kwargsr�r��funcZ saved_pos�filemode�streamr�r8r�r9r�sP%            z TarFile.opencKs |dkrtd��||||f|�S)zCOpen uncompressed tar archive name for reading or writing. r�r�)r0)r?r�r�r�r�r8r8r9r�(szTarFile.taropenr�cKs�|dkrtd��zddlm}Wntk r<td��YnXz|||d||�}Wn.tk r�|dk r||dkr|td ���YnXz|j|||f|�}WnBtk r�|��|dkr�td ���Yn|���YnXd |_ |S) zkOpen gzip compressed tar archive name for reading or writing. Appending is not allowed. �rxryr��mode must be 'r', 'w' or 'x'r)�GzipFilezgzip module is not availablerdNrxr�F) r0Zgzipr�r�rr�rr�r�r�)r?r�r�r�� compresslevelr�r�r�r8r8r9�gzopen0s0zTarFile.gzopenc Ks�|dkrtd��zddlm}Wntk r<td��YnX||pF|||d�}z|j|||f|�}WnFttfk r�|��|dkr�t d���Yn|���YnXd |_ |S) zlOpen bzip2 compressed tar archive name for reading or writing. Appending is not allowed. r�r�r)�BZ2Filer�)r�rxznot a bzip2 fileF) r0r�r�r�rr�r��EOFErrorr�rr�)r?r�r�r�r�r�r�r�r8r8r9�bz2openQs&zTarFile.bz2openc Ks�|dkrtd��zddlm}m}Wntk r@td��YnX||pJ|||d�}z|j|||f|�}WnF|tfk r�|��|dkr�t d���Yn|���YnXd |_ |S) zkOpen lzma compressed tar archive name for reading or writing. Appending is not allowed. r�r�r)�LZMAFiler�r�)�presetrxznot an lzma fileF) r0r�r�r�r�rr�r�r�rr�) r?r�r�r�r�r�r�r�r�r8r8r9�xzopenms&zTarFile.xzopenr�r�r�r�)r�r�r�r�cCs�|jr dSd|_z`|jdkrn|j�ttd�|jtd7_t |jt �\}}|dkrn|j�tt |�W5|js�|j��XdS)zlClose the TarFile. In write-mode, two finishing zero blocks are appended to the archive. NTr�r$r) r�r�r�r�r�r^r3r�r�r\� RECORDSIZE)r�rbrcr8r8r9r��s z TarFile.closecCs"|�|�}|dkrtd|��|S)aReturn 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. Nzfilename %r not found)� _getmember�KeyError)r�r�r�r8r8r9� getmember�s  zTarFile.getmembercCs|��|js|��|jS)z�Return the members of the archive as a list of TarInfo objects. The list has the same order as the members in the archive. )�_checkr��_loadr�r�r8r8r9� getmembers�szTarFile.getmemberscCsdd�|��D�S)z�Return the members of the archive as a list of their names. It has the same order as the list returned by getmembers(). cSsg|] }|j�qSr8r)r�r�r8r8r9r��sz$TarFile.getnames..)r�r�r8r8r9�getnames�szTarFile.getnamesc Cs^|�d�|dk r|j}|dkr$|}tj�|�\}}|�tjd�}|�d�}|��}||_ |dkr�|j stt� |�}q�t� |�}nt� |���}d}|j}t �|�r�|j|jf} |j s�|jdkr�| |jkr�||j| kr�t} |j| }nt} | dr�||j| <nht �|��rt} nVt �|��r"t} nDt �|��r>t} t�|�}n(t �|��rPt} nt � |��rbt!} ndS||_||_"|j#|_$|j%|_&| tk�r�|j'|_(nd|_(|j)|_*| |_+||_,t-�r�zt-�.|j$�d|_/Wnt0k �r�YnXt1�rzt1�2|j&�d|_3Wnt0k �rYnX| tt!fk�rZt4td��rZt4td��rZt�5|j6�|_7t�8|j6�|_9|S) 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. �awxNr�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_ISCHRrD�S_ISBLKrEr��st_uidr)�st_gidr*�st_sizer'�st_mtimer(rr�pwd�getpwuidr+r��grpZgetgrgidr,r�r��st_rdevrr�r) r�r��arcnamer�Zdrvr�ZstatresrZstmd�inoderr8r8r9� gettarinfo�s�      � �          zTarFile.gettarinfoT)r�cCs*|��|dkr|}|D�] }|r�|jdkr6td�ntt�|j��td|jpT|j|jp^|jf�|� �sv|� �r�tdd|j |j f�ntd|j �|jdkr�td�ntdt�|j�dd ��t|j|��r�d nd �|�r|���rtd |j�|���rtd |j�t�qdS)aPrint 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 ??????????z%s/%sz%10sz%d,%dz%10dz????-??-?? ??:??:??z%d-%02d-%02d %02d:%02d:%02dr�r�r�z-> zlink to )r�r�rlr�r�r+r)r,r*r�r�rrr'r(r�� localtimer�rrrrrk)r��verboser�r�r8r8r9r~'s8    ��  �  z TarFile.list��filterc Cs6|�d�|dkr|}|jdk rFtj�|�|jkrF|�dd|�dS|�d|�|�||�}|dkrz|�dd|�dS|dk r�||�}|dkr�|�dd|�dS|��r�t|d��}|� ||�W5QRXn`|� ��r(|� |�|�r2t t� |��D]*}|j tj�||�tj�||�||d �q�n |� |�dS) 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$ztarfile: Skipped %rr#ztarfile: Unsupported type %r�tarfile: Excluded %rr�r�)r�r�r{r%r��_dbgr�r�r��addfilerr��listdir�addr�)r�r�r�� recursiver�r��fr8r8r9r�Ms8      � z TarFile.addcCs�|�d�t�|�}|�|j|j|j�}|j�|�|jt |�7_|j }|dk r�t ||j|j |d�t |j t�\}}|dkr�|j�tt|�|d7}|j|t7_|j�|�dS)aAdd 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)rarr#)r�rr)rSr6r7r�r^r�r2r�r[r'r\r�r3r�r�)r�r�r�rWrarbrcr8r8r9r��s   zTarFile.addfilec CsB|dkr�|j}|dkr�tj�d�}|dkr�z tt�}Wntk rJYnBXddl}|jddd�}|�|� |�W5QRX|jdddd�}|r�z t |}Wn&t k r�t d|�d ��d�YnX||_|St r�t�d t�tStSt|t�r�td ��|St|��r |Sz t |WSt k �r<t d|�d ��d�YnXdS) NZ PYTHON_TARFILE_EXTRACTION_FILTERr)�#)Z interpolationZcomment_prefixesr�r�)Zfallbackzfilter z not foundaThe default behavior of tarfile extraction has been changed to disallow common exploits (including CVE-2007-4559). By default, absolute/parent paths are disallowed and some mode bits are cleared. See https://access.redhat.com/articles/7004769 for more details.zrString names are not supported for TarFile.extraction_filter. Use a function such as tarfile.data_filter directly.)�extraction_filterr{�environrCr��_CONFIG_FILENAME�FileNotFoundError� configparserZ ConfigParserZ read_file�_NAMED_FILTERSr�r0�_RH_SAFER_DEFAULT�warnings�warn�RuntimeWarningr r r8r:� TypeError�callable)r�r�r��filer�Zconfr8r8r9�_get_filter_function�sZ  �� �  �  zTarFile._get_filter_function�.)� numeric_ownerr�c Cs�g}|�|�}|dkr|}|D]F}|�|||�}|dkr:q|��rL|�|�|j|||�� |d�q|jdd�dd�|D]n}tj�||j �} z,|j || |d�|� || �|� || �Wq|t k r�} z|�| �W5d} ~ XYq|Xq|dS)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_attrsrcSs|jSr�r)r�r8r8r9��r�z$TarFile.extractall..T)r��reverse)r)r�_get_extract_tarinforr�� _extract_one�sortr{r%r�r��chown�utime�chmodr �_handle_nonfatal_error) r�r%r�rr�Z directories�filter_functionrr��dirpathr�r8r8r9� extractall�s,  � zTarFile.extractallr�cCs4|�|�}|�|||�}|dk r0|�||||�dS)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)rr r )r�rr%rrr�rr�r8r8r9�extract�s zTarFile.extractc Cs�t|t�r|�|�}n|}|}z|||�}WnZttfk r\}z|�|�W5d}~XYn,tk r�}z|�|�W5d}~XYnX|dkr�|�dd|j �dS|� �r�t � |�}t j �||j�|_|S)z@Get filtered TarInfo (or None) from member, which might be a strNr$r�)r8r:r�r�r��_handle_fatal_errorr rr�r�rrr{r%r�rr)r�rrr%r�Z unfilteredr�r8r8r9r  s"   zTarFile._get_extract_tarinfoc Cs�|�d�z"|j|tj�||j�||d�WnVtk rX}z|�|�W5d}~XYn,tk r�}z|� |�W5d}~XYnXdS)z%Extract from filtered tarinfo to diskrxrN) r��_extract_memberr{r%r�r�r�rr r)r�r�r%rrr�r8r8r9r % s � zTarFile._extract_onecCs"|jdkr�n|�dd|�dS)z=Handle non-fatal error (ExtractError) according to errorlevelr#� tarfile: %sN)r�r��r�r�r8r8r9r2 s zTarFile._handle_nonfatal_errorcCsn|jdkr�n\t|t�rP|jdkr6|�dd|j�qj|�dd|j|jf�n|�ddt|�j|f�dS)z1Handle "fatal" error according to self.errorlevelrNr#rztarfile: %s %rztarfile: %s %s)r�r8r��filenamer��strerrorrrnrr8r8r9r9 s   zTarFile._handle_fatal_errorcCs�|�d�t|t�r |�|�}n|}|��s6|jtkrB|�||�S|��sR|� �rzt|j t �rht d��q~|� |�|��SndSdS)z�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. Otherwise, None is returned. rxz'cannot extract (sym)link as file objectN)r�r8r:r�r�rrh� fileobjectrrr�r�r � extractfile�_find_link_target)r�rr�r8r8r9rE s      zTarFile.extractfilecCsT|�d�}|�dtj�}tj�|�}|r>tj�|�s>t�|�|��sN|� �rh|� dd|j |j f�n|� d|j �|� �r�|�||�n�|��r�|�||�nx|��r�|�||�nb|��s�|��r�|�||�nD|��s�|� �r�|�||�n&|jtk�r|�||�n |�||�|�rP|�|||�|� ��sP|�||�|�||�dS)z\Extract the TarInfo object tarinfo to a physical file called targetpath. r�r#z%s -> %sN)rYr�r{r�r%rr��makedirsrrr�r�rr��makefiler�makedirr��makefifor�r��makedev�makelinkrrh� makeunknownrrr)r�r�� targetpathrrZ upperdirsr8r8r9rd s4       zTarFile._extract_membercCs@z&|jdkrt�|�n t�|d�Wntk r:YnXdS)z,Make a directory called targetpath. Ni�)r�r{�mkdir�FileExistsError�r�r�r&r8r8r9r!� s   zTarFile.makedirc Cs�|j}|�|j�|j}t|d��b}|jdk rn|jD]"\}}|�|�t|||t|�q4|�|j�|� �nt|||jt|�W5QRXdS)z'Make a file called targetpath. r�N) r�r�r�r�r�r�r[rr'�truncate)r�r�r&�sourcera�targetr�r'r8r8r9r � s      zTarFile.makefilecCs"|�||�|�dd|j�dS)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�rr)r8r8r9r%� s �zTarFile.makeunknowncCs"ttd�rt�|�ntd��dS)z'Make a fifo called targetpath. �mkfifozfifo not supported by systemN)r�r{r-r r)r8r8r9r"� s  zTarFile.makefifocCsjttd�rttd�std��|j}|dkr.d}|��rB|tjO}n |tjO}t�||t� |j |j ��dS)zrnrorprqr�r�r�r�rrSr r6r7rr�r�rr�r�r�r�rr�r�r�r�r�r�r�r�r�r�r~r�r�rrrr r rrrrr!r r%r"r#r$rrrrlr�r�r�rrBr�rGrIr8r8r8r9rBs�� oZ    �    c&3 5� -�   � 1! 6 ( cCs2zt|�}|��WdStk r,YdSXdS)zfReturn True if name points to a tar archive that we are able to handle, else return False. TFN)rr�r)r�r�r8r8r9r� s c Csddl}d}|j|d�}|jdddddd �|jd d td d �|jdd�}|jddddd�|jdddddd�|jdddddd�|jdd dd!d�|��}|jr�|jdkr�|�d"d#�|j dk �r4|j }t |��rt |d$�� }|� �t |� �tjd%�W5QRX|j�r0t d&�|��n|�d"d'�|���n�|jdk �r�|j}t |��rxt� |d(��}|j|jd)�W5QRXn|�d"d'�|���nv|jdk �rbt|j�d"k�r�|jd}tj}n,t|j�d*k�r�|j\}}n|�d"|���t |��rNt� |d(��}|j||jd+�W5QRX|j�r`|d,k�r8d-�|�} n d.�||�} t | �n|�d"d'�|��n�|jdk �r|j�d�} tj�| �\} } d/d/d0d0d1d1d1d1d2�} | | k�r�d3| | nd4}|j}t� | |��}|D]}|�|��q�W5QRX|j�rt d5�| ��dS)6Nrz3A simple command-line interface for tarfile module.)� descriptionz-vz --verbose� store_trueFzVerbose output)�action�default�helpz--filterz zFilter for extraction)�metavar�choicesrNT)Zrequiredz-lz--list� zShow listing of a tarfile)rOrNz-ez --extract�+)rQz zExtract tarfile into target dir)�nargsrOrNz-cz--create)zzzCreate tarfile from sourcesz-tz--testzTest if a tarfile is validr#z&--filter is only valid for extraction rxrCz{!r} is a tar archive.z{!r} is not a tar archive. r�)r�r$)r%r�rz{!r} file is extracted.z+{!r} file is extracted into {!r} directory.r�r�r�)r�z.tgzz.xzz.txzz.bz2z.tbzz.tbz2z.tb2zw:ryz{!r} file created.)�argparse�ArgumentParser� add_argumentr�Zadd_mutually_exclusive_group� parse_argsr�r�exitZtestrrr�rkrirDr�rSr~rr2r{�curdirZ format_helprZcreate�popr%�splitextr�)rTrJ�parserrt�argsr_r�ZtfrYrFZtar_name�_ZextZ compressionsZtar_modeZ tar_files� file_namer8r8r9�main� s�  ��  � � � �            �   � r`�__main__)T)mrq�version� __author__� __credits__�builtinsrr�rir{r�rZr�r�rVrrrr�r�r�r��AttributeError�NotImplementedErrorr5r�� NameError�__all__r�r�r3r�r�r1r,r.r-rBrrVr�r�rDrEr r�ZCONTTYPEr4r3rWr<r>rer rrrrhr�rZr�ryr9rIr�r�r �getfilesystemencodingr:r?rNrTrXr[rl� Exceptionrr rrr r rrrsrtrKrurvr��objectr�r��BufferedReaderr�r�r�r�r�r�r�rr r rr�rrrrr`rnr8r8r8r9�s<      ���� �  sh  ?� u) _