backup: add support for calling a function after backing up a file/volume

When Volume.export is called late and can be also a coroutine, it may
make sense to also have a cleanup function for changes made by it.
This commit only adjust backup code internals, but doesn't call
appropriate Volume function yet.

QubesOS/qubes-issues#5935
This commit is contained in:
Marek Marczykowski-Górecki 2020-07-06 00:47:33 +02:00
parent ebd0ca7e79
commit f48327f636
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724

View File

@ -252,7 +252,8 @@ class Backup:
# pylint: disable=too-many-instance-attributes
class FileToBackup:
# pylint: disable=too-few-public-methods
def __init__(self, file_path_or_func, subdir=None, name=None, size=None):
def __init__(self, file_path_or_func, subdir=None, name=None, size=None,
cleanup_func=None):
"""Store a single file to backup
:param file_path_or_func: path to the file or a function
@ -262,6 +263,8 @@ class Backup:
:param subdir: directory in a backup archive to place file in
:param name: name of the file in the backup archive
:param size: size
:param cleanup_func: function to call after processing the file;
the function will get the file path as an argument
"""
if callable(file_path_or_func):
assert subdir is not None \
@ -295,6 +298,8 @@ class Backup:
self.subdir = subdir
#: use this name in the archive (aka rename)
self.name = name
#: function to call after processing the file
self.cleanup_func = cleanup_func
class VMToBackup:
# pylint: disable=too-few-public-methods
@ -674,6 +679,10 @@ class Backup:
except ProcessLookupError:
pass
raise
finally:
if file_info.cleanup_func is not None:
yield from qubes.utils.coro_maybe(
file_info.cleanup_func(path))
yield from tar_sparse.wait()
if tar_sparse.returncode: