lib.ExecContext._put(): Clean up temporary file

_put() can leave temporary files behind if one of its shell commands fails, fix that.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-06-10 11:50:19 +02:00
commit cd1716ff6f
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61

View file

@ -516,13 +516,14 @@ class ExecContext(Base):
cmd: list[str] cmd: list[str]
cmd_input: Input = InputMode.NonInteractive cmd_input: Input = InputMode.NonInteractive
tmp_file: str | None = None
if wd is not None: if wd is not None:
path = wd + '/' + path path = wd + '/' + path
cmds: list[RemoteCmd] = [] cmds: list[RemoteCmd] = []
stdout = (await __run(['mktemp', path + '.XXXXXX'])).stdout_str out = path
if stdout is None: if atomic:
raise Exception(f'Failed to create tmp-directory on {self.root}') out = (await __run(['mktemp', path + '.XXXXXX'])).stdout_str.strip()
out = stdout.strip() if atomic else path tmp_file = out
cmds.append(RemoteCmd( cmds.append(RemoteCmd(
cmd = ['tee', out], cmd = ['tee', out],
cmd_input = content, cmd_input = content,
@ -552,8 +553,11 @@ class ExecContext(Base):
for cmd in cmds: for cmd in cmds:
log(DEBUG, f'{self.log_name}: Running {pretty_cmd(cmd.cmd, wd)}') log(DEBUG, f'{self.log_name}: Running {pretty_cmd(cmd.cmd, wd)}')
ret = await __run(cmd.cmd) ret = await __run(cmd.cmd)
tmp_file = None # Has been successfully moved at this point
return ret return ret
finally: finally:
if tmp_file is not None:
await self.erase(tmp_file)
await self.close() await self.close()
except Exception as e: except Exception as e:
msg = f'Failed to get {path} from {self.root} ({str(e)})' msg = f'Failed to get {path} from {self.root} ({str(e)})'