a Z^3o @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_FORMATrisustar sustar00d01234567LKSxgX)pathlinkpathsizemtimeuidgidunamegname>r+r%r&r,)Zatimectimer(r)r*r'ntutf-8cCs(|||}|d||t|tSN)encodelenNUL)slengthencodingerrorsr8/usr/lib64/python3.9/tarfile.pystns r:cCs*|d}|dkr|d|}|||S)Nr)finddecode)r4r6r7pr8r8r9ntss  r?cCs|ddvrbd}tt|dD]}|dK}|||d7}q |ddkrdt|d| }n>z"t|dd}t|p|d d}Wntytd Yn0|S) Nr)r#rAasciistrict0zinvalid header)ranger2r?intstrip ValueErrorInvalidHeaderError)r4nir8r8r9ntis    rNrBcCst|}d|kr$d|dkrDnntd|d|fdt}n|tkrd|d |krrd|dkrnnV|dkrtdg}ntdg}d||}t|dD]}|d|d@|dL}qntd |S) NrrBr#z%0*orDrCr@rAzoverflow in number field)rHbytesr3r bytearrayrGinsertrJ)rLdigitsformatr4rMr8r8r9itns  2    rTcCs0dttd|}dttd|}||fS)NrCZ 148B8x356BZ 148b8x356b)sumstruct unpack_from)bufZunsigned_chksumZ signed_chksumr8r8r9 calc_chksumss rYc Cs|pd}|dkrdS|dur.t|||dSt||\}}t|D],}||}t||krf|d||qD|dkr||}t||kr|d||dS)Ni@runexpected end of data)shutil copyfileobjdivmodrGreadr2write) srcdstr5 exceptionbufsizeblocks remainderbrXr8r8r9r\s$       r\cCs8ttjdd}|dur(||d|}t|dddS)Nr6backslashreplace )end)getattrsysstdoutr1r=print)r4r6r8r8r9 _safe_printsrnc@s eZdZdS)rN__name__ __module__ __qualname__r8r8r8r9r src@s eZdZdS)r Nror8r8r8r9r sr c@s eZdZdS)rNror8r8r8r9rsrc@s eZdZdS)rNror8r8r8r9rsrc@s eZdZdS)r Nror8r8r8r9r sr c@s eZdZdS)r Nror8r8r8r9r sr c@s eZdZdS)EmptyHeaderErrorNror8r8r8r9rssrsc@s eZdZdS)TruncatedHeaderErrorNror8r8r8r9rtsrtc@s eZdZdS)EOFHeaderErrorNror8r8r8r9ru"sruc@s eZdZdS)rKNror8r8r8r9rK%srKc@s eZdZdS)SubsequentHeaderErrorNror8r8r8r9rv(srvc@s,eZdZddZddZddZddZd S) _LowLevelFilecCsFtjtjtjBtjBd|}ttdr2|tjO}t||d|_dS)NrwO_BINARYi) osO_RDONLYO_WRONLYO_CREATO_TRUNChasattrr{rfd)selfnamemoder8r8r9__init__5s  z_LowLevelFile.__init__cCst|jdSr0)r|closerrr8r8r9r>sz_LowLevelFile.closecCst|j|Sr0)r|r^rrr'r8r8r9r^Asz_LowLevelFile.readcCst|j|dSr0)r|r_rrr4r8r8r9r_Dsz_LowLevelFile.writeN)rprqrrrrr^r_r8r8r8r9rw/s rwc@sneZdZddZddZddZddZd d Zd d Zd dZ ddZ dddZ ddZ ddZ ddZdS)_Streamc Csd|_|durt||}d|_|dkr6t|}|}|pd|fg}d|_g|_d}|j}|D]L\}}||kr||j d||df|j d||||f||7}||}qX||jkr|j d||jdfdS)NrrFT) roffsetr'positionrjrr map_indexmapr)rrrr'Z blockinfoZlastposZrealposr8r8r9r[s(    z_FileInFile.__init__cCsdSr0r8rr8r8r9rtsz_FileInFile.flushcCsdSNTr8rr8r8r9readablewsz_FileInFile.readablecCsdS)NFr8rr8r8r9writablezsz_FileInFile.writablecCs |jSr0)rseekablerr8r8r9r}sz_FileInFile.seekablecCs|jSr0)rrr8r8r9rsz_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_ENDrJ)rrwhencer8r8r9rs   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)Nrrr#rZ) r'rrrrr2rrr^rr3) rr'rXdatastartstoprr5rfr8r8r9r^s,     z_FileInFile.readcCs&|t|}||dt|<t|Sr0)r^r2)rrfrXr8r8r9readintosz_FileInFile.readintocCs d|_dSr)rrr8r8r9rsz_FileInFile.close)N)N)rprqrrrrrrrrrrrr^rrr8r8r8r9rUs  rcseZdZfddZZS) ExFileObjectcs&t|j|j|j|j}t|dSr0)rr offset_datar'sparsesuperr)rtarfiletarinfor __class__r8r9rs zExFileObject.__init__)rprqrrr __classcell__r8r8rr9rsrc@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)rrr)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)rrr)r*r'r(rREGTYPErrr+r,rrrrrrrrr8r8r9rs"zTarInfo.__init__cCs|jSr0rrr8r8r9r%sz TarInfo.pathcCs ||_dSr0rrr8r8r9r%scCs|jSr0rrr8r8r9r& szTarInfo.linkpathcCs ||_dSr0r)rrr8r8r9r&scCsd|jj|jt|fS)Nz<%s %r at %#x>)rrpridrr8r8r9__repr__szTarInfo.__repr__cCsl|j|jd@|j|j|j|j|j|j|j|j |j |j |j d }|dt krh|ddsh|dd7<|S)N) rrr)r*r'r(rrrr+r,rrrr/)rrr)r*r'r(rrrr+r,rrDIRTYPEr)rinfor8r8r9get_infos"zTarInfo.get_infosurrogateescapecCsT|}|tkr||||S|tkr4||||S|tkrH|||StddS)Nzinvalid format)rr create_ustar_headerrcreate_gnu_headerrcreate_pax_headerrJ)rrSr6r7rr8r8r9tobuf0s z TarInfo.tobufcCsnt|d<t|d||tkr(tdt|d||tkr^||d||\|d<|d<||t||S)Nmagicrzlinkname is too longrprefix) POSIX_MAGICr2r1 LENGTH_LINKrJ LENGTH_NAME_posix_split_name_create_headerr )rrr6r7r8r8r9r>s zTarInfo.create_ustar_headercCst|d<d}t|d||tkr<|||dt||7}t|d||tkrl|||dt||7}|||t ||S)Nrrrr) GNU_MAGICr2r1r _create_gnu_long_headerGNUTYPE_LONGLINKr GNUTYPE_LONGNAMErr)rrr6r7rXr8r8r9rKszTarInfo.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)Nrrr%rr&)r+r+ )r,r,rrDrE))r)rB)r*rB)r' )r(rrrBr#rr)r rcopyr r r1UnicodeEncodeErrorr2 isinstancefloatstr_create_pax_generic_headerXHDTYPErr ) rrr6rrZhnamer5rRvalrXr8r8r9rYs4     *  zTarInfo.create_pax_headercCs||tdS)Nr/)rXGLTYPE)clsrr8r8r9create_pax_global_headersz TarInfo.create_pax_global_headercCs~|d}tdt|D]T}d|d|}d||d}t|||tkrt|||tkrqvqtd||fS)Nrr#zname is too long)splitrGr2rr1 LENGTH_PREFIXr rJ)rrr6r7 componentsrMr r8r8r9r s 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)NrrrrBrrrrrrr)r*r'rr(s rrr+rr,r rz%dsriz%06orDi) getCHRTYPEBLKTYPErTr:rr rVrrrrYrO) rrSr6r7Zhas_device_fieldsrrpartsrXrr8r8r9rs4  &zTarInfo._create_headercCs.tt|t\}}|dkr*|t|t7}|S)Nr)r]r2rr3)payloadrdrer8r8r9_create_payloadszTarInfo._create_payloadcCsR|||t}i}d|d<||d<t||d<t|d<||t||||S)Nz ././@LongLinkrrr'r)r1r3r2rrr r()rrrr6r7rr8r8r9rs 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)NFr/rETrs21 hdrcharset=BINARY rrrrD = z././@PaxHeaderrrr'rr) itemsr1rr2rrOr rr r() rrrr6binarykeywordvaluerecordslrLr>rr8r8r9rs<   ( 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 headerz bad checksumrlt|ii i)iIiQiYirirriii)$r2rsrrtcountr3rurNrYrKr?rrr)r*r'r(rrrr+r,rrAREGTYPErrGNUTYPE_SPARSErGrJrboolrisdirrstrip GNU_TYPES)rrXr6r7robjr rstructsrMrnumbytes isextendedorigsizer8r8r9frombufsZ         zTarInfo.frombufcCs8|jt}|||j|j}|jt|_||Sr0) rr^rrFr6r7rr _proc_member)rrrXrAr8r8r9 fromtarfileQs zTarInfo.fromtarfilecCsT|jttfvr||S|jtkr,||S|jtttfvrF| |S| |SdSr0) rrr _proc_gnulongr< _proc_sparserrSOLARIS_XHDTYPE _proc_pax _proc_builtin)rrr8r8r9rGfs    zTarInfo._proc_membercCsR|j|_|j}|s$|jtvr4|||j7}||_| |j |j |j |Sr0) rrrisregrSUPPORTED_TYPES_blockr'r_apply_pax_inforr6r7)rrrr8r8r9rMss zTarInfo._proc_builtincCs|j||j}z||}Wnty<tdYn0|j|_|jt krdt ||j |j |_ n|jtkrt ||j |j |_|S)N missing or bad subsequent header)rr^rPr'rHr rvrrrr?r6r7rrr)rrrXnextr8r8r9rIs   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)Nrrr9i)rrr^rrGrNrJrr=rrrrPr'r) rrrBrDrErXrrMrrCr8r8r9rJs(       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]+)\nr#r/ hdrcharsetZBINARYs(\d+) ([^=]+)=rr$rRGNU.sparse.mapGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorrFr')$rr^rPr'rrrrresearchgroupr=r#r6compilematchgroupsrHrir_decode_pax_fieldr7PAX_NAME_FIELDSrHr rv_proc_gnusparse_01_proc_gnusparse_00_proc_gnusparse_10rrKrQrrrNrO)rrrXrr]rUr6Zregexrr5r.r/rSrr8r8r9rLsd       $         zTarInfo._proc_paxcCshg}td|D]}|t|dqg}td|D]}|t|dq:tt|||_dS)Ns\d+ GNU.sparse.offset=(\d+)\nr#s\d+ GNU.sparse.numbytes=(\d+)\n)rYfinditerrrHr[listzipr)rrSrrXZoffsetsr]rCr8r8r9rbszTarInfo._proc_gnusparse_00cCs@dd|ddD}tt|ddd|ddd|_dS)NcSsg|] }t|qSr8)rH).0xr8r8r9 *rz.TarInfo._proc_gnusparse_01..rV,r$r#)r rerfr)rrSrrr8r8r9ra'szTarInfo._proc_gnusparse_01cCsd}g}|jt}|dd\}}t|}t||dkrtd|vrT||jt7}|dd\}}|t|q,|j|_t t |ddd|ddd|_ dS)Nr+r#r$) rr^rr rHr2rrrrerfr)rrSrrfieldsrrXZnumberr8r8r9rc-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.namer%rWr'zGNU.sparse.realsizerr) r,setattrrH PAX_FIELDSPAX_NUMBER_FIELDSrJr?rr)rrr6r7r.r/r8r8r9rQ=s"   zTarInfo._apply_pax_infocCs2z||dWSty,|||YS0dS)NrE)r=UnicodeDecodeError)rr/r6Zfallback_encodingZfallback_errorsr8r8r9r_Ts zTarInfo._decode_pax_fieldcCs"t|t\}}|r|d7}|tS)Nr#)r]r)rr:rdrer8r8r9rP\szTarInfo._blockcCs |jtvSr0)r REGULAR_TYPESrr8r8r9rNesz TarInfo.isregcCs|Sr0)rNrr8r8r9isfileiszTarInfo.isfilecCs |jtkSr0)rrrr8r8r9r>msz TarInfo.isdircCs |jtkSr0)rSYMTYPErr8r8r9issymqsz TarInfo.issymcCs |jtkSr0)rLNKTYPErr8r8r9islnkusz TarInfo.islnkcCs |jtkSr0)rr$rr8r8r9ischrysz TarInfo.ischrcCs |jtkSr0)rr%rr8r8r9isblk}sz TarInfo.isblkcCs |jtkSr0)rFIFOTYPErr8r8r9isfifoszTarInfo.isfifocCs |jduSr0)rrr8r8r9issparseszTarInfo.issparsecCs|jtttfvSr0)rr$r%rxrr8r8r9isdevsz TarInfo.isdev)r)1rprqrrdict __slots__rpropertyr%setterr&rrrr rrrr classmethodrr  staticmethodrr(rrrFrHrGrMrIrJrLrbrarcrQr_rPrNrqr>rsrurvrwryrzr{r8r8r8r9rs      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)irrFr#Nryrc 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+bwbxbryarzrh!mode must be 'r', 'a', 'w' or 'x'rrzFrrTryrrzrh)-rJr_moder|r%exists bltn_openrrrrrrOabspathrrSr dereference ignore_zerosr6r7rrdebug errorlevel copybufsizermembers_loadedrrinodes firstmemberrSrrHrrur rrrr_r2r)rrrrrSrrrr6r7rrrrZmodeserXr8r8r9rs             &   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)ryr:*csj|dkS)Ntaropen) OPEN_METH)rrr8r9not_compressed@sz$TarFile.open..not_compressedkeyryz%file could not be opened successfully:r#rr|rxzmode must be 'r' or 'w'Frzundiscernible mode) rJsortedrrjrrrrr rrrr) rrrrrckwargsrrfuncZ saved_posfilemodestreamrr8rr9rsP%            z TarFile.opencKs$|dvrtd||||fi|S)Nrr)rJ)rrrrrr8r8r9rqszTarFile.taropenrcKs|dvrtdzddlm}Wnty:tdYn0z|||d||}Wn,ty~|durx|dkrxtdYn0z|j|||fi|}Wn@ty||dkrtdYn|Yn0d |_ |S) Nryrzrhmode must be 'r', 'w' or 'x'r)GzipFilezgzip module is not availablerfryrF) rJgziprrrrrrrr)rrrr compresslevelrrrr8r8r9gzopenys0   zTarFile.gzopenc Ks|dvrtdzddlm}Wnty:tdYn0||pD|||d}z|j|||fi|}WnDttfy||dkrt dYn|Yn0d |_ |S) Nrrr)BZ2Filer)rryznot a bzip2 fileF) rJrrrrrrEOFErrorrrr)rrrrrrrrr8r8r9bz2opens& zTarFile.bz2openc Ks|dvrtdzddlm}m}Wnty>tdYn0||pH|||d}z|j|||fi|}WnD|tfy||dkrt dYn|Yn0d |_ |S) Nrrr)LZMAFilerr)presetryznot an lzma fileF) rJrrrrrrrrrr) rrrrrrrrrr8r8r9xzopens& zTarFile.xzopenrrrr)rrrrcCs|jr dSd|_zp|jdvrn|jttd|jtd7_t|jt\}}|dkrn|jtt|W|j s|j n|j s|j 0dS)NTrr$r) rrrr_r3rrr] RECORDSIZErr)rrdrer8r8r9rs  z TarFile.closecCs"||}|durtd||S)Nzfilename %r not found) _getmemberKeyError)rrrr8r8r9 getmembers  zTarFile.getmembercCs||js||jSr0)_checkr_loadrrr8r8r9 getmembersszTarFile.getmemberscCsdd|DS)NcSsg|] }|jqSr8r)rgrr8r8r9ri rz$TarFile.getnames..)rrr8r8r9getnamesszTarFile.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)Nawxrrr#rmajorminor):rrr|r% splitdriverseplstriprrrlstatstatfstatfilenost_modeS_ISREGst_inost_devst_nlinkrrtrS_ISDIRrS_ISFIFOrxS_ISLNKrrreadlinkS_ISCHRr$S_ISBLKr%rst_uidr)st_gidr*st_sizer'st_mtimer(rrpwdgetpwuidr+rgrpgetgrgidr,rrst_rdevrrr) rrarcnamerdrvrZstatresrZstmdinoderr8r8r9 gettarinfo s                 zTarFile.gettarinfoT)rcCs||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:%02drrrz-> zlink to )rrnrrrr+r)r,r*rvrwrrr'r localtimer(rr>rsrrurm)rverboserrr8r8r9reps0 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) Nrr$ztarfile: Skipped %rr#ztarfile: Unsupported type %rztarfile: Excluded %rrr)rrr|r%r_dbgrrNraddfiler>rlistdiraddr)rrr recursiverrfr8r8r9rs6    ,   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)rcrr#)rrrrSr6r7rr_rr2rr\r'r]rr3rr)rrrrXrcrdrer8r8r9rs   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_attrsrcSs|jSr0r)rr8r8r9rz$TarFile.extractall..rrr# tarfile: %s)r>rrrextractsortreverser|r%rrchownutimechmodr rr)rr%rrZ directoriesrdirpathrr8r8r9 extractalls.    zTarFile.extractallrc 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)Nryrrr#rztarfile: %s %r)rrrrrur|r%rrr_extract_memberrrrfilenamerstrerrorr )rmemberr%rrrrr8r8r9rs*      , zTarFile.extractcCs|dt|tr ||}n|}|s6|jtvrB|||S|sR| rzt|j t rht dq~| ||SndSdS)Nryz'cannot extract (sym)link as file object)rrrrrNrrO fileobjectrursrrr extractfile_find_link_target)rrrr8r8r9r*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)Nrr#z%s -> %s)r?rr|rr%dirnamermakedirsrursrrrrNmakefiler>makedirrymakefiforvrwmakedevmakelinkrrO makeunknownrrr)rr targetpathrrZ upperdirsr8r8r9rIs4       zTarFile._extract_membercCs(zt|dWnty"Yn0dS)Nr)r|mkdirFileExistsErrorrrrr8r8r9rzs zTarFile.makedircCs|j}||j|j}t|dl}|jdurn|jD]"\}}||t|||t|q4||j| nt|||jt|Wdn1s0YdS)Nr) rrrrrrr\rr'truncate)rrrsourcerctargetrr'r8r8r9rs      zTarFile.makefilecCs"||||dd|jdS)Nr#z9tarfile: Unknown file type %r, extracted as regular file.)rrrrr8r8r9rs zTarFile.makeunknowncCs"ttdrt|ntddS)Nmkfifozfifo not supported by system)rr|r r rr8r8r9rs  zTarFile.makefifocCs^ttdrttdstd|j}|r6|tjO}n |tjO}t||t |j |j dS)Nmknodrz'special devices not supported by system) rr|r rrwrS_IFBLKS_IFCHRr rrr)rrrrr8r8r9rs  zTarFile.makedevc CszL|rt|j|n0tj|jr8t|j|n|| ||WnDt yz|| ||Wnt yt dYn0Yn0dS)Nz%unable to resolve link inside archive) rsr|symlinkrr%rrlinkrrsymlink_exceptionrr rr8r8r9rs    zTarFile.makelinkcCsttdrtdkr|j}|j}|s~ztrzShow listing of a tarfile)metavarr,z-ez --extract+)r.z zExtract tarfile into target dir)nargsr/r,z-cz--create)zzzCreate tarfile from sourcesz-tz--testzTest if a tarfile is validryr z{!r} is a tar archive.r#z{!r} is not a tar archive. r)rr$)r%rz{!r} file is extracted.z+{!r} file is extracted into {!r} directory.rrr)rz.tgzz.xzz.txzz.bz2z.tbzz.tbz2z.tb2zw:rzz{!r} file created.)argparseArgumentParser add_argumentadd_mutually_exclusive_group parse_argstestrrrrmrkr"rrSexitrerrr2r|curdir format_helprZcreatepopr%splitextr)r2r(parserr[argsr`rZtfr9r$Ztar_name_extZ compressionsZtar_modeZ tar_files file_namer8r8r9main s          0  0    ,      .rB__main__)]version __author__ __credits__builtinsrrrkr|rr[rrrVrrYrrrAttributeErrorNotImplementedErrorrr NameError__all__r3rrrr r r r!rr;rtrrr$r%rrxZCONTTYPErrr<rrrKr rrrrOrpr@rmr`rrHrnrr getfilesystemencodingr:r?rNrTrYr\rn Exceptionrr rrr r rsrtrurKrvrwrobjectrrBufferedReaderrrrrrBrpr8r8r8r9 s          qh JW