3 f'@s(dZdZdZdZdZdZddlmZddl 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_ZdddsdtZedudvZfdwdxZgdydzZhefegehd{ZieZZjejkd|ZlGd}d d eZZmGd~d d eZZndd ZoenjZddZpeqdkr$epdS)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)rr7rsrcCsi}|j}tjj|}|jdtjfr@|jjdtj}|d<tjj|rTt|tjjtjj ||}tjj ||g|krt |||j }|dk r|d@}|r|j s|jr|d@s|d M}|dO}n|js|jrd}nt|||j kr||d<|r|jdk rd|d<|jdk r.d|d <|jdk rBd|d <|jdk rVd|d <|jsj|jrtjj|jrt||jrtjj |tjj||j}ntjj ||j}tjj|}tjj ||g|krt|||S) N/ri@Iirhr)r*r+r,i)rr}r%realpathrseplstripisabsrr commonpathrrhisregislnkisdirissymrr)r*r+r,linknamerdirnamer)member dest_pathZfor_data new_attrsrZ target_pathrhr6r6r7_get_filtered_attrssZ               rcCs|S)Nr6)rrr6r6r7fully_trusted_filterCsrcCs(t||d}|r$|jf|ddiS|S)NFdeep)rr)rrrr6r6r7 tar_filterFs rcCs(t||d}|r$|jf|ddiS|S)NTrF)rr)rrrr6r6r7 data_filterLs 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)rrr6r6r7rps"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,rrresultr6r6r7rs(  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,rrAREGTYPErr GNUTYPE_SPARSErEr.rboolrrrstrip GNU_TYPES)r.rTr4r5robjrrstructsrJrnumbytes isextendedorigsizer6r6r7frombufsZ       zTarInfo.frombufcCs8|jjt}|j||j|j}|jjt|_|j|S)zOReturn the next TarInfo object from TarFile object tarfile. ) rrZrrOr4r5rr _proc_member)r.rrTrJr6r6r7 fromtarfiles 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) rrr _proc_gnulongrF _proc_sparser+r-SOLARIS_XHDTYPE _proc_pax _proc_builtin)rrr6r6r7rPs    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)rrrr6r6r7rVs 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)rrZrXr'rQr rxrrrr=r4r5rrr)rrrTnextr6r6r7rR.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!rCi)rrrZrrErKr.rrGrrrrXr'r) rrrKrMrNrTrrJrrLr6r6r7rSDs(    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#r8rs hdrcharsetsBINARYzutf-8z missing or bad subsequent headerzGNU.sparse.mapzGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorrDr')%rrZrXr'rr-rrr0_header_length_prefix_rematchrHrFgroupr.rrk partitionrr4_decode_pax_fieldr5PAX_NAME_FIELDSrQr rx_proc_gnusparse_01_proc_gnusparse_00r2_proc_gnusparse_10r+rTrYrrrrW)rrrTrrr4 raw_headersr_r3Zheader_value_end_offsetZkeyword_and_valueZ raw_keywordZequalsZ raw_valuer:rrZrr6r6r7rU`sx                 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)rrZrgZoffsetsrL_r:rr6r6r7reszTarInfo._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#)r0rhrir)rrZrrr6r6r7rdszTarInfo._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. Nr9r#r$) rrZrr0rFr0rrrrhrir)rrZrrZfieldsrrTZnumberr6r6r7rfs  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.rHrr)rrr4r5r:rr6r6r7rYs"  zTarInfo._apply_pax_infoc Cs.y |j|dStk r(|j||SXdS)z1Decode a single field from a pax record. rCN)r;UnicodeDecodeError)rrr4Zfallback_encodingZfallback_errorsr6r6r7rbs 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)rrDr`rar6r6r7rX#szTarInfo._blockcCs |jtkS)N)r REGULAR_TYPES)rr6r6r7r,sz TarInfo.isregcCs|jS)N)r)rr6r6r7isfile.szTarInfo.isfilecCs |jtkS)N)rr )rr6r6r7r0sz TarInfo.isdircCs |jtkS)N)rSYMTYPE)rr6r6r7r2sz TarInfo.issymcCs |jtkS)N)rLNKTYPE)rr6r6r7r4sz TarInfo.islnkcCs |jtkS)N)rr3)rr6r6r7ischr6sz TarInfo.ischrcCs |jtkS)N)rr4)rr6r6r7isblk8sz TarInfo.isblkcCs |jtkS)N)rFIFOTYPE)rr6r6r7isfifo:szTarInfo.isfifocCs |jdk S)N)r)rr6r6r7issparse<szTarInfo.issparsecCs|jtttfkS)N)rr3r4ry)rr6r6r7isdev>sz TarInfo.isdevN)rrhr)r*r'r(rrrr+r,rrrrrrrrr)r)6rqrrrsrt __slots__rrrpropertyr%rrr&rrrr rr rrrr classmethodr/r staticmethodrr6rr*rOrQrPrVrRrSrUrerdrfrYrbrXrrtrrrrwrxrzr{r|r6r6r6r7rbsf    1  *  3 ?  z c @seZdZdZdZdZdZdZeZ e Z dZ e ZeZdZdfdd Zedddefd d Zedgd d ZedhddZediddZedjddZdddddZddZddZddZd d!Zdkd"d#Zdldd%d&d'Z dmdd(d)d*Z!dnd+d,Z"d-d.Z#doddd0d1d2Z$dpddd0d4d5Z%d6d7Z&d8d9Z'd:d;Z(dd?Z*dqd@dAZ+dBdCZ,dDdEZ-dFdGZ.dHdIZ/dJdKZ0dLdMZ1dNdOZ2dPdQZ3dRdSZ4dTdUZ5drdVdWZ6dXdYZ7dsdZd[Z8d\d]Z9d^d_Z:d`daZ;dbdcZ.not_compressed)keyNz%file could not be opened successfully:r#rzunknown compression type %r|r{zmode must be 'r' or 'w'Frrlzundiscernible mode)rzr)rzr{)rr{rl) r.sortedrrlrrrrr0rrrr) r.rrhrr_kwargsrrfuncZ saved_posrgstreamrr6)r.r7rsP%             z TarFile.opencKs |dkrtd||||f|S)zCOpen uncompressed tar archive name for reading or writing. rzrr{rlz!mode must be 'r', 'a', 'w' or 'x')rzrr{rl)r.)r.rrhrrr6r6r7r(szTarFile.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{rlzmode must be 'r', 'w' or 'x'rNzgzip module is not availablerbznot a gzip fileF)rzr{rl) r.gzipZGzipFilerAttributeErrorrrrrrr)r.rrhr compresslevelrrrr6r6r7gzopen0s2 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{rlzmode must be 'r', 'w' or 'x'rNzbz2 module is not available)rznot a bzip2 fileF)rzr{rl) r.rrrZBZ2FilerrEOFErrorrrr)r.rrhrrrrrr6r6r7bz2openRs(  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{rlzmode must be 'r', 'w' or 'x'rNzlzma module is not available)presetznot an lzma fileF)rzr{rl) r.rrrZLZMAFilerrrrrr)r.rrhrrrrrr6r6r7xzopenos& 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{rlr$r)rr{rl) 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)rkrr6r6r7rmsz$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_nlinkrrvrS_ISDIRr S_ISFIFOryS_ISLNKrureadlinkS_ISCHRr3S_ISBLKr4rhst_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*rwrxrrr'r(rZ localtimerrrrrro)rverboserrr6r6r7rh*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 recursiveZexcluderrcrfr6r6r7rPsB       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#)rrrrPr4r5rr[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}environr2r_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|}xN|D]F}|j|||}|dkrsz$TarFile.extractall..T)rreverse)r)r_get_extract_tarinforr _extract_onesortr}r%rrchownutimechmodr _handle_nonfatal_error) rr%rrrZ directoriesfilter_functionrrdirpathrr6r6r7 extractalls*      zTarFile.extractallrcCs4|j|}|j|||}|dk r0|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%rrrrrr6r6r7extract s zTarFile.extractcCst|tr|j|}n|}|}y|||}WnZttfk r\}z|j|WYdd}~Xn,tk r}z|j|WYdd}~XnX|dkr|jdd|j dS|j rt j |}t j j||j|_|S)z@Get filtered TarInfo (or None) from member, which might be a strNr$ztarfile: Excluded %r)r'r)rrr_handle_fatal_errorr rrrrrr}r%rrr)rrrr%rZ unfilteredrr6r6r7r s"   zTarFile._get_extract_tarinfocCs|jdy"|j|tjj||j||dWnVtk rX}z|j|WYdd}~Xn,tk r}z|j |WYdd}~XnXdS)z%Extract from filtered tarinfo to diskrz)rrN) r_extract_memberr}r%rrrrr r)rrr%rrrr6r6r7r2 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)rrr6r6r7rF 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)rrrrW fileobjectrrrrr extractfile_find_link_target)rrrr6r6r7rR s      zTarFile.extractfilecCsV|jd}|jdtj}tjj|}|r@tjj| r@tj||jsP|j rj|j dd|j |j fn|j d|j |j r|j||n|jr|j||nx|jr|j||nb|js|jr|j||nD|js|j r|j||n&|jtkr|j||n |j|||rR|j||||j sR|j|||j||dS)z\Extract the TarInfo object tarinfo to a physical file called targetpath. rr#z%s -> %sN)rHrr}rr%rrmakedirsrrrrrrmakefilermakedirrzmakefiforwrxmakedevmakelinkrrW makeunknownrrr)rr targetpathrrZ upperdirsr6r6r7rq s4       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)zrqrrrsrtrrrrrrPr r4r5rrrrrrrrrrrrrrrrrrrrhrrrrrrrrrrrrrrrrrrrrrZrrrrrrrrr6r6r6r7rBs lZ  !     d&= 5,   0! 6 ( c Cs.yt|}|jdStk r(dSXdS)zfReturn True if name points to a tar archive that we are able to handle, else return False. TFN)rrr)rrr6r6r7r s c Csddl}d}|j|d}|jdddddd |jd d td d |j}|jddddd|jdddd5dd|jdddd6dd|jdddd d|j}|jr|j r|jd!d"|j r*|j }t |rt |d# }|j t |j tjd$WdQRX|jr&t d%j|n|jd!d&j|n|jr|j}t |rjtj |d'}|j|jd(WdQRXn|jd!d&j|n|jrPt|jd!kr|jd}tj}n,t|jd)kr|j\}}n|jd!|jt |rzFilter for extraction)metavarchoicesr!z-lz--list zShow listing of a tarfile)r"r!z-ez --extract+ zExtract tarfile into target dir)nargsr"r!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.)r$r&)r(r))argparseArgumentParser add_argumentrZadd_mutually_exclusive_group parse_argsrrexitZtestrrrrormrrrPrhrr0r}curdirZ format_helprZcreatepopr%splitextr)r*rparserr`argsr\rZtfr/rZtar_namerjZextZ compressionsZtar_modeZ tar_files file_namer6r6r7main s                  r5__main__)r%r&r'r(r)r*r+r,)T)rrtversion __author__Z__date__Z __cvsid__ __credits__builtinsrrrmr}rrWrfrrSrrercrrrrNotImplementedErrorr r NameError__all__rrr1rrrrrrr1rrErvrur3r4r ryZCONTTYPErrrFr+r-rTr rrrrWrsrIrprcr(rFrqrr getfilesystemencodingr8r=rKrQrUrXrgrp Exceptionrr rrr r rurvrwrHrxryrobjectrrBufferedReaderrrrrrrrrrrrrrcompiler^rrrr5rqr6r6r6r7s          wh  ? e4 b