3 0f|r'@sdZdZdZdZdZddlmZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZy ddlZWnek rdZYnXy ddlZWnek rdZYnXeefZyeef7ZWnek rYnXdd d d d d dddddddddgZdZdZedZdZdZdZdZ 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*Z/d+Z0dZ1d,Z2d-Z3e2Z4e"e#e$e%e(e)e*e&e'e+e,e-f Z5e"e#e*e-fZ6e+e,e-fZ7dkZ8d.d/d4d5hZ9e:e:e:e;e;e;d6Znej?Z>d9d:Z@d;d<ZAd=d>ZBd?e4fd@dAZCdBdCZDdedfdDdEZEdFdGZFdHdIZGGdJd d eHZIGdKddeIZJGdLd d eIZKGdMd d eIZLGdNddeIZMGdOddeIZNGdPdQdQeNZOGdRdSdSeNZPGdTdUdUeNZQGdVdWdWeNZRGdXdYdYeNZSGdZd[d[ZTGd\d]d]ZUGd^d_d_eVZWGd`dadaeVZXGdbdcdce jYZZej[ddZ\Gded d eVZ]GdfddeVZ^dgd Z_e^jZdhdiZ`eadjkr|e`dS)lz0.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_FORMATrisustar sustar00d01234567LKSxgXpathlinkpathsizemtimeuidgidunamegname)ZatimeZctimer(r)r*r'ntzutf-8cCs(|j||}|d||t|tS)N)encodelenNUL)slengthencodingerrorsr5/usr/lib64/python3.6/tarfile.pystns r7cCs*|jd}|dkr|d|}|j||S)Nrr#)finddecode)r1r3r4pr5r5r6ntss  r<c Cs|dd krfd}x0tt|dD]}|dK}|||d7}q"W|ddkrdt|d| }n@y"t|dd}t|jpd d}Wntk rtd YnX|S) Nrr#asciistrict0zinvalid header)r=r>)ranger/r<intstrip ValueErrorInvalidHeaderError)r1nir5r5r6ntis   rKr?cCst|}d|ko"d|dknrDtd|d|fdt}n|tkrd|d |kopd|dknr|dkrtdg}ntdg}d||}x6t|dD]}|jd|d@|dL}qWntd |S) Nrr?r#z%0*orAr@r=r>zoverflow in number field)rEbytesr0r bytearrayrDinsertrG)rIdigitsformatr1rJr5r5r6itns  2   rQcCs0dttjd|}dttjd|}||fS)Nr@Z 148B8x356BZ 148b8x356b)sumstructZ unpack_from)bufZunsigned_chksumZ signed_chksumr5r5r6 calc_chksumss rUc Cs|pd}|dkrdS|dkr.tj|||dSt||\}}x8t|D],}|j|}t||krh|d|j|qFW|dkr|j|}t||kr|d|j|dS)Nirzunexpected end of datai@)shutil copyfileobjdivmodrDreadr/write) srcdstr2 exceptionbufsizeblocks remainderbrTr5r5r6rXs$     rXcCs ddl}|jdtdtj|S)Nrz$deprecated in favor of stat.filemoder$)warningswarnDeprecationWarningstatfilemode)modercr5r5r6rgsrgcCs8ttjdd}|dk r(|j|dj|}t|dddS)Nr3backslashreplace )end)getattrsysstdoutr.r:print)r1r3r5r5r6 _safe_print srpc@s eZdZdS)rN)__name__ __module__ __qualname__r5r5r5r6rsc@s eZdZdS)r N)rqrrrsr5r5r5r6r sc@s eZdZdS)rN)rqrrrsr5r5r5r6rsc@s eZdZdS)rN)rqrrrsr5r5r5r6rsc@s eZdZdS)r N)rqrrrsr5r5r5r6r sc@s eZdZdS)r N)rqrrrsr5r5r5r6r "sc@s eZdZdS)EmptyHeaderErrorN)rqrrrsr5r5r5r6rt%srtc@s eZdZdS)TruncatedHeaderErrorN)rqrrrsr5r5r5r6ru(sruc@s eZdZdS)EOFHeaderErrorN)rqrrrsr5r5r5r6rv+srvc@s eZdZdS)rHN)rqrrrsr5r5r5r6rH.srHc@s eZdZdS)SubsequentHeaderErrorN)rqrrrsr5r5r5r6rw1srwc@s,eZdZddZddZddZddZd S) _LowLevelFilecCsFtjtjtjBtjBd|}ttdr2|tjO}tj||d|_dS)N)rwO_BINARYi) osO_RDONLYO_WRONLYO_CREATO_TRUNChasattrr{rfd)selfnamerhr5r5r6__init__>s   z_LowLevelFile.__init__cCstj|jdS)N)r|closer)rr5r5r6rGsz_LowLevelFile.closecCstj|j|S)N)r|rZr)rr'r5r5r6rZJsz_LowLevelFile.readcCstj|j|dS)N)r|r[r)rr1r5r5r6r[Msz_LowLevelFile.writeN)rqrrrsrrrZr[r5r5r5r6rx8s rxc@speZdZddZddZddZddZd d Zd d Zd dZ ddZ dddZ dddZ ddZ ddZdS)_Streamc Csd|_|dkrt||}d|_|dkr6t|}|j}|pt|j|jkrL|jj|jd|j|j|jd|_qWdS)N)rTr/r_rr[)rr1r5r5r6Z__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)NTrzrrrzd|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 blockinfoZlastposZrealposr5r5r6rjs(   z_FileInFile.__init__cCsdS)Nr5)rr5r5r6rsz_FileInFile.flushcCsdS)NTr5)rr5r5r6readablesz_FileInFile.readablecCsdS)NFr5)rr5r5r6writablesz_FileInFile.writablecCs |jjS)N)rseekable)rr5r5r6rsz_FileInFile.seekablecCs|jS)N)r)rr5r5r6rsz_FileInFile.tellcCs|tjkr tt|d|j|_nj|tjkr\|dkrFt|j|d|_qt|j||j|_n.|tjkrtt|j||jd|_ntd|jS)NrzInvalid argument) ioSEEK_SETminmaxr'rSEEK_CURSEEK_ENDrG)rrwhencer5r5r6rs   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)Nrrr#zunexpected end of data) r'rrrrr/rrrZrr0) rr'rTdatastartstoprr2rbr5r5r6rZs.      z_FileInFile.readcCs&|jt|}||dt|<t|S)N)rZr/)rrbrTr5r5r6readintosz_FileInFile.readintocCs d|_dS)NT)r)rr5r5r6rsz_FileInFile.close)N)N)rqrrrsrrrrrrrrrrZrrr5r5r5r6rds  rcseZdZfddZZS) ExFileObjectcs&t|j|j|j|j}tj|dS)N)rr offset_datar'sparsesuperr)rtarfiletarinfor) __class__r5r6rs  zExFileObject.__init__)rqrrrsr __classcell__r5r5)rr6rsrs([0-9]{1,20}) c@seZdZdhZdiddZddZddZeeeZddZ ddZ ee e Z d d!Z d"d#Z eed$fd%d&Zd'd(Zd)d*Zd+d,Zed-d.Zd/d0Zed1d2Zed3d4Zed5d6Zed7d8Zed9d:Zed;d<Zd=d>Zd?d@ZdAdBZ dCdDZ!dEdFZ"dGdHZ#dIdJZ$dKdLZ%dMdNZ&dOdPZ'dQdRZ(dSdTZ)dUdVZ*dWdXZ+dYdZZ,d[d\Z-d]d^Z.d_d`Z/dadbZ0dcddZ1dedfZ2dgS)jrrrhr)r*r'r(chksumtypelinknamer+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)Nirr)rrhr)r*r'r(rREGTYPErrr+r,rrrrrr)rrr5r5r6rs"zTarInfo.__init__cCs|jS)N)r)rr5r5r6_getpathszTarInfo._getpathcCs ||_dS)N)r)rrr5r5r6_setpathszTarInfo._setpathcCs|jS)N)r)rr5r5r6 _getlinkpathszTarInfo._getlinkpathcCs ||_dS)N)r)rrr5r5r6 _setlinkpath szTarInfo._setlinkpathcCsd|jj|jt|fS)Nz<%s %r at %#x>)rrqrid)rr5r5r6__repr__szTarInfo.__repr__cCsn|j|jd@|j|j|j|j|j|j|j|j |j |j |j d }|dt krj|djd rj|dd7<|S)Ni) rrhr)r*r'r(rrrr+r,rrrr/)rrhr)r*r'r(rrrr+r,rrDIRTYPEr)rinfor5r5r6get_infos  zTarInfo.get_infosurrogateescapecCsT|j}|tkr|j|||S|tkr4|j|||S|tkrH|j||StddS)Nzinvalid format)rr create_ustar_headerrcreate_gnu_headerrcreate_pax_headerrG)rrPr3r4rr5r5r6tobuf)s z TarInfo.tobufcCsnt|d<t|dj||tkr(tdt|dj||tkr^|j|d||\|d<|d<|j|t||S)Nmagicrzlinkname is too longrprefix) POSIX_MAGICr/r. LENGTH_LINKrG LENGTH_NAME_posix_split_name_create_headerr )rrr3r4r5r5r6r7s zTarInfo.create_ustar_headercCst|d<d}t|dj||tkr<||j|dt||7}t|dj||tkrl||j|dt||7}||j|t ||S)Nrrrr) GNU_MAGICr/r.r_create_gnu_long_headerGNUTYPE_LONGLINKrGNUTYPE_LONGNAMErr)rrr3r4rTr5r5r6rDszTarInfo.create_gnu_headerc Cs4t|d<|jj}xddtfddtfddfD]h\}}}||kr@q,y||jd d Wn"tk rv||||<w,YnXt|||kr,||||<q,WxldD]d\}}||krd||<q||}d|kod |dkn st|t rt |||<d||<qW|r|j |t |} nd} | |j |td dS)Nrrr%rr&r+ r,rArBr)r?r*r' r(rr#rr)r+r+r)r,r,rr)r?r*r?r'rr(r)rrrr)rrcopyrrr.UnicodeEncodeErrorr/ isinstancefloatstr_create_pax_generic_headerXHDTYPErr ) rrr3rrZhnamer2rOvalrTr5r5r6rRs4  .  zTarInfo.create_pax_headercCs|j|tdS)Nzutf-8)r XGLTYPE)clsrr5r5r6create_pax_global_headersz TarInfo.create_pax_global_headercCs|jd}xltdt|D]R}dj|d|}dj||d}t|j||tkrt|j||tkrPqWtd||fS)Nrr#zname is too long)splitrDr/rr. LENGTH_PREFIXrrG)rrr3r4Z componentsrJrr5r5r6rs zTarInfo._posix_split_namecCsVt|jddd||t|jddd@d|t|jddd|t|jd dd|t|jd dd |t|jd dd |d |jdtt|jddd|||jdtt|jddd||t|jddd||t|jddd|t|jddd|t|jddd||g}tjdtdj|}t |t dd}|ddt d|d|dd}|S) Nrrrrhrir?r)r*r'rr(s rrrr+rr,rrrrz%dsrilz%06orAieii) r7getrQrrrSrrrrUrL)rrPr3r4partsrTrr5r5r6rs&  &zTarInfo._create_headercCs.tt|t\}}|dkr*|t|t7}|S)Nr)rYr/rr0)Zpayloadr`rar5r5r6_create_payloadszTarInfo._create_payloadcCsR|j||t}i}d|d<||d<t||d<t|d<|j|t|||j|S)Nz ././@LongLinkrrr'r)r.r0r/rrr r)r rrr3r4rr5r5r6rs zTarInfo._create_gnu_long_headerc Cs:d}x@|jD]4\}}y|jddWqtk r@d}PYqXqWd}|rV|d7}x|jD]\}}|jd}|r|j|d}n |jd}t|t|d}d } } x"|tt| } | | krP| } qW|tt| d d |d |d 7}q`Wi} d| d<|| d<t|| d<t| d<|j| td d|j |S)NFzutf-8rBTrs21 hdrcharset=BINARY rrrrA = z././@PaxHeaderrrr'rr) itemsr.rr/rrLrrr r) r rrr3ZbinarykeywordvalueZrecordslrIr;rr5r5r6r s<   * z"TarInfo._create_pax_generic_headerc Cstt|dkrtdt|tkr(td|jttkr>tdt|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)Nrz empty headerztruncated headerzend of file headerz bad checksumrlt|ii i)iIiQiYirirriii)$r/rtrrucountr0rvrKrUrHr<rrhr)r*r'r(rrrr+r,rrAREGTYPErrGNUTYPE_SPARSErDrGrboolrisdirrstrip GNU_TYPES)r rTr3r4robjrrstructsrJrnumbytes isextendedorigsizer5r5r6frombufsZ       zTarInfo.frombufcCs8|jjt}|j||j|j}|jjt|_|j|S)N) rrZrr/r3r4rr _proc_member)r rrTr*r5r5r6 fromtarfileBs zTarInfo.fromtarfilecCsT|jttfkr|j|S|jtkr,|j|S|jtttfkrF|j |S|j |SdS)N) rrr _proc_gnulongr% _proc_sparser r SOLARIS_XHDTYPE _proc_pax _proc_builtin)rrr5r5r6r0Ws    zTarInfo._proc_membercCsR|jj|_|j}|js$|jtkr4||j|j7}||_|j |j |j |j |S)N) rrrisregrSUPPORTED_TYPES_blockr'r_apply_pax_inforr3r4)rrrr5r5r6r6ds zTarInfo._proc_builtinc Cs|jj|j|j}y|j|}Wntk r>tdYnX|j|_|jt krft ||j |j |_ n|jtkrt ||j |j |_|S)Nz missing or bad subsequent header)rrZr9r'r1r rwrrrr<r3r4rrr)rrrTnextr5r5r6r2us  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)Nrrr"i)rrrZrrDrKrGrr&rrrr9r'r) rrr+r-r.rTrrJrr,r5r5r6r3s(    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| dkr`|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.|jd dkrl|jddkrl|j||||jttfkr|j ||j|j|j!|_!d|kr|j"}|j#s|jt$kr||j|j7}||_!|S)Nrzinvalid headerr#rrs hdrcharsetsBINARYzutf-8z missing or bad subsequent headerzGNU.sparse.mapzGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorrCr')%rrZr9r'rr rrr/_header_length_prefix_rematchrHrEgrouprGrrk partitionrr3_decode_pax_fieldr4PAX_NAME_FIELDSr1r rw_proc_gnusparse_01_proc_gnusparse_00r_proc_gnusparse_10r r4r:rrr7r8)rrrTrrr3 raw_headersr@r2Zheader_value_end_offsetZkeyword_and_valueZ raw_keywordZequalsZ raw_valuerrr;rr5r5r6r5sx                 zTarInfo._proc_paxc Csg}g}x|D]\}}}|dkrVy|jt|jWqtk rRtdYqXq|dkry|jt|jWqtk rtdYqXqWtt|||_dS)NsGNU.sparse.offsetzinvalid headersGNU.sparse.numbytes)rrEr:rGrHlistzipr)rr;rHZoffsetsr,_rrr5r5r6rF!szTarInfo._proc_gnusparse_00cCs@dd|djdD}tt|ddd|ddd|_dS)NcSsg|] }t|qSr5)rE).0xr5r5r6 8sz.TarInfo._proc_gnusparse_01..zGNU.sparse.map,r$r#)rrIrJr)rr;rrr5r5r6rE5szTarInfo._proc_gnusparse_01cCsd}g}|jjt}|jdd\}}t|}xJt||dkrvd|krV||jjt7}|jdd\}}|jt|q.W|jj|_t t |ddd|ddd|_ dS)Nrr#r$) rrZrrrEr/rrrrIrJr)rr;rrZfieldsrrTZnumberr5r5r6rG;s  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)NzGNU.sparse.namer%zGNU.sparse.sizer'zGNU.sparse.realsizerr) rsetattrrE PAX_FIELDSPAX_NUMBER_FIELDSrGr(rr)rrr3r4rrr5r5r6r:Ks"  zTarInfo._apply_pax_infoc Cs.y |j|dStk r(|j||SXdS)NrB)r:UnicodeDecodeError)rrr3Zfallback_encodingZfallback_errorsr5r5r6rCbs zTarInfo._decode_pax_fieldcCs"t|t\}}|r|d7}|tS)Nr#)rYr)rr#r`rar5r5r6r9jszTarInfo._blockcCs |jtkS)N)r REGULAR_TYPES)rr5r5r6r7ssz TarInfo.isregcCs|jS)N)r7)rr5r5r6isfileuszTarInfo.isfilecCs |jtkS)N)rr)rr5r5r6r'wsz TarInfo.isdircCs |jtkS)N)rSYMTYPE)rr5r5r6issymysz TarInfo.issymcCs |jtkS)N)rLNKTYPE)rr5r5r6islnk{sz TarInfo.islnkcCs |jtkS)N)rCHRTYPE)rr5r5r6ischr}sz TarInfo.ischrcCs |jtkS)N)rBLKTYPE)rr5r5r6isblksz TarInfo.isblkcCs |jtkS)N)rFIFOTYPE)rr5r5r6isfifoszTarInfo.isfifocCs |jdk S)N)r)rr5r5r6issparseszTarInfo.issparsecCs|jtttfkS)N)rrZr\r^)rr5r5r6isdevsz TarInfo.isdevN)rrhr)r*r'r(rrrr+r,rrrrrrrrr)r)3rqrrrs __slots__rrrpropertyr%rrr&rrrr rrrr classmethodrr staticmethodrrrr r/r1r0r6r2r3r5rFrErGr:rCr9r7rUr'rWrYr[r]r_r`rar5r5r5r6rs^    1    3 ?  z c @seZdZdZdZdZdZeZe Z dZ e Z eZd[ddZedddefd d Zed\d d Zed]ddZed^ddZed_ddZdddddZddZddZddZdd Zd`d!d"Zdadd$d%d&Zdbdd'd(d)Zdcd*d+Z dddd-d.d/Z!dedd-d1d2Z"d3d4Z#dfd5d6Z$d7d8Z%d9d:Z&d;d<Z'd=d>Z(d?d@Z)dAdBZ*dCdDZ+dEdFZ,dGdHZ-dIdJZ.dgdKdLZ/dMdNZ0dhdOdPZ1dQdRZ2dSdTZ3dUdVZ4dWdXZ5dYdZZ6dS)irrFr#NryrcCsddddd}||krtd||_|||_|sj|jdkrVtjj| rVd|_d|_t||j}d |_n@|dkrt|d rt |j t t fr|j }t|d r|j|_d |_|rtjj |nd|_ ||_|dk r||_|dk r||_|dk r||_|dk r||_|dk r||_| |_| dk r4|jtkr4| |_ni|_| dk rJ| |_| dk rZ| |_| |_d |_g|_d |_|jj|_i|_y|jd krd|_ |j!|_ |jdkr8x|jj"|jy|jj#|}|jj$|WnTt%k r|jj"|jPYn0t&k r0}zt't |WYdd}~XnXqW|jdkrd |_|jr|jj(|jj)}|jj*||jt+|7_Wn&|js|jj,d |_YnXdS)Nrbzr+bwbZxb)ryarzrMz!mode must be 'r', 'a', 'w' or 'x'rhrzFrrhTryrM)rhrzrM)-rGrh_moder|r%exists bltn_openrrrrrrLabspathrrPr dereference ignore_zerosr3r4rrdebug errorlevel copybufsizermembers_loadedrrinodes firstmemberr;rr1rrvr rrrr[r/r)rrrhrrPrrmrnr3r4rrorprqZmodeserTr5r5r6rs             $   zTarFile.__init__c s| r| rtd|dkrfdd}xttj|dD]b}tj|}|dk r\|j} y||d|f|Sttfk r|dk r|j| w8Yq8Xq8Wtdnd|kr |jdd \} }| pd} |pd }|jkrtj|}n td |||| |f|Sd |kr|jd d \} }| p.d} |p8d }| dkrLtdt || |||} y|| | f|} Wn| j YnXd| _ | S|dkrj |||f|StddS)Nznothing to openryr:*csj|dkS)Ntaropen) OPEN_METH)r)r r5r6not_compressed<sz$TarFile.open..not_compressed)keyz%file could not be opened successfully:r#rzunknown compression type %r|rzzmode must be 'r' or 'w'FrhrMzundiscernible mode)ryrw)ryrz)rhrzrM) rGsortedryrlrrrrrrrrrx) r rrhrr_kwargsrzrfuncZ saved_posrgstreamrr5)r r6rsP%             z TarFile.opencKs |dkrtd||||f|S)NryrhrzrMz!mode must be 'r', 'a', 'w' or 'x')ryrhrzrM)rG)r rrhrrr5r5r6rxmszTarFile.taropenrcKs|d krtdyddl}|jWn ttfk rBtdYnXy|j||d||}Wn.tk r|dk r|dkrtdYnXy|j|||f|}WnBtk r|j |dkrtdYn|j YnXd |_ |S) NryrzrMzmode must be 'r', 'w' or 'x'rzgzip module is not availablerbznot a gzip fileF)ryrzrM) rGgzipZGzipFilerAttributeErrorrrrrxrr)r rrhr compresslevelrrrr5r5r6gzopenus2 zTarFile.gzopencKs|d krtdy ddl}Wntk r8tdYnX|j|pD|||d}y|j|||f|}WnFttfk r|j|dkrt dYn|jYnXd |_ |S) NryrzrMzmode must be 'r', 'w' or 'x'rzbz2 module is not available)rznot a bzip2 fileF)ryrzrM) rGrrrZBZ2FilerxrEOFErrorrrr)r rrhrrrrrr5r5r6bz2opens(  zTarFile.bz2opencKs|d krtdy ddl}Wntk r8tdYnX|j|pD|||d}y|j|||f|}WnH|jtfk r|j|dkrt dYn|jYnXd |_ |S) NryrzrMzmode must be 'r', 'w' or 'x'rzlzma module is not available)presetznot an lzma fileF)ryrzrM) rGrrrZLZMAFilerxrrrrr)r rrhrrrrrr5r5r6xzopens& zTarFile.xzopenrxrrr)rrrrc Cs|jr dSd|_z`|jdkrn|jjttd|jtd7_t|jt\}}|dkrn|jjtt|Wd|j s|jj XdS)NTrhrzrMr$r)rhrzrM) rrhrr[r0rrrY RECORDSIZErr)rr`rar5r5r6rs z TarFile.closecCs"|j|}|dkrtd||S)Nzfilename %r not found) _getmemberKeyError)rrrr5r5r6 getmembers  zTarFile.getmembercCs|j|js|j|jS)N)_checkrs_loadrr)rr5r5r6 getmembersszTarFile.getmemberscCsdd|jDS)NcSsg|] }|jqSr5)r)rLrr5r5r6rN sz$TarFile.getnames..)r)rr5r5r6getnamesszTarFile.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 tdrltj5|j6|_7tj8|j6|_9|S) Nawxrlstatrr#rmajorminor):rrr|r% splitdriverseplstriprrrrmrrffstatfilenost_modeS_ISREGst_inost_devst_nlinkrtrXrS_ISDIRrS_ISFIFOr^S_ISLNKrVreadlinkS_ISCHRrZS_ISBLKr\rhst_uidr)st_gidr*st_sizer'st_mtimer(rrpwdgetpwuidr+rgrpZgetgrgidr,rst_rdevrrr) rrarcnamerZdrvrZstatresrZstmdinoderr5r5r6 gettarinfo s~                 zTarFile.gettarinfoT)rrcCs|j|dkr|}x|D]}|rttj|jtd|jp@|j|jpJ|jf|j sb|j r|tdd|j |j fntd|j tdtj|jddt|j|jrdnd|r|jrtd |j|jrtd |jtqWdS) Nz%s/%sz%10sz%d,%dz%10dz%d-%02d-%02d %02d:%02d:%02drrrz-> zlink to )rrprfrgrhr+r)r,r*r[r]rrr'rZ localtimer(rr'rWrrYro)rverboserrrr5r5r6rIos* 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) Nrrzuse the filter argument insteadr$ztarfile: Excluded %rztarfile: Skipped %rr#ztarfile: Unsupported type %rrf)r)rrcrdre_dbgrr|r%rlrr7rkaddfiler'listdiraddr) rrr recursiveZexcluderrcrfr5r5r6rsB       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)Nr)r_rr#)rrrrPr3r4rr[rr/rqrXr'rYrr0rrr)rrrrTr_r`rar5r5r6rs   zTarFile.addfile.) numeric_ownercCsg}|dkr|}xF|D]>}|jr<|j|tj|}d|_|j|||j |dqW|jddd|jx|D]}tjj ||j }y,|j |||d|j |||j ||Wqvtk r}z$|jdkrւn|jdd|WYdd}~XqvXqvWdS) Ni) set_attrsrcSs|jS)N)r)rhr5r5r6sz$TarFile.extractall..)r{)rr#z tarfile: %s)r'rrrhextractsortreverser|r%rrchownutimechmodr rpr)rr%rrrZ directoriesrdirpathrvr5r5r6 extractalls,       zTarFile.extractallrcCs |jdt|tr |j|}n|}|jr>tjj||j|_ y"|j |tjj||j ||dWnt k r}zJ|j dkrn6|jdkr|jdd|jn|jdd|j|jfWYdd}~XnBtk r}z$|j dkrn|jdd|WYdd}~XnXdS)Nry)rrrr#z tarfile: %sztarfile: %s %r)rrrrrYr|r%rrr_extract_memberrrrpfilenamerstrerrorr )rmemberr%rrrrvr5r5r6r s(      ( zTarFile.extractcCs|jdt|tr |j|}n|}|js6|jtkrB|j||S|jsR|j rzt|j t rht dq~|j |j|SndSdS)Nryz'cannot extract (sym)link as file object)rrrrr7rr8 fileobjectrYrWrrr extractfile_find_link_target)rrrr5r5r6r3s      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)Nrr#z%s -> %s)r(rr|rr%dirnamerjmakedirsrYrWrrrr7makefiler'makedirr_makefifor[r]makedevmakelinkrr8 makeunknownrrr)rr targetpathrrZ upperdirsr5r5r6rRs4       zTarFile._extract_memberc Cs*ytj|dWntk r$YnXdS)Ni)r|mkdirFileExistsError)rrrr5r5r6rszTarFile.makedircCs|j}|j|j|j}t|df}|jdk rrx,|jD]"\}}|j|t|||t|q6W|j|j|j nt|||jt|WdQRXdS)Nrg) rrrrqrkrrXrr'truncate)rrrsourcer_targetrr'r5r5r6rs      zTarFile.makefilecCs"|j|||jdd|jdS)Nr#z9tarfile: Unknown file type %r, extracted as regular file.)rrr)rrrr5r5r6rs zTarFile.makeunknowncCs"ttdrtj|ntddS)Nmkfifozfifo not supported by system)rr|rr )rrrr5r5r6rs  zTarFile.makefifocCsbttd sttd r td|j}|jr:|tjO}n |tjO}tj||tj |j |j dS)Nmknodrz'special devices not supported by system) rr|r rhr]rfS_IFBLKS_IFCHRrrrr)rrrrhr5r5r6rs  zTarFile.makedevcCsyL|jrtj|j|n0tjj|jr8tj|j|n|j|j ||WnHt k ry|j|j ||Wnt k rt dYnXYnXdS)Nz%unable to resolve link inside archive) rWr|symlinkrr%rjrlinkrrsymlink_exceptionrr )rrrr5r5r6rs    zTarFile.makelinkcCsttdrtjdkr|j}|j}|sytrttdr:ytj||jWntk r8tdYnXdS)Nrzcould not change mode)rr|rrhrr )rrrr5r5r6rs  z TarFile.chmodc CsHttdsdSytj||j|jfWntk rBtdYnXdS)Nrz"could not change modification time)rr|rr(rr )rrrr5r5r6rs  z TarFile.utimecCs|jd|jdk r$|j}d|_|S|j|jjkrZ|jj|jd|jjdsZtdd}xby|jj |}WnHt k r}z2|j r|j dd|j|f|jt 7_wbWYdd}~Xntk r,}zN|j r|j dd|j|f|jt 7_wbn|jdkrtt|WYdd}~Xntk rT|jdkrPtdYnjtk r}z|jdkr~tt|WYdd}~Xn0tk r}ztt|WYdd}~XnXPqbW|dk r|jj|nd|_|S) NZrar#zunexpected end of datar$z0x%X: %srz empty fileT)rrurrrrrZrrr1rvrnrrrHrrtrurwrrrrs)rmrrvr5r5r6r; sL        z TarFile.nextcCsn|j}|dk r"|d|j|}|r2tjj|}x6t|D]*}|rTtjj|j}n|j}||kr<|SqzShow listing of a tarfile)metavarrz-ez --extract+ zExtract tarfile into target dir)nargsrrz-cz--createzCreate tarfile from sourcesz-tz--testzTest if a tarfile is validry)rz{!r} is a tar archive.r#z{!r} is not a tar archive. zr:*)rr$)r%rz{!r} file is extracted.z+{!r} file is extracted into {!r} directory.rrr)z.gzz.tgzz.xzz.txzz.bz2z.tbzz.tbz2z.tb2zw:rzz{!r} file created.)rr)rr)argparseArgumentParser add_argumentZadd_mutually_exclusive_group parse_argsZtestrrrrormrrrPexitrIrrr/r|curdirZ format_helprZcreatepopr%splitextr)rrparserrAargsr\rZtfrrZtar_namerKZextZ compressionsZtar_modeZ tar_files file_namer5r5r6main s                r __main__)r%r&r'r(r)r*r+r,)bversion __author__Z__date__Z __cvsid__ __credits__builtinsrrkrmr|rrWrfrrSrrerrrrNotImplementedErrorrr NameError__all__r0rrrrrrrrr$rXrVrZr\rr^ZCONTTYPErrr%r r r4r rrrr8rTr)rQrDrrErRrr getfilesystemencodingr7r<rKrQrUrXrgrp Exceptionrr rrr r rtrurvrHrwrxrobjectrrBufferedReaderrcompiler?rrrr rqr5r5r5r6 s         wh 4, Z