a 'Dg @sddlZddlZddlZddlZddlZddlZddlmZee Z dZ dZ de dddZde dd d Zd d Zejdd dZde dddZde dddZdS)N)utilz/run/cloud-init/tmpz/var/tmp/cloud-initF) needs_execCs2|dur |S|rtStdkr$tStjddS)NrZTMPDIRz/tmp)_EXE_ROOT_TMPDIRosgetuid _ROOT_TMPDIRenvironget)odirrr 8/usr/lib/python3.9/site-packages/cloudinit/temp_utils.pyget_tmp_ancestors r cCsLt||}tj|s,t|t|d|rHt|drHt 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. iZnoexeczDRequested temporal dir with exe permission `%s` is mounted as noexec) r rpathisdirmakedirschmodrZ has_mount_optLOGZwarning)r rtdirr r r _tempfile_dir_args     rc sDt|d<tjfi|dd}|_fdd}td|S)Ndirc SsHzt|Wn4tyB}z|jtjkr.|WYd}~n d}~00dSN)runlinkOSErrorerrnoENOENT)rer r r _unlink_if_exists?s  z0ExtendedTemporaryFile.._unlink_if_existscsjdSr)rnamer Zfhr r unlink_nowMsz)ExtendedTemporaryFile..unlink_nowr)rtempfileZNamedTemporaryFilersetattr)kwargsrrr rr ExtendedTemporaryFile8s   r#c ks<tfi|}z|VWtj||dntj||d0dS)N) ignore_errors)mkdtempshutilrmtree)Zrmtree_ignore_errorsr"rr r r tempdirTsr(cKst||}tjfd|i|SNr)rr r%rrr"r r r r%`s r%cKst||}tjfd|i|Sr))rr mkstempr*r r r r+es r+)NF)NF)F)NF)NF) contextlibrZloggingrr&r Z cloudinitrZ getLogger__name__rrrboolr rr#contextmanagerr(r%r+r r r r s