3 \me @sddlZddlZddlZddlZddlZddlZddlmZeje Z da dZ dZ dedddZdedd d Zd d Zejdd dZdedddZdedddZdS)N)utilz/run/cloud-init/tmpz/var/tmp/cloud-initF) needs_execCs:|dk r |S|rtStrtStjdkr,tStjjddS)NrZTMPDIRz/tmp)_EXE_ROOT_TMPDIR_TMPDIRosgetuid _ROOT_TMPDIRenvironget)odirrr /usr/lib/python3.6/temp_utils.pyget_tmp_ancestors rcCs^t||}tjj|s,tj|tj|d|rHtj|drHtj d||dkrZ| rZ|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. iZnoexeczDRequested temporal dir with exe permission `%s` is mounted as noexecN) rrpathisdirmakedirschmodrZ has_mount_optLOGZwarningr)r rtdirr r r _tempfile_dir_args     rc sTt|jdd|jdd|d<tjf|dd}|_fdd}td|S) NdirrFcSsFytj|Wn2tk r@}z|jtjkr0|WYdd}~XnXdS)N)runlinkOSErrorerrnoENOENT)rer r r _unlink_if_existsIs  z0ExtendedTemporaryFile.._unlink_if_existscsjjdS)N)rnamer )fhr r unlink_nowWsz)ExtendedTemporaryFile..unlink_nowr)rpoptempfileZNamedTemporaryFilersetattr)kwargsrrr )rr ExtendedTemporaryFile@s   r$c ks*tf|}z |VWdtj||dXdS)N) ignore_errors)mkdtempshutilZrmtree)Zrmtree_ignore_errorsr#rr r r tempdir^s  r(cKst||}tjfd|i|S)Nr)rr!r&)rrr#r r r r&js r&cKst||}tjfd|i|S)Nr)rr!mkstemp)rrr#r r r r)os r))NF)NF)F)NF)NF) contextlibrZloggingrr'r!Z cloudinitrZ getLogger__name__rrrrboolrrr$contextmanagerr(r&r)r r r r s"   "