a Z^3oc @sBdZdZdZddlmZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZz ddlZWneydZYn0z ddlZWneydZYn0eefZzeef7ZWneyYn0gdZdZdZed Zd Zd Zd Zd Zd ZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1e1Z2e e!e"e#e&e'e(e$e%e)e*e+f Z3e e!e(e+fZ4e)e*e+fZ5dZ6hdZ7e8e8e8e9e9e9d Z:ej;d!krd"Zd%d&Z?d'd(Z@d)e2fd*d+ZAd,d-ZBdedfd.d/ZCd0d1ZDGd2d3d3eEZFGd4d5d5eFZGGd6d7d7eFZHGd8d9d9eFZIGd:d;d;eFZJGdd?d?eKZLGd@dAdAeKZMGdBdCdCeKZNGdDdEdEeKZOGdFdGdGeKZPGdHdIdIZQGdJdKdKZRGdLdMdMeSZTGdNdOdOeSZUGdPdQdQejVZWGdRdSdSeSZXGdTdUdUeSZYdVdWZZeYjZdXdYZ[e\dZkr>e[dS)[z0.9.0u"Lars Gustäbel (lars@gustaebel.de)u4Gustavo Niemeyer, Niels Gustäbel, Richard Townsend.)openN)TarFileTarInfo is_tarfileTarError ReadErrorCompressionError StreamError ExtractError HeaderErrorENCODING USTAR_FORMAT GNU_FORMAT PAX_FORMATDEFAULT_FORMATriisustar sustar00ds0s1s2s3s4s5s6s7sLsKsSsxsgsX)pathlinkpathsizemtimeuidgidunamegname>rrrr)ZatimeZctimerrrrZntutf-8cCs(|||}|d||t|tSN)encodelenNUL)slengthencodingerrorsr&/usr/lib64/python3.9/tarfile.pystns r(cCs*|d}|dkr|d|}|||S)Nri)Zfinddecode)r"r$r%pr&r&r'ntss  r+cCs|ddvrbd}tt|dD]}|dK}|||d7}q |ddkrdt|d| }n>z"t|dd}t|p|d d}Wntytd Yn0|S) Nr)rr-asciistrict0zinvalid header)ranger r+intZstrip ValueErrorInvalidHeaderError)r"nir&r&r'ntis    r9r.cCst|}d|kr$d|dkrDnntd|d|fdt}n|tkrd|d |krrd|dkrnnV|dkrtdg}ntdg}d||}t|dD]}|d|d@|dL}qntd |S) Nrr.rz%0*or0r/r,r-zoverflow in number field)r4bytesr!r Z bytearrayr3Zinsertr5)r7digitsformatr"r8r&r&r'itns  2    r=cCs0dttd|}dttd|}||fS)Nr/Z 148B8x356BZ 148b8x356b)ZsumstructZ unpack_from)bufZunsigned_chksumZ signed_chksumr&r&r' calc_chksumss r@c Cs|pd}|dkrdS|dur.t|||dSt||\}}t|D],}||}t||krf|d||qD|dkr||}t||kr|d||dS)Ni@runexpected end of data)shutil copyfileobjdivmodr3readr write) srcZdstr# exceptionbufsizeblocks remainderbr?r&r&r'rCs$       rCcCs8ttjdd}|dur(||d|}t|dddS)Nr$Zbackslashreplacez )end)getattrsysZstdoutrr)print)r"r$r&r&r' _safe_printsrQc@ eZdZdS)rN__name__ __module__ __qualname__r&r&r&r'r rc@rR)r NrSr&r&r&r'r rWr c@rR)rNrSr&r&r&r'rrWrc@rR)rNrSr&r&r&r'rrWrc@rR)rNrSr&r&r&r'rrWrc@rR)r NrSr&r&r&r'r rWr c@rR)EmptyHeaderErrorNrSr&r&r&r'rXrWrXc@rR)TruncatedHeaderErrorNrSr&r&r&r'rYrWrYc@rR)EOFHeaderErrorNrSr&r&r&r'rZ"rWrZc@rR)r6NrSr&r&r&r'r6%rWr6c@rR)SubsequentHeaderErrorNrSr&r&r&r'r[(rWr[c@,eZdZddZddZddZddZd S) _LowLevelFilecCsFtjtjtjBtjBd|}ttdr2|tjO}t||d|_dS)NrwO_BINARYi) osZO_RDONLYZO_WRONLYZO_CREATZO_TRUNChasattrrarfd)selfnamemoder&r&r'__init__5s  z_LowLevelFile.__init__cCst|jdSr)rbcloserdrer&r&r'ri>z_LowLevelFile.closecCst|j|Sr)rbrErdrerr&r&r'rEArkz_LowLevelFile.readcCst|j|dSr)rbrFrdrer"r&r&r'rFDrkz_LowLevelFile.writeN)rTrUrVrhrirErFr&r&r&r'r]/s r]c@sneZdZddZddZddZddZd d Zd d Zd dZ ddZ dddZ ddZ ddZ ddZdS)_Streamc Csd|_|durt||}d|_|dkr6t|}|}|ppackr4time_Stream__writerfendswithrr!)reZ timestampr&r&r'rs z_Stream._init_write_gzcCsR|jdkr|j||j|_|jt|7_|jdkrD|j|}||dS)Nrqrv) r{rrrr}r rZcompressrrmr&r&r'rFs    z _Stream.writecCsN|j|7_t|j|jkrJ|j|jd|j|j|jd|_qdSr)r?r rIr|rFrmr&r&r'Z__writesz_Stream.__writecCs|jr dSd|_z|jdkr:|jdkr:|j|j7_|jdkr|jr|j|jd|_|jdkr|jt d|j |jt d|j d@W|j s|j n|j s|j 0dS)NTr`rvrprqrl)r~rgr{r?rflushr|rFr>rrr}rxrirjr&r&r'ris   z _Stream.closecCs|j|jj |_d|_|ddkr0td|ddkrFtdt|d}|d|d @rt|dd t|d}| ||d @r|d}|r|t krqq|d @r|d}|r|t krqq|d@r|ddS) Nrprsnot a gzip filerszunsupported compression methodr/r.i) rZ decompressobjrrr _Stream__readrrZordrEr!)reZflagZxlenr"r&r&r'rs*       z_Stream._init_read_gzcC|jSr)r}rjr&r&r'tellz _Stream.tellrcCsX||jdkrJt||j|j\}}t|D]}||jq,||ntd|jS)Nrz seeking backwards is not allowed)r}rDrIr3rEr)rer}rJrKr8r&r&r'seeks  z _Stream.seekcCs ||}|jt|7_|Sr)_readr}r )rerr?r&r&r'rEs z _Stream.readcCs|jdkr||St|j}|jg}||kr|jrB|j}d|_n|j|j}|sVqz|j |}Wn|j yt dYn0| ||t|7}q&d |}||d|_|d|S)Nrvrpzinvalid compressed data)r{rr rr?r|rErIrZ decompressrHrappendjoinrerZctr?r&r&r'r s(     z _Stream._readcCsjt|j}|jg}||krF|j|j}|s.qF|||t|7}qd|}||d|_|d|S)Nrp)r r?r|rErIrrrr&r&r'Z__read&s   z_Stream.__readN)r)rTrUrVrhrrrFrrirrrrErrr&r&r&r'rnGs F   rnc@r\) rycCs||_|jt|_dSr)r|rE BLOCKSIZEr?)rer|r&r&r'rh<sz_StreamProxy.__init__cCs|jj|_|jSr)r|rEr?rlr&r&r'rE@s z_StreamProxy.readcCsP|jdrdS|jdddkr8|jdddkr8d S|jd rHd Sd SdS) NsrqrsBZhri s1AY&SYrr)s]s7zXZrtrv)r?Z startswithrjr&r&r'rzDs $ z_StreamProxy.getcomptypecCs|jdSr)r|rirjr&r&r'riNrkz_StreamProxy.closeN)rTrUrVrhrErzrir&r&r&r'ry7s ryc@sfeZdZdddZddZddZdd Zd d Zd d Ze j fddZ dddZ ddZ ddZdS) _FileInFileNcCs||_||_||_d|_t|dd|_d|_|dur>d|fg}d|_g|_d}|j}|D]L\}}||kr||j d||df|j d||||f||7}||}qX||jkr|j d||jdfdS)NrrfFT) r|offsetrpositionrNrfr~ map_indexmapr)rer|rrZ blockinfoZlastposZrealposr&r&r'rh[s(    z_FileInFile.__init__cCsdSrr&rjr&r&r'rtrkz_FileInFile.flushcCdSNTr&rjr&r&r'readablewrkz_FileInFile.readablecCrNFr&rjr&r&r'writablezrkz_FileInFile.writablecCs |jSr)r|seekablerjr&r&r'r}rkz_FileInFile.seekablecCrr)rrjr&r&r'rrz_FileInFile.tellcCs|tjkr tt|d|j|_nj|tjkr\|dkrFt|j|d|_qt|j||j|_n.|tjkrtt|j||jd|_ntd|jS)NrzInvalid argument) ioSEEK_SETminZmaxrrZSEEK_CURZSEEK_ENDr5)rerZwhencer&r&r'rs   z_FileInFile.seekc Cs |dur|j|j}nt||j|j}d}|dkr|j|j\}}}}||jkr`|krhqnqq6|jd7_|jt|jkr6d|_q6t|||j}|r|j||j||j|}t||krt d||7}n |t |7}||8}|j|7_q,|S)NrprrrA) rrrrrr r|rrErr!) rerr?ZdatastartZstoprr#rLr&r&r'rEs,     z_FileInFile.readcCs&|t|}||dt|<t|Sr)rEr )rerLr?r&r&r'readintosz_FileInFile.readintocCs d|_dSr)r~rjr&r&r'rirkz_FileInFile.closerr)rTrUrVrhrrrrrrrrrErrir&r&r&r'rUs  rcseZdZfddZZS) ExFileObjectcs&t|j|j|j|j}t|dSr)rr| offset_datarsparseZsuperrh)retarfiletarinfor| __class__r&r'rhs zExFileObject.__init__)rTrUrVrhZ __classcell__r&r&rr'rrWrc@seZdZeddddddddd d d d d ddddddddZddddZeddZejddZeddZ e jddZ ddZ dd Z e e d!fd"d#Zd$d%Zd&d'Zd(d)Zed*d+Zd,d-Zed.d/Zed0d1Zed2d3Zed4d5Zed6d7Zed8d9Zd:d;Zdd?Zd@dAZdBdCZ dDdEZ!dFdGZ"dHdIZ#dJdKZ$dLdMZ%dNdOZ&dPdQZ'dRdSZ(dTdUZ)dVdWZ*dXdYZ+dZd[Z,d\d]Z-d^d_Z.d`daZ/dbdcZ0dS)erzName 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.zFile type. type is usually one of these constants: REGTYPE, AREGTYPE, LNKTYPE, SYMTYPE, DIRTYPE, FIFOTYPE, CONTTYPE, CHRTYPE, BLKTYPE, GNUTYPE_SPARSE.zcName of the target file name, which is only present in TarInfo objects of type LNKTYPE and SYMTYPE.z User name.z Group name.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)rfrgrrrrchksumtypelinknamerrdevmajordevminorrr pax_headersrr_sparse_structs _link_targetrocCsj||_d|_d|_d|_d|_d|_d|_t|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_i|_dS)Nirro)rfrgrrrrrREGTYPErrrrrrrrrrrerfr&r&r'rhs"zTarInfo.__init__cCrrrfrjr&r&r'rrz TarInfo.pathcC ||_dSrrrr&r&r'rcCrrrrjr&r&r'r rzTarInfo.linkpathcCrrr)rerr&r&r'rrcCsd|jj|jt|fS)Nz<%s %r at %#x>)rrTrfZidrjr&r&r'__repr__rkzTarInfo.__repr__cCsl|j|jd@|j|j|j|j|j|j|j|j |j |j |j d }|dt krh|ddsh|dd7<|S)N) rfrgrrrrrrrrrrrrrf/)rfrgrrrrrrrrrrrDIRTYPEr)reinfor&r&r'get_infos"zTarInfo.get_infosurrogateescapecCsT|}|tkr||||S|tkr4||||S|tkrH|||StddS)Nzinvalid format)rr create_ustar_headerr create_gnu_headerrcreate_pax_headerr5)rer<r$r%rr&r&r'tobuf0s z TarInfo.tobufcCsnt|d<t|d||tkr(tdt|d||tkr^||d||\|d<|d<||t||S)Nmagicrzlinkname is too longrfprefix) POSIX_MAGICr r LENGTH_LINKr5 LENGTH_NAME_posix_split_name_create_headerr )rerr$r%r&r&r'r>s zTarInfo.create_ustar_headercCst|d<d}t|d||tkr<|||dt||7}t|d||tkrl|||dt||7}|||t ||S)Nrrprrf) GNU_MAGICr rr_create_gnu_long_headerGNUTYPE_LONGLINKrGNUTYPE_LONGNAMErr )rerr$r%r?r&r&r'rKszTarInfo.create_gnu_headerc Cs(t|d<|j}ddtfddtfddfD]h\}}}||vr>q*z||dd Wn"tyt||||<Yq*Yn0t|||kr*||||<q*d D]`\}}||vrd ||<q||}d |krd |d krnn t|t rt |||<d ||<q|r| |t |} nd} | | |tddS)Nrrfrrr)rr )rrrr0r1))rr.)rr.)r )rrrr.rrpr)rrcopyrrrUnicodeEncodeErrorr isinstancefloatstr_create_pax_generic_headerXHDTYPErr ) rerr$rrfZhnamer#r;Zvalr?r&r&r'rYs4     *  zTarInfo.create_pax_headercCs||tdS)Nr)rXGLTYPE)clsrr&r&r'create_pax_global_headerz TarInfo.create_pax_global_headercCs~|d}tdt|D]T}d|d|}d||d}t|||tkrt|||tkrqvqtd||fS)Nrrzname is too long)splitr3r rr LENGTH_PREFIXrr5)rerfr$r%Z componentsr8rr&r&r'rs zTarInfo._posix_split_namec Cs|dttfv}|r@t|ddd|}t|ddd|}ntdd||}tdd||}t|ddd||t|d dd @d|t|d dd|t|d dd|t|d dd|t|ddd|d|dtt|ddd|||dtt|ddd||t|ddd||||t|ddd||g}tdt d |}t |t dd} |ddt d| d|dd}|S)Nrrrr.rrorfrrgrrrrrrs rrrrrrrz%dsrpiz%06or0i) getCHRTYPEBLKTYPEr=r(rrr>rrrr@r:) rr<r$r%Zhas_device_fieldsrrZpartsr?rr&r&r'rs4  &zTarInfo._create_headercCs.tt|t\}}|dkr*|t|t7}|S)Nr)rDr rr!)ZpayloadrJrKr&r&r'_create_payloadszTarInfo._create_payloadcCsR|||t}i}d|d<||d<t||d<t|d<||t||||S)Nz ././@LongLinkrfrrr)rr!r rrr r)rrfrr$r%rr&r&r'rs zTarInfo._create_gnu_long_headerc Cs0d}|D]6\}}z|ddWq ty@d}YqDYq 0q d}|rT|d7}|D]\}}|d}|r||d}n |d}t|t|d}d } } |tt| } | | krq| } q|tt| d d |d |d 7}q\i} d| d<|| d<t|| d<t| d<|| td d| |S)NFrr1Trps21 hdrcharset=BINARY rrrr0s s= z././@PaxHeaderrfrrrr) itemsrrr rr:rrr r) rrrr$ZbinarykeywordvalueZrecordsZlr7r*rr&r&r'rs<   ( z"TarInfo._create_pax_generic_headerc Cstt|dkrtdt|tkr(td|ttkr>tdt|dd}|t|vrbt 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 drt|_|jtkr6d}g}tdD]j} z0t|||d} t||d|d} WntyYqYn0|| | f|d7}qt|d} t|dd} || | f|_ |!rN|j "d|_ |rp|jt#vrp|d|j |_ |S)Nrz empty headerztruncated headerzend of file headeriiz bad checksumriliti|iiii i)iIiQiYirirriii)$r rXrrYcountr!rZr9r@r6r+rfrgrrrrrrrrrrrAREGTYPErrGNUTYPE_SPARSEr3r5rboolrisdirrstrip GNU_TYPES)rr?r$r%robjrr}structsr8rnumbytes isextendedorigsizer&r&r'frombufsZ         zTarInfo.frombufcCs8|jt}|||j|j}|jt|_||Sr) r|rErrr$r%rr _proc_member)rrr?rr&r&r' fromtarfileQs zTarInfo.fromtarfilecCsT|jttfvr||S|jtkr,||S|jtttfvrF| |S| |SdSr) rrr _proc_gnulongr _proc_sparserrSOLARIS_XHDTYPE _proc_pax _proc_builtin)rerr&r&r'rfs    zTarInfo._proc_membercCsR|j|_|j}|s$|jtvr4|||j7}||_| |j |j |j |Sr) r|rrisregrSUPPORTED_TYPES_blockrr_apply_pax_inforr$r%)rerrr&r&r'r ss zTarInfo._proc_builtincCs|j||j}z||}Wnty<tdYn0|j|_|jt krdt ||j |j |_ n|jtkrt ||j |j |_|S)N missing or bad subsequent header)r|rEr rrr r[rrrr+r$r%rfrr)rerr?nextr&r&r'rs   zTarInfo._proc_gnulongc Cs|j\}}}|`|r|jt}d}tdD]l}z0t|||d}t||d|d} WntyxYqYn0|r| r||| f|d7}q,t|d}q||_ |j |_ |j | |j |_||_ |S)Nrirri)rr|rErr3r9r5rrrrrr rr) rerrrrr?r}r8rrr&r&r'rs(       zTarInfo._proc_sparsecCs|j||j}|jtkr&|j}n |j}t d|}|durX| d d|d<| d}|dkrr|j }nd}td}d}|||}|sq&|\} } t| } ||dd|d| d} || dd|j} | tvr|| ||j |j} n|| dd|j} | || <|| 7}qz||} WntyPtd Yn0d |vrj|| |nHd |vr|| ||n.| d d kr| ddkr|| |||jttfvr| ||j |j|j | _ d|vr| j!} | "s| jt#vr| | | j7} | |_ | S)Ns\d+ hdrcharset=([^\n]+)\nrr hdrcharsetZBINARYs(\d+) ([^=]+)=rrrGNU.sparse.mapGNU.sparse.sizezGNU.sparse.majorZ1zGNU.sparse.minorr2r)$r|rEr rrrrrreZsearchgroupr)rr$ZcompilematchZgroupsr4rMr_decode_pax_fieldr%PAX_NAME_FIELDSrr r[_proc_gnusparse_01_proc_gnusparse_00_proc_gnusparse_10rrrrrr r )rerr?rrrr$Zregexr}r#rrrrr&r&r'r sd       $         zTarInfo._proc_paxcCshg}td|D]}|t|dqg}td|D]}|t|dq:tt|||_dS)Ns\d+ GNU.sparse.offset=(\d+)\nrs\d+ GNU.sparse.numbytes=(\d+)\n)rZfinditerrr4rlistzipr)rerrr?Zoffsetsrrr&r&r'rszTarInfo._proc_gnusparse_00cCs@dd|ddD}tt|ddd|ddd|_dS)NcSsg|] }t|qSr&)r4).0xr&r&r' *rpz.TarInfo._proc_gnusparse_01..rz,rr)rrrr)rerrrr&r&r'r'szTarInfo._proc_gnusparse_01cCsd}g}|jt}|dd\}}t|}t||dkrtd|vrT||jt7}|dd\}}|t|q,|j|_t t |ddd|ddd|_ dS)Nrrr) r|rErrr4r rrrrrr)rerrrZfieldsrr?Znumberr&r&r'r-s  zTarInfo._proc_gnusparse_10c Cs|D]\}}|dkr&t|d|q|dkr@t|dt|q|dkrZt|dt|q|tvr|tvrzt||}Wntyd}Yn0|dkr|d}t|||q||_dS)NzGNU.sparse.namerrrzGNU.sparse.realsizerr) rZsetattrr4 PAX_FIELDSPAX_NUMBER_FIELDSr5rrr)rerr$r%rrr&r&r'r=s"   zTarInfo._apply_pax_infocCs2z||dWSty,|||YS0dS)Nr1)r)ZUnicodeDecodeError)rerr$Zfallback_encodingZfallback_errorsr&r&r'rTs zTarInfo._decode_pax_fieldcCs"t|t\}}|r|d7}|tS)Nr)rDr)rerrJrKr&r&r'r \szTarInfo._blockcCs |jtvSr)r REGULAR_TYPESrjr&r&r'r erz TarInfo.isregcCs|Sr)r rjr&r&r'isfileirzTarInfo.isfilecC |jtkSr)rrrjr&r&r'rmrz TarInfo.isdircCr%r)rSYMTYPErjr&r&r'issymqrz TarInfo.issymcCr%r)rLNKTYPErjr&r&r'islnkurz TarInfo.islnkcCr%r)rrrjr&r&r'ischryrz TarInfo.ischrcCr%r)rrrjr&r&r'isblk}rz TarInfo.isblkcCr%r)rFIFOTYPErjr&r&r'isfiforzTarInfo.isfifocCs |jduSr)rrjr&r&r'issparserkzTarInfo.issparsecCs|jtttfvSr)rrrr,rjr&r&r'isdevrz TarInfo.isdev)ro)1rTrUrVZdictZ __slots__rhZpropertyrZsetterrrrrr rrrr classmethodrrZ staticmethodrrrrrrrr rrr rrrrrr r r$rr'r)r*r+r-r.r/r&r&r&r'rs      1  #   2 >  f  rc @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)irrFrNr_rc Csddddd}||vrtd||_|||_|sh|jdkrTtj|sTd|_d|_t||j}d |_n@|durt|d rt |j t t fr|j }t|d r|j|_d |_|rtj |nd|_ ||_|dur||_|dur||_|dur||_|dur||_|dur ||_| |_| dur0|jtkr0| |_ni|_| durF| |_| durV| |_| |_d |_g|_d |_|j|_i|_z|jd krd|_ |!|_ |jdkr2|j"|jz|j#|}|j$|WnXt%y|j"|jYq2Yn2t&y,}zt't |WYd}~n d}~00q|jdvr|d |_|jr||j(|j)}|j*||jt+|7_Wn&|js|j,d |_Yn0dS)Nrbzr+bwbZxbr_ar`r!mode must be 'r', 'a', 'w' or 'x'r4r`FrfrgTr_r4r`r)-r5rgZ_moderbrexists bltn_openrxrcrrfrr:abspathr|r<r dereference ignore_zerosr$r%rrdebug errorlevel copybufsizer~members_loadedrrinodes firstmemberrrrrrZr rrrrFr ri)rerfrgr|r<rr:r;r$r%rr<r=r>Zmodeser?r&r&r'rhs             &   zTarFile.__init__c s|s|std|dvrfdd}tj|dD]l}tj|}|durV|} z||d|fi|WSttfy|dur|| Yq2Yq20q2tdnd|vr|dd \} }| pd} |pd }|jvrtj|}n td |||| |fi|Sd |vr|d d \} }| p4d} |p>d }| d vrRtdt || |||} z|| | fi|} Wn| Yn0d| _ | S|dvrj |||fi|StddS)Nznothing to open)r_r:*csj|dkS)Ntaropen) OPEN_METH)r{rr&r'not_compressed@rkz$TarFile.open..not_compressedZkeyr_z%file could not be opened successfullyz:rrvrwz|r^zmode must be 'r' or 'w'Fr6zundiscernible mode) r5sortedrFrNrrrrrrnrirxrE) rrfrgr|rIkwargsrHr{ZfuncZ saved_posfilemodeZstreamrr&rGr'rsP%            z TarFile.opencKs$|dvrtd||||fi|S)Nr3r5)r5)rrfrgr|rKr&r&r'rEqszTarFile.taropenrcKs|dvrtdzddlm}Wnty:tdYn0z|||d||}Wn,ty~|durx|dkrxtdYn0z|j|||fi|}Wn@ty||dkrtdYn|Yn0d |_ |S) Nr_r`rmode must be 'r', 'w' or 'x'r)GzipFilezgzip module is not availablerLr_rF) r5ZgziprOrrrrrErirx)rrfrgr| compresslevelrKrOrr&r&r'gzopenys0   zTarFile.gzopenc Ks|dvrtdzddlm}Wnty:tdYn0||pD|||d}z|j|||fi|}WnDttfy||dkrt dYn|Yn0d |_ |S) NrMrNr)BZ2Filers)rPr_znot a bzip2 fileF) r5rrrRrrrErEOFErrorrirrx)rrfrgr|rPrKrRrr&r&r'bz2opens& zTarFile.bz2openc Ks|dvrtdzddlm}m}Wnty>tdYn0||pH|||d}z|j|||fi|}WnD|tfy||dkrt dYn|Yn0d |_ |S) NrMrNr)LZMAFilerru)presetr_znot an lzma fileF) r5rrUrrrrErSrirrx) rrfrgr|rVrKrUrrr&r&r'xzopens& zTarFile.xzopenrErQrTrW)rvrqrrrtcCs|jr dSd|_zp|jdvrn|jttd|jtd7_t|jt\}}|dkrn|jtt|W|j s|j n|j s|j 0dS)NTr6rr) r~rgr|rFr!rrrD RECORDSIZErxri)rerJrKr&r&r'ris  z TarFile.closecCs"||}|durtd||S)Nzfilename %r not found) _getmemberKeyError)rerfrr&r&r' getmembers  zTarFile.getmembercCs||js||jSr)_checkr@_loadr?rjr&r&r' getmembersszTarFile.getmemberscCsdd|DS)NcSsg|] }|jqSr&r)rrr&r&r'r  rpz$TarFile.getnames..)r^rjr&r&r'getnamesrzTarFile.getnamesc CsZ|d|dur|j}|dur$|}tj|\}}|tjd}|d}|}||_ |dur|j stt |}qt |}nt |}d}|j}t |r|j|jf} |j s|jdkr| |jvr||j| krt} |j| }nt} | dr||j| <nht |rt} nVt |r"t} nDt |r>t} t|}n(t |rPt} nt |rbt!} ndS||_||_"|j#|_$|j%|_&| tkr|j'|_(nd|_(|j)|_*| |_+||_,t-rzt-.|j$d|_/Wnt0yYn0t1rzt12|j&d|_3Wnt0yYn0| tt!fvrVt4tdrVt4tdrVt5|j6|_7t8|j6|_9|S)Nawxrrorrmajorminor):r\rfrbrZ splitdriversepZlstriprrr:ZlstatstatZfstatZfilenoZst_modeZS_ISREGZst_inoZst_devZst_nlinkrAr(rZS_ISDIRrZS_ISFIFOr,ZS_ISLNKr&ZreadlinkZS_ISCHRrZS_ISBLKrrgZst_uidrZst_gidrZst_sizerZst_mtimerrrpwdZgetpwuidrrZgrpZgetgrgidrrcraZst_rdevrrbr) rerfarcnamer|ZdrvrZstatresrZstmdZinoderr&r&r' gettarinfo s                 zTarFile.gettarinfoT)r?cCs||dur|}|D]}|rtt|jtd|jp>|j|jpH|jf| s`| rztdd|j |j fntd|j tdt|jddt|j|rdnd|r|rtd |j|rtd |jtqdS) Nz%s/%sz%10sz%d,%dz%10dz%d-%02d-%02d %02d:%02d:%02drrroz-> zlink to )r\rQrdrLrgrrrrr*r+rrrrZ localtimerrfrr'rr)rP)reverboser?rr&r&r'rps0 z TarFile.listfiltercCsL|d|dur|}|jdurFtj||jkrF|dd|dS|d||||}|durz|dd|dS|dur||}|dur|dd|dS|rt|d}| ||Wdn1s0Ynb| r>| ||rHt t |D],}|j tj||tj||||dqn | |dS) Nr`rztarfile: Skipped %rrztarfile: Unsupported type %rztarfile: Excluded %rr1rj)r\rfrbrr9_dbgrhr r8addfilerrJZlistdiraddr)rerfrgZ recursiverkrZfr&r&r'rns6    ,   z TarFile.addcCs|dt|}||j|j|j}|j||jt |7_|j }|durt ||j|j |dt |j t\}}|dkr|jtt||d7}|j|t7_|j|dS)Nr`)rIrr)r\rrr<r$r%r|rFrr r>rCrrDrr!r?r)rerr|r?rIrJrKr&r&r'rms   zTarFile.addfile. numeric_ownerc Csg}|dur|}|D]>}|r:||t|}d|_|j||| |dq|jddd||D]}tj ||j }z,|j |||d| ||| ||Wqpty}z*|jdkr΂n|dd|WYd}~qpd}~00qpdS) N set_attrsrqcSrrr)r4r&r&r'Zrpz$TarFile.extractall..rIrpr tarfile: %s)rrrrgextractZsortZreverserbrrrfchownutimechmodr r=rl)rerr?rqZ directoriesrZdirpathrCr&r&r' extractalls.    zTarFile.extractallroc Cs|dt|tr ||}n|}|r>tj||j|_ z"|j |tj||j ||dWnt y}zP|j dkr~n6|jdur|dd|jn|dd|j|jfWYd}~nLd}~0ty }z*|j dkrn|dd|WYd}~n d}~00dS)Nr_rsrrruztarfile: %s %r)r\rrr[r)rbrrrr_extract_memberrfrr=ZfilenamerlZstrerrorr )rememberrrtrqrrCr&r&r'rvs*      , zTarFile.extractcCs|dt|tr ||}n|}|s6|jtvrB|||S|sR| rzt|j t rht dq~| ||SndSdS)Nr_z'cannot extract (sym)link as file object)r\rrr[r rr  fileobjectr)r'r|rnr extractfile_find_link_target)rer|rr&r&r'r~*s      zTarFile.extractfilecCsT|d}|dtj}tj|}|r>tj|s>t||sN| rh| dd|j |j fn| d|j | r|||n|r|||nx|r|||nb|s|r|||nD|s| r|||n&|jtvr|||n ||||rP||||| sP||||||dS)Nrrz%s -> %s)rrrbrcrdirnamer7Zmakedirsr)r'rlrfrr makefilermakedirr-makefifor*r+makedevmakelinkrr  makeunknownrwryrx)rer targetpathrtrqZ upperdirsr&r&r'r{Is4       zTarFile._extract_membercCs(zt|dWnty"Yn0dS)Nrr)rbZmkdirZFileExistsErrorrerrr&r&r'rzs zTarFile.makedircCs|j}||j|j}t|dl}|jdurn|jD]"\}}||t|||t|q4||j| nt|||jt|Wdn1s0YdS)Nr2) r|rrr>r8rrCrrZtruncate)rerrZsourcerIZtargetrrr&r&r'rs      zTarFile.makefilecCs"||||dd|jdS)Nrz9tarfile: Unknown file type %r, extracted as regular file.)rrlrrr&r&r'rs zTarFile.makeunknowncCs"ttdrt|ntddS)Nmkfifozfifo not supported by system)rcrbrr rr&r&r'rs  zTarFile.makefifocCs^ttdrttdstd|j}|r6|tjO}n |tjO}t||t |j |j dS)Nmknodrz'special devices not supported by system) rcrbr rgr+rdZS_IFBLKZS_IFCHRrrrr)rerrrgr&r&r'rs  zTarFile.makedevc CszL|rt|j|n0tj|jr8t|j|n|| ||WnDt yz|| ||Wnt yt dYn0Yn0dS)Nz%unable to resolve link inside archive) r'rbZsymlinkrrr7rZlinkr{rsymlink_exceptionrZr rr&r&r'rs    zTarFile.makelinkcCsttdrtdkr|j}|j}|s~ztrzShow listing of a tarfile)metavarrz-ez --extractz+)rz zExtract tarfile into target dir)Znargsrrz-cz--create)zzzCreate tarfile from sourcesz-tz--testzTest if a tarfile is validr_rz{!r} is a tar archive.rz{!r} is not a tar archive. rD)rir)rroz{!r} file is extracted.z+{!r} file is extracted into {!r} directory.rqrtrr)rz.tgzz.xzz.txzz.bz2z.tbzz.tbz2z.tb2zw:r`z{!r} file created.)argparseZArgumentParserZ add_argumentZadd_mutually_exclusive_groupZ parse_argsZtestrrr^rPrOrrir<Zexitrrrvr rbcurdirZ format_helprzZcreateZpoprZsplitextrn)rrZparserrZargsrGrvZtfrrZtar_nameZ_ZextZ compressionsZtar_modeZ tar_filesZ file_namer&r&r'main s          0  0    ,      .rZ__main__)]ZversionZ __author__Z __credits__Zbuiltinsrr8rOrbrrBrdrr>rrrerrfZAttributeErrorZNotImplementedErrorrrZ NameErrorZ__all__r!rrXrrrrrrrr(r&rrrr,ZCONTTYPErrrrrrr r rrr r#rr!rrr4r"rfr Zgetfilesystemencodingr(r+r9r=r@rCrQZ Exceptionrr rrrr rXrYrZr6r[r]rnZobjectryrZBufferedReaderrrrrrrTr&r&r&r'Z s          qh JW