
    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a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        rt        S t        j                         dk(  rt        S t        j
                  j                  dd      S )Nr   TMPDIRz/tmp)_EXE_ROOT_TMPDIR_TMPDIRosgetuid_ROOT_TMPDIRenvironget)odirr   s     6/usr/lib/python3/dist-packages/cloudinit/temp_utils.pyget_tmp_ancestorr      sG    	yy{a::>>(F++    c                    t        | |      }t        j                  j                  |      s+t        j                  |       t        j
                  |d       |r,t        j                  |d      rt        j                  d|       | |s|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.
    i  noexeczDRequested temporal dir with exe permission `%s` is mounted as noexec)r   r	   pathisdirmakedirschmodr   has_mount_optLOGwarningr   )r   r   tdirs      r   _tempfile_dir_argr      st     D),D77==
D
vdH-KK% |IKr   c                      t        | j                  dd       | j                  dd            | d<   t        j                  di | d }|_        fd}t        d|       S )Ndirr   Fc                     	 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_existsI   s<    	IIdO 	ww%,,& '	s    	A
AA
c                  <     j                   j                         y r    )r!   name)fhs   r   
unlink_nowz)ExtendedTemporaryFile.<locals>.unlink_nowW   s    
		"''r   r*    )r   poptempfileNamedTemporaryFiler!   setattr)kwargsr&   r*   r)   s      @r   ExtendedTemporaryFiler1   @   sg    %

5$K!?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_errorsr0   r   s      r   tempdirr8   ^   s>     
 VD@
d*>?d*>?s   A+ AAAc                 H    t        | |      } t        j                  dd| i|S Nr   r+   )r   r-   r4   r   r   r0   s      r   r4   r4   j   '    
C
+C..v..r   c                 H    t        | |      } t        j                  dd| i|S r:   )r   r-   mkstempr;   s      r   r>   r>   o   r<   r   )NF)F)
contextlibr#   loggingr	   r5   r-   	cloudinitr   	getLogger__name__r   r   r   r   boolr   r   r1   contextmanagerr8   r4   r>   r+   r   r   <module>rF      s       	   g!
$( 	,4 	,D D< @ @/ /
/ /r   