o
    x[hÅ  ã                   @   sª   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 e	¡Z
dZdZddefdd„Zddefd	d
„Zdd„ Ze jddd„ƒZddefdd„Zddefdd„ZdS )é    N)Úutilz/run/cloud-init/tmpz/var/tmp/cloud-initFÚ	needs_exec                 C   s2   | d ur| S |r
t S t ¡ dkrtS tj dd¡S )Nr   ÚTMPDIRz/tmp)Ú_EXE_ROOT_TMPDIRÚosÚgetuidÚ_ROOT_TMPDIRÚenvironÚget)Úodirr   © r   ú6/usr/lib/python3/dist-packages/cloudinit/temp_utils.pyÚget_tmp_ancestor   s   r   c                 C   sL   t | |ƒ}tj |¡st |¡ t |d¡ |r$t |d¡r$t 	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   Útdirr   r   r   Ú_tempfile_dir_arg   s   

ýr   c                     sD   t ƒ | d< tjdi | ¤Ž‰ dd„ }|ˆ _‡ fdd„}tˆ d|ƒ ˆ S )NÚdirc              
   S   sH   zt  | ¡ W d S  ty# } z|jtjkr|‚W Y d }~d S d }~ww ©N)r   ÚunlinkÚOSErrorÚerrnoÚENOENT)r   Úer   r   r   Ú_unlink_if_exists?   s   ÿ€ÿz0ExtendedTemporaryFile.<locals>._unlink_if_existsc                      s   ˆ   ˆ j¡ d S r   )r   Únamer   ©Úfhr   r   Ú
unlink_nowM   s   z)ExtendedTemporaryFile.<locals>.unlink_nowr$   r   )r   ÚtempfileÚNamedTemporaryFiler   Úsetattr)Úkwargsr    r$   r   r"   r   ÚExtendedTemporaryFile8   s   
r)   c              
   k   s<   � t di |¤Ž}z|V  W tj|| d� d S tj|| d� w )N)Úignore_errorsr   )ÚmkdtempÚshutilÚrmtree)Úrmtree_ignore_errorsr(   r   r   r   r   ÚtempdirT   s
   €"r/   c                 K   ó   t | |ƒ} tjdd| i|¤ŽS ©Nr   r   )r   r%   r+   ©r   r   r(   r   r   r   r+   `   ó   
r+   c                 K   r0   r1   )r   r%   Úmkstempr2   r   r   r   r4   e   r3   r4   )NF)F)Ú
contextlibr   Úloggingr   r,   r%   Ú	cloudinitr   Ú	getLoggerÚ__name__r   r   r   Úboolr   r   r)   Úcontextmanagerr/   r+   r4   r   r   r   r   Ú<module>   s"   

