Always remove temp files and close their descriptors

The inputs temp file was left behind when no inputs existed, and
both temp files if rsync failed to start. The descriptors returned
by mkstemp were also never closed. Remove the temp files when run()
exits in any way, and write them through the mkstemp descriptors.
This commit is contained in:
Fuxino
2026-10-08 17:58:32 +02:00
parent ac7e6ec21b
commit 7d4c43f8ff
+18 -7
View File
@@ -494,6 +494,20 @@ class Backup:
def run(self) -> int:
"""Perform the backup"""
try:
return self._run()
finally:
self._remove_temp_files()
def _remove_temp_files(self) -> None:
for path in [self._inputs_path, self._exclude_path]:
if path != '':
try:
os.remove(path)
except FileNotFoundError:
pass
def _run(self) -> int:
logger.info('Starting backup...')
_notify('Starting backup...')
@@ -501,10 +515,10 @@ class Backup:
self.define_backup_dir()
self.find_last_backup()
_, self._inputs_path = mkstemp(prefix='tmp_inputs', text=True)
fd, self._inputs_path = mkstemp(prefix='tmp_inputs', text=True)
count = 0
with open(self._inputs_path, 'w', encoding='utf-8') as fp:
with os.fdopen(fd, 'w', encoding='utf-8') as fp:
for i in self.inputs:
if not os.path.exists(i):
logger.warning('Input %s not found. Skipping', i)
@@ -522,9 +536,9 @@ class Backup:
return 1
_, self._exclude_path = mkstemp(prefix='tmp_exclude', text=True)
fd, self._exclude_path = mkstemp(prefix='tmp_exclude', text=True)
with open(self._exclude_path, 'w', encoding='utf-8') as fp:
with os.fdopen(fd, 'w', encoding='utf-8') as fp:
if self.exclude is not None:
for e in self.exclude:
fp.write(e)
@@ -591,9 +605,6 @@ class Backup:
else:
logger.warning('Backup not completed successfully. Old backups will not be removed')
os.remove(self._inputs_path)
os.remove(self._exclude_path)
if self._remote:
assert self._ssh is not None