[molecule.plugins] safely using mkstemp()

This commit is contained in:
Fabio Erculiani
2010-08-08 15:21:02 +02:00
parent fa3530c7e8
commit 1ceb5b7be9
+12 -5
View File
@@ -53,11 +53,18 @@ class BuiltinHandlerMixin:
def _exec_inner_script(self, exec_script, dest_chroot):
tmp_fd, tmp_exec = tempfile.mkstemp(dir = dest_chroot,
prefix = "molecule_inner")
os.close(tmp_fd)
shutil.copy2(exec_script[0], tmp_exec)
os.chmod(tmp_exec, 0o755)
source_exec = exec_script[0]
with open(source_exec, "rb") as f_src:
tmp_fd, tmp_exec = tempfile.mkstemp(dir = dest_chroot,
prefix = "molecule_inner")
f_dst = os.fdopen(tmp_fd, "wb")
try:
shutil.copyfileobj(f_src, f_dst)
finally:
f_dst.flush()
f_dst.close()
shutil.copystat(source_exec, tmp_exec)
os.chmod(tmp_exec, 0o744)
dest_exec = os.path.basename(tmp_exec)
if not dest_exec.startswith("/"):