From f48327f6360cf3ee74aa0157fdbb6e2df6187b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Mon, 6 Jul 2020 00:47:33 +0200 Subject: [PATCH] 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 --- qubes/backup.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/qubes/backup.py b/qubes/backup.py index e5bf49c4..661af44a 100644 --- a/qubes/backup.py +++ b/qubes/backup.py @@ -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: