3 \dh'@sBdZdZdZdZdZdZddlmZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZy ddlZWnek rdZYnXy ddlZWnek rdZYnXeefZyeef7ZWnek rYnXd d d d d ddddddddddgZdZdZdZdZedZ dZ!dZ"dZ#dZ$d Z%d!Z&dZ'd"Z(d#Z)d$Z*d%Z+d&Z,d'Z-d(Z.d)Z/d*Z0d+Z1d,Z2d-Z3d.Z4dZ5d/Z6d0Z7e6Z8e&e'e(e)e,e-e.e*e+e/e0e1f Z9e&e'e.e1fZ:e/e0e1fZ;dZe>e>e?e?e?d9Z@e jAd:krd;ZBne jCZBdd?ZEd@dAZFdBe8fdCdDZGdEdFZHdedfdGdHZIdIdJZJdKdLZKGdMd d eLZMGdNddeMZNGdOd d eMZOGdPddeMZPGdQddeMZQGdRddeMZRGdSdTdTeRZSGdUdVdVeRZTGdWdXdXeRZUGdYdZdZeRZVGd[d\d\eRZWGd]d^d^ZXGd_d`d`ZYGdadbdbeZZ[GdcddddeZZ\Gdedfdfe j]Z^GdgdhdheMZ_Gdidjdje_Z`Gdkdldle_ZaGdmdndne_ZbGdodpdpe_ZcGdqdrdre_ZdGdsdtdte_Zee_eeNfZfddudvZgdwdxZhdydzZid{d|Zjeheiejd}ZkeZZlejmd~ZnGdd d eZZoGdd d eZZpdd ZqepjZddZresdkr>erdS)z,Read from and write to tar format archives. z0.9.0u"Lars Gustäbel (lars@gustaebel.de)z5$Date: 2011-02-25 17:42:01 +0200 (Fri, 25 Feb 2011) $z?$Id: tarfile.py 88586 2011-02-25 15:42:01Z marc-andre.lemburg $u4Gustavo Niemeyer, Niels Gustäbel, Richard Townsend.)openNTarFileTarInfo is_tarfileTarError ReadErrorCompressionError StreamError ExtractError HeaderErrorENCODING USTAR_FORMAT GNU_FORMAT PAX_FORMATDEFAULT_FORMATrTz/etc/python/tarfile.cfgisustar sustar00d01234567LKSxgXpathlinkpathsizemtimeuidgidunamegname)ZatimeZctimer(r)r*r'ntzutf-8cCs8|dkrtd|j||}|d||t|tS)z8Convert a string to a null-terminated bytes object. Nzmetadata cannot contain None) ValueErrorencodelenNUL)slengthencodingerrorsr6/usr/lib64/python3.6/tarfile.pystns r8cCs*|jd}|dkr|d|}|j||S)z8Convert a null-terminated bytes object to a string. rr#N)finddecode)r2r4r5pr6r6r7ntss  r=c Cs|dd krfd}x0tt|dD]}|dK}|||d7}q"W|ddkrdt|d| }n@y"t|dd}t|jpd d}Wntk rtd YnX|S) z/Convert a number field to a python number. rr#asciistrict0zinvalid header)r>r?)ranger0r=intstripr.InvalidHeaderError)r2nir6r6r7ntis   rKr@cCst|}d|ko"d|dknrDtd|d|fdt}n|tkrd|d |kopd|dknr|dkrtdg}ntdg}d||}x6t|dD]}|jd|d@|dL}qWntd |S) z/Convert a python number to a number field. rr@r#z%0*orBrAr>r?zoverflow in number field)rFbytesr1r bytearrayrEinsertr.)rIdigitsformatr2rJr6r6r7itns  2   rQcCs0dttjd|}dttjd|}||fS)aCalculate 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. rAZ 148B8x356BZ 148b8x356b)sumstructZ unpack_from)bufZunsigned_chksumZ signed_chksumr6r6r7 calc_chksumss rUc Cs|pd}|dkrdS|dkr.tj|||dSt||\}}x8t|D],}|j|}t||krh|d|j|qFW|dkr|j|}t||kr|d|j|dS)zjCopy length bytes from fileobj src to fileobj dst. If length is None, copy the entire content. irNzunexpected end of datai@)shutil copyfileobjdivmodrEreadr0write) srcdstr3 exceptionbufsizeblocks remainderbrTr6r6r7rXs$     rXcCs ddl}|jdtdtj|S)z/Deprecated in this location; use stat.filemode.rNz$deprecated in favor of stat.filemoder$)warningswarnDeprecationWarningstatfilemode)modercr6r6r7rgsrgcCs8ttjdd}|dk r(|j|dj|}t|dddS)Nr4backslashreplace )end)getattrsysstdoutr/r;print)r2r4r6r6r7 _safe_printsrpc@seZdZdZdS)rzBase exception.N)__name__ __module__ __qualname____doc__r6r6r6r7rsc@seZdZdZdS)r z%General exception for extract errors.N)rqrrrsrtr6r6r6r7r !sc@seZdZdZdS)rz&Exception for unreadable tar archives.N)rqrrrsrtr6r6r6r7r$sc@seZdZdZdS)rz.Exception for unavailable compression methods.N)rqrrrsrtr6r6r6r7r'sc@seZdZdZdS)r z=Exception for unsupported operations on stream-like TarFiles.N)rqrrrsrtr6r6r6r7r *sc@seZdZdZdS)r z!Base exception for header errors.N)rqrrrsrtr6r6r6r7r -sc@seZdZdZdS)EmptyHeaderErrorzException for empty headers.N)rqrrrsrtr6r6r6r7ru0sruc@seZdZdZdS)TruncatedHeaderErrorz Exception for truncated headers.N)rqrrrsrtr6r6r6r7rv3srvc@seZdZdZdS)EOFHeaderErrorz"Exception for end of file headers.N)rqrrrsrtr6r6r6r7rw6srwc@seZdZdZdS)rHzException for invalid headers.N)rqrrrsrtr6r6r6r7rH9srHc@seZdZdZdS)SubsequentHeaderErrorz3Exception for missing and invalid extended headers.N)rqrrrsrtr6r6r6r7rx<srxc@s0eZdZdZddZddZddZdd Zd S) _LowLevelFilezLow-level file object. Supports reading and writing. It is used instead of a regular file object for streaming access. cCsFtjtjtjBtjBd|}ttdr2|tjO}tj||d|_dS)N)rwO_BINARYi) osO_RDONLYO_WRONLYO_CREATO_TRUNChasattrr|rfd)selfnamerhr6r6r7__init__Is   z_LowLevelFile.__init__cCstj|jdS)N)r}closer)rr6r6r7rRsz_LowLevelFile.closecCstj|j|S)N)r}rZr)rr'r6r6r7rZUsz_LowLevelFile.readcCstj|j|dS)N)r}r[r)rr2r6r6r7r[Xsz_LowLevelFile.writeN)rqrrrsrtrrrZr[r6r6r6r7ryCs  ryc@steZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ dddZ dddZ ddZddZdS)_StreamaClass 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 Csd|_|dkrt||}d|_|dkr6t|}|j}|pt|j|jkrL|jj|jd|j|j|jd|_qWdS)z]Write string s to the stream if a whole new block is ready to be written. N)rTr0r_rr[)rr2r6r6r7Z__writesz_Stream.__writec Cs|jr dSd|_z|jdkr:|jdkr:|j|jj7_|jdkr|jr|jj|jd|_|jdkr|jjtj d|j |jjtj d|j d@Wd|j s|jj XdS) z[Close the _Stream object. No operation should be done on it afterwards. NTr{rrrzd|fg}d|_g|_d}|j}xT|D]L\}}||kr~|jj d||df|jj d||||f||7}||}qZW||jkr|jj d||jdfdS)NrrFT) roffsetr'positionrlrr map_indexmapr)rrrr'Z blockinfoZlastposZrealposr6r6r7rus(   z_FileInFile.__init__cCsdS)Nr6)rr6r6r7rsz_FileInFile.flushcCsdS)NTr6)rr6r6r7readablesz_FileInFile.readablecCsdS)NFr6)rr6r6r7writablesz_FileInFile.writablecCs |jjS)N)rseekable)rr6r6r7rsz_FileInFile.seekablecCs|jS)z*Return the current file position. )r)rr6r6r7rsz_FileInFile.tellcCs|tjkr tt|d|j|_nj|tjkr\|dkrFt|j|d|_qt|j||j|_n.|tjkrtt|j||jd|_ntd|jS)z(Seek to a position in the file. rzInvalid argument) ioSEEK_SETminmaxr'rSEEK_CURSEEK_ENDr.)rrwhencer6r6r7rs   z_FileInFile.seekc Cs|dkr|j|j}nt||j|j}d}x|dkr xZ|j|j\}}}}||jkob|knrlPq:|jd7_|jt|jkr:d|_q:Wt|||j}|r|jj||j||jj|}t||krt d||7}n |t |7}||8}|j|7_q.W|S)z!Read data from the file. Nrrr#zunexpected end of data) r'rrrrr0rrrZrr1) rr'rTdatastartstoprr3rbr6r6r7rZs.      z_FileInFile.readcCs&|jt|}||dt|<t|S)N)rZr0)rrbrTr6r6r7readintosz_FileInFile.readintocCs d|_dS)NT)r)rr6r6r7rsz_FileInFile.close)N)N)rqrrrsrtrrrrrrrrrrZrrr6r6r6r7ros  rcseZdZfddZZS) ExFileObjectcs&t|j|j|j|j}tj|dS)N)rr offset_datar'sparsesuperr)rtarfiletarinfor) __class__r6r7rs  zExFileObject.__init__)rqrrrsr __classcell__r6r6)rr7rsrc@s eZdZdS) FilterErrorN)rqrrrsr6r6r6r7rsrcseZdZfddZZS)AbsolutePathErrorcs ||_tjd|jddS)Nzmember z has an absolute path)rrrr)rr)rr6r7rszAbsolutePathError.__init__)rqrrrsrrr6r6)rr7rsrcseZdZfddZZS)OutsideDestinationErrorcs.||_||_tj|jd|dddS)Nz would be extracted to z, z which is outside the destination)r_pathrrr)rrr%)rr6r7rsz OutsideDestinationError.__init__)rqrrrsrrr6r6)rr7rsrcseZdZfddZZS)SpecialFileErrorcs||_tj|jddS)Nz is a special file)rrrr)rr)rr6r7rszSpecialFileError.__init__)rqrrrsrrr6r6)rr7rsrcseZdZfddZZS)AbsoluteLinkErrorcs||_tj|jddS)Nz! is a symlink to an absolute path)rrrr)rr)rr6r7rszAbsoluteLinkError.__init__)rqrrrsrrr6r6)rr7rsrcseZdZfddZZS)LinkOutsideDestinationErrorcs.||_||_tj|jd|dddS)Nz would link to z, z which is outside the destination)rrrrr)rrr%)rr6r7rsz$LinkOutsideDestinationError.__init__)rqrrrsrrr6r6)rr7rsrcseZdZfddZZS)LinkFallbackErrorcs2||_||_tjd|jdd|ddS)Nzlink z would be extracted as a zcopy of z, which was rejected)rrrrr)rrr%)rr6r7rszLinkFallbackError.__init__)rqrrrsrrr6r6)rr7rsrcCs$i}|j}tjj|tjjd}|jdtjfrH|jjdtj}|d<tjj|r\t |tjjtjj ||tjjd}tjj ||g|krt |||j }|dk r|d@}|r|js|jr|d@s|dM}|dO}n|js|jrd}nt|||j kr||d<|r |jdk r,d|d <|jdk r@d|d <|jdk rTd|d <|jdk rhd|d <|js||jr tjj|jrt|tjj|j}||jkr||d <|jrtjj |tjj||j}ntjj ||j}tjj|tjjd}tjj ||g|kr t|||S)N)rC/ri@Iirhr)r*r+r,linknamei)rr}r%realpath ALLOW_MISSINGrseplstripisabsrr commonpathrrhisregislnkisdirissymrr)r*r+r,rrnormpathdirnamer)member dest_pathZfor_data new_attrsrZ target_pathrhZ normalizedr6r6r7_get_filtered_attrssd                rcCs|S)Nr6)rrr6r6r7fully_trusted_filterTsrcCs(t||d}|r$|jf|ddiS|S)NFdeep)rr)rrrr6r6r7 tar_filterWs rcCs(t||d}|r$|jf|ddiS|S)NTrF)rr)rrrr6r6r7 data_filter]s r)Z fully_trustedrrs([0-9]{1,20}) c@seZdZdZdmZdnddZddZddZeeeZ ddZ dd Z ee e Z d!d"Z eeeeeeeed#ed$ d%d&Zd'd(Zeed)fd*d+Zd,d-Zd.d/Zd0d1Zed2d3Zd4d5Zed6d7Zed8d9Zed:d;Zedd?Zed@dAZ dBdCZ!dDdEZ"dFdGZ#dHdIZ$dJdKZ%dLdMZ&dNdOZ'dPdQZ(dRdSZ)dTdUZ*dVdWZ+dXdYZ,dZd[Z-d\d]Z.d^d_Z/d`daZ0dbdcZ1dddeZ2dfdgZ3dhdiZ4djdkZ5dlS)oraInformational 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. rrhr)r*r'r(chksumtyperr+r,devmajordevminorrr pax_headersrr_sparse_structs _link_targetrcCsj||_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. irrN)rrhr)r*r'r(rREGTYPErrr+r,rrrrrr)rrr6r6r7rs"zTarInfo.__init__cCs|jS)N)r)rr6r6r7_getpathszTarInfo._getpathcCs ||_dS)N)r)rrr6r6r7_setpathszTarInfo._setpathcCs|jS)N)r)rr6r6r7 _getlinkpathszTarInfo._getlinkpathcCs ||_dS)N)r)rrr6r6r7 _setlinkpathszTarInfo._setlinkpathcCsd|jj|jt|fS)Nz<%s %r at %#x>)rrqrid)rr6r6r7__repr__szTarInfo.__repr__T) rr(rhrr)r*r+r,r_KEEPc Cs| rtj|} n tj|} || 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. ) copydeepcopyrr(rhrr)r*r+r,) rrr(rhrr)r*r+r,rr resultr6r6r7rs(  zTarInfo.replacecCs|jdkrd}n |jd@}|j||j|j|j|j|j|j|j|j |j |j |j d }|dt kr~|djd r~|dd7<|S)z9Return the TarInfo's attributes as a dictionary. Ni) rrhr)r*r'r(rrrr+r,rrrrr)rhrr)r*r'r(rrrr+r,rrDIRTYPEr)rrhinfor6r6r7get_infos&   zTarInfo.get_infosurrogateescapecCs~|j}x(|jD]\}}|dkrtd|qW|tkrH|j|||S|tkr^|j|||S|tkrr|j||StddS)ztdt|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||}|jtkr|j jdrt|_|jtkr6d}g}xrtdD]f} y0t|||d} t||d|d} Wntk rPYnX|j| | f|d7}qWt|d} t|dd} || | f|_ |j!rN|j j"d|_ |rp|jt#krp|d|j |_ |S)zAConstruct a TarInfo object from a 512 byte bytes object. rz empty headerztruncated headerzend of file headerz bad checksumrlt|ii i)iIiQiYirirr$iii)$r0rurrvcountr1rwrKrUrHr=rrhr)r*r'r(rrrr+r,rrAREGTYPErrGNUTYPE_SPARSErEr.rboolrrrstrip GNU_TYPES)r1rTr4r5robjrrstructsrJrnumbytes isextendedorigsizer6r6r7frombufsZ       zTarInfo.frombufcCs8|jjt}|j||j|j}|jjt|_|j|S)zOReturn the next TarInfo object from TarFile object tarfile. ) rrZrrRr4r5rr _proc_member)r1rrTrMr6r6r7 fromtarfile s zTarInfo.fromtarfilecCsT|jttfkr|j|S|jtkr,|j|S|jtttfkrF|j |S|j |SdS)zYChoose the right processing method depending on the type and call it. N) rr"r! _proc_gnulongrI _proc_sparser.r0SOLARIS_XHDTYPE _proc_pax _proc_builtin)rrr6r6r7rS!s    zTarInfo._proc_membercCsR|jj|_|j}|js$|jtkr4||j|j7}||_|j |j |j |j |S)zfProcess a builtin type or an unknown type which will be treated as a regular file. ) rrrrrSUPPORTED_TYPES_blockr'r_apply_pax_inforr4r5)rrrr6r6r7rY.s zTarInfo._proc_builtinc Cs|jj|j|j}y|j|}Wntk r>tdYnX|j|_|jt krft ||j |j |_ n|jtkrt ||j |j |_|S)zSProcess the blocks that hold a GNU longname or longlink member. z missing or bad subsequent header)rrZr[r'rTr rxrrr"r=r4r5rr!r)rrrTnextr6r6r7rU?s  zTarInfo._proc_gnulongc Cs|j\}}}|`x|r|jjt}d}xvtdD]j}y0t|||d}t||d|d} Wntk rzPYnX|r| r|j|| f|d7}q0Wt|d}qW||_ |jj |_ |j |j |j |_||_ |S)z8Process a GNU sparse header plus extra headers. rr$rFi)rrrZrrErKr.rrJrrrr[r'r) rrrNrPrQrTrrJrrOr6r6r7rVUs(    zTarInfo._proc_sparsec Cs|jj|j|j}|jtkr&|j}n |jj}d}d}g}x0t||koV||dkrnt j ||}|srt dyt |j d}Wntk rt dYnX|dkrt d||t|krt d|jd|d} ||jdd| } | jd\} } } | s"| dks"|| dkr*t d|j|| | f| dkrd|dkrd| d kr`|j}nd }||7}q@W|dkr~d }x`|D]X\}} } |j| d d |j}|tkr|j| ||j|j}n|j| d d |j}|||<qWy|j|}Wntk r td YnXd |kr&|j||nFd |kr>|j||n.|jddkrl|jddkrl|j||||jttfkr|j ||j|j|j!|_!d|kr|j"}|j#s|jt$kr||j|j7}||_!|S)zVProcess an extended or global header as described in POSIX.1-2008. rNzinvalid headerr#r;rs hdrcharsetsBINARYzutf-8z missing or bad subsequent headerzGNU.sparse.mapzGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorrDr')%rrZr[r'rr0rr r0_header_length_prefix_rematchrHrFgroupr.rrk partitionrr4_decode_pax_fieldr5PAX_NAME_FIELDSrTr rx_proc_gnusparse_01_proc_gnusparse_00r5_proc_gnusparse_10r.rWr\rrrrZ)rrrTrrr4 raw_headersrbr3Zheader_value_end_offsetZkeyword_and_valueZ raw_keywordZequalsZ raw_valuer=rr]rr6r6r7rXqsx                 zTarInfo._proc_paxc Csg}g}x|D]\}}}|dkrVy|jt|jWqtk rRtdYqXq|dkry|jt|jWqtk rtdYqXqWtt|||_dS)z?Process a GNU tar extended sparse header, version 0.0. sGNU.sparse.offsetzinvalid headersGNU.sparse.numbytesN)rrFr;r.rHlistzipr)rr]rjZoffsetsrO_r=rr6r6r7rhszTarInfo._proc_gnusparse_00cCs@dd|djdD}tt|ddd|ddd|_dS)z?Process a GNU tar extended sparse header, version 0.1. cSsg|] }t|qSr6)rF).0xr6r6r7 sz.TarInfo._proc_gnusparse_01..zGNU.sparse.map,Nr$r#)r3rkrlr)rr]rrr6r6r7rgszTarInfo._proc_gnusparse_01cCsd}g}|jjt}|jdd\}}t|}xJt||dkrvd|krV||jjt7}|jdd\}}|jt|q.W|jj|_t t |ddd|ddd|_ dS)z?Process a GNU tar extended sparse header, version 1.0. Nr<r#r$) rrZrr3rFr0rrrrkrlr)rr]rrZfieldsrrTZnumberr6r6r7ris  zTarInfo._proc_gnusparse_10c Csx|jD]\}}|dkr(t|d|q |dkrBt|dt|q |dkr\t|dt|q |tkr |tkryt||}Wntk rd}YnX|dkr|jd}t|||q W|j|_dS) zoReplace fields with supplemental information from a previous pax extended or global header. zGNU.sparse.namer%zGNU.sparse.sizer'zGNU.sparse.realsizerrN) rsetattrrF PAX_FIELDSPAX_NUMBER_FIELDSr.rKr r)rrr4r5r=rr6r6r7r\s"  zTarInfo._apply_pax_infoc Cs.y |j|dStk r(|j||SXdS)z1Decode a single field from a pax record. rCN)r;UnicodeDecodeError)rrr4Zfallback_encodingZfallback_errorsr6r6r7re,s zTarInfo._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#)rYr)rrGr`rar6r6r7r[4szTarInfo._blockcCs |jtkS)N)r REGULAR_TYPES)rr6r6r7r=sz TarInfo.isregcCs|jS)N)r)rr6r6r7isfile?szTarInfo.isfilecCs |jtkS)N)rr)rr6r6r7rAsz TarInfo.isdircCs |jtkS)N)rSYMTYPE)rr6r6r7rCsz TarInfo.issymcCs |jtkS)N)rLNKTYPE)rr6r6r7rEsz TarInfo.islnkcCs |jtkS)N)rr6)rr6r6r7ischrGsz TarInfo.ischrcCs |jtkS)N)rr7)rr6r6r7isblkIsz TarInfo.isblkcCs |jtkS)N)rFIFOTYPE)rr6r6r7isfifoKszTarInfo.isfifocCs |jdk S)N)r)rr6r6r7issparseMszTarInfo.issparsecCs|jtttfkS)N)rr6r7r|)rr6r6r7isdevOsz TarInfo.isdevN)rrhr)r*r'r(rrrr+r,rrrrrrrrr)r)6rqrrrsrt __slots__rrrpropertyr%rrr&r r rrrr rrrr classmethodr2r staticmethodrr9r r-rRrTrSrYrUrVrXrhrgrir\rer[rrwrrrrzr{r}r~rr6r6r6r7rssf    1  *  3 ?  z c @seZdZdZdZdZdZdZeZ e Z dZ e ZeZdZdkdd Zedddefd d Zedld d ZedmddZednddZedoddZdddddZddZddZddZd d!Zdpd"d#Zdqdd%d&d'Z drdd(d)d*Z!dsd+d,Z"d-d.Z#dtddd0d1d2Z$d3d4Z%duddd0d6d7Z&d8d9Z'dvd:d;Z(dd?Z*d@dAZ+dwdddBdCdDZ,dEdFZ-dGdHZ.dIdJZ/dKdLZ0dMdNZ1dOdPZ2dQdRZ3dSdTZ4dUdVZ5dWdXZ6dYdZZ7dxd[d\Z8d]d^Z9dyd_d`Z:dadbZ;dcddZdidjZ?dS)zrz=The TarFile Class provides an interface to tar archives. rFr#NrzrcCsddddd}||krtd||_|||_|sj|jdkrVtjj| rVd|_d|_t||j}d |_n@|d krt|d rt |j t t fr|j }t|d r|j|_d |_|rtjj |nd |_ ||_|d k r||_|d k r||_|d k r||_|d k r||_|d k r||_| |_| d k r4|jtkr4| |_ni|_| d k rJ| |_| d k rZ| |_| |_d |_g|_d |_|jj|_i|_y|jdkrd |_ |j!|_ |jdkr8x|jj"|jy|jj#|}|jj$|WnTt%k r|jj"|jPYn0t&k r0}zt't |WYd d }~XnXqW|jdkrd |_|jr|jj(|jj)}|jj*||jt+|7_Wn&|js|jj,d |_YnXd S)aOpen 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. rbzr+bwbZxb)rzar{roz!mode must be 'r', 'a', 'w' or 'x'rr{FNrrhTrzro)rr{ro)-r.rh_moder}r%exists bltn_openrrr*rr,rLabspathrrPr dereference ignore_zerosr4r5rrdebug errorlevel copybufsizermembers_loadedrrinodes firstmemberr]rrTrrwr rr2r r[r0r)rrrhrrPrrrr4r5rrrrZmodeserTr6r6r7ros             $   zTarFile.__init__c s| r| rtd|dkrfdd}xttj|dD]b}tj|}|dk r\|j} y||d|f|Sttfk r|dk r|j| w8Yq8Xq8Wtdnd |kr |jd d \} }| pd} |pd }|jkrtj|}n td |||| |f|Sd |kr|jd d \} }| p.d} |p8d }| dkrLtdt || |||} y|| | f|} Wn| j YnXd| _ | S|dkrj |||f|StddS)aOpen 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 znothing to openrzr:*csj|dkS)Ntaropen) OPEN_METH)r)r1r6r7not_compressedsz$TarFile.open..not_compressed)keyNz%file could not be opened successfully:r#rzunknown compression type %r|r{zmode must be 'r' or 'w'Frrozundiscernible mode)rzr)rzr{)rr{ro) r.sortedrrlrrrrr3rrrr) r1rrhrr_kwargsrrfuncZ saved_posrgstreamrr6)r1r7rsP%             z TarFile.opencKs |dkrtd||||f|S)zCOpen uncompressed tar archive name for reading or writing. rzrr{roz!mode must be 'r', 'a', 'w' or 'x')rzrr{ro)r.)r1rrhrrr6r6r7r9szTarFile.taropenrcKs|d krtdyddl}|jWn ttfk rBtdYnXy|j||d||}Wn.tk r|dk r|dkrtd YnXy|j|||f|}WnBtk r|j |dkrtd Yn|j YnXd |_ |S) zkOpen gzip compressed tar archive name for reading or writing. Appending is not allowed. rzr{rozmode must be 'r', 'w' or 'x'rNzgzip module is not availablerbznot a gzip fileF)rzr{ro) r.gzipZGzipFilerAttributeErrorrrrrrr)r1rrhr compresslevelrrrr6r6r7gzopenAs2 zTarFile.gzopencKs|d krtdy ddl}Wntk r8tdYnX|j|pD|||d}y|j|||f|}WnFttfk r|j|dkrt d Yn|jYnXd |_ |S) zlOpen bzip2 compressed tar archive name for reading or writing. Appending is not allowed. rzr{rozmode must be 'r', 'w' or 'x'rNzbz2 module is not available)rznot a bzip2 fileF)rzr{ro) r.rrrZBZ2FilerrEOFErrorrrr)r1rrhrrrrrr6r6r7bz2opencs(  zTarFile.bz2opencKs|d krtdy ddl}Wntk r8tdYnX|j|pD|||d}y|j|||f|}WnH|jtfk r|j|dkrt d Yn|jYnXd |_ |S) zkOpen lzma compressed tar archive name for reading or writing. Appending is not allowed. rzr{rozmode must be 'r', 'w' or 'x'rNzlzma module is not available)presetznot an lzma fileF)rzr{ro) r.rrrZLZMAFilerrrrrr)r1rrhrrrrrr6r6r7xzopens& zTarFile.xzopenrrrr)rrrrc Cs|jr dSd|_z`|jdkrn|jjttd|jtd7_t|jt\}}|dkrn|jjtt|Wd|j s|jj XdS) zlClose the TarFile. In write-mode, two finishing zero blocks are appended to the archive. NTrr{ror$r)rr{ro) rrhrr[r1rrrY RECORDSIZErr)rr`rar6r6r7rs z TarFile.closecCs"|j|}|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) _getmemberKeyError)rrrr6r6r7 getmembers  zTarFile.getmembercCs|j|js|j|jS)zReturn 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)rr6r6r7 getmembersszTarFile.getmemberscCsdd|jDS)zReturn the members of the archive as a list of their names. It has the same order as the list returned by getmembers(). cSsg|] }|jqSr6)r)rnrr6r6r7rpsz$TarFile.getnames..)r)rr6r6r7getnamesszTarFile.getnamesc Csp|jd|dk r|j}|dkr$|}tjj|\}}|jtjd}|jd}|j}||_ |dkrt tdr|j rtj |}qtj |}ntj|j}d}|j}t j|r|j|jf} |j r|jdkr| |jkr||j| krt} |j| }nt} | drx||j| <nht j|r"t} nVt j|r4t} nDt j|rPt} tj|}n(t j|rbt } nt j!|rtt"} ndS||_||_#|j$|_%|j&|_'| tkr|j(|_)nd|_)|j*|_+| |_,||_-t.ryt.j/|j%d|_0Wnt1k rYnXt2r*yt2j3|j'd|_4Wnt1k r(YnX| t t"fkrlt tdrlt td rltj5|j6|_7tj8|j6|_9|S) aCreate 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. awxNrlstatrr#rmajorminor):rrr}r% splitdriverrrrrrrrrffstatfilenost_modeS_ISREGst_inost_devst_nlinkrryrS_ISDIRrS_ISFIFOr|S_ISLNKrxreadlinkS_ISCHRr6S_ISBLKr7rhst_uidr)st_gidr*st_sizer'st_mtimer(rrpwdgetpwuidr+rgrpZgetgrgidr,rst_rdevrrr) rrarcnamerZdrvrZstatresrZstmdinoderr6r6r7 gettarinfos~                 zTarFile.gettarinfoT)rcCs0|j|dkr|}x|D] }|r|jdkr:tdnttj|jtd|jpX|j|jpb|jf|j sz|j rtdd|j |j fntd|j |jdkrtdntdtj|jdd t|j|jrd nd |r"|jr td |j|jr"td |jtqWdS)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:%02drrrz-> zlink to )rrhrprfrgr+r)r,r*rzr{rrr'r(rZ localtimerrrrrro)rverboserrr6r6r7rk;s2      z TarFile.list)filterc Csv|jd|dkr|}|dk rPddl}|jdtd||rP|jdd|dS|jdk rtjj||jkr|jdd|dS|jd||j ||}|dkr|jdd |dS|dk r||}|dkr|jdd|dS|j rt |d }|j ||WdQRXnd|j rh|j ||rrxHtj|D].}|jtjj||tjj|||||d q4Wn |j |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. `exclude' is a function that should return True for each filename to be excluded. `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. rNrzuse the filter argument insteadr$ztarfile: Excluded %rztarfile: Skipped %rr#ztarfile: Unsupported type %rr)r)rrcrdre_dbgrr}r%rrrraddfilerlistdiraddr) rrr recursiveZexcluderrcrfr6r6r7rasB       z TarFile.addcCs|jdtj|}|j|j|j|j}|jj||jt |7_|j }|dk rt ||j|j |dt |j t\}}|dkr|jjtt||d7}|j|t7_|jj|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(). rN)r_rr#)rr rrPr4r5rr[rr0rrXr'rYrr1rr)rrrrTr_r`rar6r6r7rs   zTarFile.addfilec"CsH|dkr|j}|dkrtjjd}|dkry tt}Wntk rNYnBXddl}|jddd}||j |WdQRX|jdddd}|ry t |}Wn&t k rt d|d dYnX||_|St rtjdttStSt|trtd|St|r|Syt |St k rBt d|d dYnXdS)NZ PYTHON_TARFILE_EXTRACTION_FILTERr#)Z interpolationZcomment_prefixesrr)Zfallbackzfilter z not foundz4The default behavior of tarfile extraction has been z$changed to disallow common exploits z(including CVE-2007-4559). z1By default, absolute/parent paths are disallowed z and some mode bits are cleared. z/See https://access.redhat.com/articles/7004769 zfor more details.z#String names are not supported for z2TarFile.extraction_filter. Use a function such as ztarfile.data_filter directly.)rzXThe default behavior of tarfile extraction has been changed to disallow common exploits zsThe default behavior of tarfile extraction has been changed to disallow common exploits (including CVE-2007-4559). zThe default behavior of tarfile extraction has been changed to disallow common exploits (including CVE-2007-4559). By default, absolute/parent paths are disallowed zThe 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. zThe 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 aThe 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.zUString names are not supported for TarFile.extraction_filter. Use a function such as zrString names are not supported for TarFile.extraction_filter. Use a function such as tarfile.data_filter directly.)extraction_filterr}environr5r_CONFIG_FILENAMEFileNotFoundError configparserZ ConfigParserZ read_file_NAMED_FILTERSrr._RH_SAFER_DEFAULTrcrdRuntimeWarningrrr*r, TypeErrorcallable)rrrfilerZconfr6r6r7_get_filter_functionsR       zTarFile._get_filter_function.) numeric_ownerrc"Csg}|j|}|dkr|}xT|D]L}|j|||\}} |dkr@q |jrR|j| |j|||j ||dq W|jddddx$|D]} yy|| |}Wn4tk r} z|j| t| wWYdd} ~ XnX|dkr|j| dwt j j ||j } yt j | } Wn$tk r.|j|dwYnXtj| jsL|j|d w|j|| |d |j|| |j|| Wqtk r} z|j| WYdd} ~ XqXqWdS) aExtract 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_attrsrfilter_functioncSs|jS)N)r)rr6r6r7 sz$TarFile.extractall..T)rreversezexcluded by filterZmissingznot a directory)r)r_get_extract_tarinforr _extract_onesort_FILTER_ERRORS_log_no_directory_fixupreprr}r%rrrrrfrrchownutimechmodr _handle_nonfatal_error)rr%rrrZ directoriesrrr unfilteredexcdirpathrrr6r6r7 extractallsP       zTarFile.extractallcCs|jdd|j|fdS)Nr$z(tarfile: Not fixing up directory %r (%s))rr)rrreasonr6r6r7r1 szTarFile._log_no_directory_fixuprc Cs8|j|}|j|||\}}|dk r4|j||||dS)aExtract 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)rrr) rrr%rrrrrrr6r6r7extract5 s  zTarFile.extractcCst|tr|j|}n|}d}y|||}WnZttfk r\}z|j|WYdd}~Xn,tk r}z|j|WYdd}~XnX|dkr|jdd|j dS|j rt j |}t j j||j|_||fS)zGet (filtered, unfiltered) TarInfos from *member* *member* might be a string. Return (None, None) if not found. Nr$ztarfile: Excluded %r)NN)r*r,rrr_handle_fatal_errorr rrrrr r}r%rrr)rrrr%rfilteredrr6r6r7rI s"   zTarFile._get_extract_tarinfocCs|jdy&|j|tjj||j||||dWnVtk r\}z|j|WYdd}~Xn,tk r}z|j |WYdd}~XnXdS)zExtract from filtered tarinfo to disk. filter_function is only used when extracting a *different* member (e.g. as fallback to creating a symlink) rz)rrrextraction_rootN) r_extract_memberr}r%rrrrr r)rrr%rrrrr6r6r7rg s  zTarFile._extract_onecCs"|jdkrn|jdd|dS)z=Handle non-fatal error (ExtractError) according to errorlevelr#z tarfile: %sN)rr)rrr6r6r7r{ s zTarFile._handle_nonfatal_errorcCsn|jdkrn\t|trP|jdkr6|jdd|jqj|jdd|j|jfn|jddt|j|fdS)z1Handle "fatal" error according to self.errorlevelrNr#z tarfile: %sztarfile: %s %rztarfile: %s %s)rr*rfilenamerstrerrorrrq)rrr6r6r7r s   zTarFile._handle_fatal_errorcCs|jdt|tr |j|}n|}|js6|jtkrB|j||S|jsR|j rzt|j t rht dq~|j |j|SndSdS)zExtract 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. rzz'cannot extract (sym)link as file objectN)rr*r,rrrrZ fileobjectrrrrr extractfile_find_link_target)rrrr6r6r7r s      zTarFile.extractfile)rrcCs\|jd}|jdtj}tjj|}|r@tjj| r@tj||jsP|j rj|j dd|j |j fn|j d|j |j r|j||n|jr|j||n~|jr|j||nh|js|jr|j||nJ|js|j r|j||||dn&|jtkr|j||n |j|||rX|j||||j sX|j|||j||dS)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) rr#z%s -> %s)rrN)rKrr}rr%rrmakedirsrrrrrrmakefilermakedirr}makefiforzr{makedevmakelink_with_filterrrZ makeunknownrrr)rr targetpathrrrrZ upperdirsr6r6r7r s:        zTarFile._extract_memberc Cs@y&|jdkrtj|n tj|dWntk r:YnXdS)z,Make a directory called targetpath. Ni)rhr}mkdirFileExistsError)rrrr6r6r7r s   zTarFile.makedircCs|j}|j|j|j}t|df}|jdk rrx,|jD]"\}}|j|t|||t|q6W|j|j|j nt|||jt|WdQRXdS)z'Make a file called targetpath. rN) rrrrrrrXrr'truncate)rrrsourcer_targetrr'r6r6r7r s      zTarFile.makefilecCs"|j|||jdd|jdS)zYMake a file from a TarInfo object with an unknown type at targetpath. r#z9tarfile: Unknown file type %r, extracted as regular file.N)rrr)rrrr6r6r7r s zTarFile.makeunknowncCs"ttdrtj|ntddS)z'Make a fifo called targetpath. mkfifozfifo not supported by systemN)rr}rr )rrrr6r6r7r s  zTarFile.makefifocCsnttd sttd r td|j}|dkr2d}|jrF|tjO}n |tjO}tj||tj |j |j dS)zzFilter for extraction)metavarchoicesr/z-lz--list zShow listing of a tarfile)r0r/z-ez --extract+ zExtract tarfile into target dir)nargsr0r/z-cz--createzCreate tarfile from sourcesz-tz--testzTest if a tarfile is validr#z&--filter is only valid for extraction rz)rz{!r} is a tar archive.z{!r} is not a tar archive. zr:*)rr$)r%rrz{!r} file is extracted.z+{!r} file is extracted into {!r} directory.rrr)z.gzz.tgzz.xzz.txzz.bz2z.tbzz.tbz2z.tb2zw:r{z{!r} file created.)r2r4)r6r7)argparseArgumentParser add_argumentrZadd_mutually_exclusive_group parse_argsrrexitZtestrrrrormr%rrPrkrr0r}curdirZ format_helprZcreatepopr%splitextr)r8r+parserrcargsr\rZtfr=r'Ztar_namermZextZ compressionsZtar_modeZ tar_files file_namer6r6r7main] s                  rC__main__)r%r&r'r(r)r*r+r,)T)trtversion __author__Z__date__Z __cvsid__ __credits__builtinsrrrmr}rrWrfrrSr rercrrrrNotImplementedErrorrr NameError__all__rrr1rrrrrrr4rrHryrxr6r7rr|ZCONTTYPEr"r!rIr.r0rWr rrrrZrvrLrsrfr+rFrtrr getfilesystemencodingr8r=rKrQrUrXrgrp Exceptionrr rrr r rurvrwrHrxryrobjectrrBufferedReaderrrrrrrrrrrrrrrr compilerarrrrCrqr6r6r6r7s&         wh   D e b