From: Tobias Ulmer Date: Fri, 9 Dec 2016 17:33:05 +0000 (+0100) Subject: e2lib: add possibility of registering cleanup functions on exit X-Git-Tag: e2factory-2.3.16rc1~24 X-Git-Url: https://git.e2factory.org/?a=commitdiff_plain;h=6f9ae4b48408273f21c2358f418c3894028919a6;p=e2factory.git e2lib: add possibility of registering cleanup functions on exit Signed-off-by: Tobias Ulmer --- diff --git a/generic/e2lib.lua b/generic/e2lib.lua index 2f70df4..8e38e5b 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -677,12 +677,30 @@ function e2lib.rotate_log(file) return true end +local _cleanup = {} + +--- Register a callback that runs during e2lib.cleanup() +-- @param name Name of the function, for logging. +-- @param func Funtion to be run +-- @param ... Any number of arguments to be passed to function when it runs. +function e2lib.register_cleanup(name, func, ...) + assertIsStringN(name) + assertIsFunction(func) + table.insert(_cleanup, #_cleanup+1, { name=name, func=func, args = {...}}) +end + --- Clean up temporary files and directories, shut down plugins. function e2lib.cleanup() local rc, re = plugin.exit_plugins() if not rc then e2lib.log(1, "deinitializing plugins failed (ignoring)") end + + for _,entry in ipairs(_cleanup) do + e2lib.logf(4, "running cleanup callback %s()", tostring(entry.name)) + entry.func(unpack(entry.args)) + end + e2lib.rmtempdirs() e2lib.rmtempfiles() if e2lib.globals.lock then