Cleanup temp files. By @marco44 (#1384)

This commit is contained in:
Marc Cousin 2022-08-14 20:42:00 +02:00 committed by GitHub
parent 8a94457b8d
commit 94e76644c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 2 deletions

View File

@ -104,6 +104,7 @@ class BorgJob(JobInterface, BackupProfileMixin):
self.cwd = params.get('cwd', None)
self.params = params
self.process = None
self.cleanup_files = params.get('cleanup_files', [])
def repo_id(self):
return self.site_id
@ -182,6 +183,7 @@ class BorgJob(JobInterface, BackupProfileMixin):
ret['profile_id'] = profile.id
ret['ok'] = True
ret['cleanup_files'] = []
return ret
@ -299,6 +301,8 @@ class BorgJob(JobInterface, BackupProfileMixin):
self.process_result(result)
self.finished_event(result)
for tmpfile in self.cleanup_files:
tmpfile.close()
def process_result(self, result):
pass

View File

@ -145,10 +145,11 @@ class BorgCreateJob(BorgJob):
exclude_dirs.append(expanded_directory)
if exclude_dirs:
pattern_file = tempfile.NamedTemporaryFile('w', delete=False)
pattern_file = tempfile.NamedTemporaryFile('w', delete=True)
pattern_file.write('\n'.join(exclude_dirs))
pattern_file.flush()
cmd.extend(['--exclude-from', pattern_file.name])
ret['cleanup_files'].append(pattern_file)
if profile.exclude_if_present is not None:
for f in profile.exclude_if_present.split('\n'):

View File

@ -35,7 +35,7 @@ class BorgExtractJob(BorgJob):
# dialog.
# Unselected (and excluded) parent folders will be restored by borg
# but without the metadata stored in the archive.
pattern_file = tempfile.NamedTemporaryFile('w', delete=False)
pattern_file = tempfile.NamedTemporaryFile('w', delete=True)
pattern_file.write("P fm\n")
indexes = [QModelIndex()]
@ -54,6 +54,7 @@ class BorgExtractJob(BorgJob):
pattern_file.flush()
pattern_file.close() # wont delete temp file
cmd.extend(['--patterns-from', pattern_file.name])
ret['cleanup_files'].append(pattern_file)
ret['ok'] = True
ret['cmd'] = cmd