o ~_<*@sdZddlmZddlmZeddlmZddlZddl Z ddl Z ddl Z ddl Z ddl mZddl mZddl mZe adae ad d ZGd d d eZdS) z Provides temporary file handling cenetered around a single top-level securely created temporary directory. The public interface of this module is thread-safe. )print_function)standard_library)objectN)config)log)utilcCsLtztdustdurttjdatt_tWt St w)am Obtain the global default instance of TemporaryDirectory, creating it first if necessary. Failures are propagated to caller. Most callers are expected to use this function rather than instantiating TemporaryDirectory directly, unless they explicitly desdire to have their "own" directory for some reason. This function is thread-safe. N)temproot) _defaultLockacquire_defaultInstancedirTemporaryDirectoryrrtempfiletempdirreleaserr3/usr/lib/python3/dist-packages/duplicity/tempdir.pydefault5s   rc@sReZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ dS)r a5 A temporary directory. An instance of this class is backed by a directory in the file system created securely by the use of tempfile.mkdtemp(). Said instance can be used to obtain unique filenames inside of this directory for cases where mktemp()-like semantics is desired, or (recommended) an fd,filename pair for mkstemp()-like semantics. See further below for the security implications of using it. Each instance will keep a list of all files ever created by it, to faciliate deletion of such files and rmdir() of the directory itself. It does this in order to be able to clean out the directory without resorting to a recursive delete (ala rm -rf), which would be risky. Calling code can optionally (recommended) notify an instance of the fact that a tempfile was deleted, and thus need not be kept track of anymore. This class serves two primary purposes: Firstly, it provides a convenient single top-level directory in which all the clutter ends up, rather than cluttering up the root of the system temp directory itself with many files. Secondly, it provides a way to get mktemp() style semantics for temporary file creation, with most of the risks gone. Specifically, since the directory itself is created securely, files in this directory can be (mostly) safely created non-atomically without the usual mktemp() security implications. However, in the presence of tmpwatch, tmpreaper, or similar mechanisms that will cause files in the system tempdir to expire, a security risk is still present because the removal of the TemporaryDirectory managed directory removes all protection it offers. For this reason, use of mkstemp() is greatly preferred above use of mktemp(). In addition, since cleanup is in the form of deletion based on a list of filenames, completely independently of whether someone else already deleted the file, there exists a race here as well. The impact should however be limited to the removal of an 'attackers' file. NcCsdd}|durtjrtj}nt}t|djrt|}t dr5||r5t ddg}t| }t dd ||_ttd |jd |_i|_t|_dS) z Create a new TemporaryDirectory backed by a unique and securely created file system directory. tempbase - The temp root directory, or None to use system default (recommended). cSsBtjdtjdg}tj|}|D] }||rdSqdS)z-Determine if path point to a MAcOS system tmpz/tmpz/var/tmpTF)ospathrealpath startswith)r sys_temps user_tempsys_temprrrdefaults_to_tmps    z4TemporaryDirectory.__init__..defaults_to_tmpNDarwingetconfDARWIN_USER_TEMP_DIRz-tempdirz duplicity-zUsing temporary directory %sr)rr_initialSystemTempRoot isinstance __class__rfsdecodeplatformsystemr subprocess check_outputrstriprmkdtemp_TemporaryDirectory__dirrInfo__TemporaryDirectory__tempcount_TemporaryDirectory__pending threadingLock_TemporaryDirectory__lock)selfrrrrr__init__{s   zTemporaryDirectory.__init__cCs|jS)zC Returns the absolute pathname of the temp folder. )r*r2rrrr szTemporaryDirectory.dircCstdur |dSdS)z" Perform cleanup. N)r cleanupr4rrr__del__s zTemporaryDirectory.__del__cCs|d}|jz0|jd|_d|jf}tt|d|j}t t dt |d|j |<W|j |S|j w)aN Return a unique filename suitable for use for a temporary file. The file is not created. Subsequent calls to this method are guaranteed to never return the same filename again. As a result, it is safe to use under concurrent conditions. NOTE: mkstemp() is greatly preferred. N-%dzmktemp-z&Registering (mktemp) temporary file %s)r1r r-rfsencodermktempr*rDebugr,r#r.r)r2filenamesuffixrrrr:s      zTemporaryDirectory.mktempcCs|d}d}|jz.|jd|_d|jf}t|d|j\}}ttd|d|j |<W|j ||fS|j w)z Returns a filedescriptor and a filename, as per os.mkstemp(), but located in the temporary directory and subject to tracking and automatic cleanup. Nr7r8zmkstemp-z'Registering (mkstemp) temporary file %s) r1r r-rmkstempr*rr;r,r.r)r2fdr<r=rrrr>s      zTemporaryDirectory.mkstempcCs|\}}t|d|fS)zv Convenience wrapper around mkstemp(), with the file descriptor converted into a file object. zr+)r>rfdopen)r2r?r<rrr mkstemp_files zTemporaryDirectory.mkstemp_filecCs~|jz3||jvrttdt||j|=nttdt| W|j dSW|j dS|j w)a" Forget about the given filename previously obtained through mktemp() or mkstemp(). This should be called *after* the file has been deleted, to stop a future cleanup() from trying to delete it. Forgetting is only needed for scaling purposes; that is, to avoid n timefile creations from implying that n filenames are kept in memory. Typically this whould never matter in duplicity, but for niceness sake callers are recommended to use this method whenever possible. zForgetting temporary file %sz?Attempt to forget unknown tempfile %s - this is probably a bug.N) r1r r.rr;r,rr#Warnr)r2fnamerrrforgets   zTemporaryDirectory.forgetc Cs|jzn|jdurlt|jD],}zttdt |t t j |Wqty>ttdt |Yqwzt |jWnty]ttdt |jYnwd|_d|_W|jdSW|jdS|jw)z Cleanup any files created in the temporary directory (that have not been forgotten), and clean up the temporary directory itself. On failure they are logged, but this method will not raise an exception. Nz+Removing still remembered temporary file %sz#Cleanup of temporary file %s failedzBCleanup of temporary directory %s failed - this is probably a bug.)r1r r*listr.keysrr;r,rr#ignore_missingrunlink Exceptionr+rmdirrBr)r2filerrrr5 s0      zTemporaryDirectory.cleanup)N) __name__ __module__ __qualname____doc__r3r r6r:r>rArDr5rrrrr Ms -0 r )rO __future__rfuturerinstall_aliasesbuiltinsrrr$r&rr/ duplicityrrrr0r r gettempdirr rr rrrrs$