
"T`z                 @   s   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d a d Z d Z d d d d  Z d d   Z	 e  j
 d d	 d
   Z d d   Z d d   Z d S)    Nz/run/cloud-init/tmpz/var/tmp/cloud-initFc             C   s   |  d k	 r |  S| rO t  } t j j |  sK t j |  t j | d  | St rY t St j   d k rt t } n t j	 j
 d d  } t j j |  s t j |  t j | d  | a | S)a%  Return the proper 'dir' argument for tempfile functions.

    When root, cloud-init will use /run/cloud-init/tmp to avoid
    any cleaning that a distro boot might do on /tmp (such as
    systemd-tmpfiles-clean).

    If the caller of this function (mkdtemp or mkstemp) was provided
    with a 'dir' argument, then that is respected.

    @param odir: original 'dir' arg to 'mkdtemp' or other.
    @param needs_exe: Boolean specifying whether or not exe permissions are
        needed for tempdir. This is needed because /run is mounted noexec.
    Ni  r   TMPDIRz/tmp)_EXE_ROOT_TMPDIRospathisdirmakedirschmod_TMPDIRgetuid_ROOT_TMPDIRenvironget)Zodir	needs_exetdir r   6/usr/lib/python3/dist-packages/cloudinit/temp_utils.py_tempfile_dir_arg   s$    	r   c                 su   t  |  j d d   |  j d d   |  d <t j |      d d   } |   _   f d d   } t   d |    S)	Ndirr   Fc             S   sU   y t  j |   Wn= t k
 rP } z | j t j k r> |  WYd  d  } ~ Xn Xd  S)N)r   unlinkOSErrorerrnoENOENT)r   er   r   r   _unlink_if_exists>   s
    z0ExtendedTemporaryFile.<locals>._unlink_if_existsc                  s     j    j  d  S)N)r   namer   )fhr   r   
unlink_nowL   s    z)ExtendedTemporaryFile.<locals>.unlink_nowr   )r   poptempfileNamedTemporaryFiler   setattr)kwargsr   r   r   )r   r   ExtendedTemporaryFile6   s    (	r"   c             k   s0   t  |   } z	 | VWd  t j | d |  Xd  S)Nignore_errors)mkdtempshutilrmtree)Zrmtree_ignore_errorsr!   r   r   r   r   tempdirS   s    	r'   c              K   s8   t  |  j d d   |  j d d   |  d <t j |    S)Nr   r   F)r   r   r   r$   )r!   r   r   r   r$   _   s    (r$   c              K   s8   t  |  j d d   |  j d d   |  d <t j |    S)Nr   r   F)r   r   r   mkstemp)r!   r   r   r   r(   e   s    (r(   )
contextlibr   r   r%   r   r	   r   r   r   r"   contextmanagerr'   r$   r(   r   r   r   r   <module>   s   (