
    }f                         d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ  ej                  e	      Z
dZdZddefdZddefdZd Ze j"                  dd	       Zddefd
ZddefdZy)    N)utilz/run/cloud-init/tmpz/var/tmp/cloud-init	needs_exec                     | | S |rt         S t        j                         dk(  rt        S t        j                  j                  dd      S )Nr   TMPDIRz/tmp)_EXE_ROOT_TMPDIRosgetuid_ROOT_TMPDIRenvironget)odirr   s     6/usr/lib/python3/dist-packages/cloudinit/temp_utils.pyget_tmp_ancestorr      s?    	yy{a::>>(F++    c                    t        | |      }t        j                  j                  |      s+t        j                  |       t        j
                  |d       |r,t        j                  |d      rt        j                  d|       |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.
    i  noexeczDRequested temporal dir with exe permission `%s` is mounted as noexec)
r   r   pathisdirmakedirschmodr   has_mount_optLOGwarning)r   r   tdirs      r   _tempfile_dir_argr      sh     D),D77==
D
vdH-KK%
 Kr   c                      t               | d<   t        j                  di | d }|_        fd}t	        d|       S )Ndirc                     	 t        j                  |        y # t        $ r)}|j                  t        j                  k7  r|Y d }~y d }~ww xY wN)r   unlinkOSErrorerrnoENOENT)r   es     r   _unlink_if_existsz0ExtendedTemporaryFile.<locals>._unlink_if_exists?   s<    	IIdO 	ww%,,& '	s    	A
AA
c                  <     j                   j                         y r   )r    name)fhs   r   
unlink_nowz)ExtendedTemporaryFile.<locals>.unlink_nowM   s    
		"''r   r)    )r   tempfileNamedTemporaryFiler    setattr)kwargsr%   r)   r(   s      @r   ExtendedTemporaryFiler/   8   sH    %'F5M		$	$	.v	.B
 "BI Bj)Ir   c              +      K   t        di |}	 | t        j                  ||        y # t        j                  ||        w xY ww)N)ignore_errorsr*   )mkdtempshutilrmtree)rmtree_ignore_errorsr.   r   s      r   tempdirr6   T   s>     
 VD@
d*>?d*>?s   A+ AAAc                 H    t        | |      } t        j                  dd| i|S Nr   r*   )r   r+   r2   r   r   r.   s      r   r2   r2   `   '    
C
+C..v..r   c                 H    t        | |      } t        j                  dd| i|S r8   )r   r+   mkstempr9   s      r   r<   r<   e   r:   r   )NF)F)
contextlibr"   loggingr   r3   r+   	cloudinitr   	getLogger__name__r   r
   r   boolr   r   r/   contextmanagerr6   r2   r<   r*   r   r   <module>rD      s       	   g!$( ,4 ,D :8 @ @/ /
/ /r   