o ~_= @sdZddlmZddlmZeddlmZddlZddl Z ddl Z ddl m Z ddl m Z ddl mZdd l mZdd l mZdd l mZd d ZGddde jZdddZddZGddde jZGdddeZGdddeZGdddeZdS)zManage temporary files)print_function)standard_library)objectN)log)path) file_naming)tempdir)config)gpgcCst}t|S)z Return a new TempPath )rdefaultmktempTempPath)filenamer4/usr/lib/python3/dist-packages/duplicity/dup_temp.py new_temppath)s rc@s eZdZdZddZddZdS)r z. Path object used as a temporary file cC tj|t|jdSz# Forget and delete N)rPathdeleterr forgetnameselfrrrr5 zTempPath.deletecC"ttj||}||j|S)F Returns a fileobj. When that is closed, delete file ) FileobjHookedrropenaddhookrrmodefhrrropen_with_delete< zTempPath.open_with_deleteN)__name__ __module__ __qualname____doc__rr#rrrrr 1s r Fc stjs$tjttdt  d||d}nt j jfd}d}|r3d}t | |d||d}fdd}tjsR| ||S) a Return a file object open for writing, will write to filename Data will be processed and written to a temporary file. When the return fileobject is closed, rename to final position. filename must be a recognizable duplicity data file.  parseresultswb)tdpdirpathpartnamepermnameremname)indexabNcsdSN)renameappendrrr-r.tdr,tdpnamerrrename_and_forget[sz.get_fileobj_duppath..rename_and_forget)r restartrTemporaryDirectoryrr TempDupPathrparser filtered_openrDupPathr) r-r.r/r0 overwriter"dpr!r9rr6rget_fileobj_duppathEs$  rBcCst}t||dS)zD Return a new TempDupPath, using settings from parseresults r))rr r r<)r*rrrrnew_tempduppathes  rCc@s*eZdZdZddZddZd ddZd S) r<z1 Like TempPath, but build around DupPath cCrr)rr?rrr rrrrrrrqrzTempDupPath.deletecCr)zO Returns a filtered fileobj. When that is closed, delete file )rrr?r>rrr rrrfiltered_open_with_deletexr$z%TempDupPath.filtered_open_with_deleterbcCs.|dksJttj||}||j|S)rrE)rrr?rrrr rrrr#s  zTempDupPath.open_with_deleteN)rE)r%r&r'r(rrDr#rrrrr<ms r<c@seZdZdZ  dddZddZddZd d Zd d Zd dZ dddZ ddZ ddZ ddZ ddZddZeeZdS)rz0 Simulate a file, but add hook on close NcCs4||_d|_g|_||_||_||_||_||_dS)zF Initializer. fileobj is the file object to simulate FN)fileobjclosedhooklistr,r-r.r/r0)rrFr,r-r.r/r0rrr__init__s zFileobjHooked.__init__cC |j|S)z9 Write fileobj, return result of write() )rFwrite)rbufrrrrK zFileobjHooked.writecCs|jt|jdS)z/ Flush fileobj and force sync. N)rFflushosfsyncfilenorrrrrNs zFileobjHooked.flushcCs6tjrJ|j|j|j|j|j d=dS)zY We have achieved the first checkpoint, make file visible and permanent. rN) r r:r,r4r-r5r.rFrNrHrrrr to_partials   zFileobjHooked.to_partialcCst|j}|j|j}|j|j}t|}|jr&tj ||j t j dn|j r6tj||j tjt j dnt|j |j tj|dS)z We have written the last checkpoint, now encrypt or compress and send a copy of it to the remote for final storage. sizeN)rr=r0r-r5r.SrcIter compressedr GzipWriteFilersysmaxsize encrypted GPGWriteFiler gpg_profileshutilcopyfilebackendmove)rprsrctgtsrc_iterrrr to_remotes zFileobjHooked.to_remotecCsn|j|j}|j|j}t|}t|j}|jr-tj ||j t j dt |j dSt |j |j dS)zC We are finished, rename to final, gzip if needed. rSN)r-r5r.r/rUrr=rVr rWrrXrYrOunlinkr4)rrbrcrdrarrrto_finals zFileobjHooked.to_finalcCrJ)z7 Read fileobj, return result of read() )rFread)rlengthrrrrirMzFileobjHooked.readcCs |jS)z5 Returns current location of fileobj )rFtellrrrrrks zFileobjHooked.tellcCrJ)z0 Seeks to a location of fileobj )rFseek)roffsetrrrrlrMzFileobjHooked.seekcCs$|jrJ|jD]}|q dS)z? Close fileobj, running hooks right afterwards N)rFcloserHrhookrrrrns zFileobjHooked.closecCs|j|dS)zM Add hook (function taking no arguments) to run upon closing N)rHr5rorrrrszFileobjHooked.addhookcCs|jjS)z- Return the name of the file )rFrrrrrget_nameszFileobjHooked.get_name)NNNNN)rh)r%r&r'r(rIrKrNrRrergrirkrlrnrrqpropertyrrrrrrs"    rc@seZdZdZddZdS)Blockz+ Data block to return from SrcIter cCs ||_dSr3)data)rrtrrrrIs zBlock.__init__N)r%r&r'r(rIrrrrrss rsc@s0eZdZdZddZddZddZdd Zd S) rUz7 Iterate over source and return Block of data. cCs||_|d|_dS)NrE)rbrfp)rrbrrrrIszSrcIter.__init__c Cshz t|j|}Wnty'ttd|jj t ftj j Ynw|js2|jt|S)NzFailed to read %s: %s)rsruri get_read_size Exceptionr FatalError_rbuc_namerXexc_info ErrorCodegenericrtrn StopIteration)rresrrr__next__ s   zSrcIter.__next__cCdS)NirrrrrrvzSrcIter.get_read_sizecCr)Nrrrrr get_footerrzSrcIter.get_footerN)r%r&r'r(rIrrvrrrrrrUs  rU)F)r( __future__rfuturerinstall_aliasesbuiltinsrrOrXr] duplicityrrrrr r rrr rBrCr?r<rrsrUrrrrs,           p